Skip to content

Commit 82498e7

Browse files
Merge pull request #125 from MicrosoftDocs/main
Merge main into live
2 parents 953875e + 4922745 commit 82498e7

30 files changed

+433
-20
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
# Any repo changes beyond docs require PM owner approval.
2-
* @localden @jmprieur
3-
4-
# Conceptual and API content needs the review of the engineering team.
5-
msal-python-conceptual/* @MicrosoftDocs/identity-sdk-cca-engineering-team
1+
@MicrosoftDocs/identity-sdk-cca-engineering-team
62

73
# API documentation does not have code owners, but that is OK.
84
# Anyone can create a PR to the `main` branch and we will review
95
# the changes on the one-off basis.
10-
python/
6+
python/

msal-python-conceptual/TOC.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
items:
1212
- name: Using MSAL Python with Web Account Manager
1313
href: advanced/wam.md
14-
- name: Using MSAL Python with Authentication Brokers on macOS
14+
- name: Using MSAL Python with Auth Brokers on macOS
1515
href: advanced/macos-broker.md
16+
- name: Using MSAL Python with an Auth Broker on Linux
17+
href: advanced/linux-broker-py.md
18+
- name: Using MSAL Python with Windows Subsystem for Linux
19+
href: advanced/linux-broker-py-wsl.md
1620
- name: Migrate to MSAL Python
1721
href: advanced/migrate-python-adal-msal.md
1822
- name: Logging

msal-python-conceptual/advanced/aad-b2c.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ ms.topic: conceptual
1010
ms.date: 02/07/2024
1111
ms.author: dmwendia
1212
ms.reviewer: shermanouko, rayluo
13+
#Customer intent:
1314
---
1415

1516
# Use MSAL Python to work with Azure AD B2C
1617

17-
1818
You can use MSAL Python to sign-in users with social identities, acquire tokens, and customize the sign-in experience by using [Azure AD B2C](https://aka.ms/aadb2c).
1919

20+
> [!IMPORTANT]
21+
> Effective May 1, 2025, Azure AD B2C will no longer be available to purchase for new customers. To learn more, please see [Is Azure AD B2C still available to purchase?](/azure/active-directory-b2c/faq?tabs=app-reg-ga#azure-ad-b2c-end-of-sale) in our FAQ.
22+
2023
Azure AD B2C is built around the notion of [User Flows](/azure/active-directory-b2c/active-directory-b2c-reference-policies) (formerly known as policies). In MSAL Python, specifying a user flow translates to providing an authority.
2124

2225
* When you instantiate the client application, you need to specify the user flow in authority as

msal-python-conceptual/advanced/client-capabilities.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ms.topic: conceptual
1010
ms.date: 02/07/2024
1111
ms.author: dmwendia
1212
ms.reviewer: shermanouko, rayluo
13+
#Customer intent:
1314
---
1415

1516
# Client capabilities

msal-python-conceptual/advanced/client-credentials.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ title: Client credentials
33
description: There are two types of client credentials in MSAL Python - application secrets and certificates.
44
author: Dickson-Mwendia
55
manager: CelesteDG
6-
76
ms.service: msal
87
ms.subservice: msal-python
98
ms.topic: conceptual
109
ms.date: 02/07/2024
1110
ms.author: dmwendia
1211
ms.reviewer: shermanouko, rayluo
12+
ms.custom: sfi-image-nochange
13+
#Customer intent:
1314
---
1415

1516
# Client credentials

msal-python-conceptual/advanced/conditional-access.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ms.topic: conceptual
1010
ms.date: 02/07/2024
1111
ms.author: dmwendia
1212
ms.reviewer: shermanouko, rayluo
13+
#Customer intent:
1314
---
1415

1516
# Conditional access and claims challenges

msal-python-conceptual/advanced/instance-metadata-caching.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ms.topic: conceptual
1010
ms.date: 02/07/2024
1111
ms.author: dmwendia
1212
ms.reviewer: shermanouko, rayluo
13+
#Customer intent:
1314
---
1415

1516
# Instance metadata caching
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
title: Using MSAL Python with Windows Subsystem for Linux
3+
description: Learn how to integrate Microsoft Entra ID authentication in WSL apps using MSAL Python and the Microsoft Single Sign-on for Linux broker.
4+
author: ploegert
5+
ms.author: jploegert
6+
ms.service: msal
7+
ms.topic: how-to
8+
ms.date: 05/08/2025
9+
---
10+
11+
# Enable SSO in WSL (Windows Subsystem for Linux) apps using MSAL Python and WAM
12+
13+
MSAL is able to call the Microsoft Single Sign-on to Linux, a Linux component that is shipped independent of the Linux Distribution, however it gets installed using a package manager using `sudo apt install microsoft-identity-broker` or `sudo dnf install microsoft-identity-broker`.
14+
15+
This component acts as an authentication broker allowing the users of your app to benefit from integration with accounts known to Linux, such as the account you signed into your Linux sessions for apps that consume from the broker. It's also bundled as a dependency of applications developed by Microsoft, such as [Company Portal](/mem/intune-service/user-help/enroll-device-linux). These applications are installed when a Linux computer is enrolled in a company's device fleet via an endpoint management solution like [Microsoft Intune](/mem/intune/fundamentals/what-is-intune).
16+
17+
Using an authentication broker on Linux enables you to simplify how your users authenticate with Microsoft Entra ID from your application, and take advantage of future functionality that protects Microsoft Entra ID refresh tokens from exfiltration and misuse.
18+
19+
To enable SSO in your WSL app using MSAL Python, you must ensure the keychain is set up and unlocked, as MSAL uses `libsecret` to communicate with the keyring daemon.
20+
21+
## WSL Authentication Flow Example
22+
23+
In a situation where you have a WSL app that needs to authenticate with Microsoft Entra ID, the authentication flow for an interactive request would look like this:
24+
25+
![Auth Flow from within WSL](../media/python-msal-wsl.gif)
26+
27+
## Update to the latest version of WSL
28+
29+
Ensure you have updated to the latest WSL release. The WAM Account Control dialog is supported in WSL versions 2.4.13 and above.
30+
31+
```powershell
32+
# To check what distros are available:
33+
wsl.exe --list --online
34+
35+
wsl.exe --install Ubuntu-22.04
36+
37+
# To check the WSL version:
38+
wsl --version
39+
40+
# To update WSL:
41+
wsl --update
42+
```
43+
44+
## Linux Package Dependencies
45+
46+
Install the following dependencies on your Linux platform:
47+
48+
- `libsecret-tools` is required to interface with the Linux keychain
49+
50+
### [Ubuntu](#tab/ubuntudep)
51+
52+
To install on debian/Ubuntu based Linux distribution:
53+
54+
```bash
55+
sudo apt install libsecret-1-0 -y
56+
57+
#from Powershell, run
58+
wsl.exe --shutdown
59+
```
60+
61+
### [Red Hat Enterprise Linux](#tab/rheldep)
62+
63+
To install on Red Hat/Fedora based Linux distribution:
64+
65+
```bash
66+
sudo dnf install libsecret-1-0 -y
67+
68+
#from Powershell, run
69+
wsl.exe --shutdown
70+
```
71+
72+
---
73+
74+
> [!IMPORTANT]
75+
> In order for the keychain to work as intended, you should make sure you 1. install the dependencies, 2. Reboot/restart wsl, 3. Configure the keychain. Failure to do the steps in the correct order will result with the keychain missing the option for "Password Keychain".
76+
77+
## Set up Keyring in WSL
78+
79+
MSAL uses `libsecret` on Linux. It's required to communicate with the `keyring` daemon. Users can use [Seahorse](https://wiki.gnome.org/Apps/Seahorse/) (a GNOME application for managing encryption keys and passwords) to manage the `keyring` contents through a Graphical User Interface (GUI).
80+
81+
On Debian-based distributions, you can install the package by running `sudo apt install seahorse` and then following these instructions:
82+
83+
1. Run `seahorse` in the terminal as a regular user (not as sudo)
84+
85+
![default keychain dialog](../media/wsl1.png)
86+
87+
2. In the top left corner, select **+** and create **Password** keyring.
88+
89+
![keychain dialog selecting password keyring](../media/wsl2.png)
90+
91+
3. Create a keyring named 'login'
92+
93+
![typing login to the prompt](../media/wsl3.png)
94+
95+
4. Set the password on the next dialog.
96+
![selecting a password and confirming](../media/wsl4.png)
97+
98+
5. Run `wsl.exe --shutdown` from your Windows Terminal.
99+
100+
6. Start a new WSL session and run the sample. You should be asked for the keyring password.
101+
102+
## Run a Sample App
103+
104+
To use a broker on the Linux platform, make sure you set the `BrokerOptions` to `OperatingSystems.Linux` as shown in the below code snippet:
105+
106+
Reference the [Enable SSO in native Linux apps using MSAL Python](./linux-broker-py.md) for information of how to configure the project.
107+
108+
### **Python Dependencies**
109+
110+
To use the broker, you will need to install the broker-related packages in addition to the core MSAL from PyPI:
111+
112+
```python
113+
pip install msal[broker]>=1.31,<2
114+
pip install pymsalruntime
115+
```
116+
117+
### Run the Sample App
118+
119+
Once configured, you can call `acquire_token_interactive` to acquire a token. Save the following as `wsl_broker.py`:
120+
121+
```python
122+
import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1]
123+
import json
124+
import logging
125+
import requests
126+
import msal
127+
128+
# Optional logging
129+
# logging.basicConfig(level=logging.DEBUG)
130+
131+
var_authority = "https://login.microsoftonline.com/common"
132+
var_client_id = " your-client-id-here" # Replace with your app's client ID
133+
var_username = "your-username-here" # Replace with your username, e.g., "
134+
var_scope = ["User.ReadBasic.All"]
135+
136+
# Create a preferably long-lived app instance which maintains a token cache (Default cache is in memory only).
137+
app = msal.PublicClientApplication(
138+
var_client_id,
139+
authority=var_authority,
140+
enable_broker_on_windows=True,
141+
enable_broker_on_wsl=True
142+
)
143+
144+
# The pattern to acquire a token looks like this.
145+
result = None
146+
147+
# Firstly, check the cache to see if this end user has signed in before
148+
accounts = app.get_accounts(username=var_username)
149+
if accounts:
150+
logging.info("Account(s) exists in cache, probably with token too. Let's try.")
151+
result = app.acquire_token_silent(var_scope, account=accounts[0])
152+
153+
if not result:
154+
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
155+
156+
result = app.acquire_token_interactive(var_scope,parent_window_handle=app.CONSOLE_WINDOW_HANDLE)
157+
158+
if "access_token" in result:
159+
print("Access token is: %s" % result['access_token'])
160+
161+
else:
162+
print(result.get("error"))
163+
print(result.get("error_description"))
164+
print(result.get("correlation_id")) # You may need this when reporting a bug
165+
if 65001 in result.get("error_codes", []): # Not mean to be coded programatically, but...
166+
# AAD requires user consent for U/P flow
167+
print("Visit this to consent:", app.get_authorization_request_url(config["scope"]))
168+
```
169+
170+
### Run the Sample
171+
172+
Run the sample app using the following command:
173+
174+
```bash
175+
python wsl_broker.py
176+
```
177+
178+
You should see a prompt to:
179+
180+
- enter your username/credentials
181+
- enter your keyring password
182+
- then the app will acquire a token and print it to the console

0 commit comments

Comments
 (0)