Skip to main content

Ninja JWT

Test PyPI version PyPI version PyPI version PyPI version

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.2
  • Django-Ninja-Extra >= 0.11.0

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_jwt.controller import TokenObtainPairController, router

@router('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"}

Release files for django-ninja-jwt 5.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-ninja-jwt 5.1.4
File Size Uploaded Provenance Actions
django-ninja-jwt-5.1.4.tar.gz 1.4 MB Without provenance Details for django-ninja-jwt-5.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-ninja-jwt 5.1.4
File Interpreter ABI Platform Actions
django_ninja_jwt-5.1.4-py3-none-any.whl Python 3 none any Details for django_ninja_jwt-5.1.4-py3-none-any.whl

Total release size: 1.5 MB

Release files / django-ninja-jwt-5.1.4.tar.gz

Download URL django-ninja-jwt-5.1.4.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
653cc319a1bb0f10aaf00f0b9809d3810ebe929cc77bacd144986813345f4bf9
BLAKE2b-256 checksum
How to use checksums
993d7ffa88dead19b34aa8f66aed41f49f3262bdddafca2d0ba2f7e97bca9953
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release files / django_ninja_jwt-5.1.4-py3-none-any.whl

Download URL django_ninja_jwt-5.1.4-py3-none-any.whl
Size 69.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b898815c1dbec47d12daa29a22ad5d0a7af2861ebdd6d1491a566cfc56c43059
BLAKE2b-256 checksum
How to use checksums
0d8fbeb7a37301dd1c82f8cfe063524db04ead539f14bee02506a30934f3a063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Security for django-ninja-jwt 5.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

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