Skip to content

Commit

Permalink
Added option for audience check
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 27, 2022
1 parent 93dcd2e commit 5735b9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions validator.go
Expand Up @@ -21,6 +21,10 @@ type Validator struct {
// necessary. However, if wanted, it can be checked if the iat is
// unrealistic, i.e., in the future.
verifyIat bool

// expectedAud contains the audiences this token expects. Supplying an empty
// string will disable aud checking.
expectedAud string
}

type customValidationType interface {
Expand Down Expand Up @@ -67,6 +71,11 @@ func (v *Validator) Validate(claims Claims) error {
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
// additional custom validation based on their custom claims
cvt, ok := claims.(customValidationType)
Expand Down
11 changes: 9 additions & 2 deletions validator_option.go
Expand Up @@ -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.
func WithIssuedAtVerification() ValidatorOption {
func WithIssuedAt() ValidatorOption {
return func(v *Validator) {
v.verifyIat = true
}
}

// WithAudience returns the ValidatorOption to set the expected audience.
func WithAudience(aud string) ValidatorOption {
return func(v *Validator) {
v.expectedAud = aud
}
}

0 comments on commit 5735b9c

Please sign in to comment.