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 auth_router
from django.urls import path
api = NinjaAPI()
api.add_router("/auth/", 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.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4136d6f2b9305a27d14bb5b16c02086380f8189478f244b0f2e57811cafac677 |
|
MD5 | 73478076362e660f3d7e6b5b6e6924df |
|
BLAKE2b-256 | a937e7224e35997beaad59232ec9b8cb66e652663313afd48d7bef743e2da7b0 |
Hashes for django_ninja_simple_jwt-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb79908a714e906a19d9adb9f64afa692c2cbd0e091978b626b4a7779d8a7f05 |
|
MD5 | 6c2c9fda50646110017661cf88662fef |
|
BLAKE2b-256 | 986615a5750dda3d3ab80a6447bb20a4df17fb199b87a62d9cb88f763804394a |