|
10 | 10 | from authlib.common.errors import AuthlibBaseError |
11 | 11 | from authlib.integrations.flask_client import FlaskIntegration, OAuth |
12 | 12 | from flask import current_app, redirect, request, url_for |
13 | | -from requests.exceptions import HTTPError, RequestException |
| 13 | +from requests.exceptions import HTTPError, RequestException, Timeout |
14 | 14 |
|
15 | 15 | from flask_multipass.auth import AuthProvider |
16 | 16 | from flask_multipass.data import AuthInfo, IdentityInfo |
17 | | -from flask_multipass.exceptions import AuthenticationFailed, IdentityRetrievalFailed |
| 17 | +from flask_multipass.exceptions import AuthenticationFailed, IdentityRetrievalFailed, MultipassException |
18 | 18 | from flask_multipass.identity import IdentityProvider |
19 | 19 | from flask_multipass.util import login_view |
20 | 20 |
|
@@ -70,13 +70,17 @@ class AuthlibAuthProvider(AuthProvider): |
70 | 70 | of ``register()`` in the |
71 | 71 | `authlib docs <https://docs.authlib.org/en/latest/client/frameworks.html>`_ |
72 | 72 | for details. |
| 73 | + - ``request_timeout``: the timeout in seconds for fetching the oauth token and |
| 74 | + requesting data from the userinfo endpoint (10 by default, |
| 75 | + set to None to disable) |
73 | 76 | """ |
74 | 77 |
|
75 | 78 | def __init__(self, *args, **kwargs): |
76 | 79 | super().__init__(*args, **kwargs) |
77 | 80 | callback_uri = self.settings.get('callback_uri', f'/multipass/authlib/{self.name}') |
78 | 81 | self.authlib_client = _authlib_oauth.register(self.name, **self.authlib_settings) |
79 | 82 | self.include_token = self.settings.get('include_token', False) |
| 83 | + self.request_timeout = self.settings.get('request_timeout') |
80 | 84 | self.use_id_token = self.settings.get('use_id_token') |
81 | 85 | if self.use_id_token is None: |
82 | 86 | # default to using the id token when using the openid scope (oidc) |
@@ -121,7 +125,10 @@ def _authorize_callback(self): |
121 | 125 | raise AuthenticationFailed(error, provider=self) |
122 | 126 | try: |
123 | 127 | try: |
124 | | - token_data = self.authlib_client.authorize_access_token() |
| 128 | + token_data = self.authlib_client.authorize_access_token(timeout=self.request_timeout) |
| 129 | + except Timeout as exc: |
| 130 | + logging.getLogger('multipass.authlib').error('Getting token timed out') |
| 131 | + raise MultipassException('Token request timed out, please try again later') from exc |
125 | 132 | except HTTPError as exc: |
126 | 133 | try: |
127 | 134 | data = exc.response.json() |
|
0 commit comments