9
9
// Claims must just have a Valid method that determines
10
10
// if the token is invalid for any supported reason
11
11
type Claims interface {
12
- Valid (options ... * ValidatorOptions ) error
12
+ Valid (options ... ValidatorOption ) error
13
13
}
14
14
15
15
// RegisteredClaims are a structured version of the JWT Claims Set,
@@ -48,14 +48,13 @@ type RegisteredClaims struct {
48
48
// There is no accounting for clock skew.
49
49
// As well, if any of the above claims are not in the token, it will still
50
50
// be considered a valid claim.
51
- func (c RegisteredClaims ) Valid (opts ... * ValidatorOptions ) error {
51
+ func (c RegisteredClaims ) Valid (opts ... ValidatorOption ) error {
52
52
vErr := new (ValidationError )
53
53
now := TimeFunc ()
54
- o := MergeValidatorOptions (opts ... )
55
54
56
55
// The claims below are optional, by default, so if they are set to the
57
56
// default value in Go, let's not fail the verification for them.
58
- if ! c .VerifyExpiresAt (now , false , o ) {
57
+ if ! c .VerifyExpiresAt (now , false , opts ... ) {
59
58
delta := now .Sub (c .ExpiresAt .Time )
60
59
vErr .Inner = fmt .Errorf ("%s by %v" , delta , ErrTokenExpired )
61
60
vErr .Errors |= ValidationErrorExpired
@@ -66,7 +65,7 @@ func (c RegisteredClaims) Valid(opts ...*ValidatorOptions) error {
66
65
vErr .Errors |= ValidationErrorIssuedAt
67
66
}
68
67
69
- if ! c .VerifyNotBefore (now , false , o ) {
68
+ if ! c .VerifyNotBefore (now , false , opts ... ) {
70
69
vErr .Inner = ErrTokenNotValidYet
71
70
vErr .Errors |= ValidationErrorNotValidYet
72
71
}
@@ -86,17 +85,16 @@ func (c *RegisteredClaims) VerifyAudience(cmp string, req bool) bool {
86
85
87
86
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
88
87
// If req is false, it will return true, if exp is unset.
89
- func (c * RegisteredClaims ) VerifyExpiresAt (cmp time.Time , req bool , opts ... * ValidatorOptions ) bool {
90
- var s time.Duration
91
- o := MergeValidatorOptions (opts ... )
92
- if o != nil {
93
- s = o .leeway
88
+ func (c * RegisteredClaims ) VerifyExpiresAt (cmp time.Time , req bool , opts ... ValidatorOption ) bool {
89
+ validator := ValidatorOptions {}
90
+ for _ , o := range opts {
91
+ o (& validator )
94
92
}
95
93
if c .ExpiresAt == nil {
96
- return verifyExp (nil , cmp , req , s )
94
+ return verifyExp (nil , cmp , req , validator . leeway )
97
95
}
98
96
99
- return verifyExp (& c .ExpiresAt .Time , cmp , req , s )
97
+ return verifyExp (& c .ExpiresAt .Time , cmp , req , validator . leeway )
100
98
}
101
99
102
100
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
@@ -111,17 +109,16 @@ func (c *RegisteredClaims) VerifyIssuedAt(cmp time.Time, req bool) bool {
111
109
112
110
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
113
111
// If req is false, it will return true, if nbf is unset.
114
- func (c * RegisteredClaims ) VerifyNotBefore (cmp time.Time , req bool , opts ... * ValidatorOptions ) bool {
115
- var s time.Duration
116
- o := MergeValidatorOptions (opts ... )
117
- if o != nil {
118
- s = o .leeway
112
+ func (c * RegisteredClaims ) VerifyNotBefore (cmp time.Time , req bool , opts ... ValidatorOption ) bool {
113
+ validator := ValidatorOptions {}
114
+ for _ , o := range opts {
115
+ o (& validator )
119
116
}
120
117
if c .NotBefore == nil {
121
- return verifyNbf (nil , cmp , req , s )
118
+ return verifyNbf (nil , cmp , req , validator . leeway )
122
119
}
123
120
124
- return verifyNbf (& c .NotBefore .Time , cmp , req , s )
121
+ return verifyNbf (& c .NotBefore .Time , cmp , req , validator . leeway )
125
122
}
126
123
127
124
// VerifyIssuer compares the iss claim against cmp.
@@ -152,14 +149,13 @@ type StandardClaims struct {
152
149
// Valid validates time based claims "exp, iat, nbf". There is no accounting for clock skew.
153
150
// As well, if any of the above claims are not in the token, it will still
154
151
// be considered a valid claim.
155
- func (c StandardClaims ) Valid (opts ... * ValidatorOptions ) error {
152
+ func (c StandardClaims ) Valid (opts ... ValidatorOption ) error {
156
153
vErr := new (ValidationError )
157
154
now := TimeFunc ().Unix ()
158
- o := MergeValidatorOptions (opts ... )
159
155
160
156
// The claims below are optional, by default, so if they are set to the
161
157
// default value in Go, let's not fail the verification for them.
162
- if ! c .VerifyExpiresAt (now , false , o ) {
158
+ if ! c .VerifyExpiresAt (now , false , opts ... ) {
163
159
delta := time .Unix (now , 0 ).Sub (time .Unix (c .ExpiresAt , 0 ))
164
160
vErr .Inner = fmt .Errorf ("%s by %v" , delta , ErrTokenExpired )
165
161
vErr .Errors |= ValidationErrorExpired
@@ -170,7 +166,7 @@ func (c StandardClaims) Valid(opts ...*ValidatorOptions) error {
170
166
vErr .Errors |= ValidationErrorIssuedAt
171
167
}
172
168
173
- if ! c .VerifyNotBefore (now , false , o ) {
169
+ if ! c .VerifyNotBefore (now , false , opts ... ) {
174
170
vErr .Inner = ErrTokenNotValidYet
175
171
vErr .Errors |= ValidationErrorNotValidYet
176
172
}
@@ -190,18 +186,17 @@ func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
190
186
191
187
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
192
188
// If req is false, it will return true, if exp is unset.
193
- func (c * StandardClaims ) VerifyExpiresAt (cmp int64 , req bool , opts ... * ValidatorOptions ) bool {
194
- var s time.Duration
195
- o := MergeValidatorOptions (opts ... )
196
- if o != nil {
197
- s = o .leeway
189
+ func (c * StandardClaims ) VerifyExpiresAt (cmp int64 , req bool , opts ... ValidatorOption ) bool {
190
+ validator := ValidatorOptions {}
191
+ for _ , o := range opts {
192
+ o (& validator )
198
193
}
199
194
if c .ExpiresAt == 0 {
200
- return verifyExp (nil , time .Unix (cmp , 0 ), req , s )
195
+ return verifyExp (nil , time .Unix (cmp , 0 ), req , validator . leeway )
201
196
}
202
197
203
198
t := time .Unix (c .ExpiresAt , 0 )
204
- return verifyExp (& t , time .Unix (cmp , 0 ), req , s )
199
+ return verifyExp (& t , time .Unix (cmp , 0 ), req , validator . leeway )
205
200
}
206
201
207
202
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
@@ -217,18 +212,17 @@ func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
217
212
218
213
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
219
214
// If req is false, it will return true, if nbf is unset.
220
- func (c * StandardClaims ) VerifyNotBefore (cmp int64 , req bool , opts ... * ValidatorOptions ) bool {
221
- var s time.Duration
222
- o := MergeValidatorOptions (opts ... )
223
- if o != nil {
224
- s = o .leeway
215
+ func (c * StandardClaims ) VerifyNotBefore (cmp int64 , req bool , opts ... ValidatorOption ) bool {
216
+ validator := ValidatorOptions {}
217
+ for _ , o := range opts {
218
+ o (& validator )
225
219
}
226
220
if c .NotBefore == 0 {
227
- return verifyNbf (nil , time .Unix (cmp , 0 ), req , s )
221
+ return verifyNbf (nil , time .Unix (cmp , 0 ), req , validator . leeway )
228
222
}
229
223
230
224
t := time .Unix (c .NotBefore , 0 )
231
- return verifyNbf (& t , time .Unix (cmp , 0 ), req , s )
225
+ return verifyNbf (& t , time .Unix (cmp , 0 ), req , validator . leeway )
232
226
}
233
227
234
228
// VerifyIssuer compares the iss claim against cmp.
0 commit comments