Skip to content

Commit 4c39567

Browse files
committed
get_token_info
1 parent a94fc1c commit 4c39567

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/azure-cli-core/azure/cli/core/auth/credential_adaptor.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,47 @@ def get_token(self, *scopes, **kwargs):
3434

3535
return build_sdk_access_token(self._credential.acquire_token(list(scopes), **filtered_kwargs))
3636

37+
38+
def get_token_info(self, *scopes, options=None):
39+
"""Get an access token from the main credential."""
40+
logger.debug("CredentialAdaptor.get_token_info: scopes=%r, options=%r", scopes, options)
41+
42+
# Discard unsupported kwargs: tenant_id, enable_cae
43+
msal_kwargs = {}
44+
if 'claims' in options:
45+
msal_kwargs['claims'] = options['claims']
46+
if 'data' in options:
47+
msal_kwargs['data'] = options['data']
48+
49+
return _build_sdk_access_token_info(self._credential.acquire_token(list(scopes), **msal_kwargs))
50+
51+
3752
def get_auxiliary_tokens(self, *scopes, **kwargs):
3853
"""Get access tokens from auxiliary credentials."""
3954
# To test cross-tenant authentication, see https://github.com/Azure/azure-cli/issues/16691
4055
if self._auxiliary_credentials:
4156
return [build_sdk_access_token(cred.acquire_token(list(scopes), **kwargs))
4257
for cred in self._auxiliary_credentials]
4358
return None
59+
60+
61+
def _build_sdk_access_token_info(token_entry):
62+
# MSAL token entry sample:
63+
# {
64+
# 'access_token': 'eyJ0eXAiOiJKV...',
65+
# 'token_type': 'Bearer',
66+
# 'expires_in': 1618,
67+
# 'token_source': 'cache'
68+
# }
69+
70+
# Importing azure.core.credentials.AccessTokenInfo is expensive.
71+
# This can slow down commands that doesn't need azure.core.
72+
from .constants import ACCESS_TOKEN, EXPIRES_IN
73+
from azure.core.credentials import AccessTokenInfo
74+
75+
return AccessTokenInfo(token_entry[ACCESS_TOKEN], _now_timestamp() + token_entry[EXPIRES_IN])
76+
77+
78+
def _now_timestamp():
79+
import time
80+
return int(time.time())

0 commit comments

Comments
 (0)