Skip to main content

policy-pattern

A small, embeddable ReBAC authorization engine for Python, inspired by Google Zanzibar.

Test Pipeline Lint Pipeline Coverage Pipeline Package Version Supported Python Versions

policy-pattern is a standalone Python library for relationship-based access control (ReBAC). It models authorization as relationships between subjects and objects and evaluates those relationships through composable authorization rules.

  • No authorization server
  • No required framework
  • No required database
  • No network hop

The library is designed to be embedded directly into Python applications that need richer authorization semantics than traditional role-based access control.

Status: v1 is code-complete — model, compile(), the storage port, MemoryTupleStore, and the Evaluator all exist and are tested.

See Architecture § Scope for what's planned (v1.x/v2) and unscheduled ideas.


Table of Contents


Example

from policy_pattern.adapters.memory import MemoryTupleStore
from policy_pattern.evaluator import Evaluator
from policy_pattern.models import (
    DirectSubject,
    Model,
    Mutation,
    ObjectReference,
    Reference,
    Relation,
    Tuple,
    TupleToUserset,
    Type,
    Union,
)

model = Model()
model.types["folder"] = Type(
    name="folder",
    relations={"owner": Relation(allowed_subject_types=("user",))},
    permissions={"viewer": Reference(name="owner")},
)
model.types["document"] = Type(
    name="document",
    relations={
        "owner": Relation(allowed_subject_types=("user",)),
        "parent": Relation(allowed_subject_types=("folder",)),
    },
    permissions={
        "viewer": Union(left=Reference(name="owner"), right=TupleToUserset(tupleset="parent", computed="viewer")),
    },
)
compiled = model.compile()

store = MemoryTupleStore()
alice = DirectSubject(object=ObjectReference(type="user", id="alice"))
engineering = ObjectReference(type="folder", id="engineering")
document_42 = ObjectReference(type="document", id="42")

store.write_tuples(
    Mutation(
        additions=frozenset(
            {
                Tuple(subject=alice, relation="owner", object=engineering),
                Tuple(subject=DirectSubject(object=engineering), relation="parent", object=document_42),
            }
        ),
        deletions=frozenset(),
    )
)

evaluator = Evaluator(model=compiled, store=store)
evaluator.check(alice, "viewer", document_42)  # True — alice owns the parent folder

viewer here means "the document's own owner, or the owner of whatever folder it lives in" — a document under folder:engineering inherits access from that folder without copying a permission onto every document.

See the Usage Guide for the full, runnable walkthrough — every rewrite operator, nested groups, EvaluationBudget, and error handling.


Documentation

The repository uses specifications and architecture documents as part of the implementation contract — they describe what must be true, independent of any one adapter's implementation.

docs/
├── README.md        — documentation index, start here
├── usage/README.md  — worked examples for every rewrite operator, budgets, error handling
├── architecture.md  — the model shape, compile(), the storage port, the evaluator, and scope/roadmap
├── semantics.md     — the storage contract every TupleStore adapter must satisfy
└── errors.md        — the PPxxx error code ranges and what each one means

Start with the Usage Guide to see the library working end to end, then Architecture to understand why the model is shaped the way it is.

See CODE_STYLE.md and TESTING.md for contributor conventions no tool enforces.


Contributing

We love community help! Before you open an issue or pull request, please read:


License

Licensed under the Apache License 2.0.


References

The design is influenced by authorization systems and literature including:

policy-pattern is an independent project and is not an implementation of, or affiliated with, those projects.

Metadata

Release files for policy-pattern 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for policy-pattern 0.4.1
File Size Uploaded Provenance Actions
policy_pattern-0.4.1.tar.gz 35.3 kB With provenance Details for policy_pattern-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for policy-pattern 0.4.1
File Interpreter ABI Platform Actions
policy_pattern-0.4.1-py3-none-any.whl Python 3 none any Details for policy_pattern-0.4.1-py3-none-any.whl

Total release size: 80.3 kB

Release files / policy_pattern-0.4.1.tar.gz

Download URL policy_pattern-0.4.1.tar.gz
Size 35.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5aa70e18a08d5d1925e84ce524445031670595d804b3a8c16b6ae1dd49a3ae69
BLAKE2b-256 checksum
How to use checksums
ef5aedc40af2b24c2ca476c300b7a73d5e805214d90172e51b871ffca84d82a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Release files / policy_pattern-0.4.1-py3-none-any.whl

Download URL policy_pattern-0.4.1-py3-none-any.whl
Size 45.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c1f7464bb90ada9772175ce4a9cbfa0014cda46c41714222a0fef21a0b2544f
BLAKE2b-256 checksum
How to use checksums
756637ec9ceb778d51a1b23601e47ff6a09e646e40b85042ca2e7fafe254b1f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Security for policy-pattern 0.4.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for policy_pattern-0.4.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

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.

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