Skip to content

feat: Adding support to specify browser while launching browser to authention #305

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 4 commits into from
Sep 7, 2023
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this parameter to the doc string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clundin25 I have added the doc string

**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