Added option for audience check

This commit is contained in:
Christian Banse 2022-08-27 13:42:01 +02:00
parent 4990d2cdf3
commit eedf3ebe01
2 changed files with 18 additions and 2 deletions

View File

@ -21,6 +21,10 @@ type Validator struct {
// necessary. However, if wanted, it can be checked if the iat is // necessary. However, if wanted, it can be checked if the iat is
// unrealistic, i.e., in the future. // unrealistic, i.e., in the future.
verifyIat bool verifyIat bool
// expectedAud contains the audiences this token expects. Supplying an empty
// string will disable aud checking.
expectedAud string
} }
type customValidationType interface { type customValidationType interface {
@ -67,6 +71,11 @@ func (v *Validator) Validate(claims Claims) error {
vErr.Errors |= ValidationErrorNotValidYet vErr.Errors |= ValidationErrorNotValidYet
} }
if v.expectedAud != "" && !v.VerifyAudience(claims, v.expectedAud, false) {
vErr.Inner = ErrTokenNotValidYet
vErr.Errors |= ValidationErrorNotValidYet
}
// Finally, we want to give the claim itself some possibility to do some // Finally, we want to give the claim itself some possibility to do some
// additional custom validation based on their custom claims // additional custom validation based on their custom claims
cvt, ok := claims.(customValidationType) cvt, ok := claims.(customValidationType)

View File

@ -25,10 +25,17 @@ func WithTimeFunc(f func() time.Time) ValidatorOption {
} }
} }
// WithIssuedAtVerification returns the ValidatorOption to enable verification // WithIssuedAt returns the ValidatorOption to enable verification
// of issued-at. // of issued-at.
func WithIssuedAtVerification() ValidatorOption { func WithIssuedAt() ValidatorOption {
return func(v *Validator) { return func(v *Validator) {
v.verifyIat = true v.verifyIat = true
} }
} }
// WithAudience returns the ValidatorOption to set the expected audience.
func WithAudience(aud string) ValidatorOption {
return func(v *Validator) {
v.expectedAud = aud
}
}