How wilianzilv/streamlit_msal works
Project Type and Purpose
• The project is a Python-based library designed to integrate Microsoft Authentication Library (MSAL) functionalities into Streamlit applications.
• Its purpose is to provide an easy-to-use authentication mechanism via pop-up windows, allowing Streamlit apps to sign in users using Azure AD credentials.
• The README clarifies two main usage paths: one that bundles pre-built UI components (initialize_ui) and another that lets developers design a custom UI (“headless” mode).
Analyzing the File Structure
• Top-Level Files:
• Files like app.py and auth.py indicate entry points and core authentication logic.
• README.md gives an overview and examples.
• The presence of pyproject.toml and requirements files shows dependency management.
• Directories:
• The "pages" directory (with subdirectories like auth, im_slow, and page) suggests modular page implementations or route handling within the Streamlit application.
• The "streamlit_msal" folder is the core library, with its “__init__.py”, and a nested "web" directory that contains assets, build files, and source code (e.g., Msal.tsx, index.tsx). This part is likely built with React (as indicated by package.json, tsconfig.json, and TypeScript file extensions), hinting at a frontend element for pop-up windows.
• Configuration files and build scripts (e.g., package-lock.json, tsconfig.json) indicate that the project incorporates both Python and JavaScript (React) components.
Key Architectural Components
a. Core Python Application:
• Entry Points: app.py and auth.py. These files initialize the application and handle the MSAL logic.
• Pages Module: The "pages" directory containing various submodules (authentication-related pages, slow pages, generic pages) represents different view components for the Streamlit app.
• The usage examples in the README (both UI and headless modes) show that developers interact with an API provided by this core module.
b. MSAL Authentication Module:
• The streamlit_msal package is responsible for integrating Microsoft Authentication Library (MSAL) functionalities.
• It handles interactions between the Streamlit UI and external authentication endpoints.
c. Frontend Component (React Based):
• Within streamlit_msal/web, note the presence of React components (e.g., Msal.tsx) and build outputs in the "build" directory (HTML, JS files).
• This indicates that a modern web technology (likely React) is used to manage the authentication pop-up or UI components that support the msal functionalities.
d. External Services:
• Azure Active Directory / Microsoft login endpoints form part of the external architecture. These are configured via the client_id, authority, and other parameters.
• The OIDC setup in Azure is a critical integration point, responsible for the actual authentication.
Relationships and Interactions
• The Streamlit application (running app.py and other pages) calls into the MSAL library (streamlit_msal) for handling authentication workflows.
• Depending on the UI approach selected (initialize_ui vs. headless), the application either renders a pre-built React-based authentication component from streamlit_msal/web or provides hooks for custom UI elements.
• Once an authentication operation is triggered (e.g., sign_in), the MSAL module communicates with Azure AD external services to perform the sign-in flow.
• After successful authentication, access tokens and account details flow back into the Streamlit app for usage.
Architectural Patterns and Design Principles
• Separation of Concerns: UI components (front-end) are separated from the core authentication logic (Python module), ensuring a clear division between presentation and business logic.
• Modular Design: The splitting into directories such as "pages" and "streamlit_msal/web" indicates modularity and potential for individual testing and extension.
• API/Service Abstraction: The MSAL functions (initialize, sign_in, etc.) provide clear interfaces for the Streamlit application to interact with external mechanisms (Azure AD).