本站源代码
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.

60 lines
1.8KB

  1. package jwt
  2. import (
  3. "errors"
  4. )
  5. // Error constants
  6. var (
  7. ErrInvalidKey = errors.New("key is invalid")
  8. ErrInvalidKeyType = errors.New("key is of invalid type")
  9. ErrHashUnavailable = errors.New("the requested hash function is unavailable")
  10. )
  11. // The errors that might occur when parsing and validating a token
  12. const (
  13. ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
  14. ValidationErrorUnverifiable // Token could not be verified because of signing problems
  15. ValidationErrorSignatureInvalid // Signature validation failed
  16. // Standard Claim validation errors
  17. ValidationErrorAudience // AUD validation failed
  18. ValidationErrorExpired // EXP validation failed
  19. ValidationErrorIssuedAt // IAT validation failed
  20. ValidationErrorIssuer // ISS validation failed
  21. ValidationErrorNotValidYet // NBF validation failed
  22. ValidationErrorId // JTI validation failed
  23. ValidationErrorClaimsInvalid // Generic claims validation error
  24. )
  25. // Helper for constructing a ValidationError with a string error message
  26. func NewValidationError(errorText string, errorFlags uint32) *ValidationError {
  27. return &ValidationError{
  28. text: errorText,
  29. Errors: errorFlags,
  30. }
  31. }
  32. // The error from Parse if token is not valid
  33. type ValidationError struct {
  34. Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
  35. Errors uint32 // bitfield. see ValidationError... constants
  36. text string // errors that do not have a valid error just have text
  37. }
  38. // Validation error is an error type
  39. func (e ValidationError) Error() string {
  40. if e.Inner != nil {
  41. return e.Inner.Error()
  42. } else if e.text != "" {
  43. return e.text
  44. } else {
  45. return "token is invalid"
  46. }
  47. }
  48. // No errors
  49. func (e *ValidationError) valid() bool {
  50. return e.Errors == 0
  51. }
上海开阖软件有限公司 沪ICP备12045867号-1