123 | 123 |
that. The original exception that caused that will be stored on the
|
124 | 124 |
exception as :attr:`original_error`.
|
125 | 125 |
|
|
126 |
This can also happen with a :class:`JSONWebSignatureSerializer` that
|
|
127 |
is subclassed and uses a different serializer for the payload than
|
|
128 |
the expected one.
|
|
129 |
|
126 | 130 |
.. versionadded:: 0.15
|
127 | 131 |
"""
|
128 | 132 |
|
|
163 | 167 |
#:
|
164 | 168 |
#: .. versionadded:: 0.14
|
165 | 169 |
self.date_signed = date_signed
|
|
170 |
|
|
171 |
|
|
172 |
class BadHeader(BadSignature):
|
|
173 |
"""Raised if a signed header is invalid in some form. This only
|
|
174 |
happens for serializers that have a header that goes with the
|
|
175 |
signature.
|
|
176 |
|
|
177 |
.. versionadded:: 0.24
|
|
178 |
"""
|
|
179 |
|
|
180 |
def __init__(self, message, payload=None, header=None,
|
|
181 |
original_error=None):
|
|
182 |
BadSignature.__init__(self, message, payload)
|
|
183 |
|
|
184 |
#: If the header is actually available but just malformed it
|
|
185 |
#: might be stored here.
|
|
186 |
self.header = header
|
|
187 |
|
|
188 |
#: If available, the error that indicates why the payload
|
|
189 |
#: was not valid. This might be `None`.
|
|
190 |
self.original_error = original_error
|
166 | 191 |
|
167 | 192 |
|
168 | 193 |
class SignatureExpired(BadTimeSignature):
|
|
510 | 535 |
return serializer.loads(payload)
|
511 | 536 |
except Exception as e:
|
512 | 537 |
raise BadPayload('Could not load the payload because an '
|
513 | |
'exception ocurred on unserializing the data',
|
|
538 |
'exception occurred on unserializing the data',
|
514 | 539 |
original_error=e)
|
515 | 540 |
|
516 | 541 |
def dump_payload(self, obj):
|
|
656 | 681 |
base64d_header, base64d_payload = payload.split(b'.', 1)
|
657 | 682 |
try:
|
658 | 683 |
json_header = base64_decode(base64d_header)
|
|
684 |
except Exception as e:
|
|
685 |
raise BadHeader('Could not base64 decode the header because of '
|
|
686 |
'an exception', original_error=e)
|
|
687 |
try:
|
659 | 688 |
json_payload = base64_decode(base64d_payload)
|
660 | 689 |
except Exception as e:
|
661 | 690 |
raise BadPayload('Could not base64 decode the payload because of '
|
662 | 691 |
'an exception', original_error=e)
|
663 | |
header = Serializer.load_payload(self, json_header,
|
664 | |
serializer=json)
|
|
692 |
try:
|
|
693 |
header = Serializer.load_payload(self, json_header,
|
|
694 |
serializer=json)
|
|
695 |
except BadData as e:
|
|
696 |
raise BadHeader('Could not unserialize header because it was '
|
|
697 |
'malformed', original_error=e)
|
665 | 698 |
if not isinstance(header, dict):
|
666 | |
raise BadPayload('Header payload is not a JSON object')
|
|
699 |
raise BadHeader('Header payload is not a JSON object',
|
|
700 |
header=header)
|
667 | 701 |
payload = Serializer.load_payload(self, json_payload)
|
668 | 702 |
if return_header:
|
669 | 703 |
return payload, header
|
|
711 | 745 |
self.make_signer(salt, self.algorithm).unsign(want_bytes(s)),
|
712 | 746 |
return_header=True)
|
713 | 747 |
if header.get('alg') != self.algorithm_name:
|
714 | |
raise BadSignature('Algorithm mismatch')
|
|
748 |
raise BadHeader('Algorithm mismatch', header=header,
|
|
749 |
payload=payload)
|
715 | 750 |
if return_header:
|
716 | 751 |
return payload, header
|
717 | 752 |
return payload
|