mirror of https://github.com/golang-jwt/jwt.git
Adding inner error in the ValidationError type
This commit is contained in:
parent
2240de772c
commit
517905c5bd
|
@ -23,6 +23,7 @@ const (
|
||||||
// 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
|
err string
|
||||||
|
Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
|
||||||
Errors uint32 // bitfield. see ValidationError... constants
|
Errors uint32 // bitfield. see ValidationError... constants
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
}
|
}
|
||||||
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}
|
return token, &ValidationError{err: err.Error(), Errors: ValidationErrorUnverifiable, Inner: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check expiration times
|
// Check expiration times
|
||||||
|
|
|
@ -3,19 +3,22 @@ package jwt_test
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dgrijalva/jwt-go"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var keyFuncError error = fmt.Errorf("error loading key")
|
||||||
|
|
||||||
var (
|
var (
|
||||||
jwtTestDefaultKey []byte
|
jwtTestDefaultKey []byte
|
||||||
defaultKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestDefaultKey, nil }
|
defaultKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestDefaultKey, nil }
|
||||||
emptyKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, nil }
|
emptyKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, nil }
|
||||||
errorKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, fmt.Errorf("error loading key") }
|
errorKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, keyFuncError }
|
||||||
nilKeyFunc jwt.Keyfunc = nil
|
nilKeyFunc jwt.Keyfunc = nil
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -180,10 +183,16 @@ func TestParser_Parse(t *testing.T) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("[%v] Expecting error. Didn't get one.", data.name)
|
t.Errorf("[%v] Expecting error. Didn't get one.", data.name)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
ve := err.(*jwt.ValidationError)
|
||||||
// compare the bitfield part of the error
|
// compare the bitfield part of the error
|
||||||
if e := err.(*jwt.ValidationError).Errors; e != data.errors {
|
if e := ve.Errors; e != data.errors {
|
||||||
t.Errorf("[%v] Errors don't match expectation. %v != %v", data.name, e, data.errors)
|
t.Errorf("[%v] Errors don't match expectation. %v != %v", data.name, e, data.errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err.Error() == keyFuncError.Error() && ve.Inner != keyFuncError {
|
||||||
|
t.Errorf("[%v] Inner error does not match expectation. %v != %v", data.name, ve.Inner, keyFuncError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if data.valid && token.Signature == "" {
|
if data.valid && token.Signature == "" {
|
||||||
|
|
Loading…
Reference in New Issue