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 hashes)

Uploaded Source

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