Skip to main content

django-scoped-access

PyPI version Python Versions Django Versions Documentation Status License: MIT

Hierarchical Scoped Authorization (ABAC) + RBAC + Temporal Lifecycle + Step-Up Re-Authentication for Django & DRF.
Reference implementation of the Scoped Access Specification.


📖 Overview

django-scoped-access brings fine-grained, hierarchical multi-tenant access control to Django without polluting your business models or hardcoding organizational trees into database schemas.

Whether building a single-tenant app (flat RBAC), a multi-tenant B2B SaaS (Organization → Team), or a complex organizational system (National → Regional → District → Facility → Department), the hierarchy is pure configuration.

                   ┌──────────────────────────────┐
                   │           NATIONAL           │ (Root level)
                   └──────────────┬───────────────┘
                                  │
                   ┌──────────────▼──────────────┐
                   │           REGIONAL          │
                   └──────────────┬───────────────┘
                                  │
                   ┌──────────────▼──────────────┐
                   │           DISTRICT          │
                   └──────────────┬───────────────┘
                                  │
                   ┌──────────────▼──────────────┐
                   │           FACILITY          │ (Can manage custom roles)
                   └─────────────────────────────┘

✨ Key Features

  • 🌲 Hierarchy as Configuration, Not Schema: Configure any arbitrary tree depth or degrade to standard RBAC at depth 0.
  • 🎯 Domain Agnostic: The engine never owns your models. Hierarchy nodes and role owners link via generic relations (GenericForeignKey).
  • 🔒 Inclusive Downward Scope Coverage: A role grant at REGIONAL covers all descendant nodes (Districts, Facilities, Units) in that subtree, and never upwards.
  • 🛡️ Anti-Escalation Protection (Rule R5): Tenant admins can manage custom roles within their scope, but can never grant permissions they do not possess themselves.
  • ⏱️ Temporal Validity & Audit Trail: Assignments support valid_from / valid_until windows and status transitions (ACTIVE ⇄ SUSPENDED → REVOKED). Assignments are never hard-deleted.
  • 🔐 Step-Up Re-Authentication (ReAuth): Require fresh, single-use proof of identity (passwords, PIN, WebAuthn, TOTP) for high-risk actions. Superusers are not exempt.
  • ⚡ Database-Level Query Filtering: SQL-level filtering (scope_filter_q()) for collection views—no in-memory Python iteration.
  • 🚀 Full Django REST Framework (DRF) Integration: Drop-in permissions (ScopedModelPermission, ScopeObjectPermission), query mixins (ScopeQuerySetMixin), write guards (ScopeWriteGuardMixin), and introspection endpoints (GET /me/access/).

⚡ Quickstart (5 Minutes)

1. Installation

# With pip
pip install "django-scoped-access[drf]"

# With uv
uv add "django-scoped-access[drf]"

2. Configuration (settings.py)

INSTALLED_APPS = [
    ...,
    "scoped_access",
    "rest_framework",
]

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "scoped_access.backends.ScopedPermissionBackend",
]

SCOPED_ACCESS = {
    "HIERARCHY": [
        {"level": "GLOBAL"},
        {"level": "ORGANIZATION", "model": "accounts.Organization"},
        {"level": "TEAM", "model": "accounts.Team", "parent": "organization"},
    ],
    "ROLE_OWNER_LEVELS": ["ORGANIZATION"],
    "GRANTABLE_PERMISSIONS": "self",
    "REAUTH": {"ENABLED": True, "TTL": 300},
}

Apply database migrations:

python manage.py migrate

3. Register Resource Anchors

Declare the path from your business models to their hierarchy node:

# helpdesk/apps.py
from django.apps import AppConfig

class HelpdeskConfig(AppConfig):
    name = "helpdesk"

    def ready(self):
        from scoped_access import register
        from helpdesk.models import Ticket

        # A Ticket is anchored to a Team
        register(Ticket, anchor="team")

4. Create Roles & Grant Assignments

from scoped_access import RoleService, ScopeAssignment
from helpdesk.models import Team

support_team = Team.objects.get(name="Support")

# 1. Create a System Role
agent_role = RoleService.create(
    name="Support Agent",
    permissions=[view_ticket_perm, change_ticket_perm],
    by=admin_user,
)

# 2. Grant the role to Alice at the Team scope
ScopeAssignment.objects.grant(
    user=alice,
    role=agent_role,
    scope=support_team,
    by=admin_user,
)

5. Protect DRF Endpoints

# helpdesk/views.py
from rest_framework.viewsets import ModelViewSet
from scoped_access.drf import (
    RequireReAuth,
    ScopedModelPermission,
    ScopeObjectPermission,
    ScopeQuerySetMixin,
    ScopeWriteGuardMixin,
)

class TicketViewSet(ScopeWriteGuardMixin, ScopeQuerySetMixin, ModelViewSet):
    queryset = Ticket.objects.all()
    serializer_class = TicketSerializer
    permission_classes = [ScopedModelPermission, ScopeObjectPermission]

    def get_permissions(self):
        permissions = super().get_permissions()
        if self.action == "destroy":
            # Deleting tickets requires Step-Up Re-Authentication
            permissions.append(RequireReAuth())
        return permissions

🔍 Introspection Endpoint (GET /me/access/)

Wire the standard introspection and reauth views in urls.py:

from django.urls import path
from scoped_access.drf import MeAccessView, ReAuthView

urlpatterns = [
    path("api/me/access/", MeAccessView.as_view(), name="me-access"),
    path("api/auth/reauth/", ReAuthView.as_view(), name="reauth"),
]

Example JSON output from GET /api/me/access/:

{
  "principal": {"id": "42", "superuser": false, "active": true},
  "permissions": ["helpdesk.view_ticket", "helpdesk.change_ticket"],
  "assignments": [
    {
      "role": {"id": "7", "name": "Support Agent", "system": true},
      "level": "TEAM",
      "scope": {"id": "12", "label": "Customer Support — Acme Corp"},
      "status": "ACTIVE",
      "valid_until": null,
      "permissions": ["helpdesk.view_ticket", "helpdesk.change_ticket"]
    }
  ]
}

🧪 Testing & Conformance

Django Scoped Access is verified against a language-agnostic test suite defined in SPEC.md and conformance/:

uv run pytest

📚 Documentation

Complete documentation is available at https://django-scoped-access.readthedocs.io/:


📄 License

This project is licensed under the terms of the MIT License.

Metadata

Release files for django-scoped-access 0.1.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 django-scoped-access 0.1.1
File Size Uploaded Provenance
django_scoped_access-0.1.1.tar.gz 113.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-scoped-access 0.1.1
File Interpreter ABI Platform
django_scoped_access-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 150.7 kB

Release files / django_scoped_access-0.1.1.tar.gz

Download URL django_scoped_access-0.1.1.tar.gz
Size 113.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1fb0b807bdf9a0a69aa26ca1d83b43ea61d838d02585adadf97c1b55c1291702
BLAKE2b-256 checksum
How to use checksums
8e631ea106e3b8d2dd6eaef17f27ee05598397395bb16a88b4457bf357bcacc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 21, 2026.

Transparency log

Release files / django_scoped_access-0.1.1-py3-none-any.whl

Download URL django_scoped_access-0.1.1-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
faf3a85b436f5b46f0ba2726118db20c8aad6f4da5934abbc124d6f7df2a2c2e
BLAKE2b-256 checksum
How to use checksums
0faf79c1e169446a9f18bb9d3e5f308209aea97fc93986c15b9fe371a0359c49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 21, 2026.

Transparency log

Security for django-scoped-access 0.1.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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_scoped_access-0.1.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 Aug 21, 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.

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