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_URI": "https://example.com/o/.well-known/jwks.json",
         "AUDIENCE": "example-service",
     }
    
  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.0.tar.gz (5.6 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.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_auth_jwks-1.0.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for django_auth_jwks-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8d806fbca2084f8e71400c1f880b2ca2264399b0a59b8057eecee0d5ceb4a0e8
MD5 5e7d33aca6b1d2b4738a18bd5e92d423
BLAKE2b-256 0226287387f2098c2ef71f4191e6b60123c582394c8d0536f3619cf3b5a11dfc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_auth_jwks-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e73229f2b74c040d7a9c977f88a71a75297198b196ae66bbcb3a8db03fc7fb7
MD5 a7a9811d24e8865304047465f651847a
BLAKE2b-256 b0756857573f9d49a4c1b3ca08d845a4628d8725c9e1e97eca61f1c82df7b92f

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