diff --git a/sdk/identity/azure-identity/HISTORY.md b/sdk/identity/azure-identity/HISTORY.md index 35122e3b983a..6d99a655d604 100644 --- a/sdk/identity/azure-identity/HISTORY.md +++ b/sdk/identity/azure-identity/HISTORY.md @@ -1,6 +1,6 @@ # Release History -## 1.1.1 (Unreleased) +## 1.2.0 (2020-01-14) - All credential pipelines include `ProxyPolicy` ([#8945](https://github.com/Azure/azure-sdk-for-python/pull/8945)) diff --git a/sdk/identity/azure-identity/azure/identity/_version.py b/sdk/identity/azure-identity/azure/identity/_version.py index 8307d096a03f..87cd853a4227 100644 --- a/sdk/identity/azure-identity/azure/identity/_version.py +++ b/sdk/identity/azure-identity/azure/identity/_version.py @@ -2,4 +2,4 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------ -VERSION = "1.1.1" +VERSION = "1.2.0" diff --git a/sdk/identity/azure-identity/azure/identity/aio/_credentials/authorization_code.py b/sdk/identity/azure-identity/azure/identity/aio/_credentials/authorization_code.py index 5948baa552bd..a09e0988828f 100644 --- a/sdk/identity/azure-identity/azure/identity/aio/_credentials/authorization_code.py +++ b/sdk/identity/azure-identity/azure/identity/aio/_credentials/authorization_code.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING from azure.core.exceptions import ClientAuthenticationError +from .base import AsyncCredentialBase from .._internal import AadClient if TYPE_CHECKING: @@ -14,7 +15,7 @@ from azure.core.credentials import AccessToken -class AuthorizationCodeCredential(object): +class AuthorizationCodeCredential(AsyncCredentialBase): """Authenticates by redeeming an authorization code previously obtained from Azure Active Directory. See https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow for more information @@ -31,13 +32,19 @@ class AuthorizationCodeCredential(object): :keyword str client_secret: One of the application's client secrets. Required only for web apps and web APIs. """ + async def __aenter__(self): + if self._client: + await self._client.__aenter__() + return self + + async def close(self): + """Close the credential's transport session.""" + + if self._client: + await self._client.__aexit__() + def __init__( - self, - tenant_id: str, - client_id: str, - authorization_code: str, - redirect_uri: str, - **kwargs: "Any" + self, tenant_id: str, client_id: str, authorization_code: str, redirect_uri: str, **kwargs: "Any" ) -> None: self._authorization_code = authorization_code # type: Optional[str] self._client_id = client_id diff --git a/sdk/identity/azure-identity/tests/test_auth_code_async.py b/sdk/identity/azure-identity/tests/test_auth_code_async.py index 1f1d9719169c..ae86e8373d34 100644 --- a/sdk/identity/azure-identity/tests/test_auth_code_async.py +++ b/sdk/identity/azure-identity/tests/test_auth_code_async.py @@ -13,7 +13,7 @@ import pytest from helpers import build_aad_response, mock_response, Request -from helpers_async import async_validating_transport, wrap_in_future +from helpers_async import async_validating_transport, AsyncMockTransport, wrap_in_future @pytest.mark.asyncio @@ -32,6 +32,32 @@ async def send(*_, **__): assert policy.on_request.called +@pytest.mark.asyncio +async def test_close(): + transport = AsyncMockTransport() + credential = AuthorizationCodeCredential( + "tenant-id", "client-id", "auth-code", "http://localhost", transport=transport + ) + + await credential.close() + + assert transport.__aexit__.call_count == 1 + + +@pytest.mark.asyncio +async def test_context_manager(): + transport = AsyncMockTransport() + credential = AuthorizationCodeCredential( + "tenant-id", "client-id", "auth-code", "http://localhost", transport=transport + ) + + async with credential: + assert transport.__aenter__.call_count == 1 + + assert transport.__aenter__.call_count == 1 + assert transport.__aexit__.call_count == 1 + + @pytest.mark.asyncio async def test_user_agent(): transport = async_validating_transport(