Skip to content

Commit

Permalink
Update async credentials, docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp committed Jul 27, 2023
1 parent 28f3fe7 commit 437b2d1
Show file tree
Hide file tree
Showing 28 changed files with 106 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self, **kwargs: Any) -> None:
)

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def close(self) -> None:
self.__exit__()

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def close(self) -> None:

@log_get_token("AzureDeveloperCliCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def close(self) -> None:

@log_get_token("AzureCliCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def close(self) -> None:

@log_get_token("AzurePowerShellCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def close(self) -> None:
self.__exit__()

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request a token from each chained credential, in order, returning the first token received.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
super(DefaultAzureCredential, self).__init__(*credentials)

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def close(self) -> None:

@log_get_token("EnvironmentCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def close(self) -> None:

@log_get_token("ManagedIdentityCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def close(self) -> None:

@log_get_token("SharedTokenCacheCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Get an access token for `scopes` from the shared cache.
Expand Down Expand Up @@ -100,7 +100,7 @@ def __exit__(self, *args):
self._client.__exit__(*args)

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
if not scopes:
raise ValueError("'get_token' requires at least one scope")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __exit__(self, *args):
self._client.__exit__(*args)

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
if not scopes:
raise ValueError('"get_token" requires at least one scope')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def close(self) -> None:

@log_get_token("VSCodeCredential")
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes` as the user currently signed in to Visual Studio Code.
Expand All @@ -150,6 +150,8 @@ def get_token(
:param str scopes: desired scopes for the access token. This method requires at least one scope.
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str claims: not used by this credential; any value provided will be ignored.
:keyword str tenant_id: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _should_refresh(self, token: AccessToken) -> bool:
return True

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(
super(InteractiveCredential, self).__init__(**kwargs)

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def close(self) -> None:
self.__exit__()

def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs: Any
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
if not self._client:
raise CredentialUnavailableError(message=self.get_unavailable_message())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,29 @@ def __init__(
ManagedIdentityCredential(client_id=managed_identity_client_id, **kwargs),
)

async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
async def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Asynchronously request an access token for `scopes`.
This method is called automatically by Azure SDK clients.
:param str scopes: desired scopes for the access token. This method requires at least one scope.
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str claims: not used by this credential; any value provided will be ignored.
:keyword str tenant_id: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
:raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The exception has a
`message` attribute listing each authentication attempt and its error message.
"""
if self._successful_credential:
token = await self._successful_credential.get_token(*scopes, **kwargs)
token = await self._successful_credential.get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
_LOGGER.info(
"%s acquired a token from %s", self.__class__.__name__, self._successful_credential.__class__.__name__
)
return token

return await super().get_token(*scopes, **kwargs)
return await super().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def __init__(
self._redirect_uri = redirect_uri
super().__init__()

async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
async def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
This method is called automatically by Azure SDK clients.
Expand All @@ -81,14 +83,15 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str tenant_id: optional tenant to include in the token request.
:keyword str claims: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
:raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The error's ``message``
attribute gives a reason. Any error response from Azure Active Directory is available as the error's
``response`` attribute.
"""
return await super().get_token(*scopes, **kwargs)
return await super().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)

async def _acquire_token_silently(self, *scopes: str, **kwargs: Any) -> Optional[AccessToken]:
return self._client.get_cached_access_token(scopes, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def __init__(
self._process_timeout = process_timeout

@log_get_token_async
async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
async def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
This method is called automatically by Azure SDK clients. Applications calling this method directly must
Expand All @@ -89,6 +91,7 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str tenant_id: optional tenant to include in the token request.
:keyword str claims: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
Expand All @@ -98,15 +101,19 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
"""
# only ProactorEventLoop supports subprocesses on Windows (and it isn't the default loop on Python < 3.8)
if sys.platform.startswith("win") and not isinstance(asyncio.get_event_loop(), asyncio.ProactorEventLoop):
return _SyncAzureDeveloperCliCredential().get_token(*scopes, **kwargs)
return _SyncAzureDeveloperCliCredential().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)

if not scopes:
raise ValueError("Missing scope in request. \n")

commandString = " --scope ".join(scopes)
command = COMMAND_LINE.format(commandString)
tenant = resolve_tenant(
default_tenant=self.tenant_id, additionally_allowed_tenants=self._additionally_allowed_tenants, **kwargs
default_tenant=self.tenant_id,
tenant_id=tenant_id,
additionally_allowed_tenants=self._additionally_allowed_tenants,
claims=claims,
**kwargs,
)

if tenant:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def __init__(
self._process_timeout = process_timeout

@log_get_token_async
async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
async def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
This method is called automatically by Azure SDK clients. Applications calling this method directly must
Expand All @@ -70,6 +72,7 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str tenant_id: optional tenant to include in the token request.
:keyword str claims: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
Expand All @@ -79,12 +82,16 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
"""
# only ProactorEventLoop supports subprocesses on Windows (and it isn't the default loop on Python < 3.8)
if sys.platform.startswith("win") and not isinstance(asyncio.get_event_loop(), asyncio.ProactorEventLoop):
return _SyncAzureCliCredential().get_token(*scopes, **kwargs)
return _SyncAzureCliCredential().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)

resource = _scopes_to_resource(*scopes)
command = COMMAND_LINE.format(resource)
tenant = resolve_tenant(
default_tenant=self.tenant_id, additionally_allowed_tenants=self._additionally_allowed_tenants, **kwargs
default_tenant=self.tenant_id,
tenant_id=tenant_id,
additionally_allowed_tenants=self._additionally_allowed_tenants,
claims=claims,
**kwargs,
)

if tenant:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def __init__(
self._process_timeout = process_timeout

@log_get_token_async
async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
async def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Request an access token for `scopes`.
This method is called automatically by Azure SDK clients. Applications calling this method directly must
Expand All @@ -64,6 +66,7 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str tenant_id: optional tenant to include in the token request.
:keyword str claims: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
Expand All @@ -74,10 +77,14 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
"""
# only ProactorEventLoop supports subprocesses on Windows (and it isn't the default loop on Python < 3.8)
if sys.platform.startswith("win") and not isinstance(asyncio.get_event_loop(), asyncio.ProactorEventLoop):
return _SyncCredential().get_token(*scopes, **kwargs)
return _SyncCredential().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)

tenant_id = resolve_tenant(
default_tenant=self.tenant_id, additionally_allowed_tenants=self._additionally_allowed_tenants, **kwargs
default_tenant=self.tenant_id,
tenant_id=tenant_id,
additionally_allowed_tenants=self._additionally_allowed_tenants,
claims=claims,
**kwargs,
)
command_line = get_command_line(scopes, tenant_id)
output = await run_command_line(command_line, self._process_timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async def close(self) -> None:

await asyncio.gather(*(credential.close() for credential in self.credentials))

async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
async def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""Asynchronously request a token from each credential, in order, returning the first token received.
If no credential provides a token, raises :class:`azure.core.exceptions.ClientAuthenticationError`
Expand All @@ -61,6 +63,8 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
:param str scopes: desired scopes for the access token. This method requires at least one scope.
For more information about scopes, see
https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc.
:keyword str claims: not used by this credential; any value provided will be ignored.
:keyword str tenant_id: not used by this credential; any value provided will be ignored.
:return: An access token with the desired scopes.
:rtype: ~azure.core.credentials.AccessToken
Expand All @@ -70,7 +74,7 @@ async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken:
history = []
for credential in self.credentials:
try:
token = await credential.get_token(*scopes, **kwargs)
token = await credential.get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
_LOGGER.info("%s acquired a token from %s", self.__class__.__name__, credential.__class__.__name__)
self._successful_credential = credential
return token
Expand Down
Loading

0 comments on commit 437b2d1

Please sign in to comment.