Skip to main content

Django Magic Authorize

This middleware adds simple token-based authorization for private content to any Django project.

Installation

First, install the package using your favorite python package manager

uv add django-magic-authorization

or

pip install django-magic-authorization

Second, you need to enable the app in your Django project.

# settings.py
INSTALLED_APPS = [
  ...
  "django_magic_authorization"
  ...
]

MIDDLEWARE = [
  ...
  "django_magic_authorization.middleware.MagicAuthorizationMiddleware",
  ...
]

Third, run the migrations to add the relevant schemas to your database

python manage.py migrate

Quickstart

Authorization happens on the URL level. The package offers a drop-in replacement for django.urls.path, that is used to mark urls as protected. You can use this in conjunction with include() to protect sub-paths quickly.

# urls.py
from django.urls import path, include
from django.http import HttpResponse
from django_magic_authorization.urls import protected_path


def test_view(request):
  return HttpResponse("Hello")

urlpatterns = [
  path("unprotected", test_view),
  protected_path("protected", test_view)
]

With a running development server

$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/protected
403

This URL can now only be accessed by someone with a valid token.

There is a django admin interface for you to create, manage and delete access tokens for these protected paths. You can also do so in the shell.

>>> t = AccessToken.objects.create(path="protected", description="Test token creation")
>>> t.token
your-token-value

using this token to access the protected view:

$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/protected?token=your-token-value
200

Metadata

Release files for django-magic-authorization 0.1.3

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-magic-authorization 0.1.3
File Size Uploaded
django_magic_authorization-0.1.3.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-magic-authorization 0.1.3
File Interpreter ABI Platform
django_magic_authorization-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / django_magic_authorization-0.1.3.tar.gz

Download URL django_magic_authorization-0.1.3.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a8b3240799f695837fd01c9aa52252d49d5b61d081c88896989f675d7f9598dd
BLAKE2b-256 checksum
How to use checksums
1bcaddff7baedf84c9db66ad25d4f17c7931a6bd0607124baf69fc0dbd1c79d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Release files / django_magic_authorization-0.1.3-py3-none-any.whl

Download URL django_magic_authorization-0.1.3-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f84b9464af43ae185b899531316613cf70e7aebe7147c4912047b094897f8def
BLAKE2b-256 checksum
How to use checksums
029660feff5345707d499fea8542906850d623530d63c4fb8abca9466ebdc49a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.2

Release history Release notifications | RSS feed

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

This release

0.1.3 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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