cleaned up style and added tests

This commit is contained in:
Dave Grijalva 2016-04-04 14:42:10 -07:00
parent d8496232e5
commit 572c9130e8
2 changed files with 43 additions and 16 deletions

View File

@ -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 {

View File

@ -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() {