Skip to main content

Reusable Django library for JWT verification using JWKS with caching

Project description

django-auth-jwks

django-auth-jwks is a reusable Django app for verifying JWT tokens via JSON Web Key Sets (JWKS) with caching support. It is designed to work seamlessly with Django REST Framework (DRF) and supports both database-backed and stateless authentication scenarios.

Note: This package is currently in beta. Features and APIs may change in future releases.

Features

  • JWT Verification: Validate JWT tokens using JWKS.
  • Stateless Authentication: Supports servers without a database or user model.
  • Database Integration: Maps JWT claims to Django user models when available.
  • Caching: Efficiently caches JWKS for improved performance.
  • DRF Integration: Works with Django REST Framework for API authentication.

Installation

Install the package using pip:

pip install django-auth-jwks

Alternatively, for development purposes, you can install it in editable mode:

pip install -e .

To uninstall the package:

pip uninstall django-auth-jwks

Quickstart

  1. Add django_auth_jwks to your INSTALLED_APPS in settings.py:

    INSTALLED_APPS = [
        ...,
        "django_auth_jwks",
    ]
    
  2. Configure the AUTH_JWKS setting in your Django project to point to your JWKS URL:

    AUTH_JWKS = {
         "ISSUER": "https://example.com",
         "JWKS_ENDPOINT": "/o/.well-known/jwks.json",
         "AUDIENCE": "example-service",
         "CACHE_TTL": 300,
     }
    
  3. Use the JWTAuthentication class in your DRF settings:

    REST_FRAMEWORK = {
        "DEFAULT_AUTHENTICATION_CLASSES": [
            "django_auth_jwks.authentication.JWTAuthentication",
        ],
    }
    
  4. Optionally, use the @require_auth decorator or middleware for securing views.

  5. Run your Django server:

    python manage.py runserver
    

Usage

Securing Views with the @require_auth Decorator

You can secure individual views by applying the @require_auth decorator:

from django_auth_jwks.decorators import require_auth

@require_auth
def my_view(request):
    return JsonResponse({"message": "Authenticated!"})

Using Middleware for Global Authentication

To apply authentication globally, add the middleware to your MIDDLEWARE setting:

MIDDLEWARE = [
    ...,
    "django_auth_jwks.middleware.JWTAuthenticationMiddleware",
]

Development

To contribute to this project:

  1. Clone the repository.

  2. Install dependencies:

    pip install -r requirements.txt
    

License

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

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

Disclaimer

This package is in beta. Use it in production environments with caution, and report any issues you encounter.

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_auth_jwks-1.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

django_auth_jwks-1.0.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file django_auth_jwks-1.0.1.tar.gz.

File metadata

  • Download URL: django_auth_jwks-1.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_auth_jwks-1.0.1.tar.gz
Algorithm Hash digest
SHA256 157dfe9d527cc62d7a17817037c110af9d2214d881b561f95018f9c8e8167ca4
MD5 0ba86af50aef670df731223deb646427
BLAKE2b-256 e4ffc2626f76676d5222d4261c644c9341df18ace04fd6c11ac47b302dbd316f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_auth_jwks-1.0.1.tar.gz:

Publisher: python-publish.yml on entespotify/django-auth-jwks

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

File details

Details for the file django_auth_jwks-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auth_jwks-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 548d37088cfb7e566fb4b85e2eccab188baa0d68e9e7410941a5065e2813f111
MD5 c866f02c5705103bcb1ce447c1db34e2
BLAKE2b-256 8e7426d318ddaf0d989b3750b32db9e6490f162ede4e54c1b7f79dff450f77cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_auth_jwks-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on entespotify/django-auth-jwks

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