Commit 45070d60 authored by Dave Grijalva's avatar Dave Grijalva

Merge branch 'master' of github.com:cenkalti/jwt-go

parents b1c2dab9 93608093
...@@ -86,73 +86,73 @@ func (t *Token) SigningString() (string, error) { ...@@ -86,73 +86,73 @@ func (t *Token) SigningString() (string, error) {
// If everything is kosher, err will be nil // If everything is kosher, err will be nil
func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) { func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
parts := strings.Split(tokenString, ".") parts := strings.Split(tokenString, ".")
if len(parts) == 3 { if len(parts) != 3 {
var err error return nil, &ValidationError{err: "Token contains an invalid number of segments", Errors: ValidationErrorMalformed}
token := &Token{Raw: tokenString} }
// parse Header
var headerBytes []byte
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
}
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
}
// parse Claims var err error
var claimBytes []byte token := &Token{Raw: tokenString}
if claimBytes, err = DecodeSegment(parts[1]); err != nil { // parse Header
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed} var headerBytes []byte
} if headerBytes, err = DecodeSegment(parts[0]); err != nil {
if err = json.Unmarshal(claimBytes, &token.Claims); err != nil { return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed} }
} if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
}
// Lookup signature method // parse Claims
if method, ok := token.Header["alg"].(string); ok { var claimBytes []byte
if token.Method = GetSigningMethod(method); token.Method == nil { if claimBytes, err = DecodeSegment(parts[1]); err != nil {
return token, &ValidationError{err: "Signing method (alg) is unavailable.", Errors: ValidationErrorUnverifiable} return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
} }
} else { if err = json.Unmarshal(claimBytes, &token.Claims); err != nil {
return token, &ValidationError{err: "Signing method (alg) is unspecified.", Errors: ValidationErrorUnverifiable} return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
} }
// Lookup key // Lookup signature method
var key []byte if method, ok := token.Header["alg"].(string); ok {
if key, err = keyFunc(token); err != nil { if token.Method = GetSigningMethod(method); token.Method == nil {
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorUnverifiable} return token, &ValidationError{err: "Signing method (alg) is unavailable.", Errors: ValidationErrorUnverifiable}
} }
} else {
return token, &ValidationError{err: "Signing method (alg) is unspecified.", Errors: ValidationErrorUnverifiable}
}
// Check expiration times // Lookup key
vErr := &ValidationError{} var key []byte
now := TimeFunc().Unix() if key, err = keyFunc(token); err != nil {
if exp, ok := token.Claims["exp"].(float64); ok { return token, &ValidationError{err: err.Error(), Errors: ValidationErrorUnverifiable}
if now > int64(exp) { }
vErr.err = "Token is expired"
vErr.Errors |= ValidationErrorExpired
}
}
if nbf, ok := token.Claims["nbf"].(float64); ok {
if now < int64(nbf) {
vErr.err = "Token is not valid yet"
vErr.Errors |= ValidationErrorNotValidYet
}
}
// Perform validation // Check expiration times
if err = token.Method.Verify(strings.Join(parts[0:2], "."), parts[2], key); err != nil { vErr := &ValidationError{}
vErr.err = err.Error() now := TimeFunc().Unix()
vErr.Errors |= ValidationErrorSignatureInvalid if exp, ok := token.Claims["exp"].(float64); ok {
if now > int64(exp) {
vErr.err = "Token is expired"
vErr.Errors |= ValidationErrorExpired
} }
}
if vErr.valid() { if nbf, ok := token.Claims["nbf"].(float64); ok {
token.Valid = true if now < int64(nbf) {
return token, nil vErr.err = "Token is not valid yet"
vErr.Errors |= ValidationErrorNotValidYet
} }
}
return token, vErr // Perform validation
if err = token.Method.Verify(strings.Join(parts[0:2], "."), parts[2], key); err != nil {
vErr.err = err.Error()
vErr.Errors |= ValidationErrorSignatureInvalid
}
if vErr.valid() {
token.Valid = true
return token, nil
} }
return nil, &ValidationError{err: "Token contains an invalid number of segments", Errors: ValidationErrorMalformed}
return token, vErr
} }
// The errors that might occur when parsing and validating a token // The errors that might occur when parsing and validating a token
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment