mirror of https://github.com/golang-jwt/jwt.git
Inlining application-specific validation
This commit is contained in:
parent
66e2e01a4f
commit
1e16f55059
|
@ -121,7 +121,9 @@ type MyCustomClaims struct {
|
||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MyCustomClaims) CustomValidation() error {
|
// Validate can be used to execute additional application-specific claims
|
||||||
|
// validation.
|
||||||
|
func (m MyCustomClaims) Validate() error {
|
||||||
if m.Foo != "bar" {
|
if m.Foo != "bar" {
|
||||||
return errors.New("must be foobar")
|
return errors.New("must be foobar")
|
||||||
}
|
}
|
||||||
|
|
16
validator.go
16
validator.go
|
@ -38,14 +38,6 @@ type validator struct {
|
||||||
expectedSub string
|
expectedSub string
|
||||||
}
|
}
|
||||||
|
|
||||||
// customClaims represents a custom claims interface, which can be built upon the integrated
|
|
||||||
// claim types, such as map claims or registered claims.
|
|
||||||
type customClaims interface {
|
|
||||||
// CustomValidation can be implemented by a user-specific claim to support
|
|
||||||
// additional validation steps in addition to the regular validation.
|
|
||||||
CustomValidation() error
|
|
||||||
}
|
|
||||||
|
|
||||||
// newValidator can be used to create a stand-alone validator with the supplied
|
// newValidator can be used to create a stand-alone validator with the supplied
|
||||||
// options. This validator can then be used to validate already parsed claims.
|
// options. This validator can then be used to validate already parsed claims.
|
||||||
func newValidator(opts ...ParserOption) *validator {
|
func newValidator(opts ...ParserOption) *validator {
|
||||||
|
@ -105,10 +97,12 @@ func (v *validator) Validate(claims Claims) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 a custom function
|
// additional custom validation based on a custom Validate function.
|
||||||
cvt, ok := claims.(customClaims)
|
cvt, ok := claims.(interface {
|
||||||
|
Validate() error
|
||||||
|
})
|
||||||
if ok {
|
if ok {
|
||||||
if err := cvt.CustomValidation(); err != nil {
|
if err := cvt.Validate(); err != nil {
|
||||||
vErr.Inner = err
|
vErr.Inner = err
|
||||||
vErr.Errors |= ValidationErrorClaimsInvalid
|
vErr.Errors |= ValidationErrorClaimsInvalid
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue