Skip to content

Commit ba4b33e

Browse files
authored
Merge pull request #25 from MicrosoftDocs/docs-contrib
Update documentation
2 parents 8abc866 + c38b79d commit ba4b33e

16 files changed

+149
-84
lines changed

msal-python-conceptual/TOC.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
- name: Getting started
22
href: index.md
33
items:
4-
- name: Why use MSAL Python
5-
href: getting-started/why-use-msal-python.md
64
- name: Register your app with Azure AD
75
href: /azure/active-directory/develop/active-directory-integrating-applications
86
- name: Client applications
97
href: getting-started/client-applications.md
108
- name: Acquiring tokens
119
href: getting-started/acquiring-tokens.md
12-
- name: Calling a protected API
13-
href: getting-started/call-protected-web-api.md
1410

1511
- name: Advanced topics
1612
items:
13+
- name: Using MSAL Python with Web Account Manager
14+
href: advanced/wam.md
1715
- name: Migrate to MSAL Python
1816
href: /azure/active-directory/develop/migrate-python-adal-msal
1917
- name: Logging
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: Using MSAL Python with Web Account Manager
3+
description: "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."
4+
---
5+
6+
# Using MSAL Python with Web Account Manager
7+
8+
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).
9+
10+
>[!NOTE]
11+
>WAM is only available on Windows 10 and above, as well as Windows Server 2019 and above.
12+
13+
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.
14+
15+
## Usage
16+
17+
To use the broker, you will need to install the broker-related packages in addition to the core MSAL from PyPI:
18+
19+
```bash
20+
pip install msal[broker]>=1.20,<2
21+
```
22+
23+
>[!IMPORTANT]
24+
>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"`.
25+
26+
Next, you will need to instantiate a new [`PublicClientApplication`](xref:msal.application.PublicClientApplication) and set `allow_broker` to `True`. This will ensure that MSAL will try and communicate with WAM instead of popping up a new browser window.
27+
28+
```python
29+
from msal import PublicClientApplication
30+
31+
app = PublicClientApplication(
32+
"CLIENT_ID",
33+
authority="https://login.microsoftonline.com/common",
34+
allow_broker=True)
35+
```
36+
37+
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`:
38+
39+
```python
40+
result = app.acquire_token_interactive(["User.ReadBasic.All"],
41+
parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
42+
```
43+
44+
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.
45+
46+
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.
47+
48+
Before executing your application, make sure that you configure the redirect URL for the desktop app:
49+
50+
>[!IMPORTANT]
51+
>To use the Windows broker, your application needs to have the correct redirect URL configured in the Azure Portal, in the shape of:
52+
>
53+
>```bash
54+
>ms-appx-web://microsoft.aad.brokerplugin/YOUR_CLIENT_ID
55+
>```
56+
>
57+
>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`.
58+
59+
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.
60+
61+
![Example of WAM being called from Python](../media/wam-python.gif)
62+
63+
Worth noting that if you switch to using broker-based authentication, if the user was previously logged in and the signed-in state is still valid, calling [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive) will still result in a silent attempt to acquire a token, and only prompt when necessary. If you prefer to always prompt, you can use this optional parameter `prompt="select_account"`.
64+
65+
## Broker experience differences
66+
67+
Depending on the authority specified when instantiating [`PublicClientApplication`](xref:msal.application.PublicClientApplication), the broker user interface may be different.
68+
69+
### `/consumers`
70+
71+
Used for authenticating **only** with personal Microsoft accounts.
72+
73+
![WAM UI for consumers](../media/wam-consumers.png)
74+
75+
### `/common`
76+
77+
Used for authenticating with personal Microsoft accounts as well as work and school accounts.
78+
79+
![WAM UI for personal and work accounts](../media/wam-common.png)
80+
81+
### `/organizations`
82+
83+
Used for authenticating **only** with work and school accounts.
84+
85+
![WAM UI for work accounts only](../media/wam-organizations.png)
86+
87+
>[!NOTE]
88+
>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.
89+
90+
### `/TENANT_ID`
91+
92+
Used for authenticating **only** with work and school accounts within the specified tenant.
93+
94+
![WAM UI for tenant-specific accounts](../media/wam-tenant-specific.png)
95+
96+
>[!NOTE]
97+
>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.

msal-python-conceptual/docfx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
{
2626
"files": [
2727
"**/*.png",
28-
"**/*.jpg"
28+
"**/*.jpg",
29+
"**/*.gif"
2930
],
3031
"exclude": [
3132
"**/obj/**",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Before acquiring tokens with MSAL Python, make sure to instantiate a [client app
1313

1414
## Token acquisition methods
1515

16-
The approach to acquiring a token is different depending on the application type - public client applications (desktop and mobile) or confidential client application (web app, web API, or a daemon application like a Windows service). Each of the individual approaches is described below.
16+
The approach to acquiring a token is different depending on the application type - public client applications (desktop and mobile) or confidential client application (web app, web API, or a daemon application like a Windows service). Each of the individual approaches is described below.
1717

1818
### Public client applications
1919

msal-python-conceptual/getting-started/call-protected-web-api.md

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

msal-python-conceptual/getting-started/client-applications.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Before instantiating your app with MSAL Python:
1717
- The identity provider URL (the instance) and the sign-in audience for your application. These two parameters are collectively known as the **authority**.
1818
- If necessary, the tenant identifier (also a GUID) in case you are writing a line of business application scoped to just your organization (also known as a single-tenant application).
1919
- If you are building a confidential client app, you will need to create an application secret in the form of a string or certificate.
20-
- For web applications, you'll have also set the redirect URL that Azure AD will use to return the code.
20+
- For web applications, you'll have also set the redirect URL that Azure AD will use to return the code. For desktop applications you will need to add `http://localhost` if you're not relying on authentication brokers.
2121

2222
### Instantiating a public client application
2323

msal-python-conceptual/getting-started/why-use-msal-python.md

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

msal-python-conceptual/index.md

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,39 @@ pip install msal
2222

2323
## Setting up
2424

25-
Before using MSAL Python, make sure to [register your application](/azure/active-directory/develop/quickstart-v2-register-an-app) with the Microsoft identity platform.
25+
Before using MSAL Python, make sure to [register your application](/azure/active-directory/develop/quickstart-v2-register-an-app) with the Microsoft identity platform. You will need to take note of your **client ID** and **tenant ID**.
2626

27-
## Usage
27+
>[!IMPORTANT]
28+
>When registering the application, make sure that you set up **redirect URLs** within the **Authentication** blade. Redirect URLs vary depending on the target platform.
29+
>
30+
>![Screenshot showing redirect URLs in Azure Portal](./media/redirect-urls.png)
31+
>
32+
>For desktop and mobile applications, make sure you add `http://localhost` as redirect URL if you do not rely on authentication brokers.
33+
34+
## Basic usage
2835

2936
Acquiring tokens with MSAL Python follows a three-step pattern. There will be some variations for different flows. If you would like to see them in action, download our [samples](https://github.com/AzureAD/microsoft-authentication-library-for-python/tree/dev/sample).
3037

3138
1. MSAL relies on a clean separation between [public client and confidential client applications](https://tools.ietf.org/html/rfc6749#section-2.1). Therefore, create either a [`PublicClientApplication`](xref:msal.application.PublicClientApplication) or a [`ConfidentialClientApplication`](xref:msal.application.ConfidentialClientApplication) instance and reuse it during the lifecycle of your application. For example, for a public client application, the initalization code might look like this:
3239

33-
```python
34-
from msal import PublicClientApplication
35-
app = PublicClientApplication(
36-
"your_client_id",
37-
authority="https://login.microsoftonline.com/Enter_the_Tenant_Name_Here")
38-
```
40+
```python
41+
from msal import PublicClientApplication
3942

40-
Later, each time you would want an access token, you start by declaring a variable that will hold the token result:
43+
app = PublicClientApplication(
44+
"your_client_id",
45+
authority="https://login.microsoftonline.com/common")
46+
```
4147

42-
```python
43-
result = None # It is just an initial value. Please follow instructions below.
44-
```
48+
>[!NOTE]
49+
>The authority is set to `/common` to allow sign ins with both organizaiton and personal Microsoft accounts. You can change it to `/organizations` to only allow sign ins with work and school accounts, `/consumers` to only allow personal Microsoft accounts, or with `/YOUR_TENANT_ID` to only allow sign ins from work and school accounts associated with your tenant.
50+
51+
Instantiate a variable to hold the authentication result:
52+
53+
```python
54+
result = None # It is just an initial value. Please follow instructions below.
55+
```
4556

46-
2. The API model in MSAL provides you explicit control on how to utilize the token cache. While the caching part is technically optional, we highly recommend you to use it in your application. Using the cache you can ensure that you're not making any extra API calls and handle the token refresh automatically.
57+
2. Try and obtain the tokens from the cache first. The API model in MSAL provides you explicit control on how to utilize the token cache. While the caching part is technically optional, we highly recommend you to use it in your application. Using the cache you can ensure that you're not making any extra API calls and handle the token refresh automatically.
4758

4859
```python
4960
# We now check the cache to see
@@ -57,15 +68,15 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
5768
# Assuming the end user chose this one
5869
chosen = accounts[0]
5970
# Now let's try to find a token in cache for this account
60-
result = app.acquire_token_silent(["your_scope"], account=chosen)
71+
result = app.acquire_token_silent(["User.Read"], account=chosen)
6172
```
6273

63-
3. If there is no suitable token in the cache or you've chosen to skip the previous step, send a request to Azure AD to get a token. There are different methods based on your client type and scenario. A sample flow can look like this:
74+
3. If there is no suitable token in the cache or you've chosen to skip the previous step, send a request to Azure AD to get a token. There are different methods based on your client type and scenario, but for the purposes of the example we're showing how to use [`acquire_token_interactive`](xref:msal.application.PublicClientApplication.acquire_token_interactive) which will prompt the user to provide their credentials.
6475

6576
```python
6677
if not result:
6778
# So no suitable token exists in cache. Let's get a new one from Azure AD.
68-
result = app.acquire_token_by_one_of_the_actual_method(..., scopes=["User.Read"])
79+
result = app.acquire_token_interactive(scopes=["User.Read"])
6980
if "access_token" in result:
7081
print(result["access_token"]) # Yay!
7182
else:
@@ -74,6 +85,18 @@ Acquiring tokens with MSAL Python follows a three-step pattern. There will be so
7485
print(result.get("correlation_id")) # You may need this when reporting a bug
7586
```
7687

88+
4. Save the code into a Python file locally, such as `msaltest.py`.
89+
5. Run the code by executing `python .\msalpytest.py`.
90+
91+
>[!NOTE]
92+
>You can also download runnable samples from the [library repository](https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/1.22.0/sample/interactive_sample.py).
93+
94+
If the application was configured correctly, you should see a web browser window appear asking the user to sign in.
95+
96+
![Example of an app prompting the user to sign in with their account](./media/basic-pca-app-prompt.gif)
97+
98+
Once the authentication is completed and you closed the browser, you should be able to see the access token printed in the terminal.
99+
77100
## Usage scenarios
78101

79102
MSAL Python can be used by applications to acquire tokens to access protected APIs. Tokens can be acquired by different **application types**: desktop applications, web applications, web APIs, and applications running on devices that don't have a browser (such as IoT devices). In MSAL Python, applications are categorized as follows:
@@ -95,4 +118,8 @@ Key scenarios supported by MSAL Python:
95118
- [Desktop/service daemon application calling Web API without a user](/azure/active-directory/develop/scenario-daemon-overview)
96119
- [Application without a browser, or IOT application calling an API in the name of the user](/azure/active-directory/develop/scenario-desktop-acquire-token?tabs=python#command-line-tool-without-web-browser)
97120

98-
Can't find the scenario you are looking for? Check out the [supported scenarios and platforms](/azure/active-directory/develop/authentication-flows-app-scenarios#scenarios-and-supported-platforms-and-languages) across MSAL libraries.
121+
Can't find the scenario you are looking for? Check out the [supported scenarios and platforms](/azure/active-directory/develop/authentication-flows-app-scenarios#scenarios-and-supported-platforms-and-languages) across MSAL libraries.
122+
123+
## Releases
124+
125+
Refer to [MSAL Python releases on GitHub](https://github.com/AzureAD/microsoft-authentication-library-for-python/releases).
4.45 MB
Loading
98.2 KB
Loading

0 commit comments

Comments
 (0)