From c9eaceb2896dbb515dae7ec352b377a226a52721 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Tue, 21 Jun 2016 16:11:54 -0400 Subject: [PATCH] Parser flag to skip claims validation during token parsing --- parser.go | 21 ++++++++++++--------- parser_test.go | 9 +++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/parser.go b/parser.go index 7020c52..7bf1c4e 100644 --- a/parser.go +++ b/parser.go @@ -8,8 +8,9 @@ import ( ) type Parser struct { - ValidMethods []string // If populated, only these methods will be considered valid - UseJSONNumber bool // Use JSON Number format in JSON decoder + ValidMethods []string // If populated, only these methods will be considered valid + UseJSONNumber bool // Use JSON Number format in JSON decoder + SkipClaimsValidation bool // Skip claims validation during token parsing } // Parse, validate, and return a token. @@ -101,14 +102,16 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf vErr := &ValidationError{} // Validate Claims - if err := token.Claims.Valid(); err != nil { + if !p.SkipClaimsValidation { + if err := token.Claims.Valid(); err != nil { - // If the Claims Valid returned an error, check if it is a validation error, - // If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set - if e, ok := err.(*ValidationError); !ok { - vErr = &ValidationError{Inner: err, Errors: ValidationErrorClaimsInvalid} - } else { - vErr = e + // If the Claims Valid returned an error, check if it is a validation error, + // If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set + if e, ok := err.(*ValidationError); !ok { + vErr = &ValidationError{Inner: err, Errors: ValidationErrorClaimsInvalid} + } else { + vErr = e + } } } diff --git a/parser_test.go b/parser_test.go index 0c86801..e62714d 100644 --- a/parser_test.go +++ b/parser_test.go @@ -172,6 +172,15 @@ var jwtTestData = []struct { jwt.ValidationErrorNotValidYet | jwt.ValidationErrorExpired, &jwt.Parser{UseJSONNumber: true}, }, + { + "SkipClaimsValidation during token parsing", + "", // autogen + defaultKeyFunc, + jwt.MapClaims{"foo": "bar", "nbf": json.Number(fmt.Sprintf("%v", time.Now().Unix()+100))}, + true, + 0, + &jwt.Parser{UseJSONNumber: true, SkipClaimsValidation: true}, + }, } func TestParser_Parse(t *testing.T) {