-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathverify_test.go
163 lines (153 loc) · 4.11 KB
/
verify_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
package jwt_test
import (
"fmt"
"testing"
"time"
"github.com/gbrlsnchs/jwt/v3"
"github.com/gbrlsnchs/jwt/v3/internal"
"github.com/google/go-cmp/cmp"
)
type testPayload struct {
jwt.Payload
String string `json:"string,omitempty"`
Int int `json:"int,omitempty"`
}
type testCase struct {
alg jwt.Algorithm
payload interface{}
verifyAlg jwt.Algorithm
opts []func(*jwt.RawToken)
wantHeader jwt.Header
wantPayload testPayload
signErr error
verifyErr error
}
var (
now = time.Now()
tp = testPayload{
Payload: jwt.Payload{
Issuer: "gbrlsnchs",
Subject: "someone",
Audience: jwt.Audience{"https://golang.org", "https://jwt.io"},
ExpirationTime: jwt.NumericDate(now.Add(24 * 30 * 12 * time.Hour)),
NotBefore: jwt.NumericDate(now.Add(30 * time.Minute)),
IssuedAt: jwt.NumericDate(now),
JWTID: "foobar",
},
String: "foobar",
Int: 1337,
}
)
func TestVerify(t *testing.T) {
testCases := map[string][]testCase{
"HMAC": hmacTestCases,
"RSA": rsaTestCases,
"RSA-PSS": rsaPSSTestCases,
"ECDSA": ecdsaTestCases,
"Ed25519": ed25519TestCases,
}
for k, v := range testCases {
t.Run(k, func(t *testing.T) {
for _, tc := range v {
t.Run(tc.verifyAlg.Name(), func(t *testing.T) {
token, err := jwt.Sign(tc.payload, tc.alg)
if err != nil {
t.Fatal(err)
}
var pl testPayload
hd, err := jwt.Verify(token, tc.verifyAlg, &pl)
if want, got := tc.verifyErr, err; got != want {
t.Errorf("jwt.Verify err mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
if want, got := tc.wantHeader, hd; !cmp.Equal(got, want) {
t.Errorf("jwt.Verify header mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
if want, got := tc.wantPayload, pl; !cmp.Equal(got, want) {
t.Errorf("jwt.Verify payload mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
})
}
t.Run("non-JSON payload", func(t *testing.T) {
var (
header = "eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0" // {"typ":"JWT","alg":"none"}
payload = "NTcwMDU" // 57005
token = fmt.Sprintf("%s.%s.", header, payload)
v interface{}
)
_, err := jwt.Verify([]byte(token), jwt.None(), &v)
if want, got := jwt.ErrNotJSONObject, err; !internal.ErrorIs(got, want) {
t.Errorf("jwt.Verify JSON payload mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
func TestValidatePayload(t *testing.T) {
now := time.Now()
testCases := []struct {
pl *jwt.Payload
vds []jwt.Validator
err error
}{
{
pl: &jwt.Payload{
ExpirationTime: jwt.NumericDate(now.Add(1 * time.Second)),
},
vds: []jwt.Validator{jwt.ExpirationTimeValidator(now)},
err: nil,
},
{
pl: &jwt.Payload{
ExpirationTime: jwt.NumericDate(now.Add(1 * time.Second)),
},
vds: []jwt.Validator{jwt.ExpirationTimeValidator(now.Add(15 * time.Second))},
err: jwt.ErrExpValidation,
},
{
pl: &jwt.Payload{
Subject: "test",
ExpirationTime: jwt.NumericDate(now.Add(1 * time.Second)),
},
vds: []jwt.Validator{
jwt.SubjectValidator("test"),
jwt.ExpirationTimeValidator(now),
},
err: nil,
},
{
pl: &jwt.Payload{
Subject: "foo",
ExpirationTime: jwt.NumericDate(now.Add(1 * time.Second)),
},
vds: []jwt.Validator{
jwt.SubjectValidator("bar"),
jwt.ExpirationTimeValidator(now),
},
err: jwt.ErrSubValidation,
},
{
pl: &jwt.Payload{
Subject: "test",
ExpirationTime: jwt.NumericDate(now.Add(1 * time.Second)),
},
vds: []jwt.Validator{
jwt.SubjectValidator("test"),
jwt.ExpirationTimeValidator(now.Add(15 * time.Second)),
},
err: jwt.ErrExpValidation,
},
}
hs256 := jwt.NewHS256([]byte("secret"))
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
token, err := jwt.Sign(tc.pl, hs256)
if err != nil {
t.Fatal(err)
}
_, err = jwt.Verify(token, hs256, tc.pl, jwt.ValidatePayload(tc.pl, tc.vds...))
if want, got := tc.err, err; !internal.ErrorIs(got, want) {
t.Errorf("jwt.Verify with validators mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})
}
}