Skip to main content

Authorize request with JSON Web Tokens

Project description

django_jwt_middleware

django_jwt_middleware is a package used for authorize django request with JSON Web Tokens

1. Installation

pip3 install django-jwt-middleware

or clone

git clone https://github.com/rizki4106/django_jwt_middleware.git

2. Example

with http cookie

If you store jwt key on cookie you can use @middleware.with_cookie decorator

from django_jwt_middleware import middleware
from rest_framework.decorators import api_view
from rest_framework.response import Response

@middleware.with_cookie(cookie_name="example-token", jwt_key="supersecretkey", algorithm="HS256")
def hello_world(request):
    return Response(data={
        "message": "hello world"
    })

@middleware.with_cookie takes 3 arguments

name type description
cookie_name string name of cookie that store your jwt token
jwt_key string jsonwebtoken secret key
algoritm string algorithm to decoded your jwt key default is HS256

With http headers

If you store jwt key http headers you can use @middleware.with_header decorator

from django_jwt_middleware import middleware
from rest_framework.decorators import api_view
from rest_framework.response import Response

@middleware.with_header(header_name="example-token", jwt_key="supersecretkey", algorithm="HS256")
def hello_world(request):
    return Response(data={
        "message": "hello world"
    })

@middleware.with_header takes 3 arguments

name type description
header_name string name of header that store your jwt token
jwt_key string jsonwebtoken secret key
algoritm string algorithm to decoded your jwt key default is HS256

3. Response

Type Description
success Will continue to request destination
failed Return http status code 403 with the message in it

4. Discovered Problems

if you get error and it said jwt has no attribute decode try this command

pip3 uninstall pyjwt
pip3 install pyjwt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_jwt_middleware-1.1.3.tar.gz (3.4 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page