-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_test.go
166 lines (152 loc) · 4.02 KB
/
token_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
package jwt_test
import (
"bytes"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"github.com/CarsonSlovoka/jwt"
"github.com/CarsonSlovoka/jwt/parser"
"github.com/CarsonSlovoka/jwt/validator"
"testing"
)
func TestNew_hmac(t *testing.T) {
token := jwt.NewWithClaims(
jwt.SigningMethodHMAC256,
// 指定所有你想要的Claims
// 如果要擴展可以用MapClaims或者自定義struct把RegisteredClaims加入,可以參考:
// https://github.com/CarsonSlovoka/jwt/blob/5ff3ea21d7624f73baf3f2cd7c89ba5p a4129dedb/validator/validator_test.go#L11-L37
jwt.RegisteredClaims{
Issuer: "c.example.com",
},
)
// token.Header["xx"] = "" // 如果有需要自定義Header可以後補上
privateKey := []byte("key")
bsSignature, err := token.SignedBytes(privateKey)
if err != nil {
t.Fatal(err)
}
p := parser.New(func(v *validator.Validator) {
v.ExpectedIssuer = "c.example.com"
v.VerifyIat = true
})
vdFunc, err := p.Parse(string(bsSignature), func(method string) (jwt.ISigningMethod, error) {
return jwt.SigningMethodHMAC256, nil
})
if err != nil {
t.Fatal(err)
}
if err = vdFunc(nil, nil, func(token *jwt.Token) (key any, err error) {
return privateKey, nil
}); err != nil {
t.Fatal(err)
}
}
func TestToken_SignedBytes_hmac(t *testing.T) {
token := jwt.NewWithClaims(jwt.SigningMethodHMAC256, jwt.MapClaims{})
privateKey := []byte("helloWorld")
signedBytes, err := token.SignedBytes(privateKey)
if err != nil {
t.Fatal(err)
}
parts := bytes.Split(signedBytes, []byte{'.'})
if len(parts) != 3 {
t.Fatal()
}
signature, err := base64.RawURLEncoding.DecodeString(string(parts[2]))
if err != nil {
t.Fatal(err)
}
if err = token.SigningMethod.Verify(
signedBytes[:bytes.LastIndexByte(signedBytes, '.')],
signature,
privateKey,
); err != nil {
t.Fatal(err)
}
}
func TestToken_SignedBytes_rsa(t *testing.T) {
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
token := jwt.NewWithClaims(jwt.SigningMethodRSA512, jwt.MapClaims{})
signedBytes, err := token.SignedBytes(rsaKey)
if err != nil {
t.Fatal(err)
}
parts := bytes.Split(signedBytes, []byte{'.'})
if len(parts) != 3 {
t.Fatal()
}
signature, err := base64.RawURLEncoding.DecodeString(string(parts[2]))
if err != nil {
t.Fatal(err)
}
if err = token.SigningMethod.Verify(
signedBytes[:bytes.LastIndexByte(signedBytes, '.')],
signature,
&rsaKey.PublicKey,
); err != nil {
t.Fatal(err)
}
}
func TestNew_rsa(t *testing.T) {
rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048)
token := jwt.New(jwt.SigningMethodRSA256)
bsSignature, err := token.SignedBytes(rsaKey)
if err != nil {
t.Fatal(err)
}
vdFunc, err := parser.New().Parse(
string(bsSignature),
func(method string) (jwt.ISigningMethod, error) {
return jwt.SigningMethodRSA256, nil
},
)
if err != nil {
t.Fatal(err)
}
if err = vdFunc(nil, nil, func(token *jwt.Token) (key any, err error) {
return &rsaKey.PublicKey, nil
}); err != nil {
t.Fatal(err)
}
}
func TestNew_ed25519(t *testing.T) {
token := jwt.New(&jwt.SigningMethodED25519{})
publicKey, privateKey, _ := ed25519.GenerateKey(rand.Reader)
bsToken, err := token.SignedBytes(privateKey)
if err != nil {
t.Fatal(err)
}
vdFunc, err := parser.New().Parse(string(bsToken), func(method string) (jwt.ISigningMethod, error) {
return &jwt.SigningMethodED25519{}, nil
})
if err != nil {
t.Fatal(err)
}
if err = vdFunc(nil, nil, func(token *jwt.Token) (key any, err error) {
return publicKey, nil
}); err != nil {
t.Fatal(err)
}
}
func TestNew_ecdsa(t *testing.T) {
token := jwt.New(jwt.SigningMethodECDSA512)
key, _ := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
bsToken, err := token.SignedBytes(key)
if err != nil {
t.Fatal(err)
}
vdFunc, err := parser.New().Parse(string(bsToken), func(method string) (jwt.ISigningMethod, error) {
return jwt.SigningMethodECDSA512, nil
})
if err != nil {
t.Fatal(err)
}
if err = vdFunc(nil, nil, func(token *jwt.Token) (any, error) {
return &key.PublicKey, nil
}); err != nil {
t.Fatal(err)
}
}