Settings & Utilities
This section covers the core configuration logic and the infrastructure utilities that power the django-rebac package under the hood.
Configuration (conf.py)
The configuration module is responsible for parsing the REBAC_CONFIG dictionary defined in your core Django settings.py and falling back to sensible defaults.
Attributes
DEFAULTS
module-attribute
DEFAULTS: dict[str, Any] = {'BACKEND': 'rebac.backends.openfga.client.OpenFGABackend', 'BACKEND_OPTIONS': {}, 'BATCH_SIZE': 50, 'MAX_RETRIES': 5, 'REQUEST_HEADER_MAPPINGS': {'X-User-Id': 'rebac_user'}, 'ENABLE_OUTBOX_ADMIN': True, 'REBAC_USER_ATTR': 'rebac_user', 'REBAC_USER_PREFIX': 'user:', 'LOCAL_DEV_FALLBACK': {'USE_DJANGO_USER': True, 'STATIC_USER_ID': None}}
Sensible defaults for the django-rebac integration.
Attributes:
| Name | Type | Description |
|---|---|---|
BACKEND |
str
|
The dot-path to the ReBAC engine adapter.
Defaults to |
BACKEND_OPTIONS |
dict[str, Any]
|
Backend-specific configuration (e.g., API URLs, Store IDs, or Pre-shared Keys). |
BATCH_SIZE |
int
|
Number of items to process in a single synchronization batch.
Defaults to |
MAX_RETRIES |
int
|
How many times to retry failed synchronization attempts.
Defaults to |
REQUEST_HEADER_MAPPINGS |
dict[str, str]
|
Mapping of incoming request headers to ReBAC context variables. |
ENABLE_OUTBOX_ADMIN |
bool
|
If True, registers the ReBAC Outbox model in
the Django Admin. Defaults to |
REBAC_USER_ATTR |
str
|
The attribute on the request/user object to use for ReBAC identity. |
REBAC_USER_PREFIX |
str
|
Prefix added to user IDs (e.g., |
LOCAL_DEV_FALLBACK |
dict[str, Any]
|
Settings for local development when identity providers are absent.
|
Classes
Functions
get_setting
Fetches a setting from the REBAC dictionary in django.conf.settings.
Falls back to the DEFAULTS dictionary if not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string key of the setting to retrieve. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The resolved configuration value. |
Raises:
| Type | Description |
|---|---|
ImproperlyConfigured
|
If the setting key is not recognized by the framework. |
Source code in src/rebac/conf.py
validate_settings
Ensures the ReBAC configuration is logically consistent.
Should be called in AppConfig.ready().
Raises:
| Type | Description |
|---|---|
ImproperlyConfigured
|
If the configuration prevents the framework from operating. |
Source code in src/rebac/conf.py
ReBAC Client Utility (backends/openfga/client.py)
The get_rebac_client function is the centralized infrastructure utility responsible for instantiating and configuring the ReBAC backend Python SDK client.
By utilizing this utility, we adhere to the Single Responsibility Principle (SRP). Our application services and permission classes do not need to know how to authenticate with the ReBAC engine or where the ReBAC server lives; they simply request a ready-to-use client and execute their checks.
⚙️ How it works
Under the hood, get_rebac_client() fetches the necessary environment configuration from your Django settings.py (specifically the REBAC_CONFIG dictionary). It ensures that parameters like the API_URL and STORE_ID are properly loaded for OpenFGA Backend.
Performance Note: It utilizes Python's @lru_cache to act as a thread-safe Singleton, ensuring the underlying HTTP connection pool is reused across requests for maximum performance.
Classes
Functions
get_rebac_client
cached
Dynamically loads and instantiates the configured ReBAC backend.
Returns:
| Name | Type | Description |
|---|---|---|
BaseReBACBackend |
BaseReBACBackend
|
An instantiated adapter ready to process authorization queries. |
Raises:
| Type | Description |
|---|---|
ImproperlyConfigured
|
If the backend path is invalid or the class fails to load. |
Source code in src/rebac/utils.py
🏗️ Architectural Usage Guidelines
To maintain our Clean Architecture and strict layer separation, follow these rules when using get_rebac_client() in your own application:
Rules of Engagement
- ❌ DO NOT use this client directly inside a Django Model (Layer 3). Models should only define ReBAC mappings declaratively using the
rebac_configattribute with theRebacModelConfigdata class. - ✅ DO use this client inside Custom Permissions (Layer 3) to protect your DRF API views.
- ✅ DO use this client inside your Service Layer (Layer 2) if you need to manually query the authorization graph to make complex business logic decisions.
Example usage in a Service:
# services.py
from rebac.utils import get_rebac_client
class DocumentService:
def publish_document(self, document_id: str, user_id: str):
# 1. Fetch the configured (and cached) agnostic ReBAC client
rebac_client = get_rebac_client()
# 2. Query ReBAC to ensure the user has the 'editor' role
is_allowed = rebac_client.check(
user=f"user:{user_id}",
relation="editor",
obj=f"document:{document_id}"
)
if not is_allowed:
raise PermissionError("Only editors can publish this document.")
# ... proceed with publishing business logic ...