@@ -28,13 +28,12 @@ type ClaimsValidator interface {
28
28
Validate () error
29
29
}
30
30
31
- // validator is the core of the new Validation API. It is automatically used by
31
+ // Validator is the core of the new Validation API. It is automatically used by
32
32
// a [Parser] during parsing and can be modified with various parser options.
33
33
//
34
- // Note: This struct is intentionally not exported (yet) as we want to
35
- // internally finalize its API. In the future, we might make it publicly
36
- // available.
37
- type validator struct {
34
+ // The [NewValidator] function should be used to create an instance of this
35
+ // struct.
36
+ type Validator struct {
38
37
// leeway is an optional leeway that can be provided to account for clock skew.
39
38
leeway time.Duration
40
39
@@ -65,16 +64,28 @@ type validator struct {
65
64
expectedSub string
66
65
}
67
66
68
- // newValidator can be used to create a stand-alone validator with the supplied
67
+ // NewValidator can be used to create a stand-alone validator with the supplied
69
68
// options. This validator can then be used to validate already parsed claims.
70
- func newValidator (opts ... ParserOption ) * validator {
69
+ //
70
+ // Note: Under normal circumstances, explicitly creating a validator is not
71
+ // needed and can potentially be dangerous; instead functions of the [Parser]
72
+ // class should be used.
73
+ //
74
+ // The [Validator] is only checking the *validity* of the claims, such as its
75
+ // expiration time, but it does NOT perform *signature verification* of the
76
+ // token.
77
+ func NewValidator (opts ... ParserOption ) * Validator {
71
78
p := NewParser (opts ... )
72
79
return p .validator
73
80
}
74
81
75
82
// Validate validates the given claims. It will also perform any custom
76
83
// validation if claims implements the [ClaimsValidator] interface.
77
- func (v * validator ) Validate (claims Claims ) error {
84
+ //
85
+ // Note: It will NOT perform any *signature verification* on the token that
86
+ // contains the claims and expects that the [Claim] was already successfully
87
+ // verified.
88
+ func (v * Validator ) Validate (claims Claims ) error {
78
89
var (
79
90
now time.Time
80
91
errs []error = make ([]error , 0 , 6 )
@@ -153,7 +164,7 @@ func (v *validator) Validate(claims Claims) error {
153
164
//
154
165
// Additionally, if any error occurs while retrieving the claim, e.g., when its
155
166
// the wrong type, an ErrTokenUnverifiable error will be returned.
156
- func (v * validator ) verifyExpiresAt (claims Claims , cmp time.Time , required bool ) error {
167
+ func (v * Validator ) verifyExpiresAt (claims Claims , cmp time.Time , required bool ) error {
157
168
exp , err := claims .GetExpirationTime ()
158
169
if err != nil {
159
170
return err
@@ -174,7 +185,7 @@ func (v *validator) verifyExpiresAt(claims Claims, cmp time.Time, required bool)
174
185
//
175
186
// Additionally, if any error occurs while retrieving the claim, e.g., when its
176
187
// the wrong type, an ErrTokenUnverifiable error will be returned.
177
- func (v * validator ) verifyIssuedAt (claims Claims , cmp time.Time , required bool ) error {
188
+ func (v * Validator ) verifyIssuedAt (claims Claims , cmp time.Time , required bool ) error {
178
189
iat , err := claims .GetIssuedAt ()
179
190
if err != nil {
180
191
return err
@@ -195,7 +206,7 @@ func (v *validator) verifyIssuedAt(claims Claims, cmp time.Time, required bool)
195
206
//
196
207
// Additionally, if any error occurs while retrieving the claim, e.g., when its
197
208
// the wrong type, an ErrTokenUnverifiable error will be returned.
198
- func (v * validator ) verifyNotBefore (claims Claims , cmp time.Time , required bool ) error {
209
+ func (v * Validator ) verifyNotBefore (claims Claims , cmp time.Time , required bool ) error {
199
210
nbf , err := claims .GetNotBefore ()
200
211
if err != nil {
201
212
return err
@@ -215,7 +226,7 @@ func (v *validator) verifyNotBefore(claims Claims, cmp time.Time, required bool)
215
226
//
216
227
// Additionally, if any error occurs while retrieving the claim, e.g., when its
217
228
// the wrong type, an ErrTokenUnverifiable error will be returned.
218
- func (v * validator ) verifyAudience (claims Claims , cmp string , required bool ) error {
229
+ func (v * Validator ) verifyAudience (claims Claims , cmp string , required bool ) error {
219
230
aud , err := claims .GetAudience ()
220
231
if err != nil {
221
232
return err
@@ -251,7 +262,7 @@ func (v *validator) verifyAudience(claims Claims, cmp string, required bool) err
251
262
//
252
263
// Additionally, if any error occurs while retrieving the claim, e.g., when its
253
264
// the wrong type, an ErrTokenUnverifiable error will be returned.
254
- func (v * validator ) verifyIssuer (claims Claims , cmp string , required bool ) error {
265
+ func (v * Validator ) verifyIssuer (claims Claims , cmp string , required bool ) error {
255
266
iss , err := claims .GetIssuer ()
256
267
if err != nil {
257
268
return err
@@ -271,7 +282,7 @@ func (v *validator) verifyIssuer(claims Claims, cmp string, required bool) error
271
282
//
272
283
// Additionally, if any error occurs while retrieving the claim, e.g., when its
273
284
// the wrong type, an ErrTokenUnverifiable error will be returned.
274
- func (v * validator ) verifySubject (claims Claims , cmp string , required bool ) error {
285
+ func (v * Validator ) verifySubject (claims Claims , cmp string , required bool ) error {
275
286
sub , err := claims .GetSubject ()
276
287
if err != nil {
277
288
return err
0 commit comments