|
4 | 4 | from oauthlib.oauth2 import WebApplicationClient, InsecureTransportError |
5 | 5 | from oauthlib.oauth2 import LegacyApplicationClient |
6 | 6 | from oauthlib.oauth2 import TokenExpiredError, is_secure_transport |
| 7 | +from oauthlib.oauth2.rfc6749.errors import CustomOAuth2Error |
7 | 8 | import requests |
8 | 9 |
|
9 | 10 | log = logging.getLogger(__name__) |
@@ -199,6 +200,17 @@ def authorization_url(self, url, state=None, **kwargs): |
199 | 200 | state, |
200 | 201 | ) |
201 | 202 |
|
| 203 | + def validate_token_response(self, r): |
| 204 | + message = "" |
| 205 | + try: |
| 206 | + r.raise_for_status() |
| 207 | + except requests.HTTPError as e: |
| 208 | + message = str(e) |
| 209 | + if r.text: |
| 210 | + message += f"\nBody: {r.text}" |
| 211 | + if message: |
| 212 | + raise CustomOAuth2Error('Response error', message, uri=r.request.url, status_code=r.status_code) |
| 213 | + |
202 | 214 | def fetch_token( |
203 | 215 | self, |
204 | 216 | token_url, |
@@ -403,6 +415,7 @@ def fetch_token( |
403 | 415 | log.debug("Invoking hook %s.", hook) |
404 | 416 | r = hook(r) |
405 | 417 |
|
| 418 | + self.validate_token_response(r) |
406 | 419 | self._client.parse_request_body_response(r.text, scope=self.scope) |
407 | 420 | self.token = self._client.token |
408 | 421 | log.debug("Obtained token %s.", self.token) |
@@ -493,6 +506,7 @@ def refresh_token( |
493 | 506 | log.debug("Invoking hook %s.", hook) |
494 | 507 | r = hook(r) |
495 | 508 |
|
| 509 | + self.validate_token_response(r) |
496 | 510 | self.token = self._client.parse_request_body_response(r.text, scope=self.scope) |
497 | 511 | if "refresh_token" not in self.token: |
498 | 512 | log.debug("No new refresh token given. Re-using old.") |
|
0 commit comments