Skip to main content

A JSON Web Token authentication extension for the Django REST Framework.

Project description

drf_pyjwt

A JSON Web Token authentication extension for the Django REST Framework

PyPI Version Maintainability

Installation

Using pip

pip install drf_pyjwt

Using pipenv

pipenv install drf_pyjwt

Quick start

In your project’s settings.py add these settings.

DRF_PYJWT_JWKS_URI = "https://api.sample/jwks.json"
DRF_PYJWT_ALGORITHMS = ["RS256"]
DRF_PYJWT_KWARGS = {"audience": "https://api.sample"}

In views.py add PyJWTAuthentication class to authentication classes.

@api_view(["get"])
@authentication_classes([PyJWTAuthentication])
def example(request: Request) -> Response:
    token: dict = request.auth
    print("Access token: {token}")
    return Response({"some": "response"})

By default PyJWTAuthentication cannot lookup user by token, that's why you will get None in request.user. You can provide lookup user function using DRF_PYJWT_LOOKUP_USER setting. After this PyJWTAuthentication will populate request.user using provided function.

Settings Reference

DRF_PYJWT_JWKS_URI

Required: True

Type: str

Value which will be passed as uri argument to jwt.jwks_client.PyJWKClient function

DRF_PYJWT_ALGORITHMS

Required: False

Type: List[str]

Default: ["RS256"]

Example: DRF_PYJWT_ALGORITHMS = "https://dev-87evx9ru.auth0.com/.well-known/jwks.json"

Value which will be passed as algorithms argument to jwt.decode function.

DRF_PYJWT_OPTIONS

Required: False

Type: dict

Example: DRF_PYJWT_OPTIONS = {"verify_exp": False}

Value which will be passed as options argument to jwt.decode function.

DRF_PYJWT_KWARGS

Required: False

Type: dict

Example: DRF_PYJWT_KWARGS = {"audience": "https://app.domain"}

Value which will be passed as **kwargs argument to jwt.decode function.

DRF_PYJWT_LOOKUP_USER

Required: False

Type: str (Import path to the Callable[[dict], Optional[AbstractBaseUser]])

Example:

def lookup_user(token: dict) -> Optional[AbstractBaseUser]:
    user_id = token["custom_claim_user_id"]
    user = User.objects.filter(pk=user_id).first()
    return user

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

drf_pyjwt-0.0.8.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

drf_pyjwt-0.0.8-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file drf_pyjwt-0.0.8.tar.gz.

File metadata

  • Download URL: drf_pyjwt-0.0.8.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for drf_pyjwt-0.0.8.tar.gz
Algorithm Hash digest
SHA256 a31df84ca5156d9cf1470c3759f6c41c3fc58d39710a5ac6159d784b2b94c962
MD5 0e6a93ca11bbda1ea35b14b939a57ae6
BLAKE2b-256 af9e0233636ad9b5d6c1e43b0f2ba9eff232ddf035c0903ecf3ac244bbb84634

See more details on using hashes here.

File details

Details for the file drf_pyjwt-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: drf_pyjwt-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for drf_pyjwt-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 91f5b7516ef25b26536666a9e4ae4ba1e2c26b8f3a9389910c49f2b33e90b980
MD5 e12bdf54b11b0e0b7b3f9fcfd348a4c3
BLAKE2b-256 b3c8e5c706bee4d967a09d17ae864efaeccdc4f21146202807007b47a42159ae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page