exc.py 3.1 KB

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