-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwtantpath_test.go
420 lines (385 loc) · 10.5 KB
/
jwtantpath_test.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Package jwtantpath a plugin to verify jwt exclude configured paths.
package traefik_plugin_jwt_antpath
import (
"context"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
)
func TestNew(t *testing.T) {
tests := []struct {
desc string
paths []string
expErr bool
}{
{
desc: "should return no error",
paths: []string{`/foo/**`},
expErr: false,
},
{
desc: "should return no error",
paths: []string{"/foo/*"},
expErr: false,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
Paths: test.paths,
}
if _, err := New(context.Background(), nil, cfg, "name"); test.expErr && err == nil {
t.Errorf("expected error on bad regexp format")
}
})
}
}
func TestServeHTTP(t *testing.T) {
tests := []struct {
desc string
paths []string
reqPath string
hasHeader bool
userId int64
}{
{
desc: "should no header",
paths: []string{"/test"},
reqPath: "/test",
hasHeader: false,
userId: 1,
},
{
desc: "should no header",
paths: []string{"/test", "/toto"},
reqPath: "/toto",
hasHeader: false,
userId: 2,
},
{
desc: "should has header",
paths: []string{"/test", "/toto"},
reqPath: "/plop",
hasHeader: true,
userId: 3,
},
{
desc: "should has header",
reqPath: "/test",
hasHeader: true,
userId: 4,
},
{
desc: "should no header",
paths: []string{`/foo/**`},
reqPath: "/foo/bar/zzz",
hasHeader: false,
userId: 5,
},
{
desc: "should no header",
paths: []string{`/foo/**`},
reqPath: "/foo/bar",
hasHeader: false,
userId: 6,
},
{
desc: "should no header",
paths: []string{`/foo/**`},
reqPath: "/foo",
hasHeader: false,
userId: 7,
},
{
desc: "should no header",
paths: []string{`/*/foo/**`},
reqPath: "/xxx/foo",
hasHeader: false,
userId: 8,
},
{
desc: "should has header",
paths: []string{`/*/foo/**`},
reqPath: "/xxx/zzz/foo",
hasHeader: true,
userId: 9,
},
{
desc: "should no header",
paths: []string{`/*/foo/**`},
reqPath: "/xxx/foo/mmm/nnn",
hasHeader: false,
userId: 10,
},
{
desc: "should has header",
paths: []string{`/*/foo/**`},
reqPath: "/xxx/888/foo/mmm/nnn",
hasHeader: true,
userId: 11,
},
{
desc: "should no header",
paths: []string{`/zzz/zzz/*/foo/**`},
reqPath: "/zzz/zzz/xxx/foo/mmm/nnn",
hasHeader: false,
userId: 12,
},
{
desc: "should has header",
paths: []string{`/zzz/zxz/*/foo/**`},
reqPath: "/zzz/zzz/xxx/foo/mmm/nnn",
hasHeader: true,
userId: 13,
},
{
desc: "should no header",
paths: []string{`/zzz/zzz/*/foo/**`},
reqPath: "/zzz/zzz/xxx/foo",
hasHeader: false,
userId: 14,
},
{
desc: "should no header",
paths: []string{`/zzz/zzz/*/foo/**`},
reqPath: "/zzz/zzz/xxx/foo/{345}",
hasHeader: false,
userId: 15,
},
// endWithOneStar below....
{
desc: "should no header",
paths: []string{`/zzz/*`},
reqPath: "/zzz/xxx",
hasHeader: false,
userId: 16,
},
{
desc: "should has header",
paths: []string{`/zzz/*`},
reqPath: "/zzz/xxx/xxxx",
hasHeader: true,
userId: 17,
},
// endWithOneStar 1star: 1+ below....
{
desc: "should has header",
paths: []string{`/*/zzz/*`},
reqPath: "/zzz/xxx/xxxx",
hasHeader: true,
userId: 18,
},
{
desc: "should has header",
paths: []string{`/*/zzz/*`},
reqPath: "/zzz/xxx/xxxx/zzz",
hasHeader: true,
userId: 19,
},
{
desc: "should no header",
paths: []string{`/*/zzz/*`},
reqPath: "/zzz/zzz/xxxx",
hasHeader: false,
userId: 20,
},
{
desc: "should has header",
paths: []string{`/*/zzz/*`},
reqPath: "/zzz/zzz/xxxx/ooo",
hasHeader: true,
userId: 21,
},
{
desc: "should has header",
paths: []string{`/*/metrics/**`},
reqPath: "/z/test/zzz/xxx",
hasHeader: true,
userId: 22,
},
{
desc: "should has header",
paths: []string{`/*/metrics/**`},
reqPath: "/z/metrics/metrics/sss",
hasHeader: false,
userId: 22,
},
{
desc: "should has header",
paths: []string{`/**/metrics/**`},
reqPath: "/z/metrics/metrics/sss",
hasHeader: false,
userId: 23,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
Paths: test.paths,
}
userId := 0
hasHeader := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
v := req.Header.Get("userId")
if v != "" {
hasHeader = true
id, err := strconv.Atoi(v)
if err != nil {
fmt.Println(err.Error())
} else {
userId = id
}
}
fmt.Println("nickName: ", req.Header.Get("nickName"))
})
SECRET := "abcabc234===++++qzseddzz8992zzz001ppooq3dsedfgeanmhdr"
cfg.SecureKey = SECRET
cfg.HeaderKey = "Authorization"
user := map[string]interface{}{
"UserId": test.userId,
"NickName": "大风吹",
}
fmt.Println(time.Now().Unix() + 30*24*60*60)
token, _ := CreateToken([]byte(SECRET), user, int(time.Now().Unix())+30*24*60*60)
handler, err := New(context.Background(), next, cfg, "jwt verification")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
url := fmt.Sprintf("http://localhost%s", test.reqPath)
req := httptest.NewRequest(http.MethodGet, url, nil)
req.Header.Add("Authorization", "Bearer "+token)
handler.ServeHTTP(recorder, req)
if hasHeader != test.hasHeader {
t.Errorf("handle failed")
}
if hasHeader {
fmt.Println(test.userId)
fmt.Println(userId)
if int64(userId) != test.userId {
t.Errorf("handle failed")
}
}
})
}
}
func CreateToken(key []byte, m map[string]interface{}, expMinute int) (string, error) {
header := `{"alg":"HS256","typ":"JWT"}`
m["exp"] = expMinute
m["iat"] = time.Now().Unix()
payload, jsonErr := json.Marshal(m)
if jsonErr != nil {
return "", jsonErr
}
encodedHeader := encodeBase64(header)
encodedPayload := encodeBase64(string(payload))
HeaderAndPayload := encodedHeader + "." + encodedPayload
signature, err := sign(key, []byte(HeaderAndPayload))
if err != nil {
return "", err
}
return HeaderAndPayload + "." + signature, nil
}
func TestPrivateKeyEncrypt(t *testing.T) {
var privateKeyStr = `
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAvSicp1V0YNiNQzSvMmQTYw07ACHaZaoWfIxHkDNu1TzmBI2h
PO2MAwVoUjOAFgat0G4fzgU3h44QljGHVCRWbS2VX6iRizg6bMeutAU7EIuuqa06
dJo99ByDPUnkXPngutUfYS9ZIEC6RPODg9zG0OyEH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N
BgkqhkiG9w0BAQsFAAOCAQEAsO+mJNG8bFxxKV2S6WU9/ZYPGS7sDYnLBbOxVvH8
+G6gZH+6rnBu5qLBT1i3OB7I4NsR/zh8jeIM2ZMKpKBG7owk1zfqhjy8ms/5UMJR
LaELjpm/VqWyaDSHjWH2/uq4MUiheye4moBw3omJUMoyvcFSPIVB4+d2pAYtDDQJ
Ofi21MzZ1kMa4ZL0VkNEjEQGsNFkgiXxlDeFrXzYs+kNm6GCrH/zgntZiP10WPyJ
GBRqR6REaJM86xb+swZZSGFVlNKLaAh3cBFyNnXrM7ywVVLsv+y7AAsyOisIa5eT
W44Q5FwGTMk+6YohCFP7ZWaLVnI+3zHzLddF2M1+PnUcAQ==
-----END CERTIFICATE-----
`
fmt.Println(privateKeyStr)
fmt.Println(certStr)
privateKey, err := getPriKey([]byte(privateKeyStr))
if err != nil {
fmt.Println(err)
return
}
testStr := "hello world, start test encrypt and decrypt"
startTime := time.Now().UnixMicro()
plaload := []byte(testStr)
hh := sha256.Sum256(plaload)
signed, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hh[:])
fmt.Println(string(signed))
endTime := time.Now().UnixMicro()
fmt.Println("sign time:", endTime-startTime)
if err != nil {
fmt.Println(err)
return
}
startTime = time.Now().UnixMicro()
block, _ := pem.Decode([]byte(certStr))
if block == nil {
log.Fatal("Failed to decode PEM block")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic("parse cert failed: " + err.Error())
}
// get RSA PublicKey
publicKey, ok := cert.PublicKey.(*rsa.PublicKey)
if !ok {
t.Errorf("test failed")
return
}
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hh[:], signed)
if err != nil {
t.Errorf("handled error: %s", err.Error())
}
fmt.Println("Signature verified successfully!")
endTime = time.Now().UnixMicro()
fmt.Println("verify time:", endTime-startTime)
}