jwt/signing_method.go

32 lines
792 B
Go

package jwt
import (
"errors"
"fmt"
)
var signingMethods = map[string]func() SigningMethod{}
// Signing method
type SigningMethod interface {
Verify(signingString, signature string, key []byte) error
Sign(signingString string, key []byte)(string, error)
Alg() string
}
// Register the "alg" name and a factory function for signing method.
// This is typically done during init() in the method's implementation
func RegisterSigningMethod(alg string, f func() SigningMethod) {
signingMethods[alg] = f
}
// Get a signing method from an "alg" string
func GetSigningMethod(alg string) (method SigningMethod, err error) {
if methodF, ok := signingMethods[alg]; ok {
method = methodF()
} else {
err = errors.New(fmt.Sprintf("Invalid signing method (alg): %v", method))
}
return
}