Skip to content

Commit 17a9a87

Browse files
authored
Restore 1.5.0b2 user authentication API (Azure#15296)
1 parent 8e8324a commit 17a9a87

36 files changed

+248
-114
lines changed

sdk/identity/azure-identity/azure/identity/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# ------------------------------------
55
"""Credentials for Azure SDK clients."""
66

7-
from ._exceptions import CredentialUnavailableError
7+
from ._auth_record import AuthenticationRecord
8+
from ._exceptions import AuthenticationRequiredError, CredentialUnavailableError
89
from ._constants import AzureAuthorityHosts, KnownAuthorities
910
from ._credentials import (
1011
AzureCliCredential,
@@ -24,6 +25,8 @@
2425

2526

2627
__all__ = [
28+
"AuthenticationRecord",
29+
"AuthenticationRequiredError",
2730
"AuthorizationCodeCredential",
2831
"AzureAuthorityHosts",
2932
"AzureCliCredential",

sdk/identity/azure-identity/azure/identity/_credentials/app_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _request_token(self, *scopes, **kwargs):
4848

4949
def _get_client_args(**kwargs):
5050
# type: (dict) -> Optional[dict]
51-
identity_config = kwargs.pop("_identity_config", None) or {}
51+
identity_config = kwargs.pop("identity_config", None) or {}
5252

5353
url = os.environ.get(EnvironmentVariables.MSI_ENDPOINT)
5454
secret = os.environ.get(EnvironmentVariables.MSI_SECRET)

sdk/identity/azure-identity/azure/identity/_credentials/browser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class InteractiveBrowserCredential(InteractiveCredential):
4040
Active Directory, for example "http://localhost:8400". This is only required when passing a value for
4141
`client_id`, and must match a redirect URI in the application's registration. The credential must be able to
4242
bind a socket to this URI.
43+
:keyword AuthenticationRecord authentication_record: :class:`AuthenticationRecord` returned by :func:`authenticate`
44+
:keyword bool disable_automatic_authentication: if True, :func:`get_token` will raise
45+
:class:`AuthenticationRequiredError` when user interaction is required to acquire a token. Defaults to False.
46+
:keyword bool enable_persistent_cache: if True, the credential will store tokens in a persistent cache shared by
47+
other user credentials. Defaults to False.
48+
:keyword bool allow_unencrypted_cache: if True, the credential will fall back to a plaintext cache on platforms
49+
where encryption is unavailable. Default to False. Has no effect when `enable_persistent_cache` is False.
4350
:keyword int timeout: seconds to wait for the user to complete authentication. Defaults to 300 (5 minutes).
4451
"""
4552

sdk/identity/azure-identity/azure/identity/_credentials/certificate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class CertificateCredential(ClientCredentialBase):
3333
:keyword bool send_certificate_chain: if True, the credential will send the public certificate chain in the x5c
3434
header of each token request's JWT. This is required for Subject Name/Issuer (SNI) authentication. Defaults
3535
to False.
36+
:keyword bool enable_persistent_cache: if True, the credential will store tokens in a persistent cache. Defaults to
37+
False.
38+
:keyword bool allow_unencrypted_cache: if True, the credential will fall back to a plaintext cache when encryption
39+
is unavailable. Default to False. Has no effect when `enable_persistent_cache` is False.
3640
"""
3741

3842
def __init__(self, tenant_id, client_id, certificate_path, **kwargs):

sdk/identity/azure-identity/azure/identity/_credentials/client_secret.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class ClientSecretCredential(ClientCredentialBase):
2121
:keyword str authority: Authority of an Azure Active Directory endpoint, for example 'login.microsoftonline.com',
2222
the authority for Azure Public Cloud (which is the default). :class:`~azure.identity.AzureAuthorityHosts`
2323
defines authorities for other clouds.
24+
:keyword bool enable_persistent_cache: if True, the credential will store tokens in a persistent cache. Defaults to
25+
False.
26+
:keyword bool allow_unencrypted_cache: if True, the credential will fall back to a plaintext cache when encryption
27+
is unavailable. Default to False. Has no effect when `enable_persistent_cache` is False.
2428
"""
2529

2630
def __init__(self, tenant_id, client_id, client_secret, **kwargs):

sdk/identity/azure-identity/azure/identity/_credentials/device_code.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class DeviceCodeCredential(InteractiveCredential):
4848
- ``expires_on`` (datetime.datetime) the UTC time at which the code will expire
4949
If this argument isn't provided, the credential will print instructions to stdout.
5050
:paramtype prompt_callback: Callable[str, str, ~datetime.datetime]
51+
:keyword AuthenticationRecord authentication_record: :class:`AuthenticationRecord` returned by :func:`authenticate`
52+
:keyword bool disable_automatic_authentication: if True, :func:`get_token` will raise
53+
:class:`AuthenticationRequiredError` when user interaction is required to acquire a token. Defaults to False.
54+
:keyword bool enable_persistent_cache: if True, the credential will store tokens in a persistent cache shared by
55+
other user credentials. Defaults to False.
56+
:keyword bool allow_unencrypted_cache: if True, the credential will fall back to a plaintext cache on platforms
57+
where encryption is unavailable. Default to False. Has no effect when `enable_persistent_cache` is False.
5158
"""
5259

5360
def __init__(self, client_id=DEVELOPER_SIGN_ON_CLIENT_ID, **kwargs):

sdk/identity/azure-identity/azure/identity/_credentials/managed_identity.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class ManagedIdentityCredential(object):
4444
the keyword arguments.
4545
4646
:keyword str client_id: a user-assigned identity's client ID. This is supported in all hosting environments.
47+
:keyword identity_config: a mapping ``{parameter_name: value}`` specifying a user-assigned identity by its object
48+
or resource ID, for example ``{"object_id": "..."}``. Check the documentation for your hosting environment to
49+
learn what values it expects.
50+
:paramtype identity_config: Mapping[str, str]
4751
"""
4852

4953
def __init__(self, **kwargs):
@@ -96,7 +100,7 @@ def get_token(self, *scopes, **kwargs):
96100
class _ManagedIdentityBase(object):
97101
def __init__(self, endpoint, client_cls, config=None, client_id=None, **kwargs):
98102
# type: (str, Type, Optional[Configuration], Optional[str], **Any) -> None
99-
self._identity_config = kwargs.pop("_identity_config", None) or {}
103+
self._identity_config = kwargs.pop("identity_config", None) or {}
100104
if client_id:
101105
if os.environ.get(EnvironmentVariables.MSI_ENDPOINT) and os.environ.get(EnvironmentVariables.MSI_SECRET):
102106
# App Service: version 2017-09-1 accepts client ID as parameter "clientid"

sdk/identity/azure-identity/azure/identity/_credentials/shared_cache.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
if TYPE_CHECKING:
2525
# pylint:disable=unused-import,ungrouped-imports
2626
from typing import Any, Optional
27-
from .._auth_record import AuthenticationRecord
27+
from .. import AuthenticationRecord
2828
from .._internal import AadClientBase
2929

3030

@@ -40,12 +40,16 @@ class SharedTokenCacheCredential(SharedTokenCacheBase):
4040
defines authorities for other clouds.
4141
:keyword str tenant_id: an Azure Active Directory tenant ID. Used to select an account when the cache contains
4242
tokens for multiple identities.
43+
:keyword AuthenticationRecord authentication_record: an authentication record returned by a user credential such as
44+
:class:`DeviceCodeCredential` or :class:`InteractiveBrowserCredential`
45+
:keyword bool allow_unencrypted_cache: if True, the credential will fall back to a plaintext cache when encryption
46+
is unavailable. Defaults to False.
4347
"""
4448

4549
def __init__(self, username=None, **kwargs):
4650
# type: (Optional[str], **Any) -> None
4751

48-
self._auth_record = kwargs.pop("_authentication_record", None) # type: Optional[AuthenticationRecord]
52+
self._auth_record = kwargs.pop("authentication_record", None) # type: Optional[AuthenticationRecord]
4953
if self._auth_record:
5054
# authenticate in the tenant that produced the record unless "tenant_id" specifies another
5155
self._tenant_id = kwargs.pop("tenant_id", None) or self._auth_record.tenant_id

sdk/identity/azure-identity/azure/identity/_credentials/user_password.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class UsernamePasswordCredential(InteractiveCredential):
3333
defines authorities for other clouds.
3434
:keyword str tenant_id: tenant ID or a domain associated with a tenant. If not provided, defaults to the
3535
'organizations' tenant, which supports only Azure Active Directory work or school accounts.
36+
:keyword bool enable_persistent_cache: if True, the credential will store tokens in a persistent cache shared by
37+
other user credentials. Defaults to False.
38+
:keyword bool allow_unencrypted_cache: if True, the credential will fall back to a plaintext cache on platforms
39+
where encryption is unavailable. Default to False. Has no effect when `enable_persistent_cache` is False.
3640
"""
3741

3842
def __init__(self, client_id, username, password, **kwargs):
@@ -42,7 +46,7 @@ def __init__(self, client_id, username, password, **kwargs):
4246
# first time it's asked for a token. However, we want to ensure this first authentication is not silent, to
4347
# validate the given password. This class therefore doesn't document the authentication_record argument, and we
4448
# discard it here.
45-
kwargs.pop("_authentication_record", None)
49+
kwargs.pop("authentication_record", None)
4650
super(UsernamePasswordCredential, self).__init__(client_id=client_id, **kwargs)
4751
self._username = username
4852
self._password = password

sdk/identity/azure-identity/azure/identity/_internal/certificate_credential_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def __init__(self, tenant_id, client_id, certificate_path, **kwargs):
4646

4747
self._certificate = AadClientCertificate(pem_bytes, password=password)
4848

49-
_enable_persistent_cache = kwargs.pop("_enable_persistent_cache", False)
50-
if _enable_persistent_cache:
51-
allow_unencrypted = kwargs.pop("_allow_unencrypted_cache", False)
49+
enable_persistent_cache = kwargs.pop("enable_persistent_cache", False)
50+
if enable_persistent_cache:
51+
allow_unencrypted = kwargs.pop("allow_unencrypted_cache", False)
5252
cache = load_service_principal_cache(allow_unencrypted)
5353
else:
5454
cache = TokenCache()

0 commit comments

Comments
 (0)