Skip to content

local server bind address #202

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

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion google_auth_oauthlib/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ def run_console(
def run_local_server(
self,
host="localhost",
bind_addr=None,
port=8080,
authorization_prompt_message=_DEFAULT_AUTH_PROMPT_MESSAGE,
success_message=_DEFAULT_WEB_SUCCESS_MESSAGE,
Expand All @@ -463,6 +464,11 @@ def run_local_server(
Args:
host (str): The hostname for the local redirect server. This will
be served over http, not https.
bind_addr (str): Optionally provide an ip address for the redirect
server to listen on when it is not the same as host
(e.g. in a container). Default value is None,
which means that the redirect server will listen
on the ip address specified in the host parameter.
port (int): The port for the local redirect server.
authorization_prompt_message (str): The message to display to tell
the user to navigate to the authorization URL.
Expand All @@ -483,7 +489,7 @@ def run_local_server(
# Fail fast if the address is occupied
wsgiref.simple_server.WSGIServer.allow_reuse_address = False
local_server = wsgiref.simple_server.make_server(
host, port, wsgi_app, handler_class=_WSGIRequestHandler
bind_addr or host, port, wsgi_app, handler_class=_WSGIRequestHandler
)

redirect_uri_format = (
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,24 @@ def assign_last_request_uri(host, port, wsgi_app, **kwargs):

assert not webbrowser_mock.open.called

@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True)
@mock.patch("wsgiref.simple_server.make_server", autospec=True)
def test_run_local_server_bind_addr(
self, make_server_mock, webbrowser_mock, instance, mock_fetch_token
):
def assign_last_request_uri(host, port, wsgi_app, **kwargs):
wsgi_app.last_request_uri = self.REDIRECT_REQUEST_PATH
return mock.Mock()

make_server_mock.side_effect = assign_last_request_uri

my_ip = socket.gethostbyname(socket.gethostname())
instance.run_local_server(bind_addr=my_ip, host="localhost")

assert webbrowser_mock.open.called
name, args, kwargs = make_server_mock.mock_calls[0]
assert args[0] == my_ip

@pytest.mark.webtest
@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True)
def test_run_local_server_occupied_port(
Expand Down