jwt/signing_method.go

32 lines
792 B
Go
Raw Normal View History

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