Skip to main content

Add magic authorization links to your django project.

Project description

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.MagicAuthorizeMiddleware",
  ...
]

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

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_magic_authorization-0.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

django_magic_authorization-0.1.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file django_magic_authorization-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_magic_authorization-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d5a3ad69e45758f1d4e734c558aff778540978337b44ac232bdc18be3cc14f34
MD5 1663b61399ad2ad96b93af45283122ad
BLAKE2b-256 d4dfe2fbccf6ff0066d2b9eead579c26e7a548dccaec3cdc53cc0bad7a4ea2ee

See more details on using hashes here.

File details

Details for the file django_magic_authorization-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_magic_authorization-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9028c72865717e641b8e7450b09bf35ee2d76f7cc984a91c6c47814c5900aa0
MD5 7f7f6b25b8e86b00361ebc595b62d408
BLAKE2b-256 c9eac78cc81e191a1b1c26e3098aa923608eacd7c8be23972bf48b4fa2158113

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