File tree 3 files changed +24
-2
lines changed
3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,14 @@ func WithIssuedAt() ParserOption {
58
58
}
59
59
}
60
60
61
+ // WithExpirationRequired returns the ParserOption to make exp claim required.
62
+ // By default exp claim is optional.
63
+ func WithExpirationRequired () ParserOption {
64
+ return func (p * Parser ) {
65
+ p .validator .requireExp = true
66
+ }
67
+ }
68
+
61
69
// WithAudience configures the validator to require the specified audience in
62
70
// the `aud` claim. Validation will fail if the audience is not listed in the
63
71
// token or the `aud` claim is missing.
Original file line number Diff line number Diff line change @@ -423,6 +423,16 @@ var jwtTestData = []struct {
423
423
jwt .NewParser (jwt .WithLeeway (2 * time .Minute )),
424
424
jwt .SigningMethodRS256 ,
425
425
},
426
+ {
427
+ "rejects if exp is required but missing" ,
428
+ "" , // autogen
429
+ defaultKeyFunc ,
430
+ & jwt.RegisteredClaims {},
431
+ false ,
432
+ []error {jwt .ErrTokenInvalidClaims },
433
+ jwt .NewParser (jwt .WithExpirationRequired ()),
434
+ jwt .SigningMethodRS256 ,
435
+ },
426
436
}
427
437
428
438
// signToken creates and returns a signed JWT token using signingMethod.
Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ type validator struct {
42
42
// validation. If unspecified, this defaults to time.Now.
43
43
timeFunc func () time.Time
44
44
45
+ // requireExp specifies whether the exp claim is required
46
+ requireExp bool
47
+
45
48
// verifyIat specifies whether the iat (Issued At) claim will be verified.
46
49
// According to https://www.rfc-editor.org/rfc/rfc7519#section-4.1.6 this
47
50
// only specifies the age of the token, but no validation check is
@@ -86,8 +89,9 @@ func (v *validator) Validate(claims Claims) error {
86
89
}
87
90
88
91
// We always need to check the expiration time, but usage of the claim
89
- // itself is OPTIONAL.
90
- if err = v .verifyExpiresAt (claims , now , false ); err != nil {
92
+ // itself is OPTIONAL by default. requireExp overrides this behavior
93
+ // and makes the exp claim mandatory.
94
+ if err = v .verifyExpiresAt (claims , now , v .requireExp ); err != nil {
91
95
errs = append (errs , err )
92
96
}
93
97
You can’t perform that action at this time.
0 commit comments