Skip to content

Commit 80127c8

Browse files
committed
ClientSecretCredential uses AadClient
1 parent 248731d commit 80127c8

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
from .._authn_client import AuthnClient
6-
from .._base import ClientSecretCredentialBase
5+
from .._internal import AadClient, ClientSecretCredentialBase
76

87
try:
98
from typing import TYPE_CHECKING
@@ -28,12 +27,7 @@ class ClientSecretCredential(ClientSecretCredentialBase):
2827
defines authorities for other clouds.
2928
"""
3029

31-
def __init__(self, tenant_id, client_id, client_secret, **kwargs):
32-
# type: (str, str, str, **Any) -> None
33-
super(ClientSecretCredential, self).__init__(tenant_id, client_id, client_secret, **kwargs)
34-
self._client = AuthnClient(tenant=tenant_id, **kwargs)
35-
36-
def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
30+
def get_token(self, *scopes, **kwargs):
3731
# type: (*str, **Any) -> AccessToken
3832
"""Request an access token for `scopes`.
3933
@@ -48,8 +42,10 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
4842
if not scopes:
4943
raise ValueError("'get_token' requires at least one scope")
5044

51-
token = self._client.get_cached_token(scopes)
45+
token = self._client.get_cached_access_token(scopes)
5246
if not token:
53-
data = dict(self._form_data, scope=" ".join(scopes))
54-
token = self._client.request_token(scopes, form_data=data)
47+
token = self._client.obtain_token_by_client_secret(scopes, self._secret, **kwargs)
5548
return token
49+
50+
def _get_auth_client(self, tenant_id, client_id, **kwargs):
51+
return AadClient(tenant_id, client_id, **kwargs)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def get_default_authority():
3535
from .auth_code_redirect_handler import AuthCodeRedirectServer
3636
from .aadclient_certificate import AadClientCertificate
3737
from .certificate_credential_base import CertificateCredentialBase
38+
from .client_secret_credential_base import ClientSecretCredentialBase
3839
from .exception_wrapper import wrap_exceptions
3940
from .msal_credentials import ConfidentialClientCredential, InteractiveCredential, PublicClientCredential
4041
from .msal_transport_adapter import MsalTransportAdapter, MsalTransportResponse
@@ -60,6 +61,7 @@ def _scopes_to_resource(*scopes):
6061
"AuthCodeRedirectServer",
6162
"AadClientCertificate",
6263
"CertificateCredentialBase",
64+
"ClientSecretCredentialBase",
6365
"ConfidentialClientCredential",
6466
"get_default_authority",
6567
"InteractiveCredential",

sdk/identity/azure-identity/azure/identity/_base.py renamed to sdk/identity/azure-identity/azure/identity/_internal/client_secret_credential_base.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,33 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55
import abc
6+
from typing import TYPE_CHECKING
67

78
try:
89
ABC = abc.ABC
9-
except AttributeError: # Python 2.7, abc exists, but not ABC
10+
except AttributeError: # Python 2.7
1011
ABC = abc.ABCMeta("ABC", (object,), {"__slots__": ()}) # type: ignore
1112

12-
try:
13-
from typing import TYPE_CHECKING
14-
except ImportError:
15-
TYPE_CHECKING = False
16-
1713
if TYPE_CHECKING:
18-
# pylint:disable=unused-import
19-
from typing import Any, Optional, Union
20-
14+
# pylint:disable=unused-import,ungrouped-imports
15+
from typing import Any
2116

22-
class ClientSecretCredentialBase(object):
23-
"""Sans I/O base for client secret credentials"""
2417

25-
def __init__(self, tenant_id, client_id, secret, **kwargs): # pylint:disable=unused-argument
18+
class ClientSecretCredentialBase(ABC):
19+
def __init__(self, tenant_id, client_id, client_secret, **kwargs):
2620
# type: (str, str, str, **Any) -> None
2721
if not client_id:
2822
raise ValueError("client_id should be the id of an Azure Active Directory application")
29-
if not secret:
23+
if not client_secret:
3024
raise ValueError("secret should be an Azure Active Directory application's client secret")
3125
if not tenant_id:
3226
raise ValueError(
3327
"tenant_id should be an Azure Active Directory tenant's id (also called its 'directory id')"
3428
)
35-
self._form_data = {"client_id": client_id, "client_secret": secret, "grant_type": "client_credentials"}
36-
super(ClientSecretCredentialBase, self).__init__()
29+
30+
self._client = self._get_auth_client(tenant_id, client_id, **kwargs)
31+
self._secret = client_secret
32+
33+
@abc.abstractmethod
34+
def _get_auth_client(self, tenant_id, client_id, **kwargs):
35+
pass

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from typing import TYPE_CHECKING
66

77
from .base import AsyncCredentialBase
8-
from .._authn_client import AsyncAuthnClient
9-
from ..._base import ClientSecretCredentialBase
8+
from .._internal import AadClient
9+
from ..._internal import ClientSecretCredentialBase
1010

1111
if TYPE_CHECKING:
1212
from typing import Any
1313
from azure.core.credentials import AccessToken
1414

1515

16-
class ClientSecretCredential(ClientSecretCredentialBase, AsyncCredentialBase):
16+
class ClientSecretCredential(AsyncCredentialBase, ClientSecretCredentialBase):
1717
"""Authenticates as a service principal using a client ID and client secret.
1818
1919
:param str tenant_id: ID of the service principal's tenant. Also called its 'directory' ID.
@@ -25,10 +25,6 @@ class ClientSecretCredential(ClientSecretCredentialBase, AsyncCredentialBase):
2525
defines authorities for other clouds.
2626
"""
2727

28-
def __init__(self, tenant_id: str, client_id: str, client_secret: str, **kwargs: "Any") -> None:
29-
super(ClientSecretCredential, self).__init__(tenant_id, client_id, client_secret, **kwargs)
30-
self._client = AsyncAuthnClient(tenant=tenant_id, **kwargs)
31-
3228
async def __aenter__(self):
3329
await self._client.__aenter__()
3430
return self
@@ -38,7 +34,7 @@ async def close(self):
3834

3935
await self._client.__aexit__()
4036

41-
async def get_token(self, *scopes: str, **kwargs: "Any") -> "AccessToken": # pylint:disable=unused-argument
37+
async def get_token(self, *scopes: str, **kwargs: "Any") -> "AccessToken":
4238
"""Asynchronously request an access token for `scopes`.
4339
4440
.. note:: This method is called by Azure SDK clients. It isn't intended for use in application code.
@@ -52,8 +48,10 @@ async def get_token(self, *scopes: str, **kwargs: "Any") -> "AccessToken": # py
5248
if not scopes:
5349
raise ValueError("'get_token' requires at least one scope")
5450

55-
token = self._client.get_cached_token(scopes)
51+
token = self._client.get_cached_access_token(scopes)
5652
if not token:
57-
data = dict(self._form_data, scope=" ".join(scopes))
58-
token = await self._client.request_token(scopes, form_data=data)
59-
return token # type: ignore
53+
token = await self._client.obtain_token_by_client_secret(scopes, self._secret, **kwargs)
54+
return token
55+
56+
def _get_auth_client(self, tenant_id, client_id, **kwargs):
57+
return AadClient(tenant_id, client_id, **kwargs)

0 commit comments

Comments
 (0)