Skip to main content

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

Project description

JWT Ninja Logo

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


image Linting, Formatting and Tests

Installation

JWT Ninja is a Django app. Install it using uv or pip:

pip install jwtninja

Then add it to your Django settings INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "jwt_ninja"
]

Usage

Import the router and register it to 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)

# Add Auth Exception Handler to explicitly handle error
api.add_exception_handler(AuthAPIError, error_handler)

This will register the following endpoints:

  • /auth/login/ Create a new access_token and refresh_token pair
  • /auth/refresh/ Refresh a token
  • /auth/sessions/ List all your active sessions
  • /auth/logout/ Log out of your current session
  • /auth/logout/all/ Log out of all your sessions

Use the JWTAuth class to protect your views. You can use the AuthedRequest type to get annotations for the user and the session:

from ninja import Router
from jwt_ninja.auth_classes import JWTAuth
from jwt_ninja.auth_classes import 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()  # Explicitly save the info for the user's session
    ...

Customization and Configuration

JWT Ninja supports the following settings defined in your Django settings.py:

Setting Type Default
JWT_SECRET_KEY str django_settings.SECRET_KEY
JWT_ALGORITHM str "HS256"
JWT_ACCESS_TOKEN_EXPIRE_SECONDS int 300 (5 minutes)
JWT_REFRESH_TOKEN_EXPIRE_SECONDS int 365 * 3600 (1 year)
JWT_SESSION_EXPIRE_SECONDS int 365 * 3600 (1 year)
JWT_USER_LOGIN_AUTHENTICATOR str "jwt_ninja.authenticators.django_user_authenticator"
JWT_PAYLOAD_CLASS str "jwt_ninja.types.JWTPayload"

Custom Claims

Subclass jwt_ninja.types.JWTPayload with any additional claims:

from jwt_ninja.types import JWTPayload


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

Then add JWT_PAYLOAD_CLASS = "path.to.your.CustomJWTPayload to your settings.py.

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.5.tar.gz (11.3 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.5-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jwtninja-0.1.5.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.4

File hashes

Hashes for jwtninja-0.1.5.tar.gz
Algorithm Hash digest
SHA256 67a9a0c2748c0d39d7e49fbc7031a76c0893ec69f6f6b07f7df3fdf57b30cc70
MD5 bdf232b3d327f4ad0d4b2e079ae2559d
BLAKE2b-256 5ab76ffcee02c670960fc96d7305bea157fc1d8ff251f34055cdf17f7b7d7728

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jwtninja-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.4

File hashes

Hashes for jwtninja-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 67072e46e254a1d158e4ce3ec682f595a78285b189c2af7c062004cbe7dee95c
MD5 60ea99cf56272ffe42f190312ed88c07
BLAKE2b-256 fd19a4988be89d3f580c4913e0cc5209e3fbb8fbd9283a68a4947c48d5a283a9

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