44# --------------------------------------------------------------------------------------------
55
66"""
7- Credentials defined in this module are alternative implementations of credentials provided by Azure Identity.
8-
9- These credentials implement azure.core.credentials.TokenCredential by exposing `get_token` method for Track 2
10- SDK invocation.
11-
12- If you want to implement your own credential, the credential must also expose `get_token` method.
13-
14- `get_token` method takes `scopes` as positional arguments and other optional `kwargs`, such as `claims`, `data`.
15- The return value should be a named tuple containing two elements: token (str), expires_on (int). You may simply use
16- azure.cli.core.auth.util.AccessToken to build the return value. See below credentials as examples.
7+ Credentials to acquire tokens from MSAL.
178"""
189
1910from knack .log import get_logger
2213 ManagedIdentityClient , SystemAssignedManagedIdentity )
2314
2415from .constants import AZURE_CLI_CLIENT_ID
25- from .util import check_result , build_sdk_access_token
16+ from .util import check_result
2617
2718logger = get_logger (__name__ )
2819
2920
3021class UserCredential : # pylint: disable=too-few-public-methods
3122
3223 def __init__ (self , client_id , username , ** kwargs ):
33- """User credential implementing get_token interface.
24+ """User credential wrapping msal.application.PublicClientApplication
3425
3526 :param client_id: Client ID of the CLI.
3627 :param username: The username for user credential.
@@ -52,14 +43,15 @@ def __init__(self, client_id, username, **kwargs):
5243
5344 self ._account = accounts [0 ]
5445
55- def get_token (self , * scopes , claims = None , ** kwargs ):
56- # scopes = ['https://pas.windows.net/CheckMyAccess/Linux/.default']
57- logger .debug ("UserCredential.get_token: scopes=%r, claims=%r, kwargs=%r" , scopes , claims , kwargs )
46+ def acquire_token (self , scopes , claims = None , ** kwargs ):
47+ # scopes must be a list.
48+ # For acquiring SSH certificate, scopes is ['https://pas.windows.net/CheckMyAccess/Linux/.default']
49+ logger .debug ("UserCredential.acquire_token: scopes=%r, claims=%r, kwargs=%r" , scopes , claims , kwargs )
5850
5951 if claims :
6052 logger .warning ('Acquiring new access token silently for tenant %s with claims challenge: %s' ,
6153 self ._msal_app .authority .tenant , claims )
62- result = self ._msal_app .acquire_token_silent_with_error (list ( scopes ) , self ._account , claims_challenge = claims ,
54+ result = self ._msal_app .acquire_token_silent_with_error (scopes , self ._account , claims_challenge = claims ,
6355 ** kwargs )
6456
6557 from azure .cli .core .azclierror import AuthenticationError
@@ -82,7 +74,7 @@ def get_token(self, *scopes, claims=None, **kwargs):
8274 success_template , error_template = read_response_templates ()
8375
8476 result = self ._msal_app .acquire_token_interactive (
85- list ( scopes ) , login_hint = self ._account ['username' ],
77+ scopes , login_hint = self ._account ['username' ],
8678 port = 8400 if self ._msal_app .authority .is_adfs else None ,
8779 success_template = success_template , error_template = error_template , ** kwargs )
8880 check_result (result )
@@ -91,25 +83,25 @@ def get_token(self, *scopes, claims=None, **kwargs):
9183 # launch browser, but show the error message and `az login` command instead.
9284 else :
9385 raise
94- return build_sdk_access_token ( result )
86+ return result
9587
9688
9789class ServicePrincipalCredential : # pylint: disable=too-few-public-methods
9890
9991 def __init__ (self , client_id , client_credential , ** kwargs ):
100- """Service principal credential implementing get_token interface .
92+ """Service principal credential wrapping msal.application.ConfidentialClientApplication .
10193
10294 :param client_id: The service principal's client ID.
10395 :param client_credential: client_credential that will be passed to MSAL.
10496 """
10597 self ._msal_app = ConfidentialClientApplication (client_id , client_credential , ** kwargs )
10698
107- def get_token (self , * scopes , ** kwargs ):
108- logger . debug ( "ServicePrincipalCredential.get_token: scopes=%r, kwargs=%r" , scopes , kwargs )
109-
110- result = self ._msal_app .acquire_token_for_client (list ( scopes ) , ** kwargs )
99+ def acquire_token (self , scopes , ** kwargs ):
100+ # scopes must be a list
101+ logger . debug ( "ServicePrincipalCredential.acquire_token: scopes=%r, kwargs=%r" , scopes , kwargs )
102+ result = self ._msal_app .acquire_token_for_client (scopes , ** kwargs )
111103 check_result (result )
112- return build_sdk_access_token ( result )
104+ return result
113105
114106
115107class CloudShellCredential : # pylint: disable=too-few-public-methods
@@ -126,12 +118,12 @@ def __init__(self):
126118 # token_cache=...
127119 )
128120
129- def get_token (self , * scopes , ** kwargs ):
130- logger .debug ("CloudShellCredential.get_token : scopes=%r, kwargs=%r" , scopes , kwargs )
121+ def acquire_token (self , scopes , ** kwargs ):
122+ logger .debug ("CloudShellCredential.acquire_token : scopes=%r, kwargs=%r" , scopes , kwargs )
131123 # kwargs is already sanitized by CredentialAdaptor, so it can be safely passed to MSAL
132- result = self ._msal_app .acquire_token_interactive (list ( scopes ) , prompt = "none" , ** kwargs )
124+ result = self ._msal_app .acquire_token_interactive (scopes , prompt = "none" , ** kwargs )
133125 check_result (result , scopes = scopes )
134- return build_sdk_access_token ( result )
126+ return result
135127
136128
137129class ManagedIdentityCredential : # pylint: disable=too-few-public-methods
@@ -143,10 +135,10 @@ def __init__(self):
143135 import requests
144136 self ._msal_client = ManagedIdentityClient (SystemAssignedManagedIdentity (), http_client = requests .Session ())
145137
146- def get_token (self , * scopes , ** kwargs ):
147- logger .debug ("ManagedIdentityCredential.get_token : scopes=%r, kwargs=%r" , scopes , kwargs )
138+ def acquire_token (self , scopes , ** kwargs ):
139+ logger .debug ("ManagedIdentityCredential.acquire_token : scopes=%r, kwargs=%r" , scopes , kwargs )
148140
149141 from .util import scopes_to_resource
150142 result = self ._msal_client .acquire_token_for_client (resource = scopes_to_resource (scopes ))
151143 check_result (result )
152- return build_sdk_access_token ( result )
144+ return result
0 commit comments