Skip to main content

streamlit-msal

Basic implementation of Microsoft Authentication Library (MSAL) for Streamlit

Disclaimer

This library is designed with simplicity and user-friendliness in mind, and as such, it may not encompass all possible functionalities. It primarily relies on pop-up windows for sign-in and revalidation of authentication processes.

While this approach provides a straightforward user experience, it may not be suitable for all use cases, especially those requiring more complex or robust authentication workflows. Please consider your specific requirements when deciding to use this library.

Installation

pip install streamlit-msal

Application Configuration (Azure)

Create an App with OpenID Connect (OIDC) based sign-on

Add a Single-page application platform and configure a redirect URI. http://localhost:8501 will work for Streamlit applications running locally.

Required information:

client_id = "<Application (client) ID>"
authority = "https://login.microsoftonline.com/<Directory (tenant) ID>"

UI Example (Easiest way)

This example uses "initialize_ui", that provides a UI with the core functionality in a simple but beautiful way.

import streamlit as st
from streamlit_msal import Msal

with st.sidebar:
    auth_data = Msal.initialize_ui(
        client_id=client_id,
        authority=authority,
        scopes=[], # Optional
        # Customize (Default values):
        connecting_label="Connecting",
        disconnected_label="Disconnected",
        sign_in_label="Sign in",
        sign_out_label="Sign out"
    )

if not auth_data:
    st.write("Authenticate to access protected content")
    st.stop()

st.write("Protected content available")

Headless example (Build your UI)

This example uses "initialize", that doesn't provide a UI so you can create your own.

from streamlit_msal import Msal

auth_data = Msal.initialize(
    client_id=client_id,
    authority=authority,
    scopes=[],
)

if st.button("Sign in"):
    Msal.sign_in() # Show popup to select account

if st.button("Sign out"):
    Msal.sign_out() # Clears auth_data

if st.button("Revalidate"):
    Msal.revalidate() # Usefull to refresh "accessToken"

Accessing result value

# Getting usefull information
access_token = auth_data["accessToken"]

account = auth_data["account"]
name = account["name"]
username = account["username"]
account_id = account["localAccountId"]


# Display information
st.write(f"Hello {name}!")
st.write(f"Your username is: {username}")
st.write(f"Your account id is: {account_id}")
st.write("Your access token is:")
st.code(access_token)

st.write("Auth data:")
st.json(auth_data)

Metadata

Release files for streamlit-msal 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-msal 0.2.0
File Size Uploaded
streamlit_msal-0.2.0.tar.gz 795.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-msal 0.2.0
File Interpreter ABI Platform
streamlit_msal-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / streamlit_msal-0.2.0.tar.gz

Download URL streamlit_msal-0.2.0.tar.gz
Size 795.6 kB
Tags Source
SHA-256 checksum
How to use checksums
06e96d3e4bac840003a557d9006c3055f5c45efd49d31d6e7371cfec3b6ef1b3
BLAKE2b-256 checksum
How to use checksums
2183dfabc48363be96a17dabaae9f80019567d52736ea1adeda6933c83d96462
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / streamlit_msal-0.2.0-py3-none-any.whl

Download URL streamlit_msal-0.2.0-py3-none-any.whl
Size 807.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67ee6e754feec42f17a77a5177f5c8fe8da567e1dff9cf85d462e5e25f26e5cf
BLAKE2b-256 checksum
How to use checksums
c2611eeb09b93e0b00512dc3d787ea3a4a5cd89b70fe5debeb066b6a86e8e0df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page