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

Uploaded Python 3

File details

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

File metadata

  • Download URL: jwtninja-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 102a992090a405b13bad741ab49462fc2c69ddbef397739c0d107d106a9405ff
MD5 f3990c05ae0d11bdd8c5b18f516896ff
BLAKE2b-256 1952532638ad68d9ec00ab24db28bd6249f0009268ec58a7f90c49020fb30c7d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jwtninja-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 78ae26b8dc71bb84976ec40071c91622077ffa7eadfbab372bca5ead86101c58
MD5 3202a01078dba9093378b5e1811bc7a5
BLAKE2b-256 5a0ce4a61ceb9ff90c103a4aa9a67d21ffcb4c6c221613943cac58521cf8b934

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