-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add user authentication API to UsernamePasswordCredential #11528
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,14 @@ | |
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
from azure.core.exceptions import ClientAuthenticationError | ||
from azure.core.pipeline.policies import SansIOHTTPPolicy | ||
from azure.identity import UsernamePasswordCredential | ||
from azure.identity._internal.user_agent import USER_AGENT | ||
import pytest | ||
|
||
from helpers import ( | ||
build_aad_response, | ||
build_id_token, | ||
get_discovery_response, | ||
mock_response, | ||
Request, | ||
|
@@ -26,7 +26,7 @@ def test_no_scopes(): | |
"""The credential should raise when get_token is called with no scopes""" | ||
|
||
credential = UsernamePasswordCredential("client-id", "username", "password") | ||
with pytest.raises(ClientAuthenticationError): | ||
with pytest.raises(ValueError): | ||
xiangyan99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
credential.get_token() | ||
|
||
|
||
|
@@ -88,13 +88,50 @@ def test_username_password_credential(): | |
assert token.token == expected_token | ||
|
||
|
||
def test_cache_persistence(): | ||
"""The credential should cache only in memory""" | ||
def test_authenticate(): | ||
client_id = "client-id" | ||
environment = "localhost" | ||
issuer = "https://" + environment | ||
tenant_id = "some-tenant" | ||
authority = issuer + "/" + tenant_id | ||
|
||
expected_cache = Mock() | ||
raise_when_called = Mock(side_effect=Exception("credential shouldn't attempt to load a persistent cache")) | ||
with patch.multiple("msal_extensions.token_cache", WindowsTokenCache=raise_when_called): | ||
with patch("msal.TokenCache", Mock(return_value=expected_cache)): | ||
credential = UsernamePasswordCredential("...", "...", "...") | ||
access_token = "***" | ||
scope = "scope" | ||
|
||
assert credential._cache is expected_cache | ||
# mock AAD response with id token | ||
object_id = "object-id" | ||
home_tenant = "home-tenant-id" | ||
username = "[email protected]" | ||
id_token = build_id_token(aud=client_id, iss=issuer, object_id=object_id, tenant_id=home_tenant, username=username) | ||
auth_response = build_aad_response( | ||
uid=object_id, utid=home_tenant, access_token=access_token, refresh_token="**", id_token=id_token | ||
) | ||
|
||
transport = validating_transport( | ||
requests=[Request(url_substring=issuer)] * 4, | ||
responses=[ | ||
get_discovery_response(authority), # instance discovery | ||
get_discovery_response(authority), # tenant discovery | ||
mock_response(status_code=404), # user realm discovery | ||
mock_response(json_payload=auth_response), # token request following authenticate() | ||
], | ||
) | ||
|
||
credential = UsernamePasswordCredential( | ||
username=username, | ||
password="1234", | ||
authority=environment, | ||
client_id=client_id, | ||
tenant_id=tenant_id, | ||
transport=transport, | ||
) | ||
record = credential.authenticate(scopes=(scope,)) | ||
for auth_record in (record, credential.authentication_record): | ||
assert auth_record.authority == environment | ||
assert auth_record.home_account_id == object_id + "." + home_tenant | ||
assert auth_record.tenant_id == home_tenant | ||
assert auth_record.username == username | ||
|
||
# credential should have a cached access token for the scope passed to authenticate | ||
token = credential.get_token(scope) | ||
assert token.token == access_token |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In old code, I see
for account in accounts:
result = app.acquire_token_silent(scopes, account=account)
if result:
break
It seems to me we allow first time silent authentication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may appear so but that was actually impossible because the cache was in memory; the credential always started with an empty one.