Skip to main content

Ninja JWT

Test PyPI version PyPI version PyPI version PyPI version Codecov Downloads

Abstract

Ninja JWT is a JSON Web Token (JWT) plugin for Django-Ninja. This library is a fork of Simple JWT by Jazzband, a widely-used JWT plugin for the Django REST Framework.

Notice

This library does not address any issues present in the original SIMPLE JWT. It only adds support for Django-Ninja and removes dependencies on DRF. Subsequent updates from SIMPLE JWT will be reflected here over time.

For full documentation, visit this page.

Requirements

  • Python >= 3.6
  • Django >= 2.1
  • Django-Ninja >= 0.16.1
  • Django-Ninja-Extra >= 0.14.2

Example

Checkout this sample project: https://github.com/eadwinCode/bookstoreapi

Installation

Ninja JWT can be installed with pip:

pip install django-ninja-jwt

You also need to register the NinjaJWTDefaultController controller to your Django-Ninja API:

from ninja_jwt.controller import NinjaJWTDefaultController
from ninja_extra import NinjaExtraAPI

api = NinjaExtraAPI()
api.register_controllers(NinjaJWTDefaultController)

The NinjaJWTDefaultController includes three routes: obtain_token, refresh_token, and verify_token. It combines two subclasses, TokenVerificationController and TokenObtainPairController. If you want to customize these routes, you can inherit from these controllers and modify their implementation:

from ninja_extra import api_controller
from ninja_jwt.controller import TokenObtainPairController

@api_controller('token', tags=['Auth'])
class MyCustomController(TokenObtainPairController):
    """obtain_token and refresh_token only"""
    ...
api.register_controllers(MyCustomController)

To use localizations/translations, add ninja_jwt to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'ninja_jwt',
    ...
]

Using Ninja Router

If you prefer not to follow the NinjaExtra methodology, refer to this documentation on how to use Ninja-JWT with Django-Ninja Router.

Usage

To verify that Ninja JWT is working, you can use curl to issue a couple of test requests:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"username": "davidattenborough", "password": "boatymcboatface"}' \
  http://localhost:8000/api/token/pair

...
{
  "access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU",
  "refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"
}

You can use the returned access token to prove authentication for a protected view:

curl \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU" \
  http://localhost:8000/api/some-protected-view/

When this short-lived access token expires, you can use the longer-lived refresh token to obtain another access token:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"}' \
  http://localhost:8000/api/token/refresh

...
{"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNTY3LCJqdGkiOiJjNzE4ZTVkNjgzZWQ0NTQyYTU0NWJkM2VmMGI0ZGQ0ZSJ9.ekxRxgb9OKmHkfy-zs1Ro_xs1eMLXiR17dIDBVxeT-w"}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_ninja_jwt-5.4.5.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ninja_jwt-5.4.5-py3-none-any.whl (88.9 kB view details)

Uploaded Python 3

File details

Details for the file django_ninja_jwt-5.4.5.tar.gz.

File metadata

  • Download URL: django_ninja_jwt-5.4.5.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for django_ninja_jwt-5.4.5.tar.gz
Algorithm Hash digest
SHA256 8985db1dedca901c2dc87a4406f451853453b9b225ecb779ca7611599f416899
MD5 5aafae77d00dc0e536277cf77758e439
BLAKE2b-256 840893f38595abdafb58d247abfd5cfcdaaeebac8d6012665e2b570dcfc6fe9b

See more details on using hashes here.

File details

Details for the file django_ninja_jwt-5.4.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ninja_jwt-5.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 25362551e26001f16f6dc483e92ce211a0764130ee058d397fdb119767e8454b
MD5 07b83eb1aecdf164913f79d5e98c94de
BLAKE2b-256 a72f7dc1b2e8819d2ff13d499ebe2cd8778fbc05c18a2ad9897ae77bb491699c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

5.4.5 This release

2 files

5.4.4

2 files

5.4.3

2 files

5.4.2

2 files

5.4.0

2 files

5.3.9

2 files

5.3.7

2 files

5.3.5

2 files

5.3.4

2 files

5.3.3

2 files

5.3.2

2 files

5.3.1

2 files

5.3.0

2 files

5.2.10

2 files

5.2.9

2 files

5.2.7

2 files

5.2.6

1 file

5.2.5

2 files

5.2.4

2 files

5.2.2

2 files

5.2.0

2 files

5.1.9

2 files

5.1.8

2 files

5.1.6

2 files

5.1.5

2 files

5.1.4

2 files

5.1.3

2 files

5.1.2

2 files

5.1.1

2 files

5.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page