forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/v5/validator.go

507 lines
9.1 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"crypto/subtle"
"fmt"
"time"
)
// ClaimsValidator is an interface that can be implemented by custom claims who
2024-02-18 10:42:21 +00:00
// wish to execute any additional claims validation based on
2024-02-18 10:42:21 +00:00
// application-specific logic. The Validate function is then executed in
2024-02-18 10:42:21 +00:00
// addition to the regular claims validation and any error returned is appended
2024-02-18 10:42:21 +00:00
// to the final validation result.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// type MyCustomClaims struct {
2024-02-18 10:42:21 +00:00
// Foo string `json:"foo"`
2024-02-18 10:42:21 +00:00
// jwt.RegisteredClaims
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// func (m MyCustomClaims) Validate() error {
2024-02-18 10:42:21 +00:00
// if m.Foo != "bar" {
2024-02-18 10:42:21 +00:00
// return errors.New("must be foobar")
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
// return nil
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
type ClaimsValidator interface {
Claims
2024-02-18 10:42:21 +00:00
Validate() error
}
// validator is the core of the new Validation API. It is automatically used by
2024-02-18 10:42:21 +00:00
// a [Parser] during parsing and can be modified with various parser options.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note: This struct is intentionally not exported (yet) as we want to
2024-02-18 10:42:21 +00:00
// internally finalize its API. In the future, we might make it publicly
2024-02-18 10:42:21 +00:00
// available.
2024-02-18 10:42:21 +00:00
type validator struct {
2024-02-18 10:42:21 +00:00
// leeway is an optional leeway that can be provided to account for clock skew.
2024-02-18 10:42:21 +00:00
leeway time.Duration
// timeFunc is used to supply the current time that is needed for
2024-02-18 10:42:21 +00:00
// validation. If unspecified, this defaults to time.Now.
2024-02-18 10:42:21 +00:00
timeFunc func() time.Time
// verifyIat specifies whether the iat (Issued At) claim will be verified.
2024-02-18 10:42:21 +00:00
// According to https://www.rfc-editor.org/rfc/rfc7519#section-4.1.6 this
2024-02-18 10:42:21 +00:00
// only specifies the age of the token, but no validation check is
2024-02-18 10:42:21 +00:00
// necessary. However, if wanted, it can be checked if the iat is
2024-02-18 10:42:21 +00:00
// unrealistic, i.e., in the future.
2024-02-18 10:42:21 +00:00
verifyIat bool
// expectedAud contains the audience this token expects. Supplying an empty
2024-02-18 10:42:21 +00:00
// string will disable aud checking.
2024-02-18 10:42:21 +00:00
expectedAud string
// expectedIss contains the issuer this token expects. Supplying an empty
2024-02-18 10:42:21 +00:00
// string will disable iss checking.
2024-02-18 10:42:21 +00:00
expectedIss string
// expectedSub contains the subject this token expects. Supplying an empty
2024-02-18 10:42:21 +00:00
// string will disable sub checking.
2024-02-18 10:42:21 +00:00
expectedSub string
}
// newValidator can be used to create a stand-alone validator with the supplied
2024-02-18 10:42:21 +00:00
// options. This validator can then be used to validate already parsed claims.
2024-02-18 10:42:21 +00:00
func newValidator(opts ...ParserOption) *validator {
2024-02-18 10:42:21 +00:00
p := NewParser(opts...)
2024-02-18 10:42:21 +00:00
return p.validator
2024-02-18 10:42:21 +00:00
}
// Validate validates the given claims. It will also perform any custom
2024-02-18 10:42:21 +00:00
// validation if claims implements the [ClaimsValidator] interface.
2024-02-18 10:42:21 +00:00
func (v *validator) Validate(claims Claims) error {
2024-02-18 10:42:21 +00:00
var (
now time.Time
2024-02-18 10:42:21 +00:00
errs []error = make([]error, 0, 6)
err error
2024-02-18 10:42:21 +00:00
)
// Check, if we have a time func
2024-02-18 10:42:21 +00:00
if v.timeFunc != nil {
2024-02-18 10:42:21 +00:00
now = v.timeFunc()
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
now = time.Now()
2024-02-18 10:42:21 +00:00
}
// We always need to check the expiration time, but usage of the claim
2024-02-18 10:42:21 +00:00
// itself is OPTIONAL.
2024-02-18 10:42:21 +00:00
if err = v.verifyExpiresAt(claims, now, false); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
// We always need to check not-before, but usage of the claim itself is
2024-02-18 10:42:21 +00:00
// OPTIONAL.
2024-02-18 10:42:21 +00:00
if err = v.verifyNotBefore(claims, now, false); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
// Check issued-at if the option is enabled
2024-02-18 10:42:21 +00:00
if v.verifyIat {
2024-02-18 10:42:21 +00:00
if err = v.verifyIssuedAt(claims, now, false); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// If we have an expected audience, we also require the audience claim
2024-02-18 10:42:21 +00:00
if v.expectedAud != "" {
2024-02-18 10:42:21 +00:00
if err = v.verifyAudience(claims, v.expectedAud, true); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// If we have an expected issuer, we also require the issuer claim
2024-02-18 10:42:21 +00:00
if v.expectedIss != "" {
2024-02-18 10:42:21 +00:00
if err = v.verifyIssuer(claims, v.expectedIss, true); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// If we have an expected subject, we also require the subject claim
2024-02-18 10:42:21 +00:00
if v.expectedSub != "" {
2024-02-18 10:42:21 +00:00
if err = v.verifySubject(claims, v.expectedSub, true); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Finally, we want to give the claim itself some possibility to do some
2024-02-18 10:42:21 +00:00
// additional custom validation based on a custom Validate function.
2024-02-18 10:42:21 +00:00
cvt, ok := claims.(ClaimsValidator)
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
if err := cvt.Validate(); err != nil {
2024-02-18 10:42:21 +00:00
errs = append(errs, err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if len(errs) == 0 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
return joinErrors(errs...)
2024-02-18 10:42:21 +00:00
}
// verifyExpiresAt compares the exp claim in claims against cmp. This function
2024-02-18 10:42:21 +00:00
// will succeed if cmp < exp. Additional leeway is taken into account.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If exp is not set, it will succeed if the claim is not required,
2024-02-18 10:42:21 +00:00
// otherwise ErrTokenRequiredClaimMissing will be returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Additionally, if any error occurs while retrieving the claim, e.g., when its
2024-02-18 10:42:21 +00:00
// the wrong type, an ErrTokenUnverifiable error will be returned.
2024-02-18 10:42:21 +00:00
func (v *validator) verifyExpiresAt(claims Claims, cmp time.Time, required bool) error {
2024-02-18 10:42:21 +00:00
exp, err := claims.GetExpirationTime()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if exp == nil {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "exp")
2024-02-18 10:42:21 +00:00
}
return errorIfFalse(cmp.Before((exp.Time).Add(+v.leeway)), ErrTokenExpired)
2024-02-18 10:42:21 +00:00
}
// verifyIssuedAt compares the iat claim in claims against cmp. This function
2024-02-18 10:42:21 +00:00
// will succeed if cmp >= iat. Additional leeway is taken into account.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If iat is not set, it will succeed if the claim is not required,
2024-02-18 10:42:21 +00:00
// otherwise ErrTokenRequiredClaimMissing will be returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Additionally, if any error occurs while retrieving the claim, e.g., when its
2024-02-18 10:42:21 +00:00
// the wrong type, an ErrTokenUnverifiable error will be returned.
2024-02-18 10:42:21 +00:00
func (v *validator) verifyIssuedAt(claims Claims, cmp time.Time, required bool) error {
2024-02-18 10:42:21 +00:00
iat, err := claims.GetIssuedAt()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if iat == nil {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "iat")
2024-02-18 10:42:21 +00:00
}
return errorIfFalse(!cmp.Before(iat.Add(-v.leeway)), ErrTokenUsedBeforeIssued)
2024-02-18 10:42:21 +00:00
}
// verifyNotBefore compares the nbf claim in claims against cmp. This function
2024-02-18 10:42:21 +00:00
// will return true if cmp >= nbf. Additional leeway is taken into account.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If nbf is not set, it will succeed if the claim is not required,
2024-02-18 10:42:21 +00:00
// otherwise ErrTokenRequiredClaimMissing will be returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Additionally, if any error occurs while retrieving the claim, e.g., when its
2024-02-18 10:42:21 +00:00
// the wrong type, an ErrTokenUnverifiable error will be returned.
2024-02-18 10:42:21 +00:00
func (v *validator) verifyNotBefore(claims Claims, cmp time.Time, required bool) error {
2024-02-18 10:42:21 +00:00
nbf, err := claims.GetNotBefore()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if nbf == nil {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "nbf")
2024-02-18 10:42:21 +00:00
}
return errorIfFalse(!cmp.Before(nbf.Add(-v.leeway)), ErrTokenNotValidYet)
2024-02-18 10:42:21 +00:00
}
// verifyAudience compares the aud claim against cmp.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If aud is not set or an empty list, it will succeed if the claim is not required,
2024-02-18 10:42:21 +00:00
// otherwise ErrTokenRequiredClaimMissing will be returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Additionally, if any error occurs while retrieving the claim, e.g., when its
2024-02-18 10:42:21 +00:00
// the wrong type, an ErrTokenUnverifiable error will be returned.
2024-02-18 10:42:21 +00:00
func (v *validator) verifyAudience(claims Claims, cmp string, required bool) error {
2024-02-18 10:42:21 +00:00
aud, err := claims.GetAudience()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if len(aud) == 0 {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "aud")
2024-02-18 10:42:21 +00:00
}
// use a var here to keep constant time compare when looping over a number of claims
2024-02-18 10:42:21 +00:00
result := false
var stringClaims string
2024-02-18 10:42:21 +00:00
for _, a := range aud {
2024-02-18 10:42:21 +00:00
if subtle.ConstantTimeCompare([]byte(a), []byte(cmp)) != 0 {
2024-02-18 10:42:21 +00:00
result = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
stringClaims = stringClaims + a
2024-02-18 10:42:21 +00:00
}
// case where "" is sent in one or many aud claims
2024-02-18 10:42:21 +00:00
if stringClaims == "" {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "aud")
2024-02-18 10:42:21 +00:00
}
return errorIfFalse(result, ErrTokenInvalidAudience)
2024-02-18 10:42:21 +00:00
}
// verifyIssuer compares the iss claim in claims against cmp.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If iss is not set, it will succeed if the claim is not required,
2024-02-18 10:42:21 +00:00
// otherwise ErrTokenRequiredClaimMissing will be returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Additionally, if any error occurs while retrieving the claim, e.g., when its
2024-02-18 10:42:21 +00:00
// the wrong type, an ErrTokenUnverifiable error will be returned.
2024-02-18 10:42:21 +00:00
func (v *validator) verifyIssuer(claims Claims, cmp string, required bool) error {
2024-02-18 10:42:21 +00:00
iss, err := claims.GetIssuer()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if iss == "" {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "iss")
2024-02-18 10:42:21 +00:00
}
return errorIfFalse(iss == cmp, ErrTokenInvalidIssuer)
2024-02-18 10:42:21 +00:00
}
// verifySubject compares the sub claim against cmp.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If sub is not set, it will succeed if the claim is not required,
2024-02-18 10:42:21 +00:00
// otherwise ErrTokenRequiredClaimMissing will be returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Additionally, if any error occurs while retrieving the claim, e.g., when its
2024-02-18 10:42:21 +00:00
// the wrong type, an ErrTokenUnverifiable error will be returned.
2024-02-18 10:42:21 +00:00
func (v *validator) verifySubject(claims Claims, cmp string, required bool) error {
2024-02-18 10:42:21 +00:00
sub, err := claims.GetSubject()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
if sub == "" {
2024-02-18 10:42:21 +00:00
return errorIfRequired(required, "sub")
2024-02-18 10:42:21 +00:00
}
return errorIfFalse(sub == cmp, ErrTokenInvalidSubject)
2024-02-18 10:42:21 +00:00
}
// errorIfFalse returns the error specified in err, if the value is true.
2024-02-18 10:42:21 +00:00
// Otherwise, nil is returned.
2024-02-18 10:42:21 +00:00
func errorIfFalse(value bool, err error) error {
2024-02-18 10:42:21 +00:00
if value {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// errorIfRequired returns an ErrTokenRequiredClaimMissing error if required is
2024-02-18 10:42:21 +00:00
// true. Otherwise, nil is returned.
2024-02-18 10:42:21 +00:00
func errorIfRequired(required bool, claim string) error {
2024-02-18 10:42:21 +00:00
if required {
2024-02-18 10:42:21 +00:00
return newError(fmt.Sprintf("%s claim is required", claim), ErrTokenRequiredClaimMissing)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}