Git Product home page Git Product logo

django-ariadne-jwt's Introduction

Django Ariadne JWT

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

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

django-ariadne-jwt's People

Contributors

eightyeighth avatar marco-rubio avatar unrealsolver avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.