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.1.23.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.1.23-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jwtninja-0.1.23.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.1.23.tar.gz
Algorithm Hash digest
SHA256 34ddd3566e1db2740375548396e1ca36a1dd2535a5bd7c9d7f8722c31fe1e267
MD5 f794d70b0c90fcf391fdf9ec7f031010
BLAKE2b-256 97c1b377efbf2fdfa9ee76643856d1b33f0a2a665d643f8d3c6ab4e06cf990e0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jwtninja-0.1.23-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.1.23-py3-none-any.whl
Algorithm Hash digest
SHA256 130efef37093442198db5bde4b942b4691b142011f8104085087fe5d319c9270
MD5 bee93752fa0dee31d13912c7ef21c183
BLAKE2b-256 6a6b927c626a9eda36234ff21317d976449e8d0c10cb920e927b3fa2d38cb9f7

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