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
-
Add
django_auth_jwksto yourINSTALLED_APPSinsettings.py:INSTALLED_APPS = [ ..., "django_auth_jwks", ]
-
Configure the
AUTH_JWKSsetting 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, }
-
Use the
JWTAuthenticationclass in your DRF settings:REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": [ "django_auth_jwks.authentication.JWTAuthentication", ], }
-
Optionally, use the
@require_authdecorator or middleware for securing views. -
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:
-
Clone the repository.
-
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
157dfe9d527cc62d7a17817037c110af9d2214d881b561f95018f9c8e8167ca4
|
|
| MD5 |
0ba86af50aef670df731223deb646427
|
|
| BLAKE2b-256 |
e4ffc2626f76676d5222d4261c644c9341df18ace04fd6c11ac47b302dbd316f
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_auth_jwks-1.0.1.tar.gz -
Subject digest:
157dfe9d527cc62d7a17817037c110af9d2214d881b561f95018f9c8e8167ca4 - Sigstore transparency entry: 622357575
- Sigstore integration time:
-
Permalink:
entespotify/django-auth-jwks@d0637da012965865de151d98ccbe4183669c0643 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/entespotify
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d0637da012965865de151d98ccbe4183669c0643 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_auth_jwks-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_auth_jwks-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
548d37088cfb7e566fb4b85e2eccab188baa0d68e9e7410941a5065e2813f111
|
|
| MD5 |
c866f02c5705103bcb1ce447c1db34e2
|
|
| BLAKE2b-256 |
8e7426d318ddaf0d989b3750b32db9e6490f162ede4e54c1b7f79dff450f77cf
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_auth_jwks-1.0.1-py3-none-any.whl -
Subject digest:
548d37088cfb7e566fb4b85e2eccab188baa0d68e9e7410941a5065e2813f111 - Sigstore transparency entry: 622357581
- Sigstore integration time:
-
Permalink:
entespotify/django-auth-jwks@d0637da012965865de151d98ccbe4183669c0643 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/entespotify
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d0637da012965865de151d98ccbe4183669c0643 -
Trigger Event:
release
-
Statement type: