Skip to content

Commit

Permalink
feat: Adding support to specify browser while launching browser to au…
Browse files Browse the repository at this point in the history
…thention (#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
  • Loading branch information
anujmaurice authored Sep 7, 2023
1 parent fe08531 commit 1a9dca8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 5 additions & 1 deletion google_auth_oauthlib/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def run_local_server(
redirect_uri_trailing_slash=True,
timeout_seconds=None,
token_audience=None,
browser=None,
**kwargs
):
"""Run the flow using the server strategy.
Expand Down Expand Up @@ -416,6 +417,8 @@ def run_local_server(
token_audience (str): Passed along with the request for an access
token. Determines the endpoints with which the token can be
used. Optional.
browser (str): specify which browser to open for authentication. If not
specified this defaults to default browser.
kwargs: Additional keyword arguments passed through to
:meth:`authorization_url`.
Expand All @@ -437,7 +440,8 @@ def run_local_server(
auth_url, _ = self.authorization_url(**kwargs)

if open_browser:
webbrowser.open(auth_url, new=1, autoraise=True)
# if browser is None it defaults to default browser
webbrowser.get(browser).open(auth_url, new=1, autoraise=True)

if authorization_prompt_message:
print(authorization_prompt_message.format(url=auth_url))
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_run_local_server(self, webbrowser_mock, instance, mock_fetch_token, por
assert credentials.token == mock.sentinel.access_token
assert credentials._refresh_token == mock.sentinel.refresh_token
assert credentials.id_token == mock.sentinel.id_token
assert webbrowser_mock.open.called
assert webbrowser_mock.get().open.called
assert instance.redirect_uri == f"http://localhost:{port}/"

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

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

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

instance.run_local_server(open_browser=False)

assert not webbrowser_mock.open.called
assert not webbrowser_mock.get().open.called

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

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

Expand Down

0 comments on commit 1a9dca8

Please sign in to comment.