Skip to main content

role-capabilities

App-agnostic, capability-based roles for Python apps. Declare your own closed set of capabilities and your own role → capability bundles; resolve a principal's effective role from a seed plus append-only overrides; and check has_capability(actor, capability) anywhere — inside a web request or a plain CLI command.

Extracted from Rulebook's RBAC (design and scope: ecoop/rulebook#220). Rulebook is the first consumer. The core depends on no identity library — guest-auth integrates as an optional adapter, so an app can key roles by whatever stable principal it already has.

Status: 0.1.0. The engine is all here — capabilities, role ordering, resolution over a pluggable RoleStore (in-memory + GCS), the role-admin read/write surface, and the optional FastAPI + guest-auth adapters. In use by Rulebook, its first consumer. The API may still shift until 1.0 (see CHANGELOG).

Usage — the capability engine

from role_capabilities import CapabilityModel

MODEL = CapabilityModel(
    capabilities={"read", "write", "admin"},
    roles={
        "guest":  frozenset(),
        "member": {"read", "write"},
        "owner":  {"read", "write", "admin"},
    },
    default_role="member",
    aliases={"legacy_admin": "owner"},  # old ids keep resolving after a rename
)

MODEL.has_capability("member", "write")   # True
MODEL.has_capability("member", "admin")   # False
MODEL.capabilities_for("wizard")          # frozenset()  — unknown role fails closed
MODEL.canonical_role("legacy_admin")      # "owner"

MODEL.ordered_roles()                     # ("guest", "member", "owner")  — low → high
MODEL.role_order("owner")                 # 2   (-1 for an unranked role)

Roles order as declared, low → high; pass order=[...] to set the ladder explicitly if your roles dict isn't already in ladder order. Ordering is a presentational/sort axis only — no authorization check reads it — and names, colors, and descriptions stay in the app.

Usage — resolving a principal's role

from role_capabilities import RoleResolver, MemoryRoleStore, GcsRoleStore

resolver = RoleResolver(
    model=MODEL,
    store=MemoryRoleStore(),                    # or GcsRoleStore("bucket", "roles.jsonl")
    seed={"alice": "owner"},                    # static baseline, keyed by principal
)
resolver.assert_seeded("owner")                 # bootstrap guard — fail closed at startup

resolver.resolve("alice")                       # "owner"  (seed)
resolver.resolve("bob")                         # "member" (default)
resolver.has_capability("bob", "admin")         # False

resolver.set_role("bob", "owner", actor="alice")  # audited override; visible at once
resolver.resolve("bob")                         # "owner"
resolver.reset_role("bob", actor="alice")       # back to seed/default

resolver.assignments()                          # {principal: role} for everyone with an assignment
resolver.roster(["alice", "bob", "carol"])      # resolve each, defaults included — for an admin table

Role administration lives here — change/reset, and the assignments / roster read helpers. Identity lifecycle (adding, removing, or renaming a user) is your identity layer's job, not this library's; supply your user list to roster to show everyone.

Usage — FastAPI + guest-auth (optional adapters)

The core is framework-free. Two optional adapters (their own modules, so the core imports need no extra dependency) wire it into a FastAPI app authenticated by guest-auth:

from fastapi import Depends
from role_capabilities.fastapi_dep import make_require_capability
from role_capabilities.guest_auth_adapter import guest_principal_provider

require_capability = make_require_capability(
    resolver,
    principal_provider=guest_principal_provider(),   # or lambda: g.recipient to key by a stable id
    gate_enabled=lambda: settings.demo_mode,         # off → public tier only (fails closed)
    public_role="beginner",
)

@app.get("/golds", dependencies=[Depends(require_capability("golds.view"))])
def golds(): ...

make_require_capability needs the [fastapi] extra; guest_principal_provider needs [guest-auth]. On a different stack, supply your own principal_provider callable and skip both.

Resolution is override ▸ seed ▸ default. Overrides are an append-only log (latest row wins, reset clears), read through a RoleStore and TTL-cached; a backend read failure reuses the last good snapshot rather than failing the check. Everything is keyed by an app-supplied principal id and the checks take it explicitly, so the resolver works in a web request or a plain CLI / batch job.

Construction validates the declaration (every bundle stays within the closed set; default_role and alias targets must be real roles) and raises ValueError on a malformed one; queries fail closed.

Install

pip install role-capabilities                 # core (pure stdlib)
pip install "role-capabilities[guest-auth]"   # + guest-auth current-actor adapter
pip install "role-capabilities[gcs]"          # + GCS RoleStore backend
pip install "role-capabilities[fastapi]"      # + require_capability dependency

Design

  • App-defined vocabulary — no built-in capability names; you declare the closed set and the role → capability bundles.
  • Keyed by a stable principal, not a token — rotating a token never drops a role.
  • Pluggable persistence — a small RoleStore (read all rows / append a row) with GCS and SQL backends; seed + append-only overrides, latest row wins, a reset row falls back to the seed, TTL-cached reads.
  • Works with no web request — the core check takes the actor explicitly, so it runs in a CLI or a Cloud Run job; a thin FastAPI require_capability sits on top.
  • Fail-closed — unknown principal → the default role; unresolvable → the public tier; refuses to start with no superuser seeded.

See ecoop/rulebook#220 for the full extraction plan.

Last updated: 2026-09-30

Metadata

Release files for role-capabilities 0.1.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 role-capabilities 0.1.0
File Size Uploaded Provenance Actions
role_capabilities-0.1.0.tar.gz 70.0 kB Without provenance Details for role_capabilities-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for role-capabilities 0.1.0
File Interpreter ABI Platform Actions
role_capabilities-0.1.0-py3-none-any.whl Python 3 none any Details for role_capabilities-0.1.0-py3-none-any.whl

Total release size: 86.0 kB

Release files / role_capabilities-0.1.0.tar.gz

Download URL role_capabilities-0.1.0.tar.gz
Size 70.0 kB
Tags Source
SHA-256 checksum
How to use checksums
61db07798dd517cce88c0fb4e62e659b39868aafb25dbbdbf68b397fd8b83413
BLAKE2b-256 checksum
How to use checksums
43db8529429b19ea1fc7fabf91b480fe227ad4000ef02b7ba473680101c1ae3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Release files / role_capabilities-0.1.0-py3-none-any.whl

Download URL role_capabilities-0.1.0-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8741dbd80facee518644598f158929207617e13bb68394cde05d09e6c91f621
BLAKE2b-256 checksum
How to use checksums
e85b98d9841fb3c786c103f0e4ca792d02d0dd327fa96b21573289992651ecda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Security for role-capabilities 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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