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! 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!"}

⚙️ Default Settings

# settings.py
JWT_SECRET_KEY = SECRET_KEY  # Django's 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

If want additional data in the JWT payload, then you must subclass JWTPayload as below.

from jwt_ninja.types import JWTPayload


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

And then 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.25.tar.gz (23.8 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.25-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for jwtninja-0.1.25.tar.gz
Algorithm Hash digest
SHA256 18b5616c5f3c0a2ca700eed8c1a44775b30ef263ed9363b05b1aa687cd2ebcac
MD5 bcee073815d7a4266769c88c0975a50c
BLAKE2b-256 35c752db26774f702bbd5b328f19473ef952f9199fc2fe10b773b89d159880b2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for jwtninja-0.1.25-py3-none-any.whl
Algorithm Hash digest
SHA256 b5d99bf66960c1096b5969367a21984a043f5fd0dc0069874e93cbc2ac7fc74b
MD5 00c0902d5bfa5d7d99463019e0243146
BLAKE2b-256 81234f9b04a9ad0af949e17c60a9001b5d1c45790407d29385ff9e02275f12d2

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