-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
executable file
·182 lines (152 loc) · 4.26 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package campid
import (
"bytes"
"net"
"strings"
"github.com/ewe-studios/sabuhp"
"github.com/influx6/npkg/nerror"
)
func CopyBytes(b []byte) []byte {
var m = make([]byte, len(b))
copy(m, b)
return m
}
func CopyBufferBytes(b *bytes.Buffer) []byte {
return CopyBytes(b.Bytes())
}
func CopyValueBufferBytes(b bytes.Buffer) []byte {
return CopyBytes(b.Bytes())
}
func WriteDeviceToMessage(msg *sabuhp.Message, d Device) {
var deviceData = strings.Join([]string{
d.FingerprintId, d.ZoneId, d.UserId,
}, ":")
msg.Headers.Set(DeviceHeaderName, deviceData)
msg.Cookies = append(msg.Cookies, sabuhp.Cookie{
Name: DeviceCookieName,
Value: deviceData,
MaxAge: 365,
HttpOnly: false,
})
}
type VerifyAccess struct {
AccessToken string
RefreshToken string
}
func (v VerifyAccess) ValidateRefresh() error {
if len(v.RefreshToken) == 0 {
return nerror.New("VerifiedAccess.RefreshToken is required")
}
return nil
}
func (v VerifyAccess) ValidateAccess() error {
if len(v.AccessToken) == 0 {
return nerror.New("VerifiedAccess.AccessToken is required")
}
return nil
}
func (v VerifyAccess) ValidateAll() error {
if err := v.ValidateRefresh(); err != nil {
return err
}
if err := v.ValidateAccess(); err != nil {
return err
}
return nil
}
func ExtractJwtAuth(msg *sabuhp.Message) (VerifyAccess, error) {
var login VerifyAccess
login.AccessToken = msg.Params.Get("accessToken")
login.RefreshToken = msg.Params.Get("refreshToken")
if len(login.AccessToken) == 0 {
var accessCookie *sabuhp.Cookie
var refreshCookie *sabuhp.Cookie
for _, cookie := range msg.Cookies {
if accessCookie != nil && refreshCookie != nil {
break
}
if cookie.Name == "refreshToken" {
refreshCookie = &cookie
continue
}
if cookie.Name == "accessToken" {
accessCookie = &cookie
continue
}
}
if accessCookie != nil {
login.AccessToken = accessCookie.Value
}
if refreshCookie != nil {
login.RefreshToken = refreshCookie.Value
}
}
var refreshHeader = msg.Headers.Get(RefreshHeader)
if len(login.RefreshToken) == 0 && len(refreshHeader) != 0 {
login.RefreshToken = refreshHeader
}
var authHeader = msg.Headers.Get(AuthHeader)
if len(login.AccessToken) == 0 && len(authHeader) != 0 {
var authorizationHeader = strings.TrimPrefix(authHeader, "Bearer")
login.AccessToken = strings.TrimSpace(authorizationHeader)
}
if len(login.AccessToken) == 0 && len(login.RefreshToken) == 0 {
return login, nerror.New("no authorization info found")
}
return login, nil
}
func ExtractDeviceInfo(msg *sabuhp.Message) (DeviceInfo, error) {
var info DeviceInfo
info.IP = net.ParseIP(msg.IP)
var foundAgent bool
var agentValue = msg.Headers.Get(UserAgentHeader)
if len(agentValue) != 0 {
foundAgent = true
var parsedAgent, parsedAgentErr = ParseAgent(agentValue)
if parsedAgentErr != nil {
return info, nerror.WrapOnly(parsedAgentErr)
}
info.Agent = parsedAgent
}
var foundHeader bool
var headerValue = msg.Headers.Get(DeviceHeaderName)
if len(headerValue) != 0 {
foundHeader = true
var deviceData = strings.Split(headerValue, ":")
if len(deviceData) != 3 {
return info, nerror.New("Device header %s data does not match expected format", DeviceHeaderName)
}
info.FingerprintId = deviceData[0]
info.ZoneId = deviceData[1]
info.UserId = deviceData[2]
}
var foundCookie bool
var deviceCookie, findCookieErr = FindCookie(DeviceCookieName, msg.Cookies)
if findCookieErr == nil {
foundCookie = true
var deviceData = strings.Split(deviceCookie.Value, ":")
if len(deviceData) != 3 {
return info, nerror.New("Device cookie data does not match expected format")
}
info.FingerprintId = deviceData[0]
info.ZoneId = deviceData[1]
info.UserId = deviceData[2]
}
var fingerprintCookie, fingerprintCookieErr = FindCookie(DeviceFingerprintCookieName, msg.Cookies)
if fingerprintCookieErr == nil {
foundCookie = true
info.FingerprintId = fingerprintCookie.Value
}
if !foundAgent && !foundHeader && !foundCookie {
return info, nerror.New("")
}
return info, nil
}
func FindCookie(cookieName string, cookies []sabuhp.Cookie) (sabuhp.Cookie, error) {
for _, cookie := range cookies {
if cookie.Name == cookieName {
return cookie, nil
}
}
return sabuhp.Cookie{}, nerror.New("failed to find %q", cookieName)
}