Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Strawberry Django JWT

This project will no longer be updated. Feel free to fork the code or refer to other libraries such as strawberry-django-auth


PyPI - Downloads

GitHub commit activity GitHub last commit

Codecov Codacy grade

JSON Web Token authentication for Strawberry Django GraphQL


Disclaimer

This project is a forked version of Django GraphQL JWT that substitutes Graphene GraphQL backend for Strawberry


Installation

  1. Install last stable version from Pypi:

    pip install strawberry-django-jwt
    
  2. Add AuthenticationMiddleware middleware to your MIDDLEWARE settings:

    MIDDLEWARE = [
        ...,
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        ...,
    ]
    
  3. Add following django apps to INSTALLED_APPS:

    INSTALLED_APPS = [
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        ...,
    ]
    

    If using refresh tokens, also add strawberry_django_jwt.refresh_token

    INSTALLED_APPS = [
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        ...,
        'strawberry_django_jwt.refresh_token',
        ...,
    ]
    
  4. Add JSONWebTokenMiddleware or AsyncJSONWebTokenMiddleware middleware to your STRAWBERRY schema definition:

    from strawberry_django_jwt.middleware import JSONWebTokenMiddleware, AsyncJSONWebTokenMiddleware
    from strawberry import Schema
    
    # !! IMPORTANT !!
    # Pick only one, async middleware is needed when using AsyncGraphQLSchema
    schema = Schema(..., extensions=[
       JSONWebTokenMiddleware,
       AsyncJSONWebTokenMiddleware,
    ])
    
  5. Add JSONWebTokenBackend backend to your AUTHENTICATION_BACKENDS:

    AUTHENTICATION_BACKENDS = [
        'strawberry_django_jwt.backends.JSONWebTokenBackend',
        'django.contrib.auth.backends.ModelBackend',
    ]
    
  6. Add strawberry-django-jwt mutations to the root schema:

  • for sync:
import strawberry
import strawberry_django_jwt.mutations as jwt_mutations

@strawberry.type
class Mutation:
    token_auth = jwt_mutations.ObtainJSONWebToken.obtain
    verify_token = jwt_mutations.Verify.verify
    refresh_token = jwt_mutations.Refresh.refresh
    delete_token_cookie = jwt_mutations.DeleteJSONWebTokenCookie.delete_cookie

schema = strawberry.Schema(mutation=Mutation, query=...)
  • for async:
import strawberry
import strawberry_django_jwt.mutations as jwt_mutations

@strawberry.type
class Mutation:
    token_auth = jwt_mutations.ObtainJSONWebTokenAsync.obtain
    verify_token = jwt_mutations.VerifyAsync.verify
    refresh_token = jwt_mutations.RefreshAsync.refresh
    delete_token_cookie = jwt_mutations.DeleteJSONWebTokenCookieAsync.delete_cookie

schema = strawberry.Schema(mutation=Mutation, query=...)
  1. [OPTIONAL] Set up the custom Strawberry views

    These views set the status code of failed authentication attempts to 401 instead of the default 200.

    from django.urls import re_path
    from strawberry_django_jwt.decorators import jwt_cookie
    from strawberry_django_jwt.views import StatusHandlingGraphQLView as GQLView
    from ... import schema
    
    urlpatterns += \
    [
        re_path(r'^graphql/?$', jwt_cookie(GQLView.as_view(schema=schema))),
    ]
    

    or, for async views:

    from django.urls import re_path
    from strawberry_django_jwt.decorators import jwt_cookie
    from strawberry_django_jwt.views import AsyncStatusHandlingGraphQLView as AGQLView
    from ... import schema
    
    urlpatterns += \
    [
        re_path(r'^graphql/?$', jwt_cookie(AGQLView.as_view(schema=schema))),
    ]
    

Known Issues

  • JWT_ALLOW_ANY_CLASSES

    • Only supports return-type based filtering at the moment, because strawberry does not use class-based field definitions (so all superclasses are dropped)

    • It might be possible to create a workaround by using either a class decorator or by creating a custom graphql scheme that somehow preserves class hierarchy of types

Example Application

To start the example application, install poetry dev dependencies (poetry install will suffice) and run poetry run uvicorn tests.example_app.asgi:application

Quickstart Documentation

===============Work in Progress===============

Relay support has been temporarily removed due to lack of experience with Relay

Most of the features are conceptually the same as those provided by Django GraphQL JWT

Authenticating Fields

Fields can be set to auth-only using the login_required decorator in combination with strawberry.field or via login_field

import strawberry
from strawberry.types import Info
from strawberry_django_jwt.decorators import login_required
from strawberry_django_jwt.decorators import login_field


@strawberry.type
class Query:
    @login_field
    def hello(self, info: Info) -> str:
        return "World"

    @strawberry.field
    @login_required
    def foo(self, info: Info) -> str:
        return "Bar"

    @strawberry.field
    @login_required
    def foo2(self) -> str:
        return "Bar2"

The info argument is optional. If not provided, the login_required decorator decorates the resolver function with a custom function with info.

All required function arguments that are not present in the definition (atm. only info) will be added by the login_required decorator to the self dictionary as kwargs.

Model Mutations

You can add the login_required decorator to them as well

import strawberry
from strawberry_django_jwt.decorators import login_required
from strawberry.django import mutations


@strawberry.type
class Mutation:
    foo_create: FooType = login_required(mutations.create(FooInput))
    foo_delete: FooType = login_required(mutations.update(FooPartialInput))
    foo_update: FooType = login_required(mutations.delete())

Async Views

Should be fully supported :)

import strawberry
from strawberry_django_jwt.decorators import login_field


@strawberry.type
class Query:
    @login_field
    async def foo(self) -> str:
        return "bar"

Other

The introspection query authentication can be controlled by setting JWT_AUTHENTICATE_INTROSPECTION

Metadata

Release files for strawberry-django-auth-jwt 0.2.5.dev1688769246

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strawberry-django-auth-jwt 0.2.5.dev1688769246
File Size Uploaded
strawberry_django_auth_jwt-0.2.5.dev1688769246.tar.gz 29.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for strawberry-django-auth-jwt 0.2.5.dev1688769246
File Interpreter ABI Platform
strawberry_django_auth_jwt-0.2.5.dev1688769246-py3-none-any.whl Python 3 none any Details

Total release size: 80.7 kB

Release files / strawberry_django_auth_jwt-0.2.5.dev1688769246.tar.gz

Download URL strawberry_django_auth_jwt-0.2.5.dev1688769246.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d1952f9f8852c92c9de69a742e7de5032fa3bb7475f4111b4a246b0c6a29e67e
BLAKE2b-256 checksum
How to use checksums
a9213925441a4d085057cf1f27149286c0a4501f0f58f144a61ecd0cf7045e3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.2 Linux/5.15.0-1041-azure

Release files / strawberry_django_auth_jwt-0.2.5.dev1688769246-py3-none-any.whl

Download URL strawberry_django_auth_jwt-0.2.5.dev1688769246-py3-none-any.whl
Size 50.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ed4de3e2fdb70550c5a591e8c104bb009133fa5fbdd50025efc77a0ef9421776
BLAKE2b-256 checksum
How to use checksums
cf98805d450218941cc22c67732e2312e58b47bd50c586c7a00a9dbbcac1ae4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.2 Linux/5.15.0-1041-azure
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