-
Notifications
You must be signed in to change notification settings - Fork 3.3k
{Core} Refactor code for MSAL authentication #29439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is renamed to more accurately reflect its content. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,32 +22,23 @@ | |
|
|
||
| from .util import check_result, build_sdk_access_token | ||
|
|
||
| # OAuth 2.0 client credentials flow parameter | ||
| # https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow | ||
| _TENANT = 'tenant' | ||
| _CLIENT_ID = 'client_id' | ||
| _CLIENT_SECRET = 'client_secret' | ||
| _CERTIFICATE = 'certificate' | ||
| _CLIENT_ASSERTION = 'client_assertion' | ||
| _USE_CERT_SN_ISSUER = 'use_cert_sn_issuer' | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class UserCredential(PublicClientApplication): | ||
| class UserCredential: # pylint: disable=too-few-public-methods | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interestingly, pylint doesn't think this is worth a class. Haha
Ref: https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/too-few-public-methods.html
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to define such class so that it can be passed to SDK as a credential. |
||
|
|
||
| def __init__(self, client_id, username, **kwargs): | ||
| """User credential implementing get_token interface. | ||
|
|
||
| :param client_id: Client ID of the CLI. | ||
| :param username: The username for user credential. | ||
| """ | ||
| super().__init__(client_id, **kwargs) | ||
| self._msal_app = PublicClientApplication(client_id, **kwargs) | ||
|
|
||
| # Make sure username is specified, otherwise MSAL returns all accounts | ||
| assert username, "username must be specified, got {!r}".format(username) | ||
|
|
||
| accounts = self.get_accounts(username) | ||
| accounts = self._msal_app.get_accounts(username) | ||
|
|
||
| # Usernames are usually unique. We are collecting corner cases to better understand its behavior. | ||
| if len(accounts) > 1: | ||
|
|
@@ -65,8 +56,9 @@ def get_token(self, *scopes, claims=None, **kwargs): | |
|
|
||
| if claims: | ||
| logger.warning('Acquiring new access token silently for tenant %s with claims challenge: %s', | ||
| self.authority.tenant, claims) | ||
| result = self.acquire_token_silent_with_error(list(scopes), self._account, claims_challenge=claims, **kwargs) | ||
| self._msal_app.authority.tenant, claims) | ||
| result = self._msal_app.acquire_token_silent_with_error(list(scopes), self._account, claims_challenge=claims, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By saving |
||
| **kwargs) | ||
|
|
||
| from azure.cli.core.azclierror import AuthenticationError | ||
| try: | ||
|
|
@@ -82,13 +74,14 @@ def get_token(self, *scopes, claims=None, **kwargs): | |
| logger.warning(ex) | ||
| logger.warning("\nThe default web browser has been opened at %s for scope '%s'. " | ||
| "Please continue the login in the web browser.", | ||
| self.authority.authorization_endpoint, ' '.join(scopes)) | ||
| self._msal_app.authority.authorization_endpoint, ' '.join(scopes)) | ||
|
|
||
| from .util import read_response_templates | ||
| success_template, error_template = read_response_templates() | ||
|
|
||
| result = self.acquire_token_interactive( | ||
| list(scopes), login_hint=self._account['username'], port=8400 if self.authority.is_adfs else None, | ||
| result = self._msal_app.acquire_token_interactive( | ||
| list(scopes), login_hint=self._account['username'], | ||
| port=8400 if self._msal_app.authority.is_adfs else None, | ||
| success_template=success_template, error_template=error_template, **kwargs) | ||
| check_result(result) | ||
|
|
||
|
|
@@ -99,42 +92,19 @@ def get_token(self, *scopes, claims=None, **kwargs): | |
| return build_sdk_access_token(result) | ||
|
|
||
|
|
||
| class ServicePrincipalCredential(ConfidentialClientApplication): | ||
| class ServicePrincipalCredential: # pylint: disable=too-few-public-methods | ||
|
|
||
| def __init__(self, service_principal_auth, **kwargs): | ||
| def __init__(self, client_id, client_credential, **kwargs): | ||
| """Service principal credential implementing get_token interface. | ||
|
|
||
| :param service_principal_auth: An instance of ServicePrincipalAuth. | ||
| :param client_id: The service principal's client ID. | ||
| :param client_credential: client_credential that will be passed to MSAL. | ||
| """ | ||
| client_credential = None | ||
|
|
||
| # client_secret | ||
| client_secret = getattr(service_principal_auth, _CLIENT_SECRET, None) | ||
| if client_secret: | ||
| client_credential = client_secret | ||
|
|
||
| # certificate | ||
| certificate = getattr(service_principal_auth, _CERTIFICATE, None) | ||
| if certificate: | ||
| client_credential = { | ||
| "private_key": getattr(service_principal_auth, 'certificate_string'), | ||
| "thumbprint": getattr(service_principal_auth, 'thumbprint') | ||
| } | ||
| public_certificate = getattr(service_principal_auth, 'public_certificate', None) | ||
| if public_certificate: | ||
| client_credential['public_certificate'] = public_certificate | ||
|
|
||
| # client_assertion | ||
| client_assertion = getattr(service_principal_auth, _CLIENT_ASSERTION, None) | ||
| if client_assertion: | ||
| client_credential = {'client_assertion': client_assertion} | ||
|
|
||
| super().__init__(service_principal_auth.client_id, client_credential=client_credential, **kwargs) | ||
| self._msal_app = ConfidentialClientApplication(client_id, client_credential, **kwargs) | ||
|
|
||
| def get_token(self, *scopes, **kwargs): | ||
| logger.debug("ServicePrincipalCredential.get_token: scopes=%r, kwargs=%r", scopes, kwargs) | ||
|
|
||
| scopes = list(scopes) | ||
| result = self.acquire_token_for_client(scopes, **kwargs) | ||
| result = self._msal_app.acquire_token_for_client(list(scopes), **kwargs) | ||
| check_result(result) | ||
| return build_sdk_access_token(result) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
ServicePrincipalCredentialstops inheriting frommsal.ConfidentialClientApplication, it is no longer possible to callacquire_token_for_clienton thecred. We prepareclient_credentialand directly create aConfidentialClientApplicationinstance.