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.2.0.tar.gz (27.1 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.2.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jwtninja-0.2.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for jwtninja-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1c5c036ebe7708789230386f729b38becf036ebd963b443e3c37a9be4e041504
MD5 5b2d531548f68e817e40f6ca70d14464
BLAKE2b-256 1bb37786aa6cecb4d3737b1f17dd0484a14675445be308b1addcc158d0249e24

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jwtninja-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for jwtninja-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 180fa922af87fab1bdf4672ac35a326dd43d6aaf7e5f6bdd8de67319129f12ed
MD5 330d6bf2dc60cba745c9a88d5b6adcd7
BLAKE2b-256 94f8b399ebc72576a7e7f4d36183fc286e517c4e03f43263812791381c80ae19

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