Skip to main content

JSON Web Token for GraphQL

Project description

Pypi Wheel Build Status Codecov Code Climate

JSON Web Token authentication for Django GraphQL

Dependencies

  • Python ≥ 3.4

  • Django ≥ 1.11

Installation

Install last stable version from Pypi.

pip install django-graphql-jwt

Include the JSONWebTokenMiddleware middleware in your MIDDLEWARE settings:

MIDDLEWARE = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ...
]

Include the JSONWebTokenBackend backend in your AUTHENTICATION_BACKENDS settings:

AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]

Schema

Add mutations to the root schema.

import graphene
import graphql_jwt


class Mutations(graphene.ObjectType):
    token_auth = graphql_jwt.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.Verify.Field()
    refresh_token = graphql_jwt.Refresh.Field()

schema = graphene.Schema(mutations=Mutations)
  • tokenAuth to authenticate the user and obtain the JSON Web Token.

The mutation uses your User’s model USERNAME_FIELD, which by default is username.

mutation TokenAuth($username: String!, $password: String!) {
  tokenAuth(username: $username, password: $password) {
    token
  }
}
  • verifyToken to confirm that the token is valid.

mutation VerifyToken($token: String!) {
  verifyToken(token: $token) {
    payload
  }
}
  • refreshToken to obtain a brand new token with renewed expiration time for non-expired tokens.

[wiki] Configure your refresh token scenario and set the flag JWT_VERIFY_EXPIRATION=true.

mutation RefreshToken($token: String!) {
  refreshToken(token: $token) {
    token
    payload
  }
}

Relay

Complete support for Relay.

import graphene
import graphql_jwt


class Mutations(graphene.ObjectType):
    token_auth = graphql_jwt.relay.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.relay.Verify.Field()
    refresh_token = graphql_jwt.relay.Refresh.Field()

Customizing

If you want to customize the ObtainJSONWebToken behavior, you’ll need to customize the .do_auth() method on a subclass of JSONWebTokenMutation or .relay.JSONWebTokenMutation.

import graphene
import graphql_jwt


class ObtainJSONWebToken(graphql_jwt.JSONWebTokenMutation):
    user = graphene.Field(UserType)

    @classmethod
    def do_auth(cls, info):
        return cls(user=info.context.user)

Authenticate the user and obtain the token and the user id.

mutation TokenAuth($username: String!, $password: String!) {
  tokenAuth(username: $username, password: $password) {
    token
    user {
      id
    }
  }
}

Environment variables

JWT_ALGORITHM

Algorithm for cryptographic signing
Default: HS256

JWT_AUDIENCE

Identifies the recipients that the JWT is intended for
Default: None

JWT_ISSUER

Identifies the principal that issued the JWT
Default: None

JWT_LEEWAY

Validate an expiration time which is in the past but not very far
Default: seconds=0

JWT_SECRET_KEY

The secret key used to sign the JWT
Default: settings.SECRET_KEY

JWT_VERIFY

Secret key verification
Default: True

JWT_VERIFY_EXPIRATION

Expiration time verification
Default: False

JWT_EXPIRATION_DELTA

Timedelta added to utcnow() to set the expiration time
Default: minutes=5

JWT_ALLOW_REFRESH

Enable token refresh
Default: True

JWT_REFRESH_EXPIRATION_DELTA

Limit on token refresh
Default: days=7

JWT_AUTH_HEADER_PREFIX

Authorization prefix
Default: JWT

Credits to @jpadilla / django-rest-framework-jwt.

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-graphql-jwt-0.1.4.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_graphql_jwt-0.1.4-py2.py3-none-any.whl (14.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-graphql-jwt-0.1.4.tar.gz.

File metadata

File hashes

Hashes for django-graphql-jwt-0.1.4.tar.gz
Algorithm Hash digest
SHA256 80da76428725982fc1c00d2827dbf64e1e7aef0b3355274306e5a853a97d2ee7
MD5 2f1cb16236f93e7a58c0e989551cf289
BLAKE2b-256 1399286658792a758466f864ee3dc062332afa0f9895a791080621fd92fd0964

See more details on using hashes here.

File details

Details for the file django_graphql_jwt-0.1.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_graphql_jwt-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d66dce45eec6d41247a624a1afb9eac5e714dd04fb743390aff64b413dfd8887
MD5 4bb242ab0056466196892425a2cd9d06
BLAKE2b-256 87d8a819c72cb6fc25a6acd085506dbda735331e9de26c21740bf3a663bb6169

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page