return an error if the requested hash method hasn't been compiled in

This commit is contained in:
Dave Grijalva 2014-08-26 15:00:15 -07:00
parent 48aa7e5db1
commit 2ef58916cc
3 changed files with 18 additions and 1 deletions

View File

@ -47,6 +47,10 @@ func (m *SigningMethodHMAC) Verify(signingString, signature string, key interfac
var sig []byte var sig []byte
var err error var err error
if sig, err = DecodeSegment(signature); err == nil { if sig, err = DecodeSegment(signature); err == nil {
if !m.Hash.Available() {
return ErrHashUnavailable
}
hasher := hmac.New(m.Hash.New, keyBytes) hasher := hmac.New(m.Hash.New, keyBytes)
hasher.Write([]byte(signingString)) hasher.Write([]byte(signingString))
@ -62,6 +66,10 @@ func (m *SigningMethodHMAC) Verify(signingString, signature string, key interfac
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) { func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) {
if keyBytes, ok := key.([]byte); ok { if keyBytes, ok := key.([]byte); ok {
if !m.Hash.Available() {
return "", ErrHashUnavailable
}
hasher := hmac.New(m.Hash.New, keyBytes) hasher := hmac.New(m.Hash.New, keyBytes)
hasher.Write([]byte(signingString)) hasher.Write([]byte(signingString))

3
jwt.go
View File

@ -22,7 +22,8 @@ type Keyfunc func(*Token) (interface{}, error)
// Error constants // Error constants
var ( var (
ErrInvalidKey = errors.New("Key is invalid or of invalid type.") ErrInvalidKey = errors.New("Key is invalid or of invalid type.")
ErrHashUnavailable = errors.New("The requested Hash function is unavailable")
) )
// A JWT Token. Different fields will be used depending on whether you're // A JWT Token. Different fields will be used depending on whether you're

8
rsa.go
View File

@ -67,6 +67,9 @@ func (m *SigningMethodRSA) Verify(signingString, signature string, key interface
} }
// Create hasher // Create hasher
if !m.Hash.Available() {
return ErrHashUnavailable
}
hasher := m.Hash.New() hasher := m.Hash.New()
hasher.Write([]byte(signingString)) hasher.Write([]byte(signingString))
@ -91,7 +94,12 @@ func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string,
default: default:
return "", ErrInvalidKey return "", ErrInvalidKey
} }
// Create the hasher // Create the hasher
if !m.Hash.Available() {
return "", ErrHashUnavailable
}
hasher := m.Hash.New() hasher := m.Hash.New()
hasher.Write([]byte(signingString)) hasher.Write([]byte(signingString))