Skip to main content

Django REST Framework authentication using PASETO (pyseto)

Project description

drf-pyseto

PyPI version Python versions Django versions Documentation License

A simple, secure, and modern PASETO (Platform-Agnostic Security Tokens) authentication backend for Django REST Framework (DRF), powered by pyseto.


📖 Read the Full Documentation Here


drf-pyseto utilizes the v4.local (symmetric encryption) PASETO specification, providing a more secure alternative to JWTs by preventing cryptographic agility attacks and minimizing misconfigurations.

🌟 Features

  • Secure by Default: Implements PASETO v4.local ensuring robust, symmetric encryption for your tokens.
  • Seamless Integration: Built specifically for Django REST Framework. Drop-in replacement for traditional token or JWT authentication.
  • Configurable Lifetimes: Easily manage separate lifetimes for access and refresh tokens.
  • Modern Python & Django Support: Compatible with Python 3.10–3.14 and Django 4.2–6.0.

📦 Requirements

  • Python >= 3.10
  • Django >= 4.2
  • Django REST Framework >= 3.16
  • pyseto >= 1.7

🚀 Installation

Install the package via pip:

pip install drf-pyseto

⚙️ Configuration

1. Update Django Settings

Add the necessary configuration for drf-pyseto in your settings.py:

DRF_PYSETO = {
    # REQUIRED: A 32-byte secret key (can be plain 32-bytes or base64url encoded)
    "KEY": "<your-32-byte-secret-key-or-base64url>", 
    
    # Optional settings (defaults shown)
    "ACCESS_LIFETIME": 300,        # Access token lifetime in seconds (default: 5 minutes)
    "REFRESH_LIFETIME": 86400,     # Refresh token lifetime in seconds (default: 1 day)
    "USER_ID_FIELD": "id",         # User model field used as the subject
    "USER_ID_CLAIM": "user_id",    # Claim key for the user identifier
    "TOKEN_TYPE_CLAIM": "typ",     # Claim key for the token type
    "AUTH_HEADER_TYPE": "Bearer",  # Allowed Authorization header type

    # Additional standard PASETO claims (optional)
    # "ISSUER": "your-service",
    # "AUDIENCE": "your-clients",
}

2. Configure DRF Authentication

Set drf_pyseto as your authentication class in settings.py:

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "drf_pyseto.authentication.PASETOAuthentication",
    )
}

3. Setup Routing

Include the token endpoints in your project's urls.py:

from django.urls import path, include

urlpatterns = [
    # ...
    path("api/auth/", include("drf_pyseto.urls")),
    # ...
]

📡 Endpoints

Once configured, the following endpoints will be available to manage your tokens (assuming you included the URLs under api/auth/):

  • Obtain Tokens: POST /api/auth/token/

    • Payload: {"username": "your_username", "password": "your_password"}
    • Returns format: {"access": "v4.local....", "refresh": "v4.local...."}
  • Refresh Access Token: POST /api/auth/token/refresh/

    • Payload: {"refresh": "v4.local...."}
    • Returns format: {"access": "v4.local...."}

🛠️ Development & Testing

To set up the project for development and run the test suite:

# Clone the repository
git clone https://github.com/p-r-a-v-i-n/drf-pyseto.git
cd drf-pyseto

# Install locally with testing dependencies
pip install -e ".[test]"

# Run tests
pytest

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.

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

drf_pyseto-0.1.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

drf_pyseto-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_pyseto-0.1.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for drf_pyseto-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dce0a883abfd4fdc214a95d8c9b13047bab77f53ddac4130df53c61cb3a68b57
MD5 688beb6908e113d4cca6984fe921f545
BLAKE2b-256 673dec23135231616a932591f0a718993d27d354b2be967f2236fb1ac2a9f20f

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_pyseto-0.1.1.tar.gz:

Publisher: release.yml on p-r-a-v-i-n/drf-pyseto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: drf_pyseto-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for drf_pyseto-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6da53f9ce63e220b2af5713da4a06340d295035853dbfc1b1b36b5ea856216dc
MD5 4735efef10e915acd98fc4915e2d7f47
BLAKE2b-256 f0c1f00641c2f558a4e02af3927d42f1b4d3642399bb973920f5ce4bd9382c16

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_pyseto-0.1.1-py3-none-any.whl:

Publisher: release.yml on p-r-a-v-i-n/drf-pyseto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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