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 jwt_ninja.api import router as auth_router


api = NinjaAPI()

api.add_router("auth/", auth_router)

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 supplied AuthedRequest type to get annotations for the user and the session:

from jwt_ninja.auth_classes import JWTAuth
from jwt_ninja.types import AuthedRequest


@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.3.tar.gz (11.2 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.3-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for jwtninja-0.1.3.tar.gz
Algorithm Hash digest
SHA256 effb34892353a9d7d9ec13ba57fa7e4dc1dfe4e3df422648e6bd971e0715022c
MD5 e6a38a70bd7e83ce2dada79bbafb0c19
BLAKE2b-256 14f060c5d4e3a91be3a0beb2ab7a0ea5db59cf7bbdc1c1dad12a8d4def273c86

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for jwtninja-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2f758e8e99f2a23ec51da943948e79d756434c78a70b62b887e4230d4f88b692
MD5 fbed37e51e5928a74b69b0175b285d45
BLAKE2b-256 521e4c9828879581b4b7a073fbb1b88628af6a95c217be2c921d38f337f843b5

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