|
28 | 28 |
|
29 | 29 | logger = logging.getLogger(__name__) |
30 | 30 |
|
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 | | - |
47 | 31 |
|
48 | 32 | def extract_certs(public_cert_content): |
49 | 33 | # Parses raw public certificate file contents and returns a list of strings |
@@ -86,6 +70,25 @@ def _clean_up(result): |
86 | 70 | return result |
87 | 71 |
|
88 | 72 |
|
| 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 | + |
89 | 92 | class ClientApplication(object): |
90 | 93 |
|
91 | 94 | ACQUIRE_TOKEN_SILENT_ID = "84" |
@@ -1410,6 +1413,7 @@ def acquire_token_interactive( |
1410 | 1413 | }, |
1411 | 1414 | data=dict(kwargs.pop("data", {}), claims=claims), |
1412 | 1415 | headers=telemetry_context.generate_headers(), |
| 1416 | + browser_name=_preferred_browser(), |
1413 | 1417 | **kwargs)) |
1414 | 1418 | telemetry_context.update_telemetry(response) |
1415 | 1419 | return response |
|
0 commit comments