Skip to main content

JSON Web Token for Django Ariadne

Project description

Support for JWT based authentication for use with the ariadne graphql library running inside a Django project. It is heavily inspired by django-graph-jwt.

Installation

pip install django-ariadne-jwt

How to use

django-ariadne-jwt aims to be easy to install and use.

First add JSONWebTokenBackend to your AUTHENTICATION_BACKENDS

AUTHENTICATION_BACKENDS = [
  "django_ariadne_jwt.backends.JSONWebTokenBackend",
  # Any other authentication backends...
  "django.contrib.auth.backends.ModelBackend",
]

Then add JSONWebTokenMiddleware to your view

from django_ariadne_jwt.middleware import JSONWebTokenMiddleware

urlpatterns = [
  # Your other paths...
  path(
      "graphql/",
      csrf_exempt(
          GraphQLView.as_view(
              schema=schema, middleware=[JSONWebTokenMiddleware()]
          )
      ),
      name="graphql"
  )
]

Or to your queries:

ariadne.graphql_sync(
    schema,
    {
        "query": """
        query {
            test
        }
        """
    },
    middleware=[JSONWebTokenMiddleware()],
)

And then add the login_decorator to your resolvers before adding the field:

from django_ariadne_jwt.decorators import login_required

@query_type.field("test")
@login_required
def resolve_test(*args):
  ...

This will prevent the field from resolving and ariadne will add an error to the query result.

Finally add the type definitions and resolvers to the executable schema

from django_ariadne_jwt.resolvers import (
  auth_token_definition,
  auth_token_verification_definition,
  resolve_token_auth,
  resolve_refresh_token,
  resolve_verify_token,
)

type_definitions = """
  ...

  type Mutation {
    ...
    tokenAuth(username: String!, password: String!): AuthToken!
    refreshToken(token: String!): AuthToken!
    verifyToken(token: String!): AuthTokenVerification!
    ...
  }
"""

auth_type_definitions = [
  auth_token_definition,
  auth_token_verification_definition,
]

resolvers = [
  ...
]

auth_resolvers = [
  resolve_token_auth,
  resolve_refresh_token,
  resolve_verify_token,
]

schema = ariadne.make_executable_schema(
  [type_definitions] + auth_type_definitions, resolvers + auth_resolvers
)

Once you get an auth token, set the HTTP Authorization header to:

Token <token>

How to contribute

django-ariadne-jwt is at a very early stage. It is currently missing documentation, better testing and a lot of configuration options. Pull requests with any of these are greatly appreciated.

django-ariadne-jwt is missing feature X

Feel free to open an issue or create a pull request with the implementation

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

django-ariadne-jwt-0.2.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

django_ariadne_jwt-0.2.0-py2.py3-none-any.whl (8.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-ariadne-jwt-0.2.0.tar.gz.

File metadata

  • Download URL: django-ariadne-jwt-0.2.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for django-ariadne-jwt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9e6462803be25b878494196946b9036858487274093e1d94d8efcc9b016a278c
MD5 d807a58ceae9eb127e3d5343a4e73354
BLAKE2b-256 16fa6f36a28bb4b23c253263c0de600423a719ff233c15ca80ba5d15f0796f6a

See more details on using hashes here.

File details

Details for the file django_ariadne_jwt-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_ariadne_jwt-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for django_ariadne_jwt-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb6fd931720eea8f846bd1dbabd0e054a81bca637137630975bc9addce840427
MD5 1d8bd50d90b42038533d8f33ec827680
BLAKE2b-256 11000dede1b8d3aea384415711b53ee8954759e7d7cd4bf6f07a081c86166eee

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