exc.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from ._compat import PY2
  2. from ._compat import text_type
  3. class BadData(Exception):
  4. """Raised if bad data of any sort was encountered. This is the base
  5. for all exceptions that itsdangerous defines.
  6. .. versionadded:: 0.15
  7. """
  8. message = None
  9. def __init__(self, message):
  10. super(BadData, self).__init__(self, message)
  11. self.message = message
  12. def __str__(self):
  13. return text_type(self.message)
  14. if PY2:
  15. __unicode__ = __str__
  16. def __str__(self):
  17. return self.__unicode__().encode("utf-8")
  18. class BadSignature(BadData):
  19. """Raised if a signature does not match."""
  20. def __init__(self, message, payload=None):
  21. BadData.__init__(self, message)
  22. #: The payload that failed the signature test. In some
  23. #: situations you might still want to inspect this, even if
  24. #: you know it was tampered with.
  25. #:
  26. #: .. versionadded:: 0.14
  27. self.payload = payload
  28. class BadTimeSignature(BadSignature):
  29. """Raised if a time-based signature is invalid. This is a subclass
  30. of :class:`BadSignature`.
  31. """
  32. def __init__(self, message, payload=None, date_signed=None):
  33. BadSignature.__init__(self, message, payload)
  34. #: If the signature expired this exposes the date of when the
  35. #: signature was created. This can be helpful in order to
  36. #: tell the user how long a link has been gone stale.
  37. #:
  38. #: .. versionadded:: 0.14
  39. self.date_signed = date_signed
  40. class SignatureExpired(BadTimeSignature):
  41. """Raised if a signature timestamp is older than ``max_age``. This
  42. is a subclass of :exc:`BadTimeSignature`.
  43. """
  44. class BadHeader(BadSignature):
  45. """Raised if a signed header is invalid in some form. This only
  46. happens for serializers that have a header that goes with the
  47. signature.
  48. .. versionadded:: 0.24
  49. """
  50. def __init__(self, message, payload=None, header=None, original_error=None):
  51. BadSignature.__init__(self, message, payload)
  52. #: If the header is actually available but just malformed it
  53. #: might be stored here.
  54. self.header = header
  55. #: If available, the error that indicates why the payload was
  56. #: not valid. This might be ``None``.
  57. self.original_error = original_error
  58. class BadPayload(BadData):
  59. """Raised if a payload is invalid. This could happen if the payload
  60. is loaded despite an invalid signature, or if there is a mismatch
  61. between the serializer and deserializer. The original exception
  62. that occurred during loading is stored on as :attr:`original_error`.
  63. .. versionadded:: 0.15
  64. """
  65. def __init__(self, message, original_error=None):
  66. BadData.__init__(self, message)
  67. #: If available, the error that indicates why the payload was
  68. #: not valid. This might be ``None``.
  69. self.original_error = original_error