Skip to content

Commit b94f155

Browse files
committed
feat: add bind_addr to run_local_server
1 parent d15eeb2 commit b94f155

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

google_auth_oauthlib/flow.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ def run_console(
443443
def run_local_server(
444444
self,
445445
host="localhost",
446+
bind_addr=None,
446447
port=8080,
447448
authorization_prompt_message=_DEFAULT_AUTH_PROMPT_MESSAGE,
448449
success_message=_DEFAULT_WEB_SUCCESS_MESSAGE,
@@ -463,6 +464,11 @@ def run_local_server(
463464
Args:
464465
host (str): The hostname for the local redirect server. This will
465466
be served over http, not https.
467+
bind_addr (str): Optionally provide an ip address for the redirect
468+
server to listen on when it is not the same as host
469+
(e.g. in a container). Default value is None,
470+
which means that the redirect server will listen
471+
on the ip address specified in the host parameter.
466472
port (int): The port for the local redirect server.
467473
authorization_prompt_message (str): The message to display to tell
468474
the user to navigate to the authorization URL.
@@ -483,7 +489,7 @@ def run_local_server(
483489
# Fail fast if the address is occupied
484490
wsgiref.simple_server.WSGIServer.allow_reuse_address = False
485491
local_server = wsgiref.simple_server.make_server(
486-
host, port, wsgi_app, handler_class=_WSGIRequestHandler
492+
bind_addr or host, port, wsgi_app, handler_class=_WSGIRequestHandler
487493
)
488494

489495
redirect_uri_format = (

tests/unit/test_flow.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,24 @@ def assign_last_request_uri(host, port, wsgi_app, **kwargs):
403403

404404
assert not webbrowser_mock.open.called
405405

406+
@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True)
407+
@mock.patch("wsgiref.simple_server.make_server", autospec=True)
408+
def test_run_local_server_bind_addr(
409+
self, make_server_mock, webbrowser_mock, instance, mock_fetch_token
410+
):
411+
def assign_last_request_uri(host, port, wsgi_app, **kwargs):
412+
wsgi_app.last_request_uri = self.REDIRECT_REQUEST_PATH
413+
return mock.Mock()
414+
415+
make_server_mock.side_effect = assign_last_request_uri
416+
417+
my_ip = socket.gethostbyname(socket.gethostname())
418+
instance.run_local_server(bind_addr=my_ip, host="localhost")
419+
420+
assert webbrowser_mock.open.called
421+
name, args, kwargs = make_server_mock.mock_calls[0]
422+
assert args[0] == my_ip
423+
406424
@pytest.mark.webtest
407425
@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True)
408426
def test_run_local_server_occupied_port(

0 commit comments

Comments
 (0)