Repository navigation
Expand file tree
/
Copy pathvalidate.go
More file actions
586 lines (519 loc) · 17.4 KB
/
Copy pathvalidate.go
File metadata and controls
586 lines (519 loc) · 17.4 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package jwt
import (
"context"
"fmt"
"reflect"
"slices"
"strconv"
"time"
"github.com/lestrrat-go/option/v3"
)
type Clock interface {
Now() time.Time
}
type ClockFunc func() time.Time
func (f ClockFunc) Now() time.Time {
return f()
}
// isNilValue reports whether v is nil, including a typed-nil interface value
// that wraps a nil pointer (or other nilable kind). A plain `== nil` check
// misses the latter because the interface itself is non-nil.
func isNilValue(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Func, reflect.Map, reflect.Slice, reflect.Chan:
return rv.IsNil()
}
return false
}
func isSupportedTimeClaim(c string) error {
switch c {
case ExpirationKey, IssuedAtKey, NotBeforeKey:
return nil
}
return validateErrorf(`unsupported time claim %s in jwt.WithMaxDelta/jwt.WithMinDelta`, strconv.Quote(c))
}
func timeClaim(t Token, clock Clock, c string) time.Time {
// We don't check if the claims already exist. It should have been done
// by piggybacking on `required` check.
switch c {
case ExpirationKey:
tv, _ := t.Expiration()
return tv
case IssuedAtKey:
tv, _ := t.IssuedAt()
return tv
case NotBeforeKey:
tv, _ := t.NotBefore()
return tv
case "":
return clock.Now()
}
return time.Time{} // should *NEVER* reach here, but...
}
// Validate makes sure that the essential claims stand.
//
// See the various `WithXXX` functions for optional parameters
// that can control the behavior of this method.
func Validate(t Token, options ...ValidateOption) error {
if t == nil {
return validateErrorf(`jwt.Validate: token is nil`)
}
// Fast path: no options means default validation (iat, exp, nbf)
// with no skew, default truncation, and time.Now as clock.
// This avoids context allocation, validator struct creation, and option iteration.
if len(options) == 0 {
return validateDefault(t)
}
ctx := context.Background()
trunc := getDefaultTruncation()
var defaultClock Clock = ClockFunc(time.Now)
clock := defaultClock
var skew time.Duration
var baseValidators = []Validator{
IsIssuedAtValid(),
IsExpirationValid(),
IsNbfValid(),
}
var extraValidators []Validator
var resetValidators bool
var collectErrors bool
var ctxOpt context.Context
for _, o := range options {
switch o.Ident() {
case identClock{}:
// A nil Clock falls back to the default clock (the system
// clock, time.Now). WithClock(nil) stores an untyped nil, so
// option.Get reports ok=false; a typed-nil interface wrapping a
// nil pointer reports ok=true but isNilValue catches it. In both
// cases reset to the default so a later WithClock(nil) undoes an
// earlier custom clock, matching the documented behavior and
// avoiding a panic in clock.Now().
c, ok := option.Get[Clock](o)
if !ok || isNilValue(c) {
clock = defaultClock
} else {
clock = c
}
case identAcceptableSkew{}:
skew = option.MustGet[time.Duration](o)
if skew < 0 {
return fmt.Errorf(`jwt.Validate: WithAcceptableSkew() must not be negative`)
}
case identTruncation{}:
trunc = option.MustGet[time.Duration](o)
case identContext{}:
ctxOpt = option.MustGet[context.Context](o) //nolint:fatcontext // not nesting; selecting from options
case identResetValidators{}:
resetValidators = option.MustGet[bool](o)
case identCollectErrors{}:
collectErrors = option.MustGet[bool](o)
case identValidator{}:
// A nil Validator (including a typed-nil interface wrapping a
// nil pointer) would panic when its Validate method is called,
// so reject it up front with a clear error.
v, ok := option.Get[Validator](o)
if !ok || isNilValue(v) {
return validateErrorf(`jwt.WithValidator: nil Validator`)
}
switch v := v.(type) {
case *isInTimeRange:
if v.c1 != "" {
if err := isSupportedTimeClaim(v.c1); err != nil {
return err
}
extraValidators = append(extraValidators, IsRequired(v.c1))
}
if v.c2 != "" {
if err := isSupportedTimeClaim(v.c2); err != nil {
return err
}
extraValidators = append(extraValidators, IsRequired(v.c2))
}
}
extraValidators = append(extraValidators, v)
}
}
if ctxOpt != nil {
ctx = ctxOpt
}
ctx = SetValidationCtxSkew(ctx, skew)
ctx = SetValidationCtxClock(ctx, clock)
ctx = SetValidationCtxTruncation(ctx, trunc)
// Snapshot "now" once so all default validators observe the same
// instant — matches the fast path in validateDefault and avoids
// second-boundary inconsistency when a custom Clock is stepped
// between validator calls.
ctx = setValidationCtxNow(ctx, clock.Now().Truncate(trunc))
var validators []Validator
if !resetValidators {
validators = append(baseValidators, extraValidators...)
} else {
if len(extraValidators) == 0 {
return validateErrorf(`no validators specified: jwt.WithResetValidators(true) and no jwt.WithValidator() specified`)
}
validators = extraValidators
}
if collectErrors {
var errs []error
for _, v := range validators {
if err := v.Validate(ctx, t); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return validateErrorJoin(errs...)
}
return nil
}
for _, v := range validators {
if err := v.Validate(ctx, t); err != nil {
return validateErrorf(`validation failed: %w`, err)
}
}
return nil
}
// validateDefault is the fast path for Validate with no options.
// It inlines the default iat/exp/nbf checks without allocating
// context values, validator structs, or iterating through options.
//
// Order MUST match the slow path's baseValidators: iat, exp, nbf. A
// token failing multiple checks must produce the same concrete error
// type regardless of whether any option was supplied.
func validateDefault(t Token) error {
trunc := getDefaultTruncation()
now := time.Now().Truncate(trunc)
// iat: issued-at must not be in the future
if tv, ok := t.IssuedAt(); ok {
if now.Before(tv.Truncate(trunc)) {
return validateErrorf(`validation failed: %w`, newInvalidIssuedAtError(tv.Truncate(trunc), now, 0))
}
}
// exp: expiration must be after now
if tv, ok := t.Expiration(); ok {
if !now.Before(tv.Truncate(trunc)) {
return validateErrorf(`validation failed: %w`, newTokenExpiredError(tv.Truncate(trunc), now, 0))
}
}
// nbf: not-before must not be in the future
if tv, ok := t.NotBefore(); ok {
if now.Before(tv.Truncate(trunc)) {
return validateErrorf(`validation failed: %w`, newTokenNotYetValidError(tv.Truncate(trunc), now, 0))
}
}
return nil
}
type isInTimeRange struct {
c1 string
c2 string
dur time.Duration
less bool // if true, d =< c1 - c2. otherwise d >= c1 - c2
}
// MaxDeltaIs implements the logic behind `WithMaxDelta()` option
func MaxDeltaIs(c1, c2 string, dur time.Duration) Validator {
return &isInTimeRange{
c1: c1,
c2: c2,
dur: dur,
less: true,
}
}
// MinDeltaIs implements the logic behind `WithMinDelta()` option
func MinDeltaIs(c1, c2 string, dur time.Duration) Validator {
return &isInTimeRange{
c1: c1,
c2: c2,
dur: dur,
less: false,
}
}
func (iitr *isInTimeRange) Validate(ctx context.Context, t Token) error {
clock := ValidationCtxClock(ctx) // MUST be populated
skew := ValidationCtxSkew(ctx) // MUST be populated
t1 := timeClaim(t, clock, iitr.c1)
t2 := timeClaim(t, clock, iitr.c2)
// Defensive: reject zero-value claims before computing delta. The
// auto-IsRequired piggyback in WithValidator type-switches on the
// concrete *isInTimeRange — wrapping this validator (e.g., in
// ValidatorFunc) skips that piggyback, and a missing time claim
// would silently produce a hugely-negative delta that trivially
// satisfies the upper-bound check. Reject the missing claim
// regardless of how the validator was wrapped.
if t1.IsZero() {
return missingRequiredClaimErrorf(iitr.c1)
}
if t2.IsZero() {
return missingRequiredClaimErrorf(iitr.c2)
}
delta := t1.Sub(t2)
var msg string
if iitr.less {
// t1 - t2 <= iitr.dur + skew
if delta <= iitr.dur+skew {
return nil
}
msg = fmt.Sprintf(`delta between %s and %s exceeds %s (skew %s)`, iitr.c1, iitr.c2, iitr.dur, skew)
} else {
// t1 - t2 >= iitr.dur - skew
if delta >= iitr.dur-skew {
return nil
}
msg = fmt.Sprintf(`delta between %s and %s is less than %s (skew %s)`, iitr.c1, iitr.c2, iitr.dur, skew)
}
return newTimeDeltaError(msg, iitr.c1, iitr.c2, t1, t2, delta, iitr.dur, skew)
}
// Validator describes interface to validate a Token.
type Validator interface {
// Validate should return an error if a required conditions is not met.
Validate(context.Context, Token) error
}
// ValidatorFunc is a type of Validator that does not have any
// state, that is implemented as a function
type ValidatorFunc func(context.Context, Token) error
func (vf ValidatorFunc) Validate(ctx context.Context, tok Token) error {
return vf(ctx, tok)
}
type identValidationCtxClock struct{}
type identValidationCtxSkew struct{}
type identValidationCtxTruncation struct{}
type identValidationCtxNow struct{}
func setValidationCtxNow(ctx context.Context, now time.Time) context.Context {
return context.WithValue(ctx, identValidationCtxNow{}, now)
}
// validationCtxNow returns the "now" snapshotted by [Validate] for this
// validation run. If the context was not initialized by [Validate]
// (e.g. a custom validator invoked with a bare context), it falls back
// to sampling the supplied clock.
func validationCtxNow(ctx context.Context, clock Clock, trunc time.Duration) time.Time {
if v, ok := ctx.Value(identValidationCtxNow{}).(time.Time); ok {
return v
}
return clock.Now().Truncate(trunc)
}
func SetValidationCtxClock(ctx context.Context, cl Clock) context.Context {
return context.WithValue(ctx, identValidationCtxClock{}, cl)
}
func SetValidationCtxTruncation(ctx context.Context, dur time.Duration) context.Context {
return context.WithValue(ctx, identValidationCtxTruncation{}, dur)
}
func SetValidationCtxSkew(ctx context.Context, dur time.Duration) context.Context {
return context.WithValue(ctx, identValidationCtxSkew{}, dur)
}
// ValidationCtxClock returns the Clock object associated with
// the current validation context. When called from within a Validator
// invoked by [Validate], the value is always populated. If the context
// was not initialized by [Validate] (for example, a custom validator
// was invoked with a bare context), a default clock backed by
// [time.Now] is returned instead of panicking.
func ValidationCtxClock(ctx context.Context) Clock {
if cl, ok := ctx.Value(identValidationCtxClock{}).(Clock); ok {
return cl
}
return ClockFunc(time.Now)
}
// ValidationCtxSkew returns the clock skew associated with the current
// validation context. If the context was not initialized by [Validate],
// zero is returned instead of panicking.
func ValidationCtxSkew(ctx context.Context) time.Duration {
if dur, ok := ctx.Value(identValidationCtxSkew{}).(time.Duration); ok {
return dur
}
return 0
}
// ValidationCtxTruncation returns the truncation granularity associated
// with the current validation context. If the context was not initialized
// by [Validate], zero is returned instead of panicking.
func ValidationCtxTruncation(ctx context.Context) time.Duration {
if dur, ok := ctx.Value(identValidationCtxTruncation{}).(time.Duration); ok {
return dur
}
return 0
}
// IsExpirationValid is one of the default validators that will be executed.
// It does not need to be specified by users, but it exists as an
// exported field so that you can check what it does.
//
// The supplied context.Context object must have the "clock" and "skew"
// populated with appropriate values using SetValidationCtxClock() and
// SetValidationCtxSkew()
func IsExpirationValid() Validator {
return ValidatorFunc(isExpirationValid)
}
func isExpirationValid(ctx context.Context, t Token) error {
tv, ok := t.Expiration()
if !ok {
return nil
}
clock := ValidationCtxClock(ctx) // MUST be populated
skew := ValidationCtxSkew(ctx) // MUST be populated
trunc := ValidationCtxTruncation(ctx) // MUST be populated
now := validationCtxNow(ctx, clock, trunc)
ttv := tv.Truncate(trunc)
// expiration date must be after NOW
if !now.Before(ttv.Add(skew)) {
return newTokenExpiredError(ttv, now, skew)
}
return nil
}
// IsIssuedAtValid is one of the default validators that will be executed.
// It does not need to be specified by users, but it exists as an
// exported field so that you can check what it does.
//
// The supplied context.Context object must have the "clock" and "skew"
// populated with appropriate values using SetValidationCtxClock() and
// SetValidationCtxSkew()
func IsIssuedAtValid() Validator {
return ValidatorFunc(isIssuedAtValid)
}
func isIssuedAtValid(ctx context.Context, t Token) error {
tv, ok := t.IssuedAt()
if !ok {
return nil
}
clock := ValidationCtxClock(ctx) // MUST be populated
skew := ValidationCtxSkew(ctx) // MUST be populated
trunc := ValidationCtxTruncation(ctx) // MUST be populated
now := validationCtxNow(ctx, clock, trunc)
ttv := tv.Truncate(trunc)
if now.Before(ttv.Add(-1 * skew)) {
return newInvalidIssuedAtError(ttv, now, skew)
}
return nil
}
// IsNbfValid is one of the default validators that will be executed.
// It does not need to be specified by users, but it exists as an
// exported field so that you can check what it does.
//
// The supplied context.Context object must have the "clock" and "skew"
// populated with appropriate values using SetValidationCtxClock() and
// SetValidationCtxSkew()
func IsNbfValid() Validator {
return ValidatorFunc(isNbfValid)
}
func isNbfValid(ctx context.Context, t Token) error {
tv, ok := t.NotBefore()
if !ok {
return nil
}
clock := ValidationCtxClock(ctx) // MUST be populated
skew := ValidationCtxSkew(ctx) // MUST be populated
trunc := ValidationCtxTruncation(ctx) // MUST be populated
// Truncation always happens even for trunc = 0 because
// we also use this to strip monotonic clocks
now := validationCtxNow(ctx, clock, trunc)
ttv := tv.Truncate(trunc)
// "now" cannot be before t - skew, so we check for now > t - skew
if now.Before(ttv.Add(-1 * skew)) {
return newTokenNotYetValidError(ttv, now, skew)
}
return nil
}
type claimContainsString struct {
name string
value string
makeErr func(string, ...any) error
}
// ClaimContainsString can be used to check if the claim called `name`, which is
// expected to be a list of strings, contains `value`. Currently, because of the
// implementation, this will probably only work for `aud` fields.
func ClaimContainsString(name, value string) Validator {
return claimContainsString{
name: name,
value: value,
}
}
func (ccs claimContainsString) Validate(_ context.Context, t Token) error {
v, ok := t.Field(ccs.name)
if !ok {
if ccs.makeErr != nil {
return ccs.makeErr(`claim %q does not exist`, ccs.name)
}
return newClaimValidationError(ccs.name, ccs.value, nil,
fmt.Sprintf(`claim %q does not exist`, ccs.name))
}
list, ok := v.([]string)
if !ok {
if ccs.makeErr != nil {
return ccs.makeErr(`claim %q is not a []string`, ccs.name)
}
return newClaimValidationError(ccs.name, ccs.value, v,
fmt.Sprintf(`claim %q is not a []string`, ccs.name))
}
if !slices.Contains(list, ccs.value) {
if ccs.makeErr != nil {
return ccs.makeErr(`claim %q does not contain the expected value`, ccs.name)
}
return newClaimValidationError(ccs.name, ccs.value, list,
fmt.Sprintf(`claim %q does not contain the expected value`, ccs.name))
}
return nil
}
// audienceClaimContainsString can be used to check if the audience claim, which is
// expected to be a list of strings, contains `value`.
func audienceClaimContainsString(value string) Validator {
return claimContainsString{
name: AudienceKey,
value: value,
makeErr: audienceErrorf,
}
}
type claimValueIs struct {
name string
value any
makeErr func(string, ...any) error
}
// ClaimValueIs creates a Validator that checks if the value of claim `name`
// matches `value`. The comparison is done with reflect.DeepEqual, so
// slice-, map-, and struct-valued claims are supported in addition to
// scalars. Function-valued claims follow reflect.DeepEqual semantics
// (equal only when both sides are nil). If you need finer-grained
// matching than DeepEqual provides, use a custom Validator.
func ClaimValueIs(name string, value any) Validator {
return &claimValueIs{
name: name,
value: value,
}
}
func (cv *claimValueIs) Validate(_ context.Context, t Token) error {
v, ok := t.Field(cv.name)
if !ok {
if cv.makeErr != nil {
return cv.makeErr(`claim %[1]q does not exist`, cv.name)
}
return newClaimValidationError(cv.name, cv.value, nil,
fmt.Sprintf(`claim %q does not exist`, cv.name))
}
if !reflect.DeepEqual(v, cv.value) {
if cv.makeErr != nil {
return cv.makeErr(`claim %[1]q does not have the expected value`, cv.name)
}
return newClaimValidationError(cv.name, cv.value, v,
fmt.Sprintf(`claim %q does not have the expected value`, cv.name))
}
return nil
}
// issuerClaimValueIs creates a Validator that checks if the issuer claim
// matches `value`.
func issuerClaimValueIs(value string) Validator {
return &claimValueIs{
name: IssuerKey,
value: value,
makeErr: issuerErrorf,
}
}
// IsRequired creates a Validator that checks if the required claim `name`
// exists in the token
func IsRequired(name string) Validator {
return isRequired(name)
}
type isRequired string
func (ir isRequired) Validate(_ context.Context, t Token) error {
name := string(ir)
if !t.Has(name) {
return missingRequiredClaimErrorf(name)
}
return nil
}