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

158 lines
2.5 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
)
// SigningMethodRSA implements the RSA family of signing methods.
2024-02-18 10:42:21 +00:00
// Expects *rsa.PrivateKey for signing and *rsa.PublicKey for validation
2024-02-18 10:42:21 +00:00
type SigningMethodRSA struct {
Name string
2024-02-18 10:42:21 +00:00
Hash crypto.Hash
}
// Specific instances for RS256 and company
2024-02-18 10:42:21 +00:00
var (
SigningMethodRS256 *SigningMethodRSA
2024-02-18 10:42:21 +00:00
SigningMethodRS384 *SigningMethodRSA
2024-02-18 10:42:21 +00:00
SigningMethodRS512 *SigningMethodRSA
)
func init() {
2024-02-18 10:42:21 +00:00
// RS256
2024-02-18 10:42:21 +00:00
SigningMethodRS256 = &SigningMethodRSA{"RS256", crypto.SHA256}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodRS256.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodRS256
2024-02-18 10:42:21 +00:00
})
// RS384
2024-02-18 10:42:21 +00:00
SigningMethodRS384 = &SigningMethodRSA{"RS384", crypto.SHA384}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodRS384.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodRS384
2024-02-18 10:42:21 +00:00
})
// RS512
2024-02-18 10:42:21 +00:00
SigningMethodRS512 = &SigningMethodRSA{"RS512", crypto.SHA512}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodRS512.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodRS512
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
func (m *SigningMethodRSA) Alg() string {
2024-02-18 10:42:21 +00:00
return m.Name
2024-02-18 10:42:21 +00:00
}
// Verify implements token verification for the SigningMethod
2024-02-18 10:42:21 +00:00
// For this signing method, must be an *rsa.PublicKey structure.
2024-02-18 10:42:21 +00:00
func (m *SigningMethodRSA) Verify(signingString, signature string, key interface{}) error {
2024-02-18 10:42:21 +00:00
var err error
// Decode the signature
2024-02-18 10:42:21 +00:00
var sig []byte
2024-02-18 10:42:21 +00:00
if sig, err = DecodeSegment(signature); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
var rsaKey *rsa.PublicKey
2024-02-18 10:42:21 +00:00
var ok bool
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
2024-02-18 10:42:21 +00:00
return ErrInvalidKeyType
2024-02-18 10:42:21 +00:00
}
// Create hasher
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
}
2024-02-18 10:42:21 +00:00
hasher := m.Hash.New()
2024-02-18 10:42:21 +00:00
hasher.Write([]byte(signingString))
// Verify the signature
2024-02-18 10:42:21 +00:00
return rsa.VerifyPKCS1v15(rsaKey, m.Hash, hasher.Sum(nil), sig)
2024-02-18 10:42:21 +00:00
}
// Sign implements token signing for the SigningMethod
2024-02-18 10:42:21 +00:00
// For this signing method, must be an *rsa.PrivateKey structure.
2024-02-18 10:42:21 +00:00
func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string, error) {
2024-02-18 10:42:21 +00:00
var rsaKey *rsa.PrivateKey
2024-02-18 10:42:21 +00:00
var ok bool
// Validate type of key
2024-02-18 10:42:21 +00:00
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
2024-02-18 10:42:21 +00:00
return "", ErrInvalidKey
2024-02-18 10:42:21 +00:00
}
// Create the hasher
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 := m.Hash.New()
2024-02-18 10:42:21 +00:00
hasher.Write([]byte(signingString))
// Sign the string and return the encoded bytes
2024-02-18 10:42:21 +00:00
if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil {
2024-02-18 10:42:21 +00:00
return EncodeSegment(sigBytes), nil
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}