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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for jwtninja-0.1.6.tar.gz
Algorithm Hash digest
SHA256 57b82346ece9d5e4737f878a8b2bb70c0accdb7616b5f5eee40cb7e7462363bc
MD5 2303386afd738fd1773a4ea519dffa1f
BLAKE2b-256 87cd90240dab7b93af3ad9f6c2b451a5dc846a57166b3e9d46f058b5c99f1f61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jwtninja-0.1.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4b5ebbc56fd137b6a2742dfbc48adbae38689f4cfee5a09fbdbfa5a1d2165c3c
MD5 32190cddc305b401baefd93ad490ff0c
BLAKE2b-256 c7382a5ed0a30f29911577be51c7c870cda8b40cc458845093c11a885638f40b

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