Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import ssl
from typing import Any, Optional

from requests.adapters import HTTPAdapter
from requests import Session
from requests.adapters import HTTPAdapter # pylint: disable=networking-import-outside-azure-core-transport
from requests import Session # pylint: disable=networking-import-outside-azure-core-transport
from azure.core.pipeline.transport import ( # pylint: disable=non-abstract-transport-import, no-name-in-module
RequestsTransport,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""
from typing import Any, Optional

from requests import Session
from requests import Session # pylint: disable=networking-import-outside-azure-core-transport
from azure.core.pipeline.transport import ( # pylint: disable=non-abstract-transport-import, no-name-in-module
AsyncioRequestsTransport,
)
Expand Down
25 changes: 20 additions & 5 deletions sdk/identity/azure-identity/samples/custom_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import time
from typing import Optional, Union

import msal

from azure.core.credentials import AccessToken
from azure.identity import AuthenticationRequiredError, AzureAuthorityHosts
import msal


class StaticTokenCredential(object):
Expand All @@ -30,7 +31,14 @@ def __init__(self, access_token: Union[str, AccessToken]) -> None:
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""get_token is the only method a credential must implement"""
"""Get an access token for the requested scopes.

:param str scopes: desired scopes for the access token.
:keyword str claims: additional claims required in the token request.
:keyword str tenant_id: optional tenant to include in the token request.
:return: An access token with the requested scopes.
:rtype: ~azure.core.credentials.AccessToken
"""

return self._token

Expand All @@ -46,13 +54,20 @@ def __init__(self, tenant_id: str, client_id: str) -> None:
def get_token(
self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs
) -> AccessToken:
"""get_token is the only method a credential must implement"""
"""Get an access token for the requested scopes.

:param str scopes: desired scopes for the access token.
:keyword str claims: additional claims required in the token request.
:keyword str tenant_id: optional tenant to include in the token request.
:return: An access token with the requested scopes.
:rtype: ~azure.core.credentials.AccessToken
"""

now = int(time.time())
result = self._app.acquire_token_interactive(list(scopes), claims=claims, tenant_id=tenant_id, **kwargs)

try:
return AccessToken(result["access_token"], now + int(result["expires_in"]))
except:
except Exception as ex:
print("\nFailed to get a valid access token")
raise AuthenticationRequiredError(scopes)
raise AuthenticationRequiredError(scopes) from ex
Loading