Streamlit MSAL (Microsoft Authentication Library) released

Hello everyone, i just released a MSAL integration for Streamlit, it’s pretty straight forward, there is two ways to use it:

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()

account = auth_data["account"]

name = account["name"]

st.write(f"Hello {name}!")
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"

Here is the full documentation:

9 Likes

Thank you for sharing the authentication library that allows us to customize the UI, umm, one question, is there any way we can put our customized redirectUrl?

I found that this integration is very sensitive and prone to errors when dealing with the redirectUrl, but well, i could add this option, but right now i can’t invest time in further development

Thank you for quick reply, looking forward to having this option in the future.

Whenever I try to make this work it says I require admin approval, and all I’m trying to access is the signed in users name and email. How can I make this work? :open_mouth:

Thanks for the library! I’ve been testing many alternatives, but this one got me further. However, I’m stuck because while the login popup and authentication work, Streamlit is not updating. When the popup closes, I see the same page. I am quite new to this, any idea or suggestion would be helpful!

st.write("Protected content available")

so the code above is not executed even though the login looks successful.