Skip to main content

Capability based object permissions for Django.

Project description

Django-Caps provides capability based object permission system for Django applications and Django Rest Framework.

This project is inspired by Capn’Proto documentation (interesting paper).

A capability is a provided permission to a specific object. It can be shared a limited amount of time. Users never directly access the targeted object, but through a access that defines allowed capabilities for it.

In short, why use capabilities?

Documentation: https://oxylus-tech.github.io/django-caps/

Features

Here is what we provide:

  • Capability based object permissions system: objects can be shared with specific permissions to user/group. The object is then accessed by this shared object rather than directly (except for its owner).

  • Access sharing: Objects’ accesses can be shared with granular control on permissions.

  • Integration: authentication/permission backend is provided both for Django and Django Rest Framework. Views, viewsets and serializers too.

  • Agents: users can act under different profiles, as a user or group. The accesses always target other agents.

Among other things:

  • Database id obfuscation: object internal id are never exposed to the outside world. Instead uuid are used to reference them in API and urls. This mitigate attacks on predictive id.

Short example

Lets create an object:

# models.py
from django.db import models
from django.utils.translation import gettext_lazy as _

from caps.models import Owned

__all__ = ("Post",)

# Create our example model.
class Post(Owned):
    title = models.CharField(_("Title"), max_length=64)
    content = models.TextField(_("Content"))
    # ... other fields

    # Allowed permissions with allowed reshare depth
    root_grants = {
        "app.view_post": 2, # can be shared then reshared
        "app.change_post": 1, # can be shared once
        "app.delete_post": 0, # can not be shared
    }

Small examples of Django-Caps’ API usage:

from datetime import timedelta

from django.contrib.auth.models import User, Permission
from django.utils import timezone as tz

from caps.models import Agent
from .models import Post

# User has 1-1 relation with an agent
user = User.objects.all()[0]
user_1 = User.objects.all()[1]

# Create the post
post = Post.objects.create(owner=user.agent, title="Some title", content="Some content")

# Share the post to agent 1 with default grants
access = post.share(user_1.agent)
assert access.grants == {"app.view_post": 1, "app.change_post": 0}

# Get objects for user_1
objs = Post.objects.available(user_1.agent)

The views/viewsets will handle permission check depending on the action being requested.

For concrete usage, see the docs! 😉

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_caps-0.0.2.tar.gz (8.0 MB view details)

Uploaded Source

Built Distribution

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

django_caps-0.0.2-py3-none-any.whl (36.0 kB view details)

Uploaded Python 3

File details

Details for the file django_caps-0.0.2.tar.gz.

File metadata

  • Download URL: django_caps-0.0.2.tar.gz
  • Upload date:
  • Size: 8.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.11 Linux/6.17.9-arch1-1

File hashes

Hashes for django_caps-0.0.2.tar.gz
Algorithm Hash digest
SHA256 07a43a2bfaef750f818ac70453dc216d42025f0e1d1b590c38ba9fabbaf38946
MD5 f371fa4c0852bb89c148d1cc6515693f
BLAKE2b-256 b0215b91355b7b1f86cf5ee1f65f25836c7503e2f9b992b37a518ebf79606ee4

See more details on using hashes here.

File details

Details for the file django_caps-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_caps-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 36.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.11 Linux/6.17.9-arch1-1

File hashes

Hashes for django_caps-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 40e3af523885e9e016c56bd44a1cb9da0764be102fa3151bef7ea960c1bbb097
MD5 c1eba59716750b95857e7e1e84f9304f
BLAKE2b-256 c9cb08a2cdc07947540caf3cb2507fdbfd4534e837e27bfb8563376535a48665

See more details on using hashes here.

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