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

If you'd like to have 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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: jwtninja-0.1.24.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.24.tar.gz
Algorithm Hash digest
SHA256 4722be37b5f3fac17c20d8c67add5f2020a849ae762241a7d2aa7f6ce69dcfb0
MD5 1806f61fbc42ba08a775ab594f6162ba
BLAKE2b-256 3771196089cd63101ad7ec379c88d57741c2e7969fd2572fd04c7782833c63cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jwtninja-0.1.24-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.24-py3-none-any.whl
Algorithm Hash digest
SHA256 919169224fbbb00451f2eb151ff70eddd5d7e3f26d1b4d9bf332d5620ae0e0a1
MD5 a92c0265bc504c0c5f240ccbd09a5acc
BLAKE2b-256 1b7743d25fa17099da47d1f3f249d6023c6e2e0fb4b136229b10751949ebd2c6

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