From 061b8882acc119318b3a62c522475f042c4a8c63 Mon Sep 17 00:00:00 2001 From: Darrel O'Pry Date: Fri, 15 Jul 2022 14:28:27 -0400 Subject: [PATCH] fix: token error response handling --- mozilla_django_oidc/auth.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mozilla_django_oidc/auth.py b/mozilla_django_oidc/auth.py index 5a94147a..b71c97db 100644 --- a/mozilla_django_oidc/auth.py +++ b/mozilla_django_oidc/auth.py @@ -14,6 +14,7 @@ from josepy.jwk import JWK from josepy.jws import JWS, Header from requests.auth import HTTPBasicAuth +from requests.exceptions import HTTPError from mozilla_django_oidc.utils import absolutify, import_from_settings @@ -235,9 +236,20 @@ def get_token(self, payload): timeout=self.get_settings("OIDC_TIMEOUT", None), proxies=self.get_settings("OIDC_PROXY", None), ) - response.raise_for_status() + self.raise_token_response_error(response) return response.json() + def raise_token_response_error(self, response): + """Raises :class:`HTTPError`, if one occurred. + as per: https://datatracker.ietf.org/doc/html/rfc6749#section-5.2 + """ + # if there wasn't an error all is good + if response.status_code == 200: + return + # otherwise something is up... + http_error_msg = f"Get Token Error (url: {response.url}, status: {response.status_code}, body: {response.text})" + raise HTTPError(http_error_msg, response=response) + def get_userinfo(self, access_token, id_token, payload): """Return user details dictionary. The id_token and payload are not used in the default implementation, but may be used when overriding this method"""