Skip to content

Commit ed5c523

Browse files
committed
TestNew_hmac, TestNew_rsa
1 parent 06d25f7 commit ed5c523

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

parser/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (p *Parser) parseHeader(headerStr string) (map[string]any, error) {
171171
return nil, fmt.Errorf("failed to parse header: %w %w", err, jwt.ErrTokenMalformed)
172172
}
173173
if header["typ"] != "JWT" {
174-
return nil, fmt.Errorf("invalid token type: %s %w", header["typ"], jwt.ErrTokenMalformed)
174+
return nil, fmt.Errorf("invalid token type: %v %w", header["typ"], jwt.ErrTokenMalformed)
175175
}
176176
algName, ok := header["alg"]
177177
if !ok {

rsa.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func (m *SigningMethodRSA) Verify(
5858
hasher := m.Hash.New()
5959
hasher.Write(signingBytes)
6060

61-
if rsa.VerifyPKCS1v15(
61+
if err = rsa.VerifyPKCS1v15(
6262
publicKey,
6363
m.Hash, hasher.Sum(nil),
6464
signature, // 計算出來的雜湊值+公鑰+之前的簽名,可以知道是否同源
65-
) != nil {
65+
); err != nil {
6666
return fmt.Errorf("%w %w", err, ErrSignatureInvalid)
6767
}
6868
return nil

token_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,44 @@ import (
88
"encoding/base64"
99
"github.com/CarsonSlovoka/go-jwt"
1010
"github.com/CarsonSlovoka/go-jwt/parser"
11+
"github.com/CarsonSlovoka/go-jwt/validator"
1112
"testing"
1213
)
1314

15+
func TestNew_hmac(t *testing.T) {
16+
token := jwt.NewWithClaims(
17+
jwt.SigningMethodHMAC256,
18+
19+
// 指定所有你想要的Claims
20+
// 如果要擴展可以用MapClaims或者自定義struct把RegisteredClaims加入,可以參考:
21+
// https://github.com/CarsonSlovoka/jwt/blob/5ff3ea21d7624f73baf3f2cd7c89ba5p a4129dedb/validator/validator_test.go#L11-L37
22+
jwt.RegisteredClaims{
23+
Issuer: "c.example.com",
24+
},
25+
)
26+
// token.Header["xx"] = "" // 如果有需要自定義Header可以後補上
27+
privateKey := []byte("key")
28+
bsSignature, err := token.SignedBytes(privateKey)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
p := parser.New(func(v *validator.Validator) {
33+
v.ExpectedIssuer = "c.example.com"
34+
v.VerifyIat = true
35+
})
36+
vdFunc, err := p.Parse(string(bsSignature), func(method string) (jwt.ISigningMethod, error) {
37+
return jwt.SigningMethodHMAC256, nil
38+
})
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
if err = vdFunc(nil, nil, func(token *jwt.Token) (key any, err error) {
43+
return privateKey, nil
44+
}); err != nil {
45+
t.Fatal(err)
46+
}
47+
}
48+
1449
func TestToken_SignedBytes_hmac(t *testing.T) {
1550
token := jwt.NewWithClaims(jwt.SigningMethodHMAC256, jwt.MapClaims{})
1651
privateKey := []byte("helloWorld")
@@ -61,6 +96,29 @@ func TestToken_SignedBytes_rsa(t *testing.T) {
6196
}
6297
}
6398

99+
func TestNew_rsa(t *testing.T) {
100+
rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048)
101+
token := jwt.New(jwt.SigningMethodRSA256)
102+
bsSignature, err := token.SignedBytes(rsaKey)
103+
if err != nil {
104+
t.Fatal(err)
105+
}
106+
vdFunc, err := parser.New().Parse(
107+
string(bsSignature),
108+
func(method string) (jwt.ISigningMethod, error) {
109+
return jwt.SigningMethodRSA256, nil
110+
},
111+
)
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
if err = vdFunc(nil, nil, func(token *jwt.Token) (key any, err error) {
116+
return &rsaKey.PublicKey, nil
117+
}); err != nil {
118+
t.Fatal(err)
119+
}
120+
}
121+
64122
func TestNew_ed25519(t *testing.T) {
65123
token := jwt.New(&jwt.SigningMethodED25519{})
66124
publicKey, privateKey, _ := ed25519.GenerateKey(rand.Reader)

0 commit comments

Comments
 (0)