Django Ninja JWT - JSON Web Token for Django-Ninja
Project description
Ninja JWT
Abstract
Ninja JWT is JSON Web Token plugin for Django-Ninja. The library is a fork of Simple JWT by Jazzband, a popular JWT plugin for Django REST Framework.
Notice
This library does not fix any issues from the parent source. It only added support for Django-Ninja and removed restframework dependency, but be rest assured that updates from SIMPLE JWT will reflect here.
For full documentation, visit.
Requirements
- Python >= 3.6
- Django >= 2.1
- Django-Ninja >= 0.16.1
- Ninja-Schema >= 0.12.8
- Django-Ninja-Extra >= 0.14.2
Installation
Ninja JWT can be installed with pip:
pip install django-ninja-jwt
Also, you need to register NinjaJWTDefaultController controller to you Django-Ninja api.
The NinjaJWTDefaultController comes with three routes obtain_token, refresh_token and verify_token
from ninja_jwt.controller import NinjaJWTDefaultController
from ninja_extra import NinjaExtraAPI
api = NinjaExtraAPI()
api.register_controllers(NinjaJWTDefaultController)
The NinjaJWTDefaultController comes with three routes obtain_token, refresh_token and verify_token.
It is a combination of two subclass TokenVerificationController and TokenObtainPairController.
If you wish to customize these routes, you can inherit from these controllers and change its 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)
If you wish to use localizations/translations, simply add ninja_jwt to
INSTALLED_APPS.
INSTALLED_APPS = [
...
'ninja_jwt',
...
]
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"}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-ninja-jwt-5.1.6.tar.gz.
File metadata
- Download URL: django-ninja-jwt-5.1.6.tar.gz
- Upload date:
- Size: 1.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d275e4b5a75856dfbd74381a2fb4b6945901958e48ec1a9d0a6cea6638a18955
|
|
| MD5 |
8f9855c31bd20fd260b5120d6a8ebd16
|
|
| BLAKE2b-256 |
83c5de360ad7170eda25e7266c3b026f2a0bcf4684eeaa462be5323c5c43f73e
|
File details
Details for the file django_ninja_jwt-5.1.6-py3-none-any.whl.
File metadata
- Download URL: django_ninja_jwt-5.1.6-py3-none-any.whl
- Upload date:
- Size: 76.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
113fc03147378cfaa5855aec0ff2d052231ffbcb03e5c12e17a6e39bba96750e
|
|
| MD5 |
e22c05105bdb9f818174d37fd16c7fc0
|
|
| BLAKE2b-256 |
cee43aee52abb00c5323896401155cf8f06613d795552861a434de4135a0fb8e
|