You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+

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
+

86
+
87
+
2. In the top left corner, select **+** and create **Password** keyring.
@@ -50,6 +50,7 @@ An authentication broker is an application that runs on a userβs machine that
50
50
51
51
```python
52
52
pip install msal[broker]>=1.31,<2
53
+
pip install pymsalruntime
53
54
```
54
55
55
56
4. Once configured, you can call `acquire_token_interactive` to acquire a token.
@@ -69,7 +70,19 @@ The following parameters are available to configure broker support in MSAL Pytho
69
70
| enable_broker_on_wsl | `boolean` | This setting is only effective if your app is running on WSL. This parameter defaults to None, which means MSAL will not utilize a broker. </br></br>`New in MSAL Python 1.25.0`. |
70
71
| enable_broker_on_mac | `boolean` | This setting is only effective if your app is running on Mac with Company Portal installed. This parameter defaults to None, which means MSAL will not utilize a broker. </br></br>`New in MSAL Python 1.31.0`.|
71
72
| enable_broker_on_linux | `boolean` | This setting is only effective if your app is running on Linux with Intune installed. This parameter defaults to None, which means MSAL will not utilize a broker. </br></br>`New in MSAL Python 1.33.0`. |
72
-
| parent_window_handle | `int` | <i>OPTIONAL</i></br> - If your app does not opt in to use broker, you do not need to provide a parent_window_handle here.</br>- If your app opts in to use broker, parent_window_handle is required.</br>If your app is a GUI app running on Windows or Mac system, you are required to also provide its window handle, so that the sign-in window will pop up on top of your window.</br>- If your app is a console app running on Windows or Mac system, you can use a placeholder `PublicClientApplication.CONSOLE_WINDOW_HANDLE`|
The `parent_window_handle` parameter is required even though on Linux 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.
78
+
79
+
| Condition | Description |
80
+
|---|---|
81
+
|App does not want to utilize a broker|no need to specify a parent_window_handle|
82
+
|App opts to use a broker|parent_window_handle is required|
83
+
|App is a GUI app running on Windows or Mac system|required to provide its window handle, so that the sign-in window will pop up on top of your window|
84
+
|App is a console app running on Windows or Mac system|can use a placeholder `PublicClientApplication.CONSOLE_WINDOW_HANDLE`|
85
+
|App is intended to be a cross-platform application| App needs to use `enable_broker_on_windows`, as outlined in the [Using MSAL Python with Web Account Manager](wam.md) article.|
73
86
74
87
## The fallback behaviors of MSAL Pythonβs broker support
75
88
@@ -86,12 +99,112 @@ MSAL will either error out, or silently fallback to non-broker flows.
86
99
>[!IMPORTANT]
87
100
>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"`.
88
101
89
-
>[!IMPORTANT]
90
-
>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.
91
-
92
102
>[!NOTE]
93
103
>The `parent_window_handle` parameter is required even though on Linux 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.
94
104
95
105
## Token caching
96
106
97
-
The authentication broker handles refresh and access token caching. You do not need to set up custom caching.
107
+
The authentication broker handles refresh and access token caching. You do not need to set up custom caching.
108
+
109
+
## Building an example app
110
+
111
+
You can find a sample app that demonstrates how to use MSAL Python with the authentication broker on Linux in the [MSAL Python GitHub repository](https://github.com/AzureAD/microsoft-authentication-library-for-python/). The sample app is located in the `samples/console_app` directory and includes examples of how to use the broker for authentication.
112
+
113
+
### **App Registration**
114
+
115
+
Update your App registration in the Azure portal to include the broker-specific redirect URI for Linux:
0 commit comments