@@ -89,205 +89,49 @@ func Test_Validator_Validate(t *testing.T) {
89
89
}
90
90
91
91
func Test_Validator_verifyExpiresAt (t * testing.T ) {
92
- times , err := test_Validator_CreateStaticTimes (t )
93
- if err != nil {
94
- t .Fatal (err )
95
- }
96
-
97
92
type fields struct {
98
- leeway time.Duration
93
+ leeway time.Duration
94
+ timeFunc func () time.Time
99
95
}
100
-
101
96
type args struct {
102
97
claims Claims
98
+ cmp time.Time
103
99
required bool
104
100
}
105
-
106
101
tests := []struct {
107
102
name string
108
103
fields fields
109
104
args args
110
105
wantErr error
111
106
}{
112
107
{
113
- name : "required claim present and valid" ,
114
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .AfterNow )}, required : true },
115
- wantErr : nil ,
116
- },
117
- {
118
- name : "required claim present and expired" ,
119
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .BeforeNow )}, required : true },
120
- wantErr : ErrTokenExpired ,
121
- },
122
- {
123
- name : "required claim present and expired with leeway" ,
124
- fields : fields {leeway : time .Hour * 2 },
125
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .BeforeNow )}, required : true },
126
- wantErr : nil ,
127
- },
128
- {
129
- name : "required claim present and expired past leeway" ,
130
- fields : fields {leeway : time .Minute * 1 },
131
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .BeforeNow )}, required : true },
132
- wantErr : ErrTokenExpired ,
133
- },
134
- {
135
- name : "required claim not provided" ,
136
- args : args {claims : RegisteredClaims {}, required : true },
137
- wantErr : ErrTokenRequiredClaimMissing ,
138
- },
139
- {
140
- name : "required claim present with invalid type" ,
141
- args : args {claims : MapClaims {"exp" : "string" }, required : true },
142
- wantErr : ErrInvalidType ,
143
- },
144
-
145
- {
146
- name : "not required claim present and valid" ,
147
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .AfterNow )}, required : false },
148
- wantErr : nil ,
149
- },
150
- {
151
- name : "not required claim present and expired" ,
152
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .BeforeNow )}, required : false },
153
- wantErr : ErrTokenExpired ,
154
- },
155
- {
156
- name : "not required claim present and expired with leeway" ,
157
- fields : fields {leeway : time .Hour * 2 },
158
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .BeforeNow )}, required : false },
159
- wantErr : nil ,
160
- },
161
- {
162
- name : "not required claim present and expired past leeway" ,
163
- fields : fields {leeway : time .Minute * 1 },
164
- args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (times .BeforeNow )}, required : false },
165
- wantErr : ErrTokenExpired ,
166
- },
167
- {
168
- name : "not required claim not provided" ,
169
- args : args {claims : RegisteredClaims {}, required : false },
108
+ name : "good claim" ,
109
+ fields : fields {timeFunc : time .Now },
110
+ args : args {claims : RegisteredClaims {ExpiresAt : NewNumericDate (time .Now ().Add (10 * time .Minute ))}},
170
111
wantErr : nil ,
171
112
},
172
113
{
173
- name : "not required claim present with invalid type" ,
174
- args : args {claims : MapClaims {"exp" : "string" }, required : false },
114
+ name : "claims with invalid type" ,
115
+ fields : fields {},
116
+ args : args {claims : MapClaims {"exp" : "string" }},
175
117
wantErr : ErrInvalidType ,
176
118
},
177
119
}
178
120
for _ , tt := range tests {
179
121
t .Run (tt .name , func (t * testing.T ) {
180
122
v := & Validator {
181
- leeway : tt .fields .leeway ,
123
+ leeway : tt .fields .leeway ,
124
+ timeFunc : tt .fields .timeFunc ,
182
125
}
183
126
184
- err := v .verifyExpiresAt (tt .args .claims , times . Now , tt .args .required )
185
- if (err != nil || tt . wantErr != nil ) && ! errors .Is (err , tt .wantErr ) {
127
+ err := v .verifyExpiresAt (tt .args .claims , tt . args . cmp , tt .args .required )
128
+ if (err != nil ) && ! errors .Is (err , tt .wantErr ) {
186
129
t .Errorf ("validator.verifyExpiresAt() error = %v, wantErr %v" , err , tt .wantErr )
187
130
}
188
131
})
189
132
}
190
133
}
191
134
192
- func Test_Validator_verifyNotBefore (t * testing.T ) {
193
- times , err := test_Validator_CreateStaticTimes (t )
194
- if err != nil {
195
- t .Fatal (err )
196
- }
197
-
198
- type fields struct {
199
- leeway time.Duration
200
- }
201
- type args struct {
202
- claims Claims
203
- required bool
204
- }
205
- tests := []struct {
206
- name string
207
- fields fields
208
- args args
209
- wantErr error
210
- }{
211
- {
212
- name : "required claim present and valid" ,
213
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .BeforeNow )}, required : true },
214
- wantErr : nil ,
215
- },
216
- {
217
- name : "required claim present and in future" ,
218
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .AfterNow )}, required : true },
219
- wantErr : ErrTokenNotValidYet ,
220
- },
221
- {
222
- name : "required claim present and in future with leeway" ,
223
- fields : fields {leeway : time .Hour * 2 },
224
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .AfterNow )}, required : true },
225
- wantErr : nil ,
226
- },
227
- {
228
- name : "required claim present and in future past leeway" ,
229
- fields : fields {leeway : time .Minute * 1 },
230
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .AfterNow )}, required : true },
231
- wantErr : ErrTokenNotValidYet ,
232
- },
233
- {
234
- name : "required claim not provided" ,
235
- args : args {claims : RegisteredClaims {}, required : true },
236
- wantErr : ErrTokenRequiredClaimMissing ,
237
- },
238
- {
239
- name : "required claim present with invalid type" ,
240
- args : args {claims : MapClaims {"nbf" : "string" }, required : true },
241
- wantErr : ErrInvalidType ,
242
- },
243
-
244
- {
245
- name : "not required claim present and valid" ,
246
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .BeforeNow )}, required : false },
247
- wantErr : nil ,
248
- },
249
- {
250
- name : "not required claim present and in future" ,
251
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .AfterNow )}, required : false },
252
- wantErr : ErrTokenNotValidYet ,
253
- },
254
- {
255
- name : "not required claim present and in future with leeway" ,
256
- fields : fields {leeway : time .Hour * 2 },
257
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .AfterNow )}, required : false },
258
- wantErr : nil ,
259
- },
260
- {
261
- name : "not required claim present and in future past leeway" ,
262
- fields : fields {leeway : time .Minute * 1 },
263
- args : args {claims : RegisteredClaims {NotBefore : NewNumericDate (times .AfterNow )}, required : false },
264
- wantErr : ErrTokenNotValidYet ,
265
- },
266
- {
267
- name : "not required claim not provided" ,
268
- args : args {claims : RegisteredClaims {}, required : false },
269
- wantErr : nil ,
270
- },
271
- {
272
- name : "not required claim present with invalid type" ,
273
- args : args {claims : MapClaims {"nbf" : "string" }, required : false },
274
- wantErr : ErrInvalidType ,
275
- },
276
- }
277
- for _ , tt := range tests {
278
- t .Run (tt .name , func (t * testing.T ) {
279
- v := & Validator {
280
- leeway : tt .fields .leeway ,
281
- }
282
-
283
- err := v .verifyNotBefore (tt .args .claims , times .Now , tt .args .required )
284
- if (err != nil || tt .wantErr != nil ) && ! errors .Is (err , tt .wantErr ) {
285
- t .Errorf ("validator.verifyNotBefore() error = %v, wantErr %v" , err , tt .wantErr )
286
- }
287
- })
288
- }
289
- }
290
-
291
135
func Test_Validator_verifyIssuer (t * testing.T ) {
292
136
type fields struct {
293
137
expectedIss string
@@ -372,15 +216,15 @@ func Test_Validator_verifySubject(t *testing.T) {
372
216
373
217
func Test_Validator_verifyIssuedAt (t * testing.T ) {
374
218
type fields struct {
219
+ leeway time.Duration
220
+ timeFunc func () time.Time
375
221
verifyIat bool
376
222
}
377
-
378
223
type args struct {
379
224
claims Claims
380
225
cmp time.Time
381
226
required bool
382
227
}
383
-
384
228
tests := []struct {
385
229
name string
386
230
fields fields
@@ -406,14 +250,76 @@ func Test_Validator_verifyIssuedAt(t *testing.T) {
406
250
}
407
251
for _ , tt := range tests {
408
252
t .Run (tt .name , func (t * testing.T ) {
409
- v := & Validator {}
410
- if err := v .verifyIssuedAt (tt .args .claims , tt .args .cmp , tt .args .required ); (err != nil || tt .wantErr != nil ) && ! errors .Is (err , tt .wantErr ) {
253
+ v := & Validator {
254
+ leeway : tt .fields .leeway ,
255
+ timeFunc : tt .fields .timeFunc ,
256
+ verifyIat : tt .fields .verifyIat ,
257
+ }
258
+ if err := v .verifyIssuedAt (tt .args .claims , tt .args .cmp , tt .args .required ); (err != nil ) && ! errors .Is (err , tt .wantErr ) {
411
259
t .Errorf ("validator.verifyIssuedAt() error = %v, wantErr %v" , err , tt .wantErr )
412
260
}
413
261
})
414
262
}
415
263
}
416
264
265
+ func Test_Validator_requireNotBefore (t * testing.T ) {
266
+ type fields struct {
267
+ leeway time.Duration
268
+ timeFunc func () time.Time
269
+ requireNbf bool
270
+ }
271
+ type args struct {
272
+ claims Claims
273
+ cmp time.Time
274
+ required bool
275
+ }
276
+ tests := []struct {
277
+ name string
278
+ fields fields
279
+ args args
280
+ wantErr error
281
+ }{
282
+ {
283
+ name : "good claim without nbf" ,
284
+ fields : fields {requireNbf : false },
285
+ args : args {claims : MapClaims {}, required : false },
286
+ wantErr : nil ,
287
+ },
288
+ {
289
+ name : "good claim with nbf" ,
290
+ fields : fields {requireNbf : true },
291
+ args : args {
292
+ claims : RegisteredClaims {NotBefore : NewNumericDate (time .Now ().Add (time .Minute * - 10 ))},
293
+ cmp : time .Now ().Add (10 * time .Minute ),
294
+ required : true ,
295
+ },
296
+ wantErr : nil ,
297
+ },
298
+ {
299
+ name : "token nbf time is in future" ,
300
+ fields : fields {requireNbf : true , timeFunc : time .Now },
301
+ args : args {
302
+ claims : RegisteredClaims {NotBefore : NewNumericDate (time .Now ().Add (time .Minute * + 10 ))},
303
+ cmp : time .Now ().Add (10 * time .Minute ),
304
+ required : true ,
305
+ },
306
+ wantErr : ErrTokenNotValidYet ,
307
+ },
308
+ }
309
+ for _ , tt := range tests {
310
+ t .Run (tt .name , func (t * testing.T ) {
311
+ v := & Validator {
312
+ leeway : tt .fields .leeway ,
313
+ timeFunc : tt .fields .timeFunc ,
314
+ verifyIat : tt .fields .requireNbf ,
315
+ }
316
+ if err := v .verifyNotBefore (tt .args .claims , tt .args .cmp , tt .args .required ); (err != nil ) && ! errors .Is (err , tt .wantErr ) {
317
+ t .Errorf ("validator.requireNotBefore() error = %v, wantErr %v" , err , tt .wantErr )
318
+ }
319
+ })
320
+ }
321
+ }
322
+
417
323
func Test_Validator_verifyAudience (t * testing.T ) {
418
324
type fields struct {
419
325
expectedAud []string
@@ -526,29 +432,3 @@ func Test_Validator_verifyAudience(t *testing.T) {
526
432
})
527
433
}
528
434
}
529
-
530
- // testStaticTimes is a struct that contains 3 timestamps, that are intended to be used to validate functionality
531
- // of registered claim validation.
532
- type testStaticTimes struct {
533
- BeforeNow time.Time
534
- Now time.Time
535
- AfterNow time.Time
536
- }
537
-
538
- // test_Validator_CreateStaticTimes returns a set of timestamps that can be used to validate functionality
539
- // without requiring the use of "time.Now()", which can cause "flakey" tests if there is a delay in when the tests
540
- // run vs when they were started.
541
- func test_Validator_CreateStaticTimes (t * testing.T ) (testStaticTimes , error ) {
542
- t .Helper ()
543
-
544
- staticNow , err := time .Parse (time .RFC3339 , "2025-01-02T15:04:05Z" )
545
- if err != nil {
546
- return testStaticTimes {}, err
547
- }
548
-
549
- return testStaticTimes {
550
- BeforeNow : staticNow .Add (time .Hour * - 1 ),
551
- Now : staticNow ,
552
- AfterNow : staticNow .Add (time .Hour * 1 ),
553
- }, nil
554
- }
0 commit comments