Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions msal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ def acquire_token_by_username_password(
self, username, password, scopes=None, **kwargs):
"""Gets a token for a given resource via user credentails.

See this page for constraints of Username Password Flow.
https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication

:param str username: Typically a UPN in the form of an email address.
:param str password: The password.
:param list[str] scopes:
Expand Down Expand Up @@ -494,6 +497,11 @@ def _acquire_token_by_username_password_federated(
wstrust_endpoint = mex_send_request(
user_realm_result["federation_metadata_url"],
verify=verify, proxies=proxies)
if wstrust_endpoint is None:
raise ValueError("Unable to find wstrust endpoint from MEX. "
"This typically happens when attempting MSA accounts. "
"More details available here. "
"https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication")
logger.debug("wstrust_endpoint = %s", wstrust_endpoint)
wstrust_result = wst_send_request(
username, password, user_realm_result.get("cloud_audience_urn"),
Expand Down
3 changes: 2 additions & 1 deletion msal/wstrust_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def send_request(
soap_action = Mex.ACTION_2005
elif '/trust/13/usernamemixed' in endpoint_address:
soap_action = Mex.ACTION_13
assert soap_action in (Mex.ACTION_13, Mex.ACTION_2005) # A loose check here
assert soap_action in (Mex.ACTION_13, Mex.ACTION_2005), ( # A loose check here
"Unsupported soap action: %s" % soap_action)
data = _build_rst(
username, password, cloud_audience_urn, endpoint_address, soap_action)
resp = requests.post(endpoint_address, data=data, headers={
Expand Down
2 changes: 2 additions & 0 deletions sample/username_password_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

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

Expand Down