Skip to main content

A declarative, strictly-typed ReBAC framework for Django.

Project description

Django ReBAC

A declarative, strictly-typed ReBAC framework for Django.

This package provides a complete "Holy Trinity" for enterprise authorization:

  1. Data Layer: Automatically synchronizes Django models to OpenFGA using the Transactional Outbox pattern.
  2. Routing Layer: Secures Django REST Framework (DRF) Views with zero-business-logic mixins and permission classes.
  3. Presentation Layer: Injects high-performance, batch-evaluated permission flags into DRF Serializers for seamless React/Vue frontend integration.

📦 Installation

Install the package via pip or uv:

pip install django-rebac

Add it to your INSTALLED_APPS and include the Middleware in settings.py:

INSTALLED_APPS = [
    # ... your other apps ...
    'rebac',
]

MIDDLEWARE = [
    # ... standard middleware ...
    'rebac.middleware.TraefikIdentityMiddleware',
]

Run migrations to create the Outbox table in your database:

python manage.py migrate rebac

⚙️ Configuration

Configure the package by adding the REBAC_CONFIG dictionary to your settings.py.

# settings.py

REBAC_CONFIG = {
    # REQUIRED: The Store ID provisioned by the Central Auth Service
    "BACKEND_OPTIONS":{
        "STORE_ID": "01H...XYZ",
        "API_URL": "http://localhost:8080",
    },
    # Core Settings
    "BATCH_SIZE": 50,
    "MAX_RETRIES": 5,

    # Identity Management (Traefik / API Gateway integration)
    "REQUEST_HEADER_MAPPINGS": {
        "X-User-Id": "rebac_user",
    },
    "REBAC_USER_ATTR": "rebac_user",
    "REBAC_USER_PREFIX": "user:",
}

💡 Usage

1. Synchronizing Models (RebacModelSyncMixin)

Inherit from RebacModelSyncMixin and define your rebac_config using the RebacModelConfig dataclass. The package handles tuple generation, diffing, and outbox queuing automatically.

from django.db import models
from typing import ClassVar
from rebac.mixins import RebacModelSyncMixin
from rebac.structs import RebacModelConfig, RebacParentConfig, RebacCreatorConfig

class Document(RebacModelSyncMixin, models.Model):
    title = models.CharField(max_length=255)
    folder_id = models.CharField(max_length=255)
    creator_id = models.CharField(max_length=255)

    rebac_config: ClassVar[RebacModelConfig] = RebacModelConfig(
        object_type="document",
        parents=[
            RebacParentConfig(
                relation="folder",
                parent_type="folder",
                local_field="folder_id"
            )
        ],
        creators=[
            RebacCreatorConfig(
                relation="editor",
                local_field="creator_id"
            )
        ]
    )

2. Securing API Views (RebacViewMixin)

Secure your DRF endpoints instantly using simple, declarative dictionary configurations. No complex permission classes required. RebacViewMixin handles queryset filtering (lists), parent checks (creation), and object checks (updates/deletes).

from rest_framework import viewsets
from rebac.mixins import RebacViewMixin
from rebac.structs import RebacViewConfig
from .models import Document
from .serializers import DocumentSerializer

class DocumentViewSet(RebacViewMixin, viewsets.ModelViewSet):
    queryset = Document.objects.all()
    serializer_class = DocumentSerializer

    rebac_config = RebacViewConfig(
        object_type="document",
        read_relation="can_read_document",
        update_relation="can_update",
        delete_relation="can_delete",

        # Parent-Level Authorization for Creation (POST)
        # Verifies the user has permission on the parent scope before allowing creation
        create_parent_type="folder",
        create_parent_field="folder_id",
        create_relation="can_add_items"
    )

3. Frontend Integration (RebacPermissionSerializerMixin)

Inject ReBAC evaluations directly into your API responses so your frontend knows exactly which action buttons to render. The mixin utilizes advanced custom list serializers to prevent N+1 queries, batching all checks into a single OpenRebac network request.

from rest_framework import serializers
from rebac.serializers import RebacPermissionSerializerMixin
from .models import Document

class DocumentSerializer(RebacPermissionSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = Document
        # The mixin automatically injects "_permissions" into this tuple!
        fields = ("id", "title", "folder_id")

        # Declarative rules processed by the mixin
        rebac_object_type = "document"
        rebac_permissions = ("can_update", "can_delete")

Resulting JSON Payload:

{
  "id": 101,
  "title": "Q3 Financials",
  "folder_id": "folder_55",
  "_permissions": {
    "can_update": true,
    "can_delete": false
  }
}

🕸️ Celery Configuration

Because this package uses the Transactional Outbox pattern for model syncing, you must have Celery configured in your project to process the queued network requests.

Configure a Celery Beat sweeper to run periodically as a fail-safe:

# celery.py
from celery.schedules import crontab

app.conf.beat_schedule = {
    'rebac-outbox-sweeper': {
        'task': 'rebac.tasks.process_rebac_outbox_batch',
        'schedule': crontab(minute='*/5'), # Sweep the Outbox every 5 minutes
    },
}

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

django_rebac-0.1.2.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

django_rebac-0.1.2-py3-none-any.whl (48.3 kB view details)

Uploaded Python 3

File details

Details for the file django_rebac-0.1.2.tar.gz.

File metadata

  • Download URL: django_rebac-0.1.2.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_rebac-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ad7c337f264b9fff4817292b42b978e1179e57db626281ab565ffe047ab61670
MD5 ef659e29fcb58bf0c8b23a45185c652d
BLAKE2b-256 fd3e3fd493738c761134d47ca3b01ac16e88fea1d6a89ae50f63ee682a7d9f41

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rebac-0.1.2.tar.gz:

Publisher: publish.yml on OrmusLabs/django-rebac

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

File details

Details for the file django_rebac-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: django_rebac-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 48.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_rebac-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b2ff83fbb3fa4d04ed4c564c286d4a511c03ff48cda3348bf8d27ade1264be8
MD5 faf7dcc5ddc3e764568c9a97f1e2b750
BLAKE2b-256 7e5a3a757c3cd666686cb71ff82941742be25059d4a332ce6c273146cc2719a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rebac-0.1.2-py3-none-any.whl:

Publisher: publish.yml on OrmusLabs/django-rebac

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