Skip to main content

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

Metadata

Release files for django-jwt-middleware 1.1.3

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-jwt-middleware 1.1.3
File Size Uploaded
django_jwt_middleware-1.1.3.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-jwt-middleware 1.1.3
File Interpreter ABI Platform
django_jwt_middleware-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / django_jwt_middleware-1.1.3.tar.gz

Download URL django_jwt_middleware-1.1.3.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fcc0ce419cfbbbe3e53cc0257de984edea45ba1176e4359761737960731548c7
BLAKE2b-256 checksum
How to use checksums
9f115359f3c6946982e49d458036005ca789949dc024fe68713452b1f58485db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / django_jwt_middleware-1.1.3-py3-none-any.whl

Download URL django_jwt_middleware-1.1.3-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70616e30049e22befc425d1c01e98c62cc179db5a0ca0f67d982c1981784569e
BLAKE2b-256 checksum
How to use checksums
19ba09adf2c0ade4ce0ec432a52601b4fa944b71f1d41fbdab2dfa3cdf392b0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

1 release file

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