@@ -53,7 +53,8 @@ func newValidator(opts ...ParserOption) *validator {
53
53
return p .validator
54
54
}
55
55
56
- // Validate validates the given claims. It will also perform any custom validation if claims implements the CustomValidator interface.
56
+ // Validate validates the given claims. It will also perform any custom
57
+ // validation if claims implements the CustomValidator interface.
57
58
func (v * validator ) Validate (claims Claims ) error {
58
59
var now time.Time
59
60
vErr := new (ValidationError )
@@ -65,13 +66,15 @@ func (v *validator) Validate(claims Claims) error {
65
66
now = time .Now ()
66
67
}
67
68
68
- // We always need to check the expiration time, but the claim itself is OPTIONAL
69
+ // We always need to check the expiration time, but usage of the claim
70
+ // itself is OPTIONAL
69
71
if ! v .VerifyExpiresAt (claims , now , false ) {
70
72
vErr .Inner = ErrTokenExpired
71
73
vErr .Errors |= ValidationErrorExpired
72
74
}
73
75
74
- // We always need to check not-before, but the claim itself is OPTIONAL
76
+ // We always need to check not-before, but usage of the claim itself is
77
+ // OPTIONAL
75
78
if ! v .VerifyNotBefore (claims , now , false ) {
76
79
vErr .Inner = ErrTokenNotValidYet
77
80
vErr .Errors |= ValidationErrorNotValidYet
@@ -102,7 +105,7 @@ func (v *validator) Validate(claims Claims) error {
102
105
}
103
106
104
107
// Finally, we want to give the claim itself some possibility to do some
105
- // additional custom validation based on their custom claims
108
+ // additional custom validation based on a custom function
106
109
cvt , ok := claims .(CustomClaims )
107
110
if ok {
108
111
if err := cvt .CustomValidation (); err != nil {
@@ -118,90 +121,86 @@ func (v *validator) Validate(claims Claims) error {
118
121
return vErr
119
122
}
120
123
121
- // VerifyAudience compares the aud claim against cmp.
122
- // If required is false, this method will return true if the value matches or is unset
123
- func (v * validator ) VerifyAudience (claims Claims , cmp string , req bool ) bool {
124
- aud , err := claims .GetAudience ()
125
- if err != nil {
126
- return false
127
- }
128
-
129
- return verifyAud (aud , cmp , req )
130
- }
131
-
132
- // VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
133
- // If req is false, it will return true, if exp is unset.
134
- func (v * validator ) VerifyExpiresAt (claims Claims , cmp time.Time , req bool ) bool {
135
- var time * time.Time = nil
136
-
124
+ // VerifyExpiresAt compares the exp claim in claims against cmp. This function
125
+ // will return true if cmp < exp. Additional leeway is taken into account.
126
+ //
127
+ // If exp is not set, it will return true if the claim is not required,
128
+ // otherwise false will be returned.
129
+ //
130
+ // Additionally, if any error occurs while retrieving the claim, e.g., when its
131
+ // the wrong type, false will be returned.
132
+ func (v * validator ) VerifyExpiresAt (claims Claims , cmp time.Time , required bool ) bool {
137
133
exp , err := claims .GetExpirationTime ()
138
134
if err != nil {
139
135
return false
140
- } else if exp != nil {
141
- time = & exp .Time
142
136
}
143
137
144
- return verifyExp (time , cmp , req , v .leeway )
138
+ if exp != nil {
139
+ return cmp .Before ((exp .Time ).Add (+ v .leeway ))
140
+ } else {
141
+ return ! required
142
+ }
145
143
}
146
144
147
- // VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
148
- // If req is false, it will return true, if iat is unset.
149
- func (v * validator ) VerifyIssuedAt (claims Claims , cmp time.Time , req bool ) bool {
150
- var time * time.Time = nil
151
-
145
+ // VerifyIssuedAt compares the iat claim in claims against cmp. This function
146
+ // will return true if cmp >= iat. Additional leeway is taken into account.
147
+ //
148
+ // If iat is not set, it will return true if the claim is not required,
149
+ // otherwise false will be returned.
150
+ //
151
+ // Additionally, if any error occurs while retrieving the claim, e.g., when its
152
+ // the wrong type, false will be returned.
153
+ func (v * validator ) VerifyIssuedAt (claims Claims , cmp time.Time , required bool ) bool {
152
154
iat , err := claims .GetIssuedAt ()
153
155
if err != nil {
154
156
return false
155
- } else if iat != nil {
156
- time = & iat .Time
157
157
}
158
158
159
- return verifyIat (time , cmp , req , v .leeway )
159
+ if iat != nil {
160
+ return ! cmp .Before (iat .Add (- v .leeway ))
161
+ } else {
162
+ return ! required
163
+ }
160
164
}
161
165
162
- // VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
163
- // If req is false, it will return true, if nbf is unset.
164
- func (v * validator ) VerifyNotBefore (claims Claims , cmp time.Time , req bool ) bool {
165
- var time * time.Time = nil
166
-
166
+ // VerifyNotBefore compares the nbf claim in claims against cmp. This function
167
+ // will return true if cmp >= nbf. Additional leeway is taken into account.
168
+ //
169
+ // If nbf is not set, it will return true if the claim is not required,
170
+ // otherwise false will be returned.
171
+ //
172
+ // Additionally, if any error occurs while retrieving the claim, e.g., when its
173
+ // the wrong type, false will be returned.
174
+ func (v * validator ) VerifyNotBefore (claims Claims , cmp time.Time , required bool ) bool {
167
175
nbf , err := claims .GetNotBefore ()
168
176
if err != nil {
169
177
return false
170
- } else if nbf != nil {
171
- time = & nbf .Time
172
178
}
173
179
174
- return verifyNbf (time , cmp , req , v .leeway )
175
- }
176
-
177
- // VerifyIssuer compares the iss claim against cmp.
178
- // If required is false, this method will return true if the value matches or is unset
179
- func (v * validator ) VerifyIssuer (claims Claims , cmp string , req bool ) bool {
180
- iss , err := claims .GetIssuer ()
181
- if err != nil {
182
- return false
180
+ if nbf != nil {
181
+ return ! cmp .Before (nbf .Add (- v .leeway ))
182
+ } else {
183
+ return ! required
183
184
}
184
-
185
- return verifyIss (iss , cmp , req )
186
185
}
187
186
188
- // VerifySubject compares the sub claim against cmp.
189
- // If required is false, this method will return true if the value matches or is unset
190
- func (v * validator ) VerifySubject (claims Claims , cmp string , req bool ) bool {
191
- iss , err := claims .GetSubject ()
187
+ // VerifyAudience compares the aud claim against cmp.
188
+ //
189
+ // If aud is not set or an empty list, it will return true if the claim is not
190
+ // required, otherwise false will be returned.
191
+ //
192
+ // Additionally, if any error occurs while retrieving the claim, e.g., when its
193
+ // the wrong type, false will be returned.
194
+ func (v * validator ) VerifyAudience (claims Claims , cmp string , required bool ) bool {
195
+ aud , err := claims .GetAudience ()
192
196
if err != nil {
193
197
return false
194
198
}
195
199
196
- return verifySub (iss , cmp , req )
197
- }
198
-
199
- // ----- helpers
200
-
201
- func verifyAud (aud []string , cmp string , required bool ) bool {
202
200
if len (aud ) == 0 {
203
201
return ! required
204
202
}
203
+
205
204
// use a var here to keep constant time compare when looping over a number of claims
206
205
result := false
207
206
@@ -221,41 +220,39 @@ func verifyAud(aud []string, cmp string, required bool) bool {
221
220
return result
222
221
}
223
222
224
- func verifyExp (exp * time.Time , now time.Time , required bool , skew time.Duration ) bool {
225
- if exp == nil {
226
- return ! required
227
- }
228
-
229
- return now .Before ((* exp ).Add (+ skew ))
230
- }
231
-
232
- func verifyIat (iat * time.Time , now time.Time , required bool , skew time.Duration ) bool {
233
- if iat == nil {
234
- return ! required
235
- }
236
-
237
- t := iat .Add (- skew )
238
- return ! now .Before (t )
239
- }
240
-
241
- func verifyNbf (nbf * time.Time , now time.Time , required bool , skew time.Duration ) bool {
242
- if nbf == nil {
243
- return ! required
223
+ // VerifyIssuer compares the iss claim in claims against cmp.
224
+ //
225
+ // If iss is not set, it will return true if the claim is not required,
226
+ // otherwise false will be returned.
227
+ //
228
+ // Additionally, if any error occurs while retrieving the claim, e.g., when its
229
+ // the wrong type, false will be returned.
230
+ func (v * validator ) VerifyIssuer (claims Claims , cmp string , required bool ) bool {
231
+ iss , err := claims .GetIssuer ()
232
+ if err != nil {
233
+ return false
244
234
}
245
235
246
- t := nbf .Add (- skew )
247
- return ! now .Before (t )
248
- }
249
-
250
- func verifyIss (iss string , cmp string , required bool ) bool {
251
236
if iss == "" {
252
237
return ! required
253
238
}
254
239
255
240
return iss == cmp
256
241
}
257
242
258
- func verifySub (sub string , cmp string , required bool ) bool {
243
+ // VerifySubject compares the sub claim against cmp.
244
+ //
245
+ // If sub is not set, it will return true if the claim is not required,
246
+ // otherwise false will be returned.
247
+ //
248
+ // Additionally, if any error occurs while retrieving the claim, e.g., when its
249
+ // the wrong type, false will be returned.
250
+ func (v * validator ) VerifySubject (claims Claims , cmp string , required bool ) bool {
251
+ sub , err := claims .GetSubject ()
252
+ if err != nil {
253
+ return false
254
+ }
255
+
259
256
if sub == "" {
260
257
return ! required
261
258
}
0 commit comments