diff --git a/parser.go b/parser.go index bf8cc88..e2fb28f 100644 --- a/parser.go +++ b/parser.go @@ -90,26 +90,26 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) { var exp, nbf int64 var vexp, vnbf bool - if p.UseJSONNumber { - if num, ok := token.Claims["exp"].(json.Number); ok { - if exp, err = num.Int64(); err == nil { - vexp = true - } - } - if num, ok := token.Claims["nbf"].(json.Number); ok { - if nbf, err = num.Int64(); err == nil { - vnbf = true - } - } - } else { - if num, ok := token.Claims["exp"].(float64); ok { + // Parse 'exp' claim + switch num := token.Claims["exp"].(type) { + case json.Number: + if exp, err = num.Int64(); err == nil { vexp = true - exp = int64(num) } - if num, ok := token.Claims["nbf"].(float64); ok { + case float64: + vexp = true + exp = int64(num) + } + + // Parse 'nbf' claim + switch num := token.Claims["nbf"].(type) { + case json.Number: + if nbf, err = num.Int64(); err == nil { vnbf = true - nbf = int64(num) } + case float64: + vnbf = true + nbf = int64(num) } if vexp && now > exp { diff --git a/parser_test.go b/parser_test.go index 9115017..80250ef 100644 --- a/parser_test.go +++ b/parser_test.go @@ -127,6 +127,33 @@ var jwtTestData = []struct { 0, &jwt.Parser{UseJSONNumber: true}, }, + { + "JSON Number - basic expired", + "", // autogen + defaultKeyFunc, + map[string]interface{}{"foo": "bar", "exp": json.Number(fmt.Sprintf("%v", time.Now().Unix()-100))}, + false, + jwt.ValidationErrorExpired, + &jwt.Parser{UseJSONNumber: true}, + }, + { + "JSON Number - basic nbf", + "", // autogen + defaultKeyFunc, + map[string]interface{}{"foo": "bar", "nbf": json.Number(fmt.Sprintf("%v", time.Now().Unix()+100))}, + false, + jwt.ValidationErrorNotValidYet, + &jwt.Parser{UseJSONNumber: true}, + }, + { + "JSON Number - expired and nbf", + "", // autogen + defaultKeyFunc, + map[string]interface{}{"foo": "bar", "nbf": json.Number(fmt.Sprintf("%v", time.Now().Unix()+100)), "exp": json.Number(fmt.Sprintf("%v", time.Now().Unix()-100))}, + false, + jwt.ValidationErrorNotValidYet | jwt.ValidationErrorExpired, + &jwt.Parser{UseJSONNumber: true}, + }, } func init() {