Skip to content

Commit 061b888

Browse files
dopryakatsoulas
authored andcommitted
fix: token error response handling
1 parent adea094 commit 061b888

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mozilla_django_oidc/auth.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from josepy.jwk import JWK
1515
from josepy.jws import JWS, Header
1616
from requests.auth import HTTPBasicAuth
17+
from requests.exceptions import HTTPError
1718

1819
from mozilla_django_oidc.utils import absolutify, import_from_settings
1920

@@ -235,9 +236,20 @@ def get_token(self, payload):
235236
timeout=self.get_settings("OIDC_TIMEOUT", None),
236237
proxies=self.get_settings("OIDC_PROXY", None),
237238
)
238-
response.raise_for_status()
239+
self.raise_token_response_error(response)
239240
return response.json()
240241

242+
def raise_token_response_error(self, response):
243+
"""Raises :class:`HTTPError`, if one occurred.
244+
as per: https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
245+
"""
246+
# if there wasn't an error all is good
247+
if response.status_code == 200:
248+
return
249+
# otherwise something is up...
250+
http_error_msg = f"Get Token Error (url: {response.url}, status: {response.status_code}, body: {response.text})"
251+
raise HTTPError(http_error_msg, response=response)
252+
241253
def get_userinfo(self, access_token, id_token, payload):
242254
"""Return user details dictionary. The id_token and payload are not used in
243255
the default implementation, but may be used when overriding this method"""

0 commit comments

Comments
 (0)