A simple and fully-typed auth library for Django Ninja based on PyJWT.
Project description
A simple session-backed and fully-typed auth library for Django Ninja based on PyJWT.
Installation
JWT Ninja is a Django app. Install it using uv or pip:
pip install jwtninja
Then add it to your Django settings INSTALLED_APPS:
INSTALLED_APPS = [
...
"jwt_ninja"
]
Usage
Import the router and register it to your Ninja API:
from jwt_ninja.api import router as auth_router
api = NinjaAPI()
api.add_router("auth/", auth_router)
This will register the following endpoints:
/auth/login/Create a newaccess_tokenandrefresh_tokenpair/auth/refresh/Refresh a token/auth/sessions/List all your active sessions/auth/logout/Log out of your current session/auth/logout/all/Log out of all your sessions
Use the JWTAuth class to protect your views. You can use the AuthedRequest type to get annotations for the user and the session:
from ninja import Router
from jwt_ninja.auth_classes import JWTAuth
from jwt_ninja.auth_classes import AuthedRequest
router = Router()
@router.get("/my_protected_endpoint/", auth=JWTAuth())
def my_protected_route(request: AuthedRequest):
request.auth.session.data["foo"] = 123
request.auth.session.save() # Explicitly save the info for the user's session
...
Customization and Configuration
JWT Ninja supports the following settings defined in your Django settings.py:
| Setting | Type | Default |
|---|---|---|
JWT_SECRET_KEY |
str | django_settings.SECRET_KEY |
JWT_ALGORITHM |
str | "HS256" |
JWT_ACCESS_TOKEN_EXPIRE_SECONDS |
int | 300 (5 minutes) |
JWT_REFRESH_TOKEN_EXPIRE_SECONDS |
int | 365 * 3600 (1 year) |
JWT_SESSION_EXPIRE_SECONDS |
int | 365 * 3600 (1 year) |
JWT_USER_LOGIN_AUTHENTICATOR |
str | "jwt_ninja.authenticators.django_user_authenticator" |
JWT_PAYLOAD_CLASS |
str | "jwt_ninja.types.JWTPayload" |
Custom Claims
Subclass jwt_ninja.types.JWTPayload with any additional claims:
from jwt_ninja.types import JWTPayload
class CustomJWTPayload(JWTPayload):
discord_user_id: str
ip_address: str
email: str
Then add JWT_PAYLOAD_CLASS = "path.to.your.CustomJWTPayload to your settings.py.
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 jwtninja-0.1.4.tar.gz.
File metadata
- Download URL: jwtninja-0.1.4.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0070f9d90ef5df2de13aad939483be6955bbedcad07aeaf41d434b8d80484b0
|
|
| MD5 |
89759ebd2aa556fba21785a75da3f7e1
|
|
| BLAKE2b-256 |
5de7532cb2903b247f915cb14922761ded88d9e72bc635bbdb870cb627548dcd
|
File details
Details for the file jwtninja-0.1.4-py3-none-any.whl.
File metadata
- Download URL: jwtninja-0.1.4-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12b7d55e3ac89bab0b74dc759f3c25901c77fb2f3cadbccda0e33c9d05fadf4f
|
|
| MD5 |
d272ff3062a78af01068ba9a19a0f3ae
|
|
| BLAKE2b-256 |
1778d7899ebde6335e1e4bf3d2501f84ba76478c755138f4109837abd548a004
|