Strawberry-graphql port of the graphene-django-jwt package
Project description
Strawberry Django JWT
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
-
Install last stable version from Pypi:
pip install strawberry-django-jwt
-
Add
AuthenticationMiddleware
middleware to your MIDDLEWARE settings:MIDDLEWARE = [ ..., 'django.contrib.auth.middleware.AuthenticationMiddleware', ..., ]
-
Add
JSONWebTokenMiddleware
orAsyncJSONWebTokenMiddleware
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, ])
-
Add
JSONWebTokenBackend
backend to your AUTHENTICATION_BACKENDS:AUTHENTICATION_BACKENDS = [ 'strawberry_django_jwt.backends.JSONWebTokenBackend', 'django.contrib.auth.backends.ModelBackend', ]
-
Add strawberry-django-jwt mutations to the root schema:
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=...)
-
[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
-
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"
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 strawberry-django-jwt-0.1.7.dev1635582694.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07a17bd2d0320d23aaa971fb51e08142ab2bae78ab123f58ba2303348f69c16a |
|
MD5 | 08a181229b88a4ce9509cc49d6d7ec31 |
|
BLAKE2b-256 | 1bf5233b0816b17dcc9806cbd19a321dead98de5fb843ac907164d4b4ba208bd |
Hashes for strawberry_django_jwt-0.1.7.dev1635582694-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e867130f6c9b837d4e978843640664f316fc295f2b340a9a7b5337644a22f0d |
|
MD5 | 2f7018bb2b0522dfeb5333dadb4f9842 |
|
BLAKE2b-256 | 0f614cc89e9adc60693069d397aaad9ea2530d099382e0074331e3e0863c6c5c |