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