mirror of https://github.com/golang-jwt/jwt.git
Improve ErrInvalidKeyType error messages (#361)
* Improve ErrInvalidKeyType error message * add specific expected type to error message * fix ErrInvalidKey error to ErrInvalidKeyType in rsa and rsapss * format * revert changes from example_test.go remove the comments * fix: udpate the signing names to uppercase
This commit is contained in:
parent
a49fa5d91d
commit
b05644bf94
4
ecdsa.go
4
ecdsa.go
|
@ -62,7 +62,7 @@ func (m *SigningMethodECDSA) Verify(signingString string, sig []byte, key interf
|
||||||
case *ecdsa.PublicKey:
|
case *ecdsa.PublicKey:
|
||||||
ecdsaKey = k
|
ecdsaKey = k
|
||||||
default:
|
default:
|
||||||
return ErrInvalidKeyType
|
return newError("ECDSA verify expects *ecsda.PublicKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(sig) != 2*m.KeySize {
|
if len(sig) != 2*m.KeySize {
|
||||||
|
@ -96,7 +96,7 @@ func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) ([]byte
|
||||||
case *ecdsa.PrivateKey:
|
case *ecdsa.PrivateKey:
|
||||||
ecdsaKey = k
|
ecdsaKey = k
|
||||||
default:
|
default:
|
||||||
return nil, ErrInvalidKeyType
|
return nil, newError("ECDSA sign expects *ecsda.PrivateKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the hasher
|
// Create the hasher
|
||||||
|
|
|
@ -38,7 +38,7 @@ func (m *SigningMethodEd25519) Verify(signingString string, sig []byte, key inte
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
if ed25519Key, ok = key.(ed25519.PublicKey); !ok {
|
if ed25519Key, ok = key.(ed25519.PublicKey); !ok {
|
||||||
return ErrInvalidKeyType
|
return newError("Ed25519 verify expects ed25519.PublicKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ed25519Key) != ed25519.PublicKeySize {
|
if len(ed25519Key) != ed25519.PublicKeySize {
|
||||||
|
@ -60,7 +60,7 @@ func (m *SigningMethodEd25519) Sign(signingString string, key interface{}) ([]by
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
if ed25519Key, ok = key.(crypto.Signer); !ok {
|
if ed25519Key, ok = key.(crypto.Signer); !ok {
|
||||||
return nil, ErrInvalidKeyType
|
return nil, newError("Ed25519 sign expects crypto.Signer", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := ed25519Key.Public().(ed25519.PublicKey); !ok {
|
if _, ok := ed25519Key.Public().(ed25519.PublicKey); !ok {
|
||||||
|
|
4
hmac.go
4
hmac.go
|
@ -59,7 +59,7 @@ func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interfa
|
||||||
// Verify the key is the right type
|
// Verify the key is the right type
|
||||||
keyBytes, ok := key.([]byte)
|
keyBytes, ok := key.([]byte)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrInvalidKeyType
|
return newError("HMAC verify expects []byte", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can we use the specified hashing method?
|
// Can we use the specified hashing method?
|
||||||
|
@ -91,7 +91,7 @@ func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interfa
|
||||||
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) ([]byte, error) {
|
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) ([]byte, error) {
|
||||||
if keyBytes, ok := key.([]byte); ok {
|
if keyBytes, ok := key.([]byte); ok {
|
||||||
if !m.Hash.Available() {
|
if !m.Hash.Available() {
|
||||||
return nil, ErrHashUnavailable
|
return nil, newError("HMAC sign expects []byte", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
hasher := hmac.New(m.Hash.New, keyBytes)
|
hasher := hmac.New(m.Hash.New, keyBytes)
|
||||||
|
|
4
rsa.go
4
rsa.go
|
@ -51,7 +51,7 @@ func (m *SigningMethodRSA) Verify(signingString string, sig []byte, key interfac
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
|
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
|
||||||
return ErrInvalidKeyType
|
return newError("RSA verify expects *rsa.PublicKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create hasher
|
// Create hasher
|
||||||
|
@ -73,7 +73,7 @@ func (m *SigningMethodRSA) Sign(signingString string, key interface{}) ([]byte,
|
||||||
|
|
||||||
// Validate type of key
|
// Validate type of key
|
||||||
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
|
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
|
||||||
return nil, ErrInvalidKey
|
return nil, newError("RSA sign expects *rsa.PrivateKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the hasher
|
// Create the hasher
|
||||||
|
|
|
@ -88,7 +88,7 @@ func (m *SigningMethodRSAPSS) Verify(signingString string, sig []byte, key inter
|
||||||
case *rsa.PublicKey:
|
case *rsa.PublicKey:
|
||||||
rsaKey = k
|
rsaKey = k
|
||||||
default:
|
default:
|
||||||
return ErrInvalidKey
|
return newError("RSA-PSS verify expects *rsa.PublicKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create hasher
|
// Create hasher
|
||||||
|
@ -115,7 +115,7 @@ func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) ([]byt
|
||||||
case *rsa.PrivateKey:
|
case *rsa.PrivateKey:
|
||||||
rsaKey = k
|
rsaKey = k
|
||||||
default:
|
default:
|
||||||
return nil, ErrInvalidKeyType
|
return nil, newError("RSA-PSS sign expects *rsa.PrivateKey", ErrInvalidKeyType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the hasher
|
// Create the hasher
|
||||||
|
|
Loading…
Reference in New Issue