Ninja JWT
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"}
Metadata
Release files for django-ninja-jwt 5.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_ninja_jwt-5.4.4.tar.gz | 40.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_ninja_jwt-5.4.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 128.9 kB
Release files / django_ninja_jwt-5.4.4.tar.gz
| Download URL | django_ninja_jwt-5.4.4.tar.gz |
|---|---|
| Size | 40.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
14b145b89955ac5c89ed23a430dac388118f7bd9479cbb562fc076f1c3e13f6c
|
|
BLAKE2b-256 checksum How to use checksums |
3e15819d52707db7b9f1fb16f021c961a3d7825c9000061d272b3918658654fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / django_ninja_jwt-5.4.4-py3-none-any.whl
| Download URL | django_ninja_jwt-5.4.4-py3-none-any.whl |
|---|---|
| Size | 88.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc27b86542b4255fa21af0dc652aea15b59066ad5f46296f0c6faa193c5e5924
|
|
BLAKE2b-256 checksum How to use checksums |
26b3cc46af94ed93537d0680fffe5ca80080a3e9a7f4996051625a04b611689f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|