Skip to content

Commit 1a9dca8

Browse files
authored
feat: Adding support to specify browser while launching browser to authention (#305)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/google-auth-library-python-oauthlib/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #303
1 parent fe08531 commit 1a9dca8

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

google_auth_oauthlib/flow.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ def run_local_server(
379379
redirect_uri_trailing_slash=True,
380380
timeout_seconds=None,
381381
token_audience=None,
382+
browser=None,
382383
**kwargs
383384
):
384385
"""Run the flow using the server strategy.
@@ -416,6 +417,8 @@ def run_local_server(
416417
token_audience (str): Passed along with the request for an access
417418
token. Determines the endpoints with which the token can be
418419
used. Optional.
420+
browser (str): specify which browser to open for authentication. If not
421+
specified this defaults to default browser.
419422
kwargs: Additional keyword arguments passed through to
420423
:meth:`authorization_url`.
421424
@@ -437,7 +440,8 @@ def run_local_server(
437440
auth_url, _ = self.authorization_url(**kwargs)
438441

439442
if open_browser:
440-
webbrowser.open(auth_url, new=1, autoraise=True)
443+
# if browser is None it defaults to default browser
444+
webbrowser.get(browser).open(auth_url, new=1, autoraise=True)
441445

442446
if authorization_prompt_message:
443447
print(authorization_prompt_message.format(url=auth_url))

tests/unit/test_flow.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def test_run_local_server(self, webbrowser_mock, instance, mock_fetch_token, por
304304
assert credentials.token == mock.sentinel.access_token
305305
assert credentials._refresh_token == mock.sentinel.refresh_token
306306
assert credentials.id_token == mock.sentinel.id_token
307-
assert webbrowser_mock.open.called
307+
assert webbrowser_mock.get().open.called
308308
assert instance.redirect_uri == f"http://localhost:{port}/"
309309

310310
expected_auth_response = auth_redirect_url.replace("http", "https")
@@ -343,7 +343,7 @@ def test_run_local_server_audience(
343343
assert credentials.token == mock.sentinel.access_token
344344
assert credentials._refresh_token == mock.sentinel.refresh_token
345345
assert credentials.id_token == mock.sentinel.id_token
346-
assert webbrowser_mock.open.called
346+
assert webbrowser_mock.get().open.called
347347
assert instance.redirect_uri == f"http://localhost:{port}/"
348348

349349
expected_auth_response = auth_redirect_url.replace("http", "https")
@@ -385,7 +385,7 @@ def test_run_local_server_code_verifier(
385385
assert credentials.token == mock.sentinel.access_token
386386
assert credentials._refresh_token == mock.sentinel.refresh_token
387387
assert credentials.id_token == mock.sentinel.id_token
388-
assert webbrowser_mock.open.called
388+
assert webbrowser_mock.get().open.called
389389
assert instance.redirect_uri == f"http://localhost:{port}"
390390

391391
expected_auth_response = auth_redirect_url.replace("http", "https")
@@ -410,7 +410,7 @@ def assign_last_request_uri(host, port, wsgi_app, **kwargs):
410410

411411
instance.run_local_server(open_browser=False)
412412

413-
assert not webbrowser_mock.open.called
413+
assert not webbrowser_mock.get().open.called
414414

415415
@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True)
416416
@mock.patch("wsgiref.simple_server.make_server", autospec=True)
@@ -426,7 +426,7 @@ def assign_last_request_uri(host, port, wsgi_app, **kwargs):
426426
my_ip = socket.gethostbyname(socket.gethostname())
427427
instance.run_local_server(bind_addr=my_ip, host="localhost")
428428

429-
assert webbrowser_mock.open.called
429+
assert webbrowser_mock.get().open.called
430430
name, args, kwargs = make_server_mock.mock_calls[0]
431431
assert args[0] == my_ip
432432

0 commit comments

Comments
 (0)