Skip to content

Commit 66efa24

Browse files
authored
Update Authorization Code client_secret kwarg usage (#34862)
* fix client_secret - use from get_token method if present - use from init if not in get_token * consume client_secret kwarg * fix lint errors. * test client secret. * remove client_secret from get_token - client_secret should be utilized from within the constructor instead of the method call * add client_secret to get_token method. * lint
1 parent efad456 commit 66efa24

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def get_token(
8686
``response`` attribute.
8787
"""
8888
# pylint:disable=useless-super-delegation
89-
return super(AuthorizationCodeCredential, self).get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
89+
return super(AuthorizationCodeCredential, self).get_token(
90+
*scopes, claims=claims, tenant_id=tenant_id, client_secret=self._client_secret, **kwargs
91+
)
9092

9193
def _acquire_token_silently(self, *scopes: str, **kwargs) -> Optional[AccessToken]:
9294
return self._client.get_cached_access_token(scopes, **kwargs)

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

+3
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ def _get_refresh_token_request(self, scopes: Iterable[str], refresh_token: str,
302302
"client_id": self._client_id,
303303
"client_info": 1, # request Microsoft Entra ID include home_account_id in its response
304304
}
305+
client_secret = kwargs.pop("client_secret", None)
306+
if client_secret:
307+
data["client_secret"] = client_secret
305308

306309
claims = _merge_claims_challenge_and_capabilities(
307310
["CP1"] if kwargs.get("enable_cae") else [], kwargs.get("claims")

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ async def get_token(
9292
attribute gives a reason. Any error response from Microsoft Entra ID is available as the error's
9393
``response`` attribute.
9494
"""
95-
return await super().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
95+
return await super(AuthorizationCodeCredential, self).get_token(
96+
*scopes, claims=claims, tenant_id=tenant_id, client_secret=self._client_secret, **kwargs
97+
)
9698

9799
async def _acquire_token_silently(self, *scopes: str, **kwargs: Any) -> Optional[AccessToken]:
98100
return self._client.get_cached_access_token(scopes, **kwargs)

sdk/identity/azure-identity/tests/test_auth_code.py

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def test_tenant_id():
7878

7979
def test_auth_code_credential():
8080
client_id = "client id"
81+
secret = "fake-client-secret"
8182
tenant_id = "tenant"
8283
expected_code = "auth code"
8384
redirect_uri = "https://localhost"
@@ -92,6 +93,7 @@ def test_auth_code_credential():
9293
url_substring=tenant_id,
9394
required_data={
9495
"client_id": client_id,
96+
"client_secret": secret,
9597
"code": expected_code,
9698
"grant_type": "authorization_code",
9799
"redirect_uri": redirect_uri,
@@ -102,6 +104,7 @@ def test_auth_code_credential():
102104
url_substring=tenant_id,
103105
required_data={
104106
"client_id": client_id,
107+
"client_secret": secret,
105108
"grant_type": "refresh_token",
106109
"refresh_token": expected_refresh_token,
107110
"scope": expected_scope,
@@ -114,6 +117,7 @@ def test_auth_code_credential():
114117

115118
credential = AuthorizationCodeCredential(
116119
client_id=client_id,
120+
client_secret=secret,
117121
tenant_id=tenant_id,
118122
authorization_code=expected_code,
119123
redirect_uri=redirect_uri,

sdk/identity/azure-identity/tests/test_auth_code_async.py

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ async def test_tenant_id():
102102

103103
async def test_auth_code_credential():
104104
client_id = "client id"
105+
secret = "fake-client-secret"
105106
tenant_id = "tenant"
106107
expected_code = "auth code"
107108
redirect_uri = "https://localhost"
@@ -116,6 +117,7 @@ async def test_auth_code_credential():
116117
url_substring=tenant_id,
117118
required_data={
118119
"client_id": client_id,
120+
"client_secret": secret,
119121
"code": expected_code,
120122
"grant_type": "authorization_code",
121123
"redirect_uri": redirect_uri,
@@ -126,6 +128,7 @@ async def test_auth_code_credential():
126128
url_substring=tenant_id,
127129
required_data={
128130
"client_id": client_id,
131+
"client_secret": secret,
129132
"grant_type": "refresh_token",
130133
"refresh_token": expected_refresh_token,
131134
"scope": expected_scope,
@@ -138,6 +141,7 @@ async def test_auth_code_credential():
138141

139142
credential = AuthorizationCodeCredential(
140143
client_id=client_id,
144+
client_secret=secret,
141145
tenant_id=tenant_id,
142146
authorization_code=expected_code,
143147
redirect_uri=redirect_uri,

0 commit comments

Comments
 (0)