errors.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ErrTokenMalformed = errors.New("token is malformed")
  11. ErrTokenUnverifiable = errors.New("token is unverifiable")
  12. ErrTokenSignatureInvalid = errors.New("token signature is invalid")
  13. ErrTokenInvalidAudience = errors.New("token has invalid audience")
  14. ErrTokenExpired = errors.New("token is expired")
  15. ErrTokenUsedBeforeIssued = errors.New("token used before issued")
  16. ErrTokenInvalidIssuer = errors.New("token has invalid issuer")
  17. ErrTokenNotValidYet = errors.New("token is not valid yet")
  18. ErrTokenInvalidId = errors.New("token has invalid id")
  19. ErrTokenInvalidClaims = errors.New("token has invalid claims")
  20. )
  21. // The errors that might occur when parsing and validating a token
  22. const (
  23. ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
  24. ValidationErrorUnverifiable // Token could not be verified because of signing problems
  25. ValidationErrorSignatureInvalid // Signature validation failed
  26. // Standard Claim validation errors
  27. ValidationErrorAudience // AUD validation failed
  28. ValidationErrorExpired // EXP validation failed
  29. ValidationErrorIssuedAt // IAT validation failed
  30. ValidationErrorIssuer // ISS validation failed
  31. ValidationErrorNotValidYet // NBF validation failed
  32. ValidationErrorId // JTI validation failed
  33. ValidationErrorClaimsInvalid // Generic claims validation error
  34. )
  35. // NewValidationError is a helper for constructing a ValidationError with a string error message
  36. func NewValidationError(errorText string, errorFlags uint32) *ValidationError {
  37. return &ValidationError{
  38. text: errorText,
  39. Errors: errorFlags,
  40. }
  41. }
  42. // ValidationError represents an error from Parse if token is not valid
  43. type ValidationError struct {
  44. Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
  45. Errors uint32 // bitfield. see ValidationError... constants
  46. text string // errors that do not have a valid error just have text
  47. }
  48. // Error is the implementation of the err interface.
  49. func (e ValidationError) Error() string {
  50. if e.Inner != nil {
  51. return e.Inner.Error()
  52. } else if e.text != "" {
  53. return e.text
  54. } else {
  55. return "token is invalid"
  56. }
  57. }
  58. // Unwrap gives errors.Is and errors.As access to the inner error.
  59. func (e *ValidationError) Unwrap() error {
  60. return e.Inner
  61. }
  62. // No errors
  63. func (e *ValidationError) valid() bool {
  64. return e.Errors == 0
  65. }
  66. // Is checks if this ValidationError is of the supplied error. We are first checking for the exact error message
  67. // by comparing the inner error message. If that fails, we compare using the error flags. This way we can use
  68. // custom error messages (mainly for backwards compatability) and still leverage errors.Is using the global error variables.
  69. func (e *ValidationError) Is(err error) bool {
  70. // Check, if our inner error is a direct match
  71. if errors.Is(errors.Unwrap(e), err) {
  72. return true
  73. }
  74. // Otherwise, we need to match using our error flags
  75. switch err {
  76. case ErrTokenMalformed:
  77. return e.Errors&ValidationErrorMalformed != 0
  78. case ErrTokenUnverifiable:
  79. return e.Errors&ValidationErrorUnverifiable != 0
  80. case ErrTokenSignatureInvalid:
  81. return e.Errors&ValidationErrorSignatureInvalid != 0
  82. case ErrTokenInvalidAudience:
  83. return e.Errors&ValidationErrorAudience != 0
  84. case ErrTokenExpired:
  85. return e.Errors&ValidationErrorExpired != 0
  86. case ErrTokenUsedBeforeIssued:
  87. return e.Errors&ValidationErrorIssuedAt != 0
  88. case ErrTokenInvalidIssuer:
  89. return e.Errors&ValidationErrorIssuer != 0
  90. case ErrTokenNotValidYet:
  91. return e.Errors&ValidationErrorNotValidYet != 0
  92. case ErrTokenInvalidId:
  93. return e.Errors&ValidationErrorId != 0
  94. case ErrTokenInvalidClaims:
  95. return e.Errors&ValidationErrorClaimsInvalid != 0
  96. }
  97. return false
  98. }