mirror of https://github.com/golang-jwt/jwt.git
expose inner error within ValidationError
This commit is contained in:
parent
36d317022e
commit
9249eabf87
13
errors.go
13
errors.go
|
@ -20,19 +20,26 @@ const (
|
||||||
ValidationErrorNotValidYet // NBF validation failed
|
ValidationErrorNotValidYet // NBF validation failed
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Helper for constructing a ValidationError with a string error message
|
||||||
|
func NewValidationError(errorText string, errorFlags uint32) *ValidationError {
|
||||||
|
return &ValidationError{
|
||||||
|
Inner: errors.New(errorText),
|
||||||
|
Errors: errorFlags,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The error from Parse if token is not valid
|
// The error from Parse if token is not valid
|
||||||
type ValidationError struct {
|
type ValidationError struct {
|
||||||
err string
|
|
||||||
Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
|
Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
|
||||||
Errors uint32 // bitfield. see ValidationError... constants
|
Errors uint32 // bitfield. see ValidationError... constants
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validation error is an error type
|
// Validation error is an error type
|
||||||
func (e ValidationError) Error() string {
|
func (e ValidationError) Error() string {
|
||||||
if e.err == "" {
|
if e.Inner == nil {
|
||||||
return "token is invalid"
|
return "token is invalid"
|
||||||
}
|
}
|
||||||
return e.err
|
return e.Inner.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
// No errors
|
// No errors
|
||||||
|
|
10
none.go
10
none.go
|
@ -13,10 +13,8 @@ type unsafeNoneMagicConstant string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
SigningMethodNone = &signingMethodNone{}
|
SigningMethodNone = &signingMethodNone{}
|
||||||
NoneSignatureTypeDisallowedError = &ValidationError{
|
NoneSignatureTypeDisallowedError = NewValidationError("'none' signature type is not allowed", ValidationErrorSignatureInvalid)
|
||||||
"'none' signature type is not allowed",
|
|
||||||
ValidationErrorSignatureInvalid,
|
|
||||||
}
|
|
||||||
RegisterSigningMethod(SigningMethodNone.Alg(), func() SigningMethod {
|
RegisterSigningMethod(SigningMethodNone.Alg(), func() SigningMethod {
|
||||||
return SigningMethodNone
|
return SigningMethodNone
|
||||||
})
|
})
|
||||||
|
@ -35,10 +33,10 @@ func (m *signingMethodNone) Verify(signingString, signature string, key interfac
|
||||||
}
|
}
|
||||||
// If signing method is none, signature must be an empty string
|
// If signing method is none, signature must be an empty string
|
||||||
if signature != "" {
|
if signature != "" {
|
||||||
return &ValidationError{
|
return NewValidationError(
|
||||||
"'none' signing method with non-empty signature",
|
"'none' signing method with non-empty signature",
|
||||||
ValidationErrorSignatureInvalid,
|
ValidationErrorSignatureInvalid,
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accept 'none' signing method.
|
// Accept 'none' signing method.
|
||||||
|
|
28
parser.go
28
parser.go
|
@ -18,7 +18,7 @@ type Parser struct {
|
||||||
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
parts := strings.Split(tokenString, ".")
|
parts := strings.Split(tokenString, ".")
|
||||||
if len(parts) != 3 {
|
if len(parts) != 3 {
|
||||||
return nil, &ValidationError{err: "token contains an invalid number of segments", Errors: ValidationErrorMalformed}
|
return nil, NewValidationError("token contains an invalid number of segments", ValidationErrorMalformed)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -27,34 +27,34 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
var headerBytes []byte
|
var headerBytes []byte
|
||||||
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
||||||
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
|
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
|
||||||
return token, &ValidationError{err: "tokenstring should not contain 'bearer '", Errors: ValidationErrorMalformed}
|
return token, NewValidationError("tokenstring should not contain 'bearer '", ValidationErrorMalformed)
|
||||||
}
|
}
|
||||||
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
|
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
||||||
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
|
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse Claims
|
// parse Claims
|
||||||
var claimBytes []byte
|
var claimBytes []byte
|
||||||
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
||||||
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
|
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
}
|
}
|
||||||
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
|
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
|
||||||
if p.UseJSONNumber {
|
if p.UseJSONNumber {
|
||||||
dec.UseNumber()
|
dec.UseNumber()
|
||||||
}
|
}
|
||||||
if err = dec.Decode(&token.Claims); err != nil {
|
if err = dec.Decode(&token.Claims); err != nil {
|
||||||
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorMalformed}
|
return token, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup signature method
|
// Lookup signature method
|
||||||
if method, ok := token.Header["alg"].(string); ok {
|
if method, ok := token.Header["alg"].(string); ok {
|
||||||
if token.Method = GetSigningMethod(method); token.Method == nil {
|
if token.Method = GetSigningMethod(method); token.Method == nil {
|
||||||
return token, &ValidationError{err: "signing method (alg) is unavailable.", Errors: ValidationErrorUnverifiable}
|
return token, NewValidationError("signing method (alg) is unavailable.", ValidationErrorUnverifiable)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return token, &ValidationError{err: "signing method (alg) is unspecified.", Errors: ValidationErrorUnverifiable}
|
return token, NewValidationError("signing method (alg) is unspecified.", ValidationErrorUnverifiable)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify signing method is in the required set
|
// Verify signing method is in the required set
|
||||||
|
@ -69,7 +69,7 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
}
|
}
|
||||||
if !signingMethodValid {
|
if !signingMethodValid {
|
||||||
// signing method is not in the listed set
|
// signing method is not in the listed set
|
||||||
return token, &ValidationError{err: fmt.Sprintf("signing method %v is invalid", alg), Errors: ValidationErrorSignatureInvalid}
|
return token, NewValidationError(fmt.Sprintf("signing method %v is invalid", alg), ValidationErrorSignatureInvalid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,11 +77,11 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
var key interface{}
|
var key interface{}
|
||||||
if keyFunc == nil {
|
if keyFunc == nil {
|
||||||
// keyFunc was not provided. short circuiting validation
|
// keyFunc was not provided. short circuiting validation
|
||||||
return token, &ValidationError{err: "no Keyfunc was provided.", Errors: ValidationErrorUnverifiable}
|
return token, NewValidationError("no Keyfunc was provided.", ValidationErrorUnverifiable)
|
||||||
}
|
}
|
||||||
if key, err = keyFunc(token); err != nil {
|
if key, err = keyFunc(token); err != nil {
|
||||||
// keyFunc returned an error
|
// keyFunc returned an error
|
||||||
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorUnverifiable, Inner: err}
|
return token, &ValidationError{Inner: err, Errors: ValidationErrorUnverifiable}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check expiration times
|
// Check expiration times
|
||||||
|
@ -113,19 +113,19 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if vexp && now > exp {
|
if vexp && now > exp {
|
||||||
vErr.err = "token is expired"
|
vErr.Inner = fmt.Errorf("token is expired")
|
||||||
vErr.Errors |= ValidationErrorExpired
|
vErr.Errors |= ValidationErrorExpired
|
||||||
}
|
}
|
||||||
|
|
||||||
if vnbf && now < nbf {
|
if vnbf && now < nbf {
|
||||||
vErr.err = "token is not valid yet"
|
vErr.Inner = fmt.Errorf("token is not valid yet")
|
||||||
vErr.Errors |= ValidationErrorNotValidYet
|
vErr.Errors |= ValidationErrorNotValidYet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform validation
|
// Perform validation
|
||||||
token.Signature = parts[2]
|
token.Signature = parts[2]
|
||||||
if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, key); err != nil {
|
if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, key); err != nil {
|
||||||
vErr.err = err.Error()
|
vErr.Inner = err
|
||||||
vErr.Errors |= ValidationErrorSignatureInvalid
|
vErr.Errors |= ValidationErrorSignatureInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue