Simple JWT-based authentication using Django and Django-ninja
Project description
django-ninja-simple-jwt
Provides simple JWT based stateless authentication for Django-ninja
Quick start
Install package:
pip install django-ninja-simple-jwt
add ninja_simple_jwt
to list of INSTALLED_APPS
in Django settings:
# settings.py
INSTALLED_APPS = [
...,
"ninja_simple_jwt"
]
Expose Django-ninja
's API and add ninja_simple_jwt
;s auth API endpoint router to the API, ie:
# urls.py
from ninja import NinjaAPI
from ninja_simple_jwt.auth.views.api import mobile_auth_router, web_auth_router
from django.urls import path
api = NinjaAPI()
api.add_router("/auth/mobile/", mobile_auth_router)
api.add_router("/auth/web/", web_auth_router)
urlpatterns = [path("api/", api.urls)]
This would provide 4 available auth API endpoints for mobile and web sign in and token refresh:
- {{server_url}}/api/auth/mobile/sign-in
- {{server_url}}/api/auth/mobile/token-refresh
- {{server_url}}/api/auth/web/sign-in
- {{server_url}}/api/auth/web/token-refresh
To provide a resource API, you can use the HttpJwtAuth
as the auth argument when instantiating a Router, ie:
# views.py
from ninja_simple_jwt.auth.ninja_auth import HttpJwtAuth
from ninja import Router
some_resource_router = Router(auth=HttpJwtAuth())
@some_resource_router.get("/hello")
def hello(request):
return "Hello world"
Finally, before starting up the server, create a key pair to be used by the server for signing and verifying JWT:
python manage.py make_rsa
You should see two files created in the root of project repository:
- jwt-signing.pem # this is the private key used to sign a JWT, keep this secret, store appropriately
- jwt-signing.pub # this is the public key used to verify a JWT
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
Hashes for django-ninja-simple-jwt-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 066a52fa90d724948294cbe92a97a5734099018fe12ab21dd7aed8b1aeae68f4 |
|
MD5 | 0fc09767067d41b138b4e2eddad2d0bd |
|
BLAKE2b-256 | 4acd29e5c38abd4707a1ba4905fd84dede26119daedbe6ab298ef6cd247e3e78 |
Hashes for django_ninja_simple_jwt-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f464d87746d526d011eef0fa5e4cf5534f346d79774ae638459ec535874f016 |
|
MD5 | 4510e8fcb6f269be89c0f7679528a465 |
|
BLAKE2b-256 | 8e504591e6a7faed85ecb2c45168f419c04c5c81181843988b74cca46f9e7811 |