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,27 +90,27 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
var exp, nbf int64 var exp, nbf int64
var vexp, vnbf bool var vexp, vnbf bool
if p.UseJSONNumber { // Parse 'exp' claim
if num, ok := token.Claims["exp"].(json.Number); ok { switch num := token.Claims["exp"].(type) {
case json.Number:
if exp, err = num.Int64(); err == nil { if exp, err = num.Int64(); err == nil {
vexp = true vexp = true
} }
} case float64:
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 {
vexp = true vexp = true
exp = int64(num) exp = int64(num)
} }
if num, ok := token.Claims["nbf"].(float64); ok {
// Parse 'nbf' claim
switch num := token.Claims["nbf"].(type) {
case json.Number:
if nbf, err = num.Int64(); err == nil {
vnbf = true
}
case float64:
vnbf = true vnbf = true
nbf = int64(num) nbf = int64(num)
} }
}
if vexp && now > exp { if vexp && now > exp {
vErr.err = "token is expired" vErr.err = "token is expired"

View File

@ -127,6 +127,33 @@ var jwtTestData = []struct {
0, 0,
&jwt.Parser{UseJSONNumber: true}, &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() { func init() {