Skip to content

Commit 449c250

Browse files
committed
Switch to less intrusive register(browser_name...)
1 parent 7e322d3 commit 449c250

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

msal/application.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828

2929
logger = logging.getLogger(__name__)
3030

31-
if True: # Prefer launching Edge when running on Linux to support device-based CA.
32-
# This section only works when being run before webbrowser.open().
33-
# We could theoretically put it inside acquire_token_interactive(),
34-
# but that won't work for some of our major downstream applications
35-
# because they happen to invoke webbrowser.open() before calling MSAL.
36-
# So, we choose to add this logic at MSAL's module level.
37-
if ("BROWSER" not in os.environ # Customize it when end user has no preference
38-
and sys.platform == "linux"): # On Linux, only Edge will have CA support
39-
os.environ["BROWSER"] = ( # This is the executable file name
40-
# Hard-coding a well-known location can avoid unwittingly invoking
41-
# a potentially malicious "microsoft-edge" in current working directory.
42-
"/usr/bin/microsoft-edge")
43-
# Unavailable browser will be silently ignored and fall back to the default
44-
# More details at https://docs.python.org/3/library/webbrowser.html
45-
logger.debug("Prefer %s as browser for sign-in.", os.environ.get("BROWSER"))
46-
4731

4832
def extract_certs(public_cert_content):
4933
# Parses raw public certificate file contents and returns a list of strings
@@ -86,6 +70,25 @@ def _clean_up(result):
8670
return result
8771

8872

73+
def _preferred_browser():
74+
"""Register Edge and return a name suitable for webbrowser.get(...),
75+
if running on Linux and there is no BROWSER env var, otherwise return None.
76+
"""
77+
browser_path = "/usr/bin/microsoft-edge" # Use a full path owned by sys admin
78+
browser_name = "microsoft-edge" # Use a generic meaningful name
79+
if ("BROWSER" not in os.environ # Customize it when end user has no preference
80+
and sys.platform == "linux" # On Linux, only Edge will have CA support
81+
and os.path.exists(browser_path)): # Edge is usually installed here
82+
try:
83+
import webbrowser # Lazy import. Some distro may not have this.
84+
webbrowser.register(
85+
browser_name, None, webbrowser.BackgroundBrowser(browser_path))
86+
return browser_name
87+
except ImportError:
88+
pass # We may still proceed
89+
return None
90+
91+
8992
class ClientApplication(object):
9093

9194
ACQUIRE_TOKEN_SILENT_ID = "84"
@@ -1410,6 +1413,7 @@ def acquire_token_interactive(
14101413
},
14111414
data=dict(kwargs.pop("data", {}), claims=claims),
14121415
headers=telemetry_context.generate_headers(),
1416+
browser_name=_preferred_browser(),
14131417
**kwargs))
14141418
telemetry_context.update_telemetry(response)
14151419
return response

0 commit comments

Comments
 (0)