Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return RequiredClaimValidationError error when required claim is missing #713

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,13 @@ func TestGH430(t *testing.T) {
}
}

func TestGH706(t *testing.T) {
tok := jwt.New()
if !assert.ErrorIs(t, jwt.Validate(tok, jwt.WithRequiredClaim("foo")), &jwt.RequiredClaimValidationError{}, `jwt.Validate should fail`) {
return
}
}

func TestBenHigginsByPassRegression(t *testing.T) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion jwt/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ func (err *validationError) Unwrap() error {
return err.error
}

type RequiredClaimValidationError struct {
claim string
}

func (err *RequiredClaimValidationError) Error() string {
return fmt.Sprintf("%q not satisfied: required claim not found", err.claim)
}

func (err *RequiredClaimValidationError) Is(target error) bool {
_, ok := target.(*RequiredClaimValidationError)
return ok
}

var errTokenExpired = NewValidationError(fmt.Errorf(`"exp" not satisfied`))
var errInvalidIssuedAt = NewValidationError(fmt.Errorf(`"iat" not satisfied`))
var errTokenNotYetValid = NewValidationError(fmt.Errorf(`"nbf" not satisfied`))
Expand Down Expand Up @@ -379,7 +392,7 @@ type isRequired string
func (ir isRequired) Validate(_ context.Context, t Token) ValidationError {
_, ok := t.Get(string(ir))
if !ok {
return NewValidationError(fmt.Errorf(`%q not satisfied: required claim not found`, string(ir)))
return NewValidationError(&RequiredClaimValidationError{claim: string(ir)})
}
return nil
}