Skip to main content

A simple and fully-typed auth library for Django Ninja based on PyJWT.

Project description

JWT Ninja Logo

JWT Ninja

A session‑backed, fully‑typed authentication library for Django Ninja, powered by PyJWT

PyPI CI Status License

❤️ Contributions Welcome! Whether you’re fixing a bug, or want a new feature, feel free to submit a PR.


🚀 Quick Start

Installation

JWT Ninja is a standard Django app.

Install it with uv or pip:

pip install jwtninja

Add it to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...,
    "jwt_ninja",
]

Lastly, don't forget to run migrations:

python manage.py migrate

Usage

Register the built‑in router on your Ninja API:

from ninja import NinjaAPI
from jwt_ninja.api import router as auth_router
from jwt_ninja.errors import APIError as AuthAPIError
from jwt_ninja.handlers import error_handler

api = NinjaAPI()
api.add_router("auth/", auth_router)

# Explicit control over how you went errors to be returned
api.add_exception_handler(AuthAPIError, error_handler)

Endpoints created:

Method Path Purpose
POST /auth/login/ Issue a new access & refresh token pair
POST /auth/refresh/ Refresh an access token
GET /auth/sessions/ List active sessions
POST /auth/logout/ Log out of the current session
POST /auth/logout/all/ Log out of all sessions

Protect views with JWTAuth and enjoy typed requests:

from ninja import Router
from jwt_ninja.auth_classes import JWTAuth, 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()  # Persist session changes
    return {"message": "Success!"}

⚙️Settings

# settings.py defaults
JWT_SECRET_KEY = SECRET_KEY
JWT_ALGORITHM = "HS256"
JWT_ACCESS_TOKEN_EXPIRE_SECONDS = 300        # 5 minutes
JWT_REFRESH_TOKEN_EXPIRE_SECONDS = 365 * 3600  # 1 year
JWT_SESSION_EXPIRE_SECONDS = 365 * 3600       # 1 year
JWT_USER_LOGIN_AUTHENTICATOR = "jwt_ninja.authenticators.django_user_authenticator"
JWT_PAYLOAD_CLASS = "jwt_ninja.types.JWTPayload"

Custom Claims

from jwt_ninja.types import JWTPayload

class CustomJWTPayload(JWTPayload):
    discord_user_id: str
    ip_address: str
    email: str

Configure it:

JWT_PAYLOAD_CLASS = "path.to.CustomJWTPayload"

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

jwtninja-0.0.0.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

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

jwtninja-0.0.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file jwtninja-0.0.0.tar.gz.

File metadata

  • Download URL: jwtninja-0.0.0.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.13

File hashes

Hashes for jwtninja-0.0.0.tar.gz
Algorithm Hash digest
SHA256 2cb0481828be874d9336c1af3750db93e51e0a94a5d1cb068d2458bbec3577b1
MD5 b38b75675bfdd705cd9b4d9981ad1b8f
BLAKE2b-256 0c920beebe0305a5c94ecd9731efc928745e78d5f7e4cd7bc7014aad7e8e09ee

See more details on using hashes here.

File details

Details for the file jwtninja-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: jwtninja-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.13

File hashes

Hashes for jwtninja-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ddae3ded55db43c606fc72bbc024dbf0acb56d5bc18c325f623587ec586e2f8
MD5 9e485eb77c78c8193df51631c8c61f77
BLAKE2b-256 dc2243a62e1194654fcdeea317e1a155b040b6e82cffa6ed30d892109c79e09f

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