You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add documentation on difference between MSAL and azure.identity #299
While developing an application, which uploads data to a blob storage and requires authentication, I was wondering what the best Azure authentication module is in Python since there is two:
For me it's difficult to understand the difference between the two, and to decide when to use which one since there is quite some overlap. On the internet I couldn't find any discussions or documentation on the difference. It would be nice to know what the best practice is when using Python to authenticate to Azure. Can you explain the difference or add documentation? Thanks.
My general suggestion on choosing libraries/frameworks would be to look into their API surface and samples, to see which one matches your particular use case more closely.
MSAL is an authentication (and authorization) library. MSAL's API surface helps you to obtain the tokens, which can then be used to make a Rest API call using any generic HTTP library. MSAL's samples demonstrates some common scenarios and patterns.
Azure-Identity provides closer integration with other Azure SDK components, with a different API surface.
Reacted by Sam, David Swinkels, Jiashuo Li, jimas13, Vladyslav Fedoriuk, jackStalnaker, Ahmed Refaey and Shevach Marants
azure-identity is the Azure SDK's authentication library, so if you're using the Azure SDK, start there. MSAL and azure-identity support the same authentication flows but azure-identity has a higher level, simpler API because it's designed to integrate with Azure SDK clients and hide the complexity of authentication from applications. If you find yourself wanting to use MSAL directly with an Azure SDK client, please open an issue on our repo because you may have an interesting feature request 😄
If your application is not using the Azure SDK, I think the choice depends on how it authenticates and whether it needs a lower level API. For example, azure-identity can send a user through the authorization code flow but doesn't have an API for acquiring a code without completing the flow.
Reacted by Sam, David Swinkels, Jiashuo Li, Mars Wang, Cameron Taggart, Shevach Marants and Xiang ZHU
If your application is not using the Azure SDK, I think the choice depends on how it authenticates and whether it needs a lower level API. For example, azure-identity can send a user through the authorization code flow but doesn't have an API for acquiring a code without completing the flow.
Good point on the API "level". By the way, the latest version of MSAL (1.8.0) did provide the higher level acquire_token_interactive(...) API, and this is its sample. So, let's say if one is building a CLI only needs user login, this can be adequate.
As the developer of Azure CLI core and authentication which use Azure Identity Python and MSAL heavily, the most distinguished difference I noticed is that Azure Identity's various credentials (e.g. InteractiveBrowserCredential) all implement the TokenCredential protocol/interface which exposes get_token method.
get_token severs as a bridge between Azure Python SDK clients and MSAL.
When any SDK client is trying to invoke a REST API, BearerTokenCredentialPolicy from Azure Core calls the credential's get_token to retrieve the access token and adds it to the request's Authorization header.
classBearerTokenCredentialPolicy(_BearerTokenCredentialPolicyBase, SansIOHTTPPolicy):
defon_request(self, request):
ifself._tokenisNoneorself._need_new_token:
self._token=self._credential.get_token(*self._scopes) # Get access tokenself._update_headers(request.http_request.headers, self._token.token) # Add the access token to header
Within the implementation of get_token, Azure Identity uses methods exposed by MSAL (e.g. acquire_token_silent_with_error) to retrieve the access token.
Reacted by Ray Luo, Yong Zhang, David Swinkels, Zhichao Li, neo, rob miller, Dasith Wijesiriwardena, Paul Robert W., jackStalnaker, Anoop R Desai and 1 more
Reacted by Yong Zhang, Mikhail Astafev, Carlos Domingues, rob miller, Dasith Wijesiriwardena, Guillaume FORTAINE and Ahmed Refaey
Activity
rayluo commentedon Jan 27, 2021
Great question!
MSAL is one of the building blocks of Azure Identity. When Y is built on top of X, it is better to have Y to talk about the difference. :-) CC Charles Lowell (@chlowell)
My general suggestion on choosing libraries/frameworks would be to look into their API surface and samples, to see which one matches your particular use case more closely.
MSAL is an authentication (and authorization) library. MSAL's API surface helps you to obtain the tokens, which can then be used to make a Rest API call using any generic HTTP library. MSAL's samples demonstrates some common scenarios and patterns.
Azure-Identity provides closer integration with other Azure SDK components, with a different API surface.
chlowell commentedon Jan 27, 2021
azure-identity is the Azure SDK's authentication library, so if you're using the Azure SDK, start there. MSAL and azure-identity support the same authentication flows but azure-identity has a higher level, simpler API because it's designed to integrate with Azure SDK clients and hide the complexity of authentication from applications. If you find yourself wanting to use MSAL directly with an Azure SDK client, please open an issue on our repo because you may have an interesting feature request 😄
If your application is not using the Azure SDK, I think the choice depends on how it authenticates and whether it needs a lower level API. For example, azure-identity can send a user through the authorization code flow but doesn't have an API for acquiring a code without completing the flow.
rayluo commentedon Jan 27, 2021
Good point on the API "level". By the way, the latest version of MSAL (1.8.0) did provide the higher level
acquire_token_interactive(...)API, and this is its sample. So, let's say if one is building a CLI only needs user login, this can be adequate.jiasli commentedon Jan 28, 2021
As the developer of Azure CLI core and authentication which use Azure Identity Python and MSAL heavily, the most distinguished difference I noticed is that Azure Identity's various credentials (e.g.
InteractiveBrowserCredential) all implement theTokenCredentialprotocol/interface which exposesget_tokenmethod.get_tokensevers as a bridge between Azure Python SDK clients and MSAL.When any SDK client is trying to invoke a REST API,
BearerTokenCredentialPolicyfrom Azure Core calls the credential'sget_tokento retrieve the access token and adds it to the request'sAuthorizationheader.Within the implementation of
get_token, Azure Identity uses methods exposed by MSAL (e.g.acquire_token_silent_with_error) to retrieve the access token.So the invocation hierarchy is
Hope it helps.
rayluo commentedon Jan 28, 2021
LOVE LOVE LOVE this community. So many valuable insights! :-)
Davidswinkels commentedon Jan 28, 2021
Thanks for explaining the difference very clearly! This helped me to know the best approach. So it is indeed valuable :)