Skip to main content

Streamlit Authentication library based on MSAL.JS

Project description

OpenID Connect (OIDC) authentication component for Streamlit

About

This Streamlit component enables client-side authentication using Azure AD work and school accounts (AAD), Microsoft personal accounts (MSA) and social identity providers like Facebook, Google, LinkedIn, Microsoft accounts, etc. through Azure AD B2C service. The component is achieving this by applying the Microsoft MSAL JS Library inside of a React project. Since the component is based on MSAL, it can be configured to support any provider that supports the OpenID Connect Authorization Code Flow (PKCE). For more information on MSAL, consult the Github project and its offical documentation.

Usage

Below is a sample Python snippet displaying how to apply the component. Visually, the component gives rise to a single button in the Streamlit Dashboard with a text that depends on whether an active login session exists. The auth and cache parameters are entirely equivalent to the properties mentioned in the Github documentation. The login_request and logout_request parameters are covered here.

import streamlit as st
from msal_streamlit_t2 import msal_authentication


login_token = msal_authentication(
    auth={
        "clientId": "aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeee",
        "authority": "https://login.microsoftonline.com/aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeee",
        "redirectUri": "/",
        "postLogoutRedirectUri": "/"
    }, # Corresponds to the 'auth' configuration for an MSAL Instance
    cache={
        "cacheLocation": "sessionStorage", # or 'localStorage' for sharing sessions between tabs 
        "storeAuthStateInCookie": False
    }, # Corresponds to the 'cache' configuration for an MSAL Instance
    login_request={
        "scopes": ["aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeee/.default"]
    }, # Optional
    logout_request={}, # Optional
    login_button_text="Login", # Optional, defaults to "Login"
    logout_button_text="Logout", # Optional, defaults to "Logout"
    class_name="css_button_class_selector", # Optional, defaults to None. Corresponds to HTML class.
    html_id="html_id_for_button", # Optional, defaults to None. Corresponds to HTML id.
    key="your_streamlit_key" # Optional if only a single instance is needed
    hide_button=hide_button, # Optional, sometimes you can hide the button in some pages
    hide_button_text="loading...", # Optional, display some text when button is hidden
)
st.write("Recevied login token:", login_token)

or wrap it in a function and reuse it in other pages:

import streamlit as st
from msal_streamlit_t2 import msal_authentication


def app_msal(hide_button=True):
    return msal_authentication(
        auth={
            ..........


##page1.py:

app_msal()

##page2.py

app_msal(False)

A minimal sample project using the library can be found here. Note that it is Dockerized.

The component currently expects for the user to go through a popup based login flow. Further flows may be supported at a later time. As discussed here, the protocolMode parameter in auth can be used to configure OIDC providers that differ from Azure AD.

Inspiration

Inspired by official Streamlit template, this tutorial (Github) and the official Streamlit NPM component-lib. Speciall thanks to msal_streamlit_authentication, which is a project hasn't been updated for a long time. This library is basically an updated version of that.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msal_streamlit_t2-1.1.5.tar.gz (167.0 kB view details)

Uploaded Source

Built Distribution

msal_streamlit_t2-1.1.5-py3-none-any.whl (167.2 kB view details)

Uploaded Python 3

File details

Details for the file msal_streamlit_t2-1.1.5.tar.gz.

File metadata

  • Download URL: msal_streamlit_t2-1.1.5.tar.gz
  • Upload date:
  • Size: 167.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.9 Darwin/23.4.0

File hashes

Hashes for msal_streamlit_t2-1.1.5.tar.gz
Algorithm Hash digest
SHA256 9cbbf3709a192726929dd2ccc73cb63093568088cb006c35c61bbb514056a44b
MD5 81c140023c6bf3bb0c0a1f47d9f03788
BLAKE2b-256 85c02875101276bdb66bac8619719234336df324251cfcd7ce7505a89aa5f345

See more details on using hashes here.

File details

Details for the file msal_streamlit_t2-1.1.5-py3-none-any.whl.

File metadata

  • Download URL: msal_streamlit_t2-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 167.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.9 Darwin/23.4.0

File hashes

Hashes for msal_streamlit_t2-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e736684d45b074773ece166f7669c2c87e7bf1b0a4c81684c0c62c3ff2934271
MD5 de6d9383f0661c13ec5ad98e5bc1d552
BLAKE2b-256 5a62bfe9f29456c4b521b1f2c8e926c2a2052a69b9de1448b4e468839f3050f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page