You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signing_method.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package jwt
  2. import (
  3. "sync"
  4. )
  5. var signingMethods = map[string]func() SigningMethod{}
  6. var signingMethodLock = new(sync.RWMutex)
  7. // SigningMethod can be used add new methods for signing or verifying tokens. It
  8. // takes a decoded signature as an input in the Verify function and produces a
  9. // signature in Sign. The signature is then usually base64 encoded as part of a
  10. // JWT.
  11. type SigningMethod interface {
  12. Verify(signingString string, sig []byte, key interface{}) error // Returns nil if signature is valid
  13. Sign(signingString string, key interface{}) ([]byte, error) // Returns signature or error
  14. Alg() string // returns the alg identifier for this method (example: 'HS256')
  15. }
  16. // RegisterSigningMethod registers the "alg" name and a factory function for signing method.
  17. // This is typically done during init() in the method's implementation
  18. func RegisterSigningMethod(alg string, f func() SigningMethod) {
  19. signingMethodLock.Lock()
  20. defer signingMethodLock.Unlock()
  21. signingMethods[alg] = f
  22. }
  23. // GetSigningMethod retrieves a signing method from an "alg" string
  24. func GetSigningMethod(alg string) (method SigningMethod) {
  25. signingMethodLock.RLock()
  26. defer signingMethodLock.RUnlock()
  27. if methodF, ok := signingMethods[alg]; ok {
  28. method = methodF()
  29. }
  30. return
  31. }
  32. // GetAlgorithms returns a list of registered "alg" names
  33. func GetAlgorithms() (algs []string) {
  34. signingMethodLock.RLock()
  35. defer signingMethodLock.RUnlock()
  36. for alg := range signingMethods {
  37. algs = append(algs, alg)
  38. }
  39. return
  40. }