forked from mirror/jwt
cleaned up style and added tests
This commit is contained in:
parent
d8496232e5
commit
572c9130e8
24
parser.go
24
parser.go
|
@ -90,27 +90,27 @@ 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 {
|
||||
// Parse 'exp' claim
|
||||
switch num := token.Claims["exp"].(type) {
|
||||
case json.Number:
|
||||
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 {
|
||||
case float64:
|
||||
vexp = true
|
||||
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
|
||||
nbf = int64(num)
|
||||
}
|
||||
}
|
||||
|
||||
if vexp && now > exp {
|
||||
vErr.err = "token is expired"
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue