forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/hmac.go

150 lines
2.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"crypto"
"crypto/hmac"
"errors"
)
// Implements the HMAC-SHA family of signing methods signing methods
2024-02-18 10:42:21 +00:00
// Expects key type of []byte for both signing and validation
2024-02-18 10:42:21 +00:00
type SigningMethodHMAC struct {
Name string
2024-02-18 10:42:21 +00:00
Hash crypto.Hash
}
// Specific instances for HS256 and company
2024-02-18 10:42:21 +00:00
var (
SigningMethodHS256 *SigningMethodHMAC
SigningMethodHS384 *SigningMethodHMAC
SigningMethodHS512 *SigningMethodHMAC
2024-02-18 10:42:21 +00:00
ErrSignatureInvalid = errors.New("signature is invalid")
)
func init() {
2024-02-18 10:42:21 +00:00
// HS256
2024-02-18 10:42:21 +00:00
SigningMethodHS256 = &SigningMethodHMAC{"HS256", crypto.SHA256}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodHS256.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodHS256
2024-02-18 10:42:21 +00:00
})
// HS384
2024-02-18 10:42:21 +00:00
SigningMethodHS384 = &SigningMethodHMAC{"HS384", crypto.SHA384}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodHS384.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodHS384
2024-02-18 10:42:21 +00:00
})
// HS512
2024-02-18 10:42:21 +00:00
SigningMethodHS512 = &SigningMethodHMAC{"HS512", crypto.SHA512}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodHS512.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodHS512
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
func (m *SigningMethodHMAC) Alg() string {
2024-02-18 10:42:21 +00:00
return m.Name
2024-02-18 10:42:21 +00:00
}
// Verify the signature of HSXXX tokens. Returns nil if the signature is valid.
2024-02-18 10:42:21 +00:00
func (m *SigningMethodHMAC) Verify(signingString, signature string, key interface{}) error {
2024-02-18 10:42:21 +00:00
// Verify the key is the right type
2024-02-18 10:42:21 +00:00
keyBytes, ok := key.([]byte)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return ErrInvalidKeyType
2024-02-18 10:42:21 +00:00
}
// Decode signature, for comparison
2024-02-18 10:42:21 +00:00
sig, err := DecodeSegment(signature)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Can we use the specified hashing method?
2024-02-18 10:42:21 +00:00
if !m.Hash.Available() {
2024-02-18 10:42:21 +00:00
return ErrHashUnavailable
2024-02-18 10:42:21 +00:00
}
// This signing method is symmetric, so we validate the signature
2024-02-18 10:42:21 +00:00
// by reproducing the signature from the signing string and key, then
2024-02-18 10:42:21 +00:00
// comparing that against the provided signature.
2024-02-18 10:42:21 +00:00
hasher := hmac.New(m.Hash.New, keyBytes)
2024-02-18 10:42:21 +00:00
hasher.Write([]byte(signingString))
2024-02-18 10:42:21 +00:00
if !hmac.Equal(sig, hasher.Sum(nil)) {
2024-02-18 10:42:21 +00:00
return ErrSignatureInvalid
2024-02-18 10:42:21 +00:00
}
// No validation errors. Signature is good.
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// Implements the Sign method from SigningMethod for this signing method.
2024-02-18 10:42:21 +00:00
// Key must be []byte
2024-02-18 10:42:21 +00:00
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) {
2024-02-18 10:42:21 +00:00
if keyBytes, ok := key.([]byte); ok {
2024-02-18 10:42:21 +00:00
if !m.Hash.Available() {
2024-02-18 10:42:21 +00:00
return "", ErrHashUnavailable
2024-02-18 10:42:21 +00:00
}
hasher := hmac.New(m.Hash.New, keyBytes)
2024-02-18 10:42:21 +00:00
hasher.Write([]byte(signingString))
return EncodeSegment(hasher.Sum(nil)), nil
2024-02-18 10:42:21 +00:00
}
return "", ErrInvalidKeyType
2024-02-18 10:42:21 +00:00
}