Skip to main content

An authorization library that supports access control models like ACL, RBAC, ABAC in Django

Project description

Django Authorization

Django-authorization is an authorization library for Django framework.

tests Coverage Status Version Download Gitter

Based on Casbin and Django-casbin (middleware, light weight of this plugin), an authorization library that that supports access control models like ACL, RBAC, ABAC.

image

All you need to learn to use Casbin first.

[toc]

Installation and Configure

pip install django-authorization

We recommend that you first configure the adapter for persistent storage of the policy, such as:

django-orm-adapter, After integrating it into the project continue with the configuration of django-authrization

# 1. Add the app to INSTALLED_APPS
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "dauthz.apps.DauthzConfig",	# add this app to INSTALLED_APPS
]

# 2. Add configure of dauthz
DAUTHZ = {
    # DEFAULT Dauthz enforcer
    "DEFAULT": {
        # Casbin model setting.
        "MODEL": {
            # Available Settings: "file", "text"
            "CONFIG_TYPE": "file",
            "CONFIG_FILE_PATH": Path(__file__).parent.joinpath("dauthz-model.conf"),
            "CONFIG_TEXT": "",
        },
        # Casbin adapter .
        "ADAPTER": {
            "NAME": "casbin_adapter.adapter.Adapter",
            # 'OPTION_1': '',
        },
        "LOG": {
            # Changes whether Dauthz will log messages to the Logger.
            "ENABLED": False,
        },
    },
}

Usage

Some Important Concepts:

such as .conf file, policy, sub, obj, act, please refer to the casbin website

Middleware Usage

# Install middleware for django-authorization as required
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "dauthz.middlewares.request_middleware.RequestMiddleware",	# add the middleware 
]

You can freely set the casbin enforcer for the middleware via API: set_enforcer_for_request_middleware(enforcer_name) and set_enforcer_for_enforcer_middleware(enforcer_name)

Decorator Usage

Request decorator will check the authorization status of user, path, method

# use request decorator
@request_decorator
def some_view(request):
    return HttpResponse("Hello World")

Enforcer decorator will check the authorization status of user, obj, edit. example:

# use enforcer decorator
# sub: user in request obj: "artical" act: "edit"
@enforcer_decorator("artical", "edit")
def some_view(request):
    return HttpResponse("Hello World")

Command Line Usage

The command line operation allows you to operate directly on the enforcer's database. Three sets of commands are available: policy commands, group commands and role commands.

Add/Get policy, usage: 
python manage.py policy [opt: --enforcer=<enforcer_name>] add <sub> <obj> <act>
python manage.py policy [opt: --enforcer=<enforcer_name>] get <sub> <obj> <act>

Assign/Get role to user, usage: 
python manage.py role [opt: --enforcer=<enforcer_name>] assign <user> <role>
python manage.py role [opt: --enforcer=<enforcer_name>] get <user>

Add/Get group policy, usage:
python manage.py group [opt: --enforcer=<enforcer_name>] add <user> <role> [opt:<domain>]
python manage.py group [opt: --enforcer=<enforcer_name>] get <user> <role> [opt:<domain>]

License

This project is licensed under the Apache 2.0 license.

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-authorization-1.0.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

django_authorization-1.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file django-authorization-1.0.0.tar.gz.

File metadata

  • Download URL: django-authorization-1.0.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for django-authorization-1.0.0.tar.gz
Algorithm Hash digest
SHA256 23ef73e5b87c67b6939e52d475d1b67dfd1274d24d422f2f9f90093cc35eaeb3
MD5 2bf8b47a8c1996ff89f79e5d777d09c4
BLAKE2b-256 456262bcbe19bee09ed84606cfab0d5dedb87926bbc5c901e87b13d4d8215877

See more details on using hashes here.

File details

Details for the file django_authorization-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_authorization-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68edb8d5573c4bc3c4777364bd1aa1d1c24bd79fd92f162808363c1625304e37
MD5 c78382254ef46d823949116960c1d658
BLAKE2b-256 b099a84584081148fc32d11d2f338b2ba042852ee47ef0a97eb878340fa7d6dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page