Skip to content

feat: Raise meaningful exception when oauth callback times out #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions google_auth_oauthlib/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,9 @@ def run_local_server(
in the user's browser.
redirect_uri_trailing_slash (bool): whether or not to add trailing
slash when constructing the redirect_uri. Default value is True.
timeout_seconds (int): It will raise an error after the timeout timing
if there are no credentials response. The value is in seconds.
timeout_seconds (int): It will raise a WSGITimeout exception after the
timeout timing if there are no credentials response. The value is in
seconds.
When set to None there is no timeout.
Default value is None.
token_audience (str): Passed along with the request for an access
Expand All @@ -425,6 +426,10 @@ def run_local_server(
Returns:
google.oauth2.credentials.Credentials: The OAuth 2.0 credentials
for the user.

Raises:
WSGITimeout: If there is a timeout when waiting for the response from the
authorization server.
"""
wsgi_app = _RedirectWSGIApp(success_message)
# Fail fast if the address is occupied
Expand Down Expand Up @@ -452,6 +457,10 @@ def run_local_server(
local_server.timeout = timeout_seconds
local_server.handle_request()

if wsgi_app.last_request_uri is None:
# Timeout occurred
raise WSGITimeout("Timed out waiting for response from authorization server")

# Note: using https here because oauthlib is very picky that
# OAuth 2.0 should only occur over https.
authorization_response = wsgi_app.last_request_uri.replace("http", "https")
Expand Down Expand Up @@ -505,3 +514,7 @@ def __call__(self, environ, start_response):
start_response("200 OK", [("Content-type", "text/plain; charset=utf-8")])
self.last_request_uri = wsgiref.util.request_uri(environ)
return [self._success_message.encode("utf-8")]


class WSGITimeout(Exception):
"""Raised when the WSGI server times out waiting for a response."""