Skip to main content

Jwt authentication for Django Strawberry GraphQl

Project description

jwtberry

jwtberry Logo

jwtberry is a Python package that simplifies JWT (JSON Web Token) authentication for Django Strawberry GraphQL projects. It provides easy-to-use tools for implementing JWT authentication within your Strawberry-based GraphQL API.

Features

  • Seamless integration with Django Strawberry GraphQL.
  • Simplified JWT authentication setup for your API.
  • Customizable token generation and validation.

Installation

You can install jwtberry using pip:

pip install jwtberry

Usage

  1. Add 'jwtberry.blackberry' app to your Django INSTALLED_APPS in the settings.py file:
INSTALLED_APPS = [
    # ...
    'jwtberry.blackberry',
]
  1. Configure your Django project's AUTHENTICATION_BACKENDS in settings.py to include the JwtAuthBackend from jwtberry:
AUTHENTICATION_BACKENDS = [
    "jwtberry.backends.JwtAuthBackend",
    "django.contrib.auth.backends.ModelBackend",
]
  1. Update your project's urls.py by importing JwtAsyncGraphQLView from jwtberry.views and adding a path for the GraphQL endpoint:
from jwtberry.views import JwtAsyncGraphQLView

urlpatterns = [
    # ...
    path("graphql/", JwtAsyncGraphQLView.as_view(schema=schema)),
]
  1. In your schema.py (where you define your GraphQL schema), make the following changes to include JWT-based authentication:
import strawberry
from jwtberry.mutations import auth_token
from jwtberry.permission import IsAuthenticated
from jwtberry.types import JwtAuthResponse

@strawberry.type
class Query:
    hi: str = strawberry.field(resolver=lambda: "Hello", permission_classes=[IsAuthenticated])

@strawberry.type
class Mutation:
    login: JwtAuthResponse = auth_token

schema = strawberry.Schema(
    query=Query,
    mutation=Mutation,
)

IsAuthenticated is a permission class that checks whether the user has authenticated by verifying the Authorization header.

For more detailed information and examples, check the example project.

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

jwtberry-0.2.0.tar.gz (12.8 kB view details)

Uploaded Source

File details

Details for the file jwtberry-0.2.0.tar.gz.

File metadata

  • Download URL: jwtberry-0.2.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for jwtberry-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dd4da8bb4a8db8d9379a63e4009d06ab9437ad8a8c7d84ee3b763dce4a8d6713
MD5 13a77b7b0905fa8260098363d9d5f0aa
BLAKE2b-256 0326357b26439ff57dd86bce19d743a0a4944bc52ec57344be5034c5e0a067c3

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