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.

none.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package jwt
  2. // SigningMethodNone implements the none signing method. This is required by the spec
  3. // but you probably should never use it.
  4. var SigningMethodNone *signingMethodNone
  5. const UnsafeAllowNoneSignatureType unsafeNoneMagicConstant = "none signing method allowed"
  6. var NoneSignatureTypeDisallowedError error
  7. type signingMethodNone struct{}
  8. type unsafeNoneMagicConstant string
  9. func init() {
  10. SigningMethodNone = &signingMethodNone{}
  11. NoneSignatureTypeDisallowedError = newError("'none' signature type is not allowed", ErrTokenUnverifiable)
  12. RegisterSigningMethod(SigningMethodNone.Alg(), func() SigningMethod {
  13. return SigningMethodNone
  14. })
  15. }
  16. func (m *signingMethodNone) Alg() string {
  17. return "none"
  18. }
  19. // Only allow 'none' alg type if UnsafeAllowNoneSignatureType is specified as the key
  20. func (m *signingMethodNone) Verify(signingString string, sig []byte, key interface{}) (err error) {
  21. // Key must be UnsafeAllowNoneSignatureType to prevent accidentally
  22. // accepting 'none' signing method
  23. if _, ok := key.(unsafeNoneMagicConstant); !ok {
  24. return NoneSignatureTypeDisallowedError
  25. }
  26. // If signing method is none, signature must be an empty string
  27. if len(sig) != 0 {
  28. return newError("'none' signing method with non-empty signature", ErrTokenUnverifiable)
  29. }
  30. // Accept 'none' signing method.
  31. return nil
  32. }
  33. // Only allow 'none' signing if UnsafeAllowNoneSignatureType is specified as the key
  34. func (m *signingMethodNone) Sign(signingString string, key interface{}) ([]byte, error) {
  35. if _, ok := key.(unsafeNoneMagicConstant); ok {
  36. return []byte{}, nil
  37. }
  38. return nil, NoneSignatureTypeDisallowedError
  39. }