Skip to content

Commit 7ffbdd3

Browse files
authored
Merge pull request #34 from AzureAD/username-password-supportability
Improve supportability for Username Password Flow
2 parents ba797e7 + c425682 commit 7ffbdd3

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

msal/application.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ def acquire_token_by_username_password(
466466
self, username, password, scopes=None, **kwargs):
467467
"""Gets a token for a given resource via user credentails.
468468
469+
See this page for constraints of Username Password Flow.
470+
https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication
471+
469472
:param str username: Typically a UPN in the form of an email address.
470473
:param str password: The password.
471474
:param list[str] scopes:
@@ -494,6 +497,11 @@ def _acquire_token_by_username_password_federated(
494497
wstrust_endpoint = mex_send_request(
495498
user_realm_result["federation_metadata_url"],
496499
verify=verify, proxies=proxies)
500+
if wstrust_endpoint is None:
501+
raise ValueError("Unable to find wstrust endpoint from MEX. "
502+
"This typically happens when attempting MSA accounts. "
503+
"More details available here. "
504+
"https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication")
497505
logger.debug("wstrust_endpoint = %s", wstrust_endpoint)
498506
wstrust_result = wst_send_request(
499507
username, password, user_realm_result.get("cloud_audience_urn"),

msal/wstrust_request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def send_request(
4747
soap_action = Mex.ACTION_2005
4848
elif '/trust/13/usernamemixed' in endpoint_address:
4949
soap_action = Mex.ACTION_13
50-
assert soap_action in (Mex.ACTION_13, Mex.ACTION_2005) # A loose check here
50+
assert soap_action in (Mex.ACTION_13, Mex.ACTION_2005), ( # A loose check here
51+
"Unsupported soap action: %s" % soap_action)
5152
data = _build_rst(
5253
username, password, cloud_audience_urn, endpoint_address, soap_action)
5354
resp = requests.post(endpoint_address, data=data, headers={

sample/username_password_sample.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
if not result:
4747
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
48+
# See this page for constraints of Username Password Flow.
49+
# https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication
4850
result = app.acquire_token_by_username_password(
4951
config["username"], config["password"], scopes=config["scope"])
5052

0 commit comments

Comments
 (0)