Skip to content

Commit 3376060

Browse files
Merge pull request #120 from SHERMANOUKO/freshness-updates-1
Update MSAL Python conceptual docs for freshness
2 parents 7a5e886 + d920170 commit 3376060

File tree

7 files changed

+52
-74
lines changed

7 files changed

+52
-74
lines changed

.openpublishing.redirection.msal-python-conceptual.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"source_path_from_root": "/msal-python-conceptual/overview-msal",
55
"redirect_url": "/msal-python-conceptual/index",
66
"redirect_document_id": true
7+
},
8+
{
9+
"source_path_from_root": "/msal-python-conceptual/advanced/best-practices.md",
10+
"redirect_url": "/entra/msal/python/",
11+
"redirect_document_id": false
712
}
813
]
914
}

msal-python-conceptual/TOC.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
href: advanced/instance-metadata-caching.md
4040
- name: Client capabilities
4141
href: advanced/client-capabilities.md
42-
- name: Best practices
43-
href: advanced/best-practices.md
4442

4543
- name: Additional resources
4644
items:

msal-python-conceptual/advanced/best-practices.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

msal-python-conceptual/advanced/wam.md

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ manager: CelesteDG
77
ms.service: msal
88
ms.subservice: msal-python
99
ms.topic: conceptual
10-
ms.date: 02/07/2024
10+
ms.date: 04/24/2025
1111
ms.author: dmwendia
1212
ms.reviewer: shermanouko, rayluo
1313
---
1414

1515
# Using MSAL Python with Web Account Manager
1616

17-
If you are building a Windows application, you might consider simplifying how users authenticate with the help of an _authentication broker_ - the [Web Account Manager](/windows/uwp/security/web-account-manager) (WAM).
17+
If you are building a Windows application, you might consider simplifying how users authenticate with the help of an *authentication broker*. [Web Account Manager](/windows/uwp/security/web-account-manager) (WAM) is an authentication broker that works with MSAL Python. WAM is only available on Windows 10 and above, as well as Windows Server 2019 and above.
1818

19-
>[!NOTE]
20-
>WAM is only available on Windows 10 and above, as well as Windows Server 2019 and above.
21-
22-
To learn more about the benefits of using an authentication broker, refer to [What is a broker](/entra/msal/dotnet/acquiring-tokens/desktop-mobile/wam#what-is-a-broker) in the MSAL.NET documentation.
19+
For more information on the benefits of using an authentication broker, see [What is a broker](/entra/msal/dotnet/acquiring-tokens/desktop-mobile/wam#what-is-a-broker) in the MSAL.NET documentation.
2320

2421
## Usage
2522

@@ -29,13 +26,9 @@ To use the broker, you will need to install the broker-related packages in addit
2926
pip install msal[broker]>=1.20,<2
3027
```
3128

32-
>[!IMPORTANT]
33-
>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.20,<2"`.
34-
35-
Next, you will need to instantiate a new [`PublicClientApplication`](xref:msal.application.PublicClientApplication) and set `enable_broker_on_windows` to `True`. This will ensure that MSAL will try and communicate with WAM instead of popping up a new browser window.
29+
If broker-related packages aren't installed and you try to use the authentication broker, you will get the error *ImportError: You need to install dependency by: pip install "msal[broker]>=1.20,<2"*.
3630

37-
>[!IMPORTANT]
38-
>If you are writing a cross-platform application, you will also need to use `enable_broker_on_mac`, as outlined in the [Using MSAL Python with an Authentication Broker on macOS](macos-broker.md) article.
31+
Next, instantiate a new [`PublicClientApplication`](xref:msal.application.PublicClientApplication) and set `enable_broker_on_windows` to `True`. This will ensure that MSAL will try and communicate with WAM instead of popping up a new browser window. If you are writing a cross-platform application, you will also need to use `enable_broker_on_mac`, as outlined in the [Using MSAL Python with an Authentication Broker on macOS](macos-broker.md) article.
3932

4033
```python
4134
from msal import PublicClientApplication
@@ -46,27 +39,26 @@ app = PublicClientApplication(
4639
enable_broker_on_windows=True)
4740
```
4841

49-
You can now acquire a token by calling [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive) and specifying a parent window handle through `parent_window_handle`:
42+
You can now acquire a token by calling [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive) and specifying a parent window handle through *parent_window_handle*:
5043

5144
```python
5245
result = app.acquire_token_interactive(["User.ReadBasic.All"],
5346
parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
5447
```
5548

56-
A parent window handle is required by WAM to ensure that the dialog is shown correctly on top of the requesting window. MSAL does not infer this directly due to the fact that there are many variables that might influence what window WAM needs to bind to, and developers building applications are best suited to decide what window that should be.
49+
A parent window handle is required by WAM to ensure that the dialog is shown correctly on top of the requesting window. MSAL doesn't infer this directly due to the fact that there are many variables that might influence what window WAM needs to bind to, and developers building applications are best suited to decide what window that should be.
5750

58-
For console applications, MSAL makes it easy by offering an out-of-the-box solution to getting the window handle for the terminal - [`CONSOLE_WINDOW_HANDLE`](xref:msal.application.PublicClientApplication.CONSOLE_WINDOW_HANDLE). For desktop applications, additional work with the Windows API might be required to [get the window handle](/windows/apps/develop/ui-input/retrieve-hwnd). Helper packages, like [pywin32](https://pypi.org/project/pywin32/) can help with API calls.
51+
For console applications, MSAL makes it easy by offering an out-of-the-box solution to getting the window handle for the terminal - [`CONSOLE_WINDOW_HANDLE`](xref:msal.application.PublicClientApplication.CONSOLE_WINDOW_HANDLE). For desktop applications, more work with the Windows API might be required to [get the window handle](/windows/apps/develop/ui-input/retrieve-hwnd). Helper packages, like [pywin32](https://pypi.org/project/pywin32/) can help with API calls.
5952

6053
Before executing your application, make sure that you configure the redirect URL for the desktop app:
6154

62-
>[!IMPORTANT]
63-
>To use the Windows broker, your application needs to have the correct redirect URL configured in the Azure Portal, in the shape of:
64-
>
65-
>```bash
66-
>ms-appx-web://microsoft.aad.brokerplugin/YOUR_CLIENT_ID
67-
>```
68-
>
69-
>If the redirect URL is not configured, you will get a `broker_error` similar to `(pii). Status: Response_Status.Status_ApiContractViolation, Error code: 3399614473, Tag: 557973642`.
55+
To use the Windows broker, your application needs to have the correct redirect URL configured in the Azure Portal, in the shape of:
56+
57+
```bash
58+
ms-appx-web://microsoft.aad.brokerplugin/YOUR_CLIENT_ID
59+
```
60+
61+
If the redirect URL isn't configured, you will get a *broker_error* similar to *(pii). Status: Response_Status.Status_ApiContractViolation, Error code: 3399614473, Tag: 557973642*.
7062

7163
If configuration and instantiation was correct, once you run the application you should see the authentication broker kick in and allow the user to select the account they want to authenticate with.
7264

@@ -78,32 +70,30 @@ Worth noting that if you switch to using broker-based authentication, if the use
7870

7971
Depending on the authority specified when instantiating [`PublicClientApplication`](xref:msal.application.PublicClientApplication), the broker user interface may be different.
8072

81-
### `/consumers`
73+
### /consumers
8274

8375
Used for authenticating **only** with personal Microsoft accounts.
8476

8577
![WAM UI for consumers](../media/wam-consumers.png)
8678

87-
### `/common`
79+
### /common
8880

8981
Used for authenticating with personal Microsoft accounts as well as work and school accounts.
9082

9183
![WAM UI for personal and work accounts](../media/wam-common.png)
9284

93-
### `/organizations`
85+
### /organizations
9486

9587
Used for authenticating **only** with work and school accounts.
9688

9789
![WAM UI for work accounts only](../media/wam-organizations.png)
9890

99-
>[!NOTE]
100-
>If `login_hint` is provided but the account is not yet registered in WAM, the hint will be automatically filled in the "Email or phone" field.
91+
If *login_hint* is provided, but the account isn't yet registered in WAM, the hint will be automatically filled in the *Email or phone* field.
10192

102-
### `/TENANT_ID`
93+
### /TENANT_ID
10394

10495
Used for authenticating **only** with work and school accounts within the specified tenant.
10596

10697
![WAM UI for tenant-specific accounts](../media/wam-tenant-specific.png)
10798

108-
>[!NOTE]
109-
>If `login_hint` is provided but the account is not yet registered in WAM, the hint will be automatically filled in the "Email or phone" field.
99+
If *login_hint* is provided, but the account isn't yet registered in WAM, the hint will be automatically filled in the *Email or phone* field.

msal-python-conceptual/getting-started/acquiring-tokens.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ manager: CelesteDG
66
ms.service: msal
77
ms.subservice: msal-python
88
ms.topic: conceptual
9-
ms.date: 03/06/2024
9+
ms.date: 04/24/2025
1010
ms.author: shermanouko
1111
ms.reviewer: dmwendia, rayluo
1212
# zone_pivot_groups: msal-python-acquire-token
@@ -122,9 +122,9 @@ else:
122122

123123
### Username and password
124124

125-
It's also possible (but not recommended) to get a token with a [username and password](/entra/identity-platform/v2-oauth-ropc). MSAL Python provides the [`acquire_token_by_username_password`](/python/api/msal/msal.application.clientapplication#msal-application-clientapplication-acquire-token-by-username-password) method for this use case.
125+
We don't recommend using this approach. It's also possible to get a token with a [username and password](/entra/identity-platform/v2-oauth-ropc). MSAL Python provides the [`acquire_token_by_username_password`](/python/api/msal/msal.application.clientapplication#msal-application-clientapplication-acquire-token-by-username-password) method for this use case. It's not recommended because the application will be asking a user for their password directly, which is an insecure pattern.
126126

127-
Microsoft does not recommend the username and password flow because the application will be asking a user for their password directly, which is an insecure pattern. In most scenarios, there exist more secure flows that you can use. Learn more in the [username and password authentication flow](../advanced/username-password-authentication.md) guidance.
127+
Microsoft doesn't recommend the username and password flow because the application will be asking a user for their password directly, which is an insecure pattern. In most scenarios, there exist more secure flows that you can use. Learn more in the [username and password authentication flow](../advanced/username-password-authentication.md) guidance.
128128

129129
```python
130130
result = app.acquire_token_by_username_password(
@@ -155,7 +155,7 @@ else:
155155

156156
### Acquire token on behalf of
157157

158-
In the case of web apps or web APIs calling another downstream Web API in the name of the user, use the [On Behalf Of flow](/entra/identity-platform/v2-oauth2-on-behalf-of-flow) to acquire a token based on a user assertion (for example, SAML, JWT). The current app is a middle-tier service that was called with a token representing an end user. The current app can use such token, also known as a user assertion, to request another token to access downstream web API on behalf of that user. The middle-tier app has no user interaction to obtain consent. For information on gaining consent upfront for your middle-tier app, see the [documentation](/entra/identity-platform/v2-oauth2-on-behalf-of-flow#gaining-consent-for-the-middle-tier-application).
158+
In the case of web apps or web APIs calling another downstream Web API in the name of the user, use the [On Behalf Of flow](/entra/identity-platform/v2-oauth2-on-behalf-of-flow) to acquire a token based on a user assertion. For example, SAML and JWT. The current app is a middle-tier service that was called with a token representing an end user. The current app can use such token, also known as a user assertion, to request another token to access downstream web API on behalf of that user. The middle-tier app has no user interaction to obtain consent. For information on gaining consent upfront for your middle-tier app, see the [documentation](/entra/identity-platform/v2-oauth2-on-behalf-of-flow#gaining-consent-for-the-middle-tier-application).
159159

160160
Here's an example of code that acquires an access token using the [`acquire_token_on_behalf_of`](/python/api/msal/msal.application.confidentialclientapplication#msal-application-confidentialclientapplication-acquire-token-on-behalf-of) method.
161161

@@ -183,11 +183,11 @@ def get(self, request): # a web service endpoint receiving a request
183183
print(result.get("error"))
184184
```
185185

186-
## Acquire token by authorization code flow
186+
### Acquire token by authorization code flow
187187

188188
For web apps authenticating in the name of a user, acquire tokens through [authorization code](/entra/identity-platform/v2-oauth2-auth-code-flow) after letting the user sign-in through the authorization request URL. This is typically the mechanism used by an application that lets the user sign-in and access web APIs for this particular user.
189189

190-
You'll first need to initiate auth code flow using the [`initiate_auth_code_flow`](/python/api/msal/msal.application.clientapplication#msal-application-clientapplication-initiate-auth-code-flow). This method takes in among other parameters a redirect URI and state string. The value of the state parameter is also included in token response. If this value is absent, MSAL Python will automatically generate one internally. The redirect URI provided must match the redirect URI registered in the Microsoft Entra admin center. This method returns the auth code flow that is a dictionary containing auth_uri and a state. The auth_uri is the URL that the user needs to visit to sign in.
190+
You'll first need to initiate auth code flow using the [`initiate_auth_code_flow`](/python/api/msal/msal.application.clientapplication#msal-application-clientapplication-initiate-auth-code-flow). This method takes in among other parameters a redirect URI and state string. The value of the state parameter is also included in token response. If this value is absent, MSAL Python will automatically generate one internally. The redirect URI provided must match the redirect URI registered in the Microsoft Entra admin center. This method returns the auth code flow that is a dictionary containing `auth_uri` and `state`. The `auth_uri` is the URL that the user needs to visit to sign in.
191191

192192
```python
193193
flow = app.initiate_auth_code_flow(
@@ -202,7 +202,7 @@ session["auth_flow"] = flow
202202
# At this point, the app should guide the user to visit the auth ur (session["auth_flow"]["auth_uri"])
203203
```
204204

205-
The response from visiting the auth_uri endpoints is used in the [`acquire_token_by_auth_code_flow`](/python/api/msal/msal.application.clientapplication#msal-application-clientapplication-acquire-token-by-auth-code-flow) method. The state is a unique identifier that you can use to verify the response from the authorization server. The user should consent to scopes during sign-in.
205+
The response from visiting the auth uri endpoints is used in the [`acquire_token_by_auth_code_flow`](/python/api/msal/msal.application.clientapplication#msal-application-clientapplication-acquire-token-by-auth-code-flow) method. The state is a unique identifier that you can use to verify the response from the authorization server. The user should consent to scopes during sign-in.
206206

207207
```python
208208
# The uth_response value from visiting the auth_uri endpoint is passed as a query string
@@ -220,6 +220,6 @@ except ValueError: # Usually caused by CSRF
220220

221221
## MSAL Python token caching
222222

223-
Both public and confidential client applications support token caching, handled direct by MSAL Python. Applications should try to get a token from the cache first before relying on any other means. Take a look at the [recommended token acquisition pattern](/azure/active-directory/develop/scenario-desktop-acquire-token?tabs=python) to learn more.
223+
Both public and confidential client applications support token caching, handled direct by MSAL Python. Applications should try to get a token from the cache first before relying on any other means. For more information, see [recommended token acquisition pattern](/azure/active-directory/develop/scenario-desktop-acquire-token?tabs=python).
224224

225225
To be able to persist the cache, the developers need to configure the [token cache serialization](/azure/active-directory/develop/msal-python-token-cache-serialization) logic.

0 commit comments

Comments
 (0)