Skip to main content

Lightweight RBAC library for Streamlit applications

Project description

streamlit-rbac

CI PyPI Python License

Lightweight Role-Based Access Control (RBAC) library for Streamlit applications.

Inspired by Spring Security's authorization API, streamlit-rbac provides a thin, declarative layer for role-based access control — without pulling in authentication, user management, or any heavyweight framework.


✨ Features

  • Pure function corehas_role(), has_any_role(), has_all_roles() are stateless and side-effect-free, making them trivial to unit test.
  • Declarative decorator@require_roles() guards functions with a single line.
  • Streamlit integrationauthorize_page() stops unauthorized page rendering via st.stop().
  • Bring your own auth — inject any role_loader function to resolve roles from IdP tokens, session state, databases, or anything else.
  • Zero required dependencies — core functions use only the Python standard library. Streamlit is an optional dependency.

🏗️ Architecture

+---------------------------------------------+
|          Streamlit Integration               |  authorize_page()
|          (optional dependency)               |
+---------------------------------------------+
|          Decorator Layer                     |  @require_roles()
+---------------------------------------------+
|          Core Functions                      |  has_role()
|          (pure, zero dependencies)           |  has_any_role()
|                                              |  has_all_roles()
+---------------------------------------------+

Each layer depends only on the one below it. Core functions work anywhere — no Streamlit required.


📦 Installation

pip install streamlit-rbac

# With Streamlit integration
pip install streamlit-rbac[streamlit]

Requires Python 3.11+


🚀 Quick Start

Pure functions (great for testing)

from streamlit_rbac import has_role, has_any_role, has_all_roles

# Single role check
has_role("Admin", user_roles=["Admin", "User"])  # True
has_role("Admin", user_roles=["User"])            # False

# Any of these roles (OR)
has_any_role("Admin", "Manager", user_roles=["Manager"])  # True

# All of these roles (AND)
has_all_roles("Admin", "Auditor", user_roles=["Admin", "Auditor"])  # True
has_all_roles("Admin", "Auditor", user_roles=["Admin"])              # False

Role loader (deferred evaluation)

Instead of passing roles directly, supply a callable that returns them:

from streamlit_rbac import has_role

def get_user_roles() -> list[str]:
    # Fetch from IdP, database, session, etc.
    return ["Admin", "User"]

has_role("Admin", role_loader=get_user_roles)  # True

user_roles and role_loader are mutually exclusive — pass one or the other, never both.

Decorator

from streamlit_rbac import require_roles

@require_roles("Admin", role_loader=get_user_roles)
def delete_user(user_id: str) -> None:
    ...

Raises PermissionError if the user lacks the required role. An optional on_denied callback runs before the error:

@require_roles("Admin", role_loader=get_user_roles, on_denied=log_violation)
def delete_user(user_id: str) -> None:
    ...

Streamlit page guard

import streamlit as st
from streamlit_rbac import authorize_page

def get_user_roles() -> list[str]:
    claims = st.session_state.get("token_claims", {})
    return claims.get("roles", [])

# Place at the top of each page script
authorize_page("Admin", role_loader=get_user_roles)

st.title("Admin Page")  # Only rendered if authorized

If the user lacks the required role, authorize_page displays an error message and calls st.stop() — nothing below it executes.


🎛️ Component-Level Control

Mix page guards with inline checks for fine-grained control:

from streamlit_rbac import authorize_page, has_role

authorize_page("User", "Admin", role_loader=get_user_roles)
st.title("Dashboard")

# Only SuperAdmins see this section
if has_role("SuperAdmin", role_loader=get_user_roles):
    with st.expander("System Settings"):
        st.write("Dangerous operations...")

📖 API Reference

Core Functions

Function Description
has_role(required, *, user_roles, role_loader) Single role check
has_any_role(*required, *, user_roles, role_loader) Any of the given roles (OR)
has_all_roles(*required, *, user_roles, role_loader) All of the given roles (AND)

All return bool. All raise ValueError if both or neither of user_roles / role_loader are provided.

Edge cases: has_any_role() with no required roles returns False. has_all_roles() with no required roles returns True (vacuous truth).

Decorator API

Function Description
@require_roles(*roles, *, user_roles, role_loader, on_denied) Guard a function (OR logic). Raises PermissionError on denial.

Streamlit Integration

Function Description
authorize_page(*roles, *, role_loader, login_url, denied_message) Page-level guard. Calls st.stop() on denial.

Types

Type Definition
RoleLoader Callable[[], Iterable[str]]
OnDeniedHandler Callable[[], None]

💡 Design Principles

Principle Detail
Authorization only No authentication, no user management, no database schemas. This library answers one question: does this user have the required role?
Developer stays in control Role resolution is entirely your responsibility via role_loader. The library never dictates where roles come from.
Pure core, optional integration Core functions have zero dependencies. Streamlit is only imported when you use authorize_page().

📄 License

MIT

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

streamlit_rbac-0.1.0.tar.gz (117.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_rbac-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_rbac-0.1.0.tar.gz.

File metadata

  • Download URL: streamlit_rbac-0.1.0.tar.gz
  • Upload date:
  • Size: 117.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for streamlit_rbac-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa298ddd325a5815d8930cb479dd0451c06751d60b2f694f3081b70e635d12b6
MD5 a8010f3c5abef1855c53adfec7f6c63d
BLAKE2b-256 9902ef82dc348819b7ab95b049f5589b276943a9a086a5ea94d7824ca82ba554

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_rbac-0.1.0.tar.gz:

Publisher: release.yml on izuno4t/streamlit-rbac

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file streamlit_rbac-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: streamlit_rbac-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for streamlit_rbac-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97b465fcb73b86ac1056a6cde6acdf0eba885f9b8c03ef430b82caf9127ce60c
MD5 b4f3f3a684599670a88ec9708fcb5a36
BLAKE2b-256 04c0c687016b12771c0206073327fbaa03fd1de738105a28ea55a9f43ceadf26

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_rbac-0.1.0-py3-none-any.whl:

Publisher: release.yml on izuno4t/streamlit-rbac

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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