Skip to content

Commit 5735b9c

Browse files
committed
Added option for audience check
1 parent 93dcd2e commit 5735b9c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

validator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type Validator struct {
2121
// necessary. However, if wanted, it can be checked if the iat is
2222
// unrealistic, i.e., in the future.
2323
verifyIat bool
24+
25+
// expectedAud contains the audiences this token expects. Supplying an empty
26+
// string will disable aud checking.
27+
expectedAud string
2428
}
2529

2630
type customValidationType interface {
@@ -67,6 +71,11 @@ func (v *Validator) Validate(claims Claims) error {
6771
vErr.Errors |= ValidationErrorNotValidYet
6872
}
6973

74+
if v.expectedAud != "" && !v.VerifyAudience(claims, v.expectedAud, false) {
75+
vErr.Inner = ErrTokenNotValidYet
76+
vErr.Errors |= ValidationErrorNotValidYet
77+
}
78+
7079
// Finally, we want to give the claim itself some possibility to do some
7180
// additional custom validation based on their custom claims
7281
cvt, ok := claims.(customValidationType)

validator_option.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ func WithTimeFunc(f func() time.Time) ValidatorOption {
2525
}
2626
}
2727

28-
// WithIssuedAtVerification returns the ValidatorOption to enable verification
28+
// WithIssuedAt returns the ValidatorOption to enable verification
2929
// of issued-at.
30-
func WithIssuedAtVerification() ValidatorOption {
30+
func WithIssuedAt() ValidatorOption {
3131
return func(v *Validator) {
3232
v.verifyIat = true
3333
}
3434
}
35+
36+
// WithAudience returns the ValidatorOption to set the expected audience.
37+
func WithAudience(aud string) ValidatorOption {
38+
return func(v *Validator) {
39+
v.expectedAud = aud
40+
}
41+
}

0 commit comments

Comments
 (0)