|
| 1 | +--- |
| 2 | +title: Using MSAL Python with an Authentication Broker on macOS |
| 3 | +description: "Using an authentication broker on macOS enables you to simplify how your users authenticate with Microsoft Entra ID from your application, as well as take advantage of advanced functionality such as token binding, protecting any issued tokens from exfiltration and misuse." |
| 4 | +author: localden |
| 5 | +manager: CelesteDG |
| 6 | + |
| 7 | +ms.service: msal |
| 8 | +ms.subservice: msal-python |
| 9 | +ms.topic: conceptual |
| 10 | +ms.date: 09/06/2024 |
| 11 | +ms.author: ddelimarsky |
| 12 | +ms.reviewer: shermanouko, rayluo |
| 13 | +--- |
| 14 | + |
| 15 | +# Using MSAL Python with an Authentication Broker on macOS |
| 16 | + |
| 17 | +>[!NOTE] |
| 18 | +>macOS authentication broker support is introduced with `msal` version 1.31.0. |
| 19 | +
|
| 20 | +Using an authentication brokers on macOS enables you to simplify how your users authenticate with Microsoft Entra ID from your application, |
| 21 | +as well as take advantage of future functionality that protects Microsoft Entra ID refresh tokens from exfiltration and misuse. |
| 22 | + |
| 23 | +Authentication brokers are **not** pre-installed on macOS but are applications developed by Microsoft, such as [Company Portal](/mem/intune/apps/apps-company-portal-macos). These applications are usually installed when a macOS computer is enrolled in a company's device fleet via an endpoint management solution like [Microsoft Intune](/mem/intune/fundamentals/what-is-intune). To learn more about Apple device set up with the Microsoft Identity Platform, refer to [Microsoft Enterprise SSO plug-in for Apple devices](/entra/identity-platform/apple-sso-plugin). |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +To use the broker, you will need to install the broker-related packages in addition to the core MSAL from PyPI: |
| 28 | + |
| 29 | +```bash |
| 30 | +pip install msal[broker]>=1.31,<2 |
| 31 | +``` |
| 32 | + |
| 33 | +>[!IMPORTANT] |
| 34 | +>If broker-related packages are not installed and you will try to use the authentication broker, you will get an error: `ImportError: You need to install dependency by: pip install "msal[broker]>=1.31,<2"`. |
| 35 | +
|
| 36 | +Typically, on macOS your [public client](/entra/identity-platform/msal-client-applications) Python applications would [acquire tokens](../getting-started/acquiring-tokens.md) via the system browser. To use authentication brokers installed on a macOS system instead, you will need to pass an additional argument in the `PublicClientApplication` constructor - `enable_broker_on_mac`: |
| 37 | + |
| 38 | +```python |
| 39 | +from msal import PublicClientApplication |
| 40 | + |
| 41 | +app = PublicClientApplication( |
| 42 | + "CLIENT_ID", |
| 43 | + authority="https://login.microsoftonline.com/common", |
| 44 | + enable_broker_on_mac =True) |
| 45 | +``` |
| 46 | + |
| 47 | +>[!IMPORTANT] |
| 48 | +>If you are writing a cross-platform application, you will also need to use `enable_broker_on_windows`, as outlined in the [Using MSAL Python with Web Account Manager](wam.md) article. |
| 49 | +
|
| 50 | +In addition to the constructor change, your application needs to support broker-specific redirect URIs. For _unsigned_ applications, the URI is: |
| 51 | + |
| 52 | +```text |
| 53 | +msauth.com.msauth.unsignedapp://auth |
| 54 | +``` |
| 55 | + |
| 56 | +For signed applications, the redirect URI should be: |
| 57 | + |
| 58 | +```text |
| 59 | +msauth.BUNDLE_ID://auth |
| 60 | +``` |
| 61 | + |
| 62 | +If the redirect URIs are not correctly set in the app configuration within the Entra portal, you will receive error like this: |
| 63 | + |
| 64 | +```text |
| 65 | +Error detected... |
| 66 | +tag=508170375 |
| 67 | +context=AADSTS50011 Description: (pii), Domain: MSAIMSIDOAuthErrorDomain.Error was thrown in location: Broker |
| 68 | +errorCode=-51411 |
| 69 | +status=Response_Status.Status_Unexpected |
| 70 | +``` |
| 71 | + |
| 72 | +Once configured, you can call `acquire_token_interactive` to acquire a token. |
| 73 | + |
| 74 | +```python |
| 75 | +result = app.acquire_token_interactive(["User.ReadBasic.All"], |
| 76 | + parent_window_handle=app.CONSOLE_WINDOW_HANDLE) |
| 77 | +``` |
| 78 | + |
| 79 | +>[!NOTE] |
| 80 | +>The `parent_window_handle` parameter is required even though on macOS it is not used. For GUI applications, the login prompt location will be determined ad-hoc and currently cannot be bound to a specific window. In a future update, this parameter will be used to determine the _actual_ parent window. |
| 81 | +
|
| 82 | +## Token caching |
| 83 | + |
| 84 | +The authentication broker handles refresh and access token caching. You do not need to set up custom caching. |
0 commit comments