Skip to content

Commit be55e2b

Browse files
authored
Merge pull request #400 from AzureAD/release-1.14.0
Release 1.14.0
2 parents b82f0c0 + 24959a9 commit be55e2b

15 files changed

+897
-95
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ docs/_build/
5757
# The test configuration file(s) could potentially contain credentials
5858
tests/config.json
5959

60+
61+
.env

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Microsoft Authentication Library (MSAL) for Python
22

3-
43
| `dev` branch | Reference Docs | # of Downloads per different platforms | # of Downloads per recent MSAL versions |
54
|---------------|---------------|----------------------------------------|-----------------------------------------|
6-
[![Build status](https://api.travis-ci.org/AzureAD/microsoft-authentication-library-for-python.svg?branch=dev)](https://travis-ci.org/AzureAD/microsoft-authentication-library-for-python) | [![Documentation Status](https://readthedocs.org/projects/msal-python/badge/?version=latest)](https://msal-python.readthedocs.io/en/latest/?badge=latest) | [![Downloads](https://pepy.tech/badge/msal)](https://pypistats.org/packages/msal) | [![Download monthly](https://pepy.tech/badge/msal/month)](https://pepy.tech/project/msal)
5+
[![Build status](https://github.com/AzureAD/microsoft-authentication-library-for-python/actions/workflows/python-package.yml/badge.svg?branch=dev)](https://github.com/AzureAD/microsoft-authentication-library-for-python/actions) | [![Documentation Status](https://readthedocs.org/projects/msal-python/badge/?version=latest)](https://msal-python.readthedocs.io/en/latest/?badge=latest) | [![Downloads](https://pepy.tech/badge/msal)](https://pypistats.org/packages/msal) | [![Download monthly](https://pepy.tech/badge/msal/month)](https://pepy.tech/project/msal)
76

87
The Microsoft Authentication Library for Python enables applications to integrate with the [Microsoft identity platform](https://aka.ms/aaddevv2). It allows you to sign in users or apps with Microsoft identities ([Azure AD](https://azure.microsoft.com/services/active-directory/), [Microsoft Accounts](https://account.microsoft.com) and [Azure AD B2C](https://azure.microsoft.com/services/active-directory-b2c/) accounts) and obtain tokens to call Microsoft APIs such as [Microsoft Graph](https://graph.microsoft.io/) or your own APIs registered with the Microsoft identity platform. It is built using industry standard OAuth2 and OpenID Connect protocols
98

msal/application.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import warnings
1111
from threading import Lock
12+
import os
1213

1314
import requests
1415

@@ -20,10 +21,11 @@
2021
from .token_cache import TokenCache
2122
import msal.telemetry
2223
from .region import _detect_region
24+
from .throttled_http_client import ThrottledHttpClient
2325

2426

2527
# The __init__.py will import this. Not the other way around.
26-
__version__ = "1.13.0"
28+
__version__ = "1.14.0"
2729

2830
logger = logging.getLogger(__name__)
2931

@@ -69,6 +71,46 @@ def _clean_up(result):
6971
return result
7072

7173

74+
def _preferred_browser():
75+
"""Register Edge and return a name suitable for subsequent webbrowser.get(...)
76+
when appropriate. Otherwise return None.
77+
"""
78+
# On Linux, only Edge will provide device-based Conditional Access support
79+
if sys.platform != "linux": # On other platforms, we have no browser preference
80+
return None
81+
browser_path = "/usr/bin/microsoft-edge" # Use a full path owned by sys admin
82+
user_has_no_preference = "BROWSER" not in os.environ
83+
user_wont_mind_edge = "microsoft-edge" in os.environ.get("BROWSER", "") # Note:
84+
# BROWSER could contain "microsoft-edge" or "/path/to/microsoft-edge".
85+
# Python documentation (https://docs.python.org/3/library/webbrowser.html)
86+
# does not document the name being implicitly register,
87+
# so there is no public API to know whether the ENV VAR browser would work.
88+
# Therefore, we would not bother examine the env var browser's type.
89+
# We would just register our own Edge instance.
90+
if (user_has_no_preference or user_wont_mind_edge) and os.path.exists(browser_path):
91+
try:
92+
import webbrowser # Lazy import. Some distro may not have this.
93+
browser_name = "msal-edge" # Avoid popular name "microsoft-edge"
94+
# otherwise `BROWSER="microsoft-edge"; webbrowser.get("microsoft-edge")`
95+
# would return a GenericBrowser instance which won't work.
96+
try:
97+
registration_available = isinstance(
98+
webbrowser.get(browser_name), webbrowser.BackgroundBrowser)
99+
except webbrowser.Error:
100+
registration_available = False
101+
if not registration_available:
102+
logger.debug("Register %s with %s", browser_name, browser_path)
103+
# By registering our own browser instance with our own name,
104+
# rather than populating a process-wide BROWSER enn var,
105+
# this approach does not have side effect on non-MSAL code path.
106+
webbrowser.register( # Even double-register happens to work fine
107+
browser_name, None, webbrowser.BackgroundBrowser(browser_path))
108+
return browser_name
109+
except ImportError:
110+
pass # We may still proceed
111+
return None
112+
113+
72114
class ClientApplication(object):
73115

74116
ACQUIRE_TOKEN_SILENT_ID = "84"
@@ -295,6 +337,10 @@ def __init__(
295337
a = requests.adapters.HTTPAdapter(max_retries=1)
296338
self.http_client.mount("http://", a)
297339
self.http_client.mount("https://", a)
340+
self.http_client = ThrottledHttpClient(
341+
self.http_client,
342+
{} # Hard code an in-memory cache, for now
343+
)
298344

299345
self.app_name = app_name
300346
self.app_version = app_version
@@ -371,7 +417,7 @@ def _get_regional_authority(self, central_authority):
371417
self._region_configured if is_region_specified else self._region_detected)
372418
if region_to_use:
373419
logger.info('Region to be used: {}'.format(repr(region_to_use)))
374-
regional_host = ("{}.login.microsoft.com".format(region_to_use)
420+
regional_host = ("{}.r.login.microsoftonline.com".format(region_to_use)
375421
if central_authority.instance in (
376422
# The list came from https://github.com/AzureAD/microsoft-authentication-library-for-python/pull/358/files#r629400328
377423
"login.microsoftonline.com",
@@ -392,6 +438,7 @@ def _build_client(self, client_credential, authority, skip_regional_client=False
392438
"x-client-sku": "MSAL.Python", "x-client-ver": __version__,
393439
"x-client-os": sys.platform,
394440
"x-client-cpu": "x64" if sys.maxsize > 2 ** 32 else "x86",
441+
"x-ms-lib-capability": "retry-after, h429",
395442
}
396443
if self.app_name:
397444
default_headers['x-app-name'] = self.app_name
@@ -1393,6 +1440,7 @@ def acquire_token_interactive(
13931440
},
13941441
data=dict(kwargs.pop("data", {}), claims=claims),
13951442
headers=telemetry_context.generate_headers(),
1443+
browser_name=_preferred_browser(),
13961444
**kwargs))
13971445
telemetry_context.update_telemetry(response)
13981446
return response

0 commit comments

Comments
 (0)