Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/itsdangerous/exc.py: 56%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import typing as t
4from datetime import datetime
7class BadData(Exception):
8 """Raised if bad data of any sort was encountered. This is the base
9 for all exceptions that ItsDangerous defines.
11 .. versionadded:: 0.15
12 """
14 def __init__(self, message: str):
15 super().__init__(message)
16 self.message = message
18 def __str__(self) -> str:
19 return self.message
22class BadSignature(BadData):
23 """Raised if a signature does not match."""
25 def __init__(self, message: str, payload: t.Any | None = None):
26 super().__init__(message)
28 #: The payload that failed the signature test. In some
29 #: situations you might still want to inspect this, even if
30 #: you know it was tampered with.
31 #:
32 #: .. versionadded:: 0.14
33 self.payload: t.Any | None = payload
36class BadTimeSignature(BadSignature):
37 """Raised if a time-based signature is invalid. This is a subclass
38 of :class:`BadSignature`.
39 """
41 def __init__(
42 self,
43 message: str,
44 payload: t.Any | None = None,
45 date_signed: datetime | None = None,
46 ):
47 super().__init__(message, payload)
49 #: If the signature expired this exposes the date of when the
50 #: signature was created. This can be helpful in order to
51 #: tell the user how long a link has been gone stale.
52 #:
53 #: .. versionchanged:: 2.0
54 #: The datetime value is timezone-aware rather than naive.
55 #:
56 #: .. versionadded:: 0.14
57 self.date_signed = date_signed
60class SignatureExpired(BadTimeSignature):
61 """Raised if a signature timestamp is older than ``max_age``. This
62 is a subclass of :exc:`BadTimeSignature`.
63 """
66class BadHeader(BadSignature):
67 """Raised if a signed header is invalid in some form. This only
68 happens for serializers that have a header that goes with the
69 signature.
71 .. versionadded:: 0.24
72 """
74 def __init__(
75 self,
76 message: str,
77 payload: t.Any | None = None,
78 header: t.Any | None = None,
79 original_error: Exception | None = None,
80 ):
81 super().__init__(message, payload)
83 #: If the header is actually available but just malformed it
84 #: might be stored here.
85 self.header: t.Any | None = header
87 #: If available, the error that indicates why the payload was
88 #: not valid. This might be ``None``.
89 self.original_error: Exception | None = original_error
92class BadPayload(BadData):
93 """Raised if a payload is invalid. This could happen if the payload
94 is loaded despite an invalid signature, or if there is a mismatch
95 between the serializer and deserializer. The original exception
96 that occurred during loading is stored on as :attr:`original_error`.
98 .. versionadded:: 0.15
99 """
101 def __init__(self, message: str, original_error: Exception | None = None):
102 super().__init__(message)
104 #: If available, the error that indicates why the payload was
105 #: not valid. This might be ``None``.
106 self.original_error: Exception | None = original_error