AzureAD / microsoft-authentication-library-for-python

Microsoft Authentication Library (MSAL) for Python makes it easy to authenticate to Microsoft Entra ID. General docs are available here https://learn.microsoft.com/entra/msal/python/ Stable APIs are documented here https://msal-python.readthedocs.io. Questions can be asked on www.stackoverflow.com with tag "msal" + "python".
https://stackoverflow.com/questions/tagged/azure-ad-msal+python
Other
788 stars 194 forks source link

Broker integration #415

Closed rayluo closed 1 year ago

rayluo commented 2 years ago

What is this?

A broker is a component installed on your device. Broker implicitly gives your device an identity. By using a broker, your device becomes a factor that can satisfy MFA (Multi-factor authentication). This factor would become mandatory if/when a tenant's admin enables a corresponding Conditional Access (CA) policy. The broker's presence allows Microsoft identity platform to have higher confidence that the tokens are being issued to your device, and that is more secure.

An additional benefit of broker is, it runs as a long-lived process with your device's OS, and maintains its own cache, so that your broker-enabled apps (even CLI) could automatically SSO from a previously established signed-in session.

Currently, broker is available on recent Windows platforms.

In this PR, MSAL Python utilizes the broker to acquire tokens. When enabled, broker behaviors will kick in when your PublicClientApplication app calls MSAL Python's acquire_token_interactive(), acquire_token_by_username_password(), acquire_token_silent(), acquire_token_silent_with_error(). Most noticeably, the acquire_token_interactive([...], prompt="select_account") will trigger a pop-up window, rather than a browser.

API reference docs has also been updated and staged at here (you would need to scroll up a little bit).

Prerequisite for an app

  1. Because this feature branch is not yet officially released with a version number, you will need to clean up your test environment by pip uninstall msal pymsalruntime -y, especially when you want to reset your test environment to test the latest changes in this PR.

  2. Use a recent version of MSAL Python. Currently, this can be achieved by installing the MSAL Python from this feature branch: pip install "git+https://github.com/AzureAD/microsoft-authentication-library-for-python.git@wam"

  3. Install the broker package. The broker package pymsalruntime needs to be available in your Python environment, ~so that MSAL Python will automatically utilize it~. This can be satisfied by pip install "msal[broker] @ git+https://github.com/AzureAD/microsoft-authentication-library-for-python.git@wam". In the future (after this PR is merged and shipped), your app can meet the two prerequisites above by a simpler pip install "msal[broker]>=1.19,<2" (The actual release version is not yet determined.). (We do NOT recommend directly install pymsalruntime, because its latest version may not have been tested with MSAL Python. Stick with the installation command above so that you will always get the latest version of pymsalruntime which has been tested with MSAL Python.)

  4. Register one more redirect URI. Your app would need to register this one more redirect_uri beforehand, in this form: ms-appx-web://Microsoft.AAD.BrokerPlugin/your_client_id.

  5. Opt-in. ~Currently, MSAL Python does not provide an API-level opt-in flag. App developer opts in by declaring needed dependencies AND registering a new redirect URL. Once all prerequisites are met, broker behavior will kick in, otherwise, it will gracefully be fallback to use non-broker behavior. The idea is your app does not need to hardcode your opt-in/opt-out decision, or to implement an opt-in or opt-out setting. The broker functionality can be toggled flexibly, without any source code changes to your app. This approach would make the adoption easier.~ Opt-in by PublicClientApplication(..., allow_broker=True).

Action items for a downstream library

Regardless, you would still want to use your own test app to test the broker behavior to understand its impact on your library's experience.

What/How to test

To report an issue, please share with us your test configuration. For example, you can do that by copy and paste the console history of your interaction with our test script python msaltest.py.

Roadmap

The following features are not yet supported, but they are expected to be available in the near future. But you should not wait. Please start testing your app with this PR asap, and report back your findings or concerns.

P.S.:

CC: @jiasli , @xiangyan99