Skip to content

Commit e94dda5

Browse files
committed
Merge branch 'o2c' into dev
2 parents ee96522 + efa5668 commit e94dda5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

msal/oauth2cli/authcode.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ def is_wsl():
4545
return platform_name == 'linux' and 'microsoft' in release
4646

4747

48-
def _browse(auth_uri): # throws ImportError, possibly webbrowser.Error in future
48+
def _browse(auth_uri, browser_name=None): # throws ImportError, webbrowser.Error
49+
"""Browse uri with named browser. Default browser is customizable by $BROWSER"""
4950
import webbrowser # Lazy import. Some distro may not have this.
50-
browser_opened = webbrowser.open(auth_uri) # Use default browser. Customizable by $BROWSER
51+
browser_opened = webbrowser.get(browser_name).open(auth_uri)
5152

5253
# In WSL which doesn't have www-browser, try launching browser with PowerShell
5354
if not browser_opened and is_wsl():
@@ -147,6 +148,7 @@ def get_port(self):
147148
def get_auth_response(self, auth_uri=None, timeout=None, state=None,
148149
welcome_template=None, success_template=None, error_template=None,
149150
auth_uri_callback=None,
151+
browser_name=None,
150152
):
151153
"""Wait and return the auth response. Raise RuntimeError when timeout.
152154
@@ -173,6 +175,12 @@ def get_auth_response(self, auth_uri=None, timeout=None, state=None,
173175
A function with the shape of lambda auth_uri: ...
174176
When a browser was unable to be launch, this function will be called,
175177
so that the app could tell user to manually visit the auth_uri.
178+
:param str browser_name:
179+
If you did
180+
``webbrowser.register("xyz", None, BackgroundBrowser("/path/to/browser"))``
181+
beforehand, you can pass in the name "xyz" to use that browser.
182+
The default value ``None`` means using default browser,
183+
which is customizable by env var $BROWSER.
176184
:return:
177185
The auth response of the first leg of Auth Code flow,
178186
typically {"code": "...", "state": "..."} or {"error": "...", ...}
@@ -190,7 +198,7 @@ def get_auth_response(self, auth_uri=None, timeout=None, state=None,
190198
logger.info("Open a browser on this device to visit: %s" % _uri)
191199
browser_opened = False
192200
try:
193-
browser_opened = _browse(_uri)
201+
browser_opened = _browse(_uri, browser_name=browser_name)
194202
except: # Had to use broad except, because the potential
195203
# webbrowser.Error is purposely undefined outside of _browse().
196204
# Absorb and proceed. Because browser could be manually run elsewhere.

msal/oauth2cli/oauth2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,10 @@ def obtain_token_by_browser(
579579
timeout=None,
580580
welcome_template=None,
581581
success_template=None,
582+
error_template=None,
582583
auth_params=None,
583584
auth_uri_callback=None,
585+
browser_name=None,
584586
**kwargs):
585587
"""A native app can use this method to obtain token via a local browser.
586588
@@ -613,6 +615,14 @@ def obtain_token_by_browser(
613615
These parameters will be sent to authorization_endpoint.
614616
615617
:param int timeout: In seconds. None means wait indefinitely.
618+
619+
:param str browser_name:
620+
If you did
621+
``webbrowser.register("xyz", None, BackgroundBrowser("/path/to/browser"))``
622+
beforehand, you can pass in the name "xyz" to use that browser.
623+
The default value ``None`` means using default browser,
624+
which is customizable by env var $BROWSER.
625+
616626
:return: Same as :func:`~obtain_token_by_auth_code_flow()`
617627
"""
618628
_redirect_uri = urlparse(redirect_uri or "http://127.0.0.1:0")
@@ -638,7 +648,9 @@ def obtain_token_by_browser(
638648
timeout=timeout,
639649
welcome_template=welcome_template,
640650
success_template=success_template,
651+
error_template=error_template,
641652
auth_uri_callback=auth_uri_callback,
653+
browser_name=browser_name,
642654
)
643655
except PermissionError:
644656
if 0 < listen_port < 1024:

0 commit comments

Comments
 (0)