/
common.go
262 lines (236 loc) · 5.54 KB
/
common.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package command
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"math"
"strconv"
"strings"
)
//tokenSignLen token default len
const tokenSignLen = 11
//Base token base msg
type Base struct {
Version int8 `json:"version"`
CreateAt int64 `json:"create_at"`
Namespace []byte `json:"namespace"`
// Sign string `json:"-"`
}
//MarshalBinary Namespace SHOULD NOT contains a colon
func (t *Base) MarshalBinary() (data []byte, err error) {
data = append(data, t.Namespace...)
data = append(data, '-')
data = append(data, []byte(strconv.FormatInt(t.CreateAt, 10))...)
data = append(data, '-')
data = append(data, []byte(strconv.FormatInt(int64(t.Version), 10))...)
return data, nil
}
//UnmarshalBinary token base unmarshl
func (t *Base) UnmarshalBinary(data []byte) error {
fields := bytes.Split(data, []byte{'-'})
l := len(fields)
if l < 3 {
return errors.New("invalid token")
}
version, err := strconv.ParseInt(string(fields[l-1]), 10, 64)
if err != nil {
return err
}
t.Version = int8(version)
createAt, err := strconv.ParseInt(string(fields[l-2]), 10, 64)
if err != nil {
return err
}
t.CreateAt = createAt
t.Namespace = bytes.Join(fields[:l-2], []byte(""))
return nil
}
func Verify(token, key []byte) ([]byte, error) {
encodedSignLen := hex.EncodedLen(tokenSignLen)
if len(token) < encodedSignLen || len(key) == 0 {
return nil, errors.New("token or key is parameter illegal")
}
sign := make([]byte, tokenSignLen)
hex.Decode(sign, token[len(token)-encodedSignLen:])
meta := token[:len(token)-encodedSignLen-1] //counting in the ":"
mac := hmac.New(sha256.New, key)
mac.Write(meta)
if !hmac.Equal(mac.Sum(nil)[:tokenSignLen], sign) {
return nil, errors.New("token mismatch")
}
var t Base
if err := t.UnmarshalBinary(meta); err != nil {
return nil, err
}
return t.Namespace, nil
}
//Token token create through key server namespace create time
func Token(key, namespace []byte, createAt int64) ([]byte, error) {
t := &Base{Namespace: namespace, CreateAt: createAt, Version: 1}
data, err := t.MarshalBinary()
if err != nil {
return nil, err
}
mac := hmac.New(sha256.New, key)
mac.Write(data)
sign := mac.Sum(nil)
//truncate to 32 byte: https://tools.ietf.org/html/rfc2104#section-5
// we have 11 byte rigth of hmac,so the rest of data is token message
sign = sign[:tokenSignLen]
encodedSign := make([]byte, hex.EncodedLen(len(sign)))
hex.Encode(encodedSign, sign)
var token []byte
token = append(token, data...)
token = append(token, '-')
token = append(token, encodedSign...)
return token, nil
}
// globMatch matches s with pattern in glob-style
func globMatch(pattern, val []byte, nocase bool) bool {
if !nocase {
pattern = bytes.ToLower(pattern)
val = bytes.ToLower(val)
}
for len(pattern) > 0 {
switch pattern[0] {
case '*':
for len(pattern) >= 2 && pattern[1] == '*' {
pattern = pattern[1:]
}
if len(pattern) == 1 {
return true
}
for len(val) > 0 {
if globMatch(pattern[1:], val, nocase) {
return true
}
val = val[1:]
}
return false
case '?':
if len(val) == 0 {
return false
}
val = val[1:]
case '[':
pattern = pattern[1:]
not := false
if len(pattern) > 0 && pattern[0] == '^' {
not = true
pattern = pattern[1:]
}
var match bool
for len(pattern) > 0 {
if len(pattern) >= 2 && pattern[0] == '\\' {
pattern = pattern[1:]
if pattern[0] == val[0] {
match = true
}
} else if pattern[0] == ']' {
break
} else if len(pattern) >= 3 && pattern[1] == '-' {
if val[0] >= pattern[0] && val[0] <= pattern[2] || val[0] <= pattern[0] && val[0] >= pattern[2] {
match = true
}
pattern = pattern[2:]
} else if pattern[0] == val[0] {
match = true
} else if len(pattern) == 1 {
break
}
if len(pattern) > 0 {
pattern = pattern[1:]
}
}
if not {
match = !match
}
if !match {
return false
}
val = val[1:]
case '\\':
if len(pattern) >= 2 {
pattern = pattern[1:]
}
fallthrough
default:
if pattern[0] != val[0] {
return false
}
val = val[1:]
}
if len(pattern) > 0 {
pattern = pattern[1:]
}
if len(val) == 0 {
for len(pattern) > 0 && pattern[0] == '*' {
pattern = pattern[1:]
}
break
}
}
if len(pattern) == 0 && len(val) == 0 {
return true
}
return false
}
//globMatchPrefix Glob-style patter prefix
func globMatchPrefix(val []byte) []byte {
var v []byte
pattern := val
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '\\':
if i+1 < len(pattern) {
i++
v = append(v, pattern[i])
}
case '*', '[', ']', '?':
return v
default:
v = append(v, pattern[i])
}
}
return v
}
func getFloatAndInclude(strf string) (float64, bool, error) {
var f float64
include := true
var err error
lowerStrf := strings.ToLower(strf)
if lowerStrf[0] == '(' {
include = false
lowerStrf = lowerStrf[1:]
}
if lowerStrf == "-inf" {
f = -math.MaxFloat64
} else if lowerStrf == "+inf" || lowerStrf == "inf" {
f = math.MaxFloat64
} else {
if f, err = strconv.ParseFloat(lowerStrf, 64); err != nil {
return f, include, err
}
}
return f, include, nil
}
func getLimitParameters(offsetCount []string) (int64, int64, error) {
if len(offsetCount) < 2 {
return 0, 0, ErrSyntax
}
var offset, count, tmp int64
var err error
for i := 0; i < 2; i++ {
if tmp, err = strconv.ParseInt(offsetCount[i], 10, 64); err != nil {
return offset, count, ErrInteger
}
if i == 0 {
offset = tmp
} else {
count = tmp
}
}
return offset, count, nil
}