Django Graphql JWT Documentation: Release 0.3.1
Django Graphql JWT Documentation: Release 0.3.1
Release 0.3.1
mongkok
1 Quickstart 3
1.1 Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Authentication 5
2.1 HTTP header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Per-cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.1 Delete Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Per-argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3.1 Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.2 Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.3 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Decorators 9
3.1 @login_required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 @user_passes_test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 @permission_required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 @staff_member_required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.5 @superuser_required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Refresh token 13
4.1 Single token refresh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.1 Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.2 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Long running refresh tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2.1 Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2.2 Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2.3 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2.4 Per-cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.5 Unlimited refresh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.6 One time only use refresh token . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.7 Clear refresh tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5 Customizing 17
i
6 Relay 19
6.1 Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.2 Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.2.1 Single token refresh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.2.2 Long running refresh tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.2.3 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.3 Customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7 Signals 23
7.1 token_issued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7.2 token_refreshed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7.3 refresh_token_rotated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7.4 refresh_token_revoked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
8 Writing tests 25
9 Settings 27
9.1 PyJWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9.1.1 JWT_ALGORITHM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9.1.2 JWT_AUDIENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9.1.3 JWT_ISSUER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9.1.4 JWT_LEEWAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.5 JWT_SECRET_KEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.6 JWT_PUBLIC_KEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.7 JWT_PRIVATE_KEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.8 JWT_VERIFY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.9 JWT_ENCODE_HANDLER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.10 JWT_DECODE_HANDLER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.11 JWT_PAYLOAD_HANDLER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
9.1.12 JWT_PAYLOAD_GET_USERNAME_HANDLER . . . . . . . . . . . . . . . . . . . . . . 29
9.1.13 JWT_GET_USER_BY_NATURAL_KEY_HANDLER . . . . . . . . . . . . . . . . . . . . 29
9.2 Token expiration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.2.1 JWT_VERIFY_EXPIRATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.2.2 JWT_EXPIRATION_DELTA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.3 Refresh token . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.3.1 JWT_ALLOW_REFRESH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.3.2 JWT_REFRESH_EXPIRATION_DELTA . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.3.3 JWT_LONG_RUNNING_REFRESH_TOKEN . . . . . . . . . . . . . . . . . . . . . . . . 29
9.3.4 JWT_REFRESH_TOKEN_MODEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.3.5 JWT_REFRESH_TOKEN_N_BYTES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.3.6 JWT_REUSE_REFRESH_TOKENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.3.7 JWT_REFRESH_EXPIRED_HANDLER . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.3.8 JWT_GET_REFRESH_TOKEN_HANDLER . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.4 Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.4.1 JWT_ALLOW_ANY_HANDLER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.4.2 JWT_ALLOW_ANY_CLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.5 HTTP header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.5.1 JWT_AUTH_HEADER_NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.5.2 JWT_AUTH_HEADER_PREFIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.6 Per-argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.6.1 JWT_ALLOW_ARGUMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.6.2 JWT_ARGUMENT_NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.7 Cookie authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.7.1 JWT_COOKIE_NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
ii
9.7.2 JWT_REFRESH_TOKEN_COOKIE_NAME . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.7.3 JWT_COOKIE_SECURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.7.4 JWT_COOKIE_PATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
9.7.5 JWT_COOKIE_DOMAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
9.7.6 JWT_HIDE_TOKEN_FIELDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
9.8 CSRF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
9.8.1 JWT_CSRF_ROTATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
10 Changelog 33
10.1 0.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10.2 0.3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10.3 0.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10.4 0.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10.5 0.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10.6 0.2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10.7 0.1.14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10.8 0.1.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.9 0.1.12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.10 0.1.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.11 0.1.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.12 0.1.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.13 0.1.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.14 0.1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.15 0.1.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.16 0.1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.17 0.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.18 0.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.19 0.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.20 0.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.21 0.1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.22 0.0.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10.23 0.0.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
11 Contributors 39
11.1 Credits and thanks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Index 41
iii
iv
Django GraphQL JWT Documentation, Release 0.3.1
Contents 1
Django GraphQL JWT Documentation, Release 0.3.1
2 Contents
CHAPTER 1
Quickstart
1.1 Dependencies
• Python 3.4
• Django 1.11
1.2 Installation
MIDDLEWARE = [
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
]
GRAPHENE = {
'SCHEMA': 'mysite.myschema.schema',
'MIDDLEWARE': [
'graphql_jwt.middleware.JSONWebTokenMiddleware',
],
}
3
Django GraphQL JWT Documentation, Release 0.3.1
AUTHENTICATION_BACKENDS = [
'graphql_jwt.backends.JSONWebTokenBackend',
'django.contrib.auth.backends.ModelBackend',
]
1.3 Schema
import graphene
import graphql_jwt
class Mutation(graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
schema = graphene.Schema(mutation=Mutation)
1.4 Queries
4 Chapter 1. Quickstart
CHAPTER 2
Authentication
Django-graphql-jwt uses a Graphene middleware to hook the authenticated user into context object. The simple, raw
way to limit access to data is to check info.context.user.is_authenticated:
import graphene
class Query(graphene.ObjectType):
viewer = graphene.Field(UserType)
As a shortcut, you can use decorators for your resolvers and mutations.
Now in order to access protected API you must include the Authorization HTTP header:
POST / HTTP/1.1
Host: domake.io
Authorization: JWT <token>
Content-Type: application/json;
2.2 Per-cookie
When a token is requested and jwt_cookie decorator is set, the response will set the given cookie with the token
string:
5
Django GraphQL JWT Documentation, Release 0.3.1
urlpatterns = [
path('graphql/', jwt_cookie(GraphQLView.as_view())),
]
In order to prevent XSS (cross-site scripting) attacks, cookies have the HttpOnly flag set, so you cannot delete them
on the client-side. This package includes some mutations to delete the cookies on the server-side.
Add mutations to the root schema:
import graphene
import graphql_jwt
class Mutation(graphene.ObjectType):
delete_token_cookie = graphql_jwt.DeleteJSONWebTokenCookie.Field()
schema = graphene.Schema(mutation=Mutation)
2.3 Per-argument
Another option to send the token is using an argument within the GraphQL query, being able to send a batch of queries
authenticated with different credentials.
6 Chapter 2. Authentication
Django GraphQL JWT Documentation, Release 0.3.1
Django-graphql-jwt looks for the token in the list of arguments sent and if it does not exists, it looks for the token in
the HTTP header.
2.3.1 Settings
GRAPHQL_JWT = {
'JWT_ALLOW_ARGUMENT': True,
}
2.3.2 Schema
Add the token argument in any of your fields using the same name defined in JWT_ARGUMENT_NAME setting:
import graphene
from graphql_jwt.decorators import login_required
class Query(graphene.ObjectType):
viewer = graphene.Field(UserType, token=graphene.String(required=True))
@login_required
def resolve_viewer(self, info, **kwargs):
return info.context.user
2.3.3 Queries
2.3. Per-argument 7
Django GraphQL JWT Documentation, Release 0.3.1
8 Chapter 2. Authentication
CHAPTER 3
Decorators
3.1 @login_required
class Query(graphene.ObjectType):
viewer = graphene.Field(UserType)
@login_required
def resolve_viewer(self, info, **kwargs):
return info.context.user
3.2 @user_passes_test
import graphene
from graphql_jwt.decorators import user_passes_test
(continues on next page)
9
Django GraphQL JWT Documentation, Release 0.3.1
class Query(graphene.ObjectType):
users = graphene.List(UserType)
user_passes_test() takes a required argument: a callable that takes a User object and returns True if the user
is allowed to perform the action. Note that user_passes_test() does not automatically check that the User is
not anonymous.
3.3 @permission_required
permission_required(perm)
Decorator to check whether a user has a particular permission.
Just like the has_perm() method, permission names take the form:
<app-label>.<permission-codename>
The decorator may also take an iterable of permissions, in which case the user must have all of the permissions in
order to access the resolver or mutation:
import graphene
from graphql_jwt.decorators import permission_required
class UpdateUser(graphene.Mutation):
class Arguments:
user_id = graphene.Int()
@classmethod
@permission_required('auth.change_user')
def mutate(cls, root, info, user_id):
...
3.4 @staff_member_required
import graphene
from graphql_jwt.decorators import staff_member_required
(continues on next page)
10 Chapter 3. Decorators
Django GraphQL JWT Documentation, Release 0.3.1
class Query(graphene.ObjectType):
users = graphene.List(UserType)
@staff_member_required
def resolve_users(self, info, **kwargs):
return get_user_model().objects.all()
3.5 @superuser_required
import graphene
from graphql_jwt.decorators import superuser_required
class DeleteUser(graphene.Mutation):
class Arguments:
user_id = graphene.Int()
@classmethod
@superuser_required
def mutate(cls, root, info, user_id):
...
3.5. @superuser_required 11
Django GraphQL JWT Documentation, Release 0.3.1
12 Chapter 3. Decorators
CHAPTER 4
Refresh token
4.1.1 Settings
GRAPHQL_JWT = {
'JWT_VERIFY_EXPIRATION': True,
'JWT_EXPIRATION_DELTA': timedelta(minutes=5),
'JWT_REFRESH_EXPIRATION_DELTA': timedelta(days=7),
}
It means that you need to refresh every 5 mins (payload.exp) and even you keep on refreshing token every 5 mins,
you will still be logout in 7 days after the first token has been issued (refreshExpiresIn).
4.1.2 Queries
• refreshToken to obtain a brand new token with renewed expiration time for non-expired tokens:
13
Django GraphQL JWT Documentation, Release 0.3.1
1. Token issued
when: t = exp
exp = refresh_at + JWT_EXPIRATION_DELTA
verifyToken (t): error! if JWT_VERIFY_EXPIRATION=true
3. Refresh expiration
INSTALLED_APPS = [
...
'graphql_jwt.refresh_token.apps.RefreshTokenConfig',
...
]
4.2.1 Settings
GRAPHQL_JWT = {
'JWT_VERIFY_EXPIRATION': True,
'JWT_LONG_RUNNING_REFRESH_TOKEN': True,
'JWT_EXPIRATION_DELTA': timedelta(minutes=5),
'JWT_REFRESH_EXPIRATION_DELTA': timedelta(days=7),
}
It means that you need to refresh every 5 mins (payload.exp) and you need to replace your refresh token in 7 days
after it has been issued (refreshExpiresIn).
4.2.2 Schema
import graphene
import graphql_jwt
class Mutation(graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
revoke_token = graphql_jwt.Revoke.Field()
schema = graphene.Schema(mutation=Mutation)
4.2.3 Queries
• tokenAuth to authenticate the user and obtain a JSON Web Token and Refresh Token:
• refreshToken to refresh your token, using the refreshToken you already got during authorization:
• revokeToken to revoke a valid refreshToken. The invalidation takes place immediately, and the
refreshToken cannot be used again after the revocation:
4.2.4 Per-cookie
When a refresh token is requested and jwt_cookie decorator is set, the response will set the given cookie with the
refresh token string.
Configure the JWT_REFRESH_EXPIRED_HANDLER setting that checks if the refresh token is expired:
GRAPHQL_JWT = {
'JWT_VERIFY_EXPIRATION': True,
'JWT_LONG_RUNNING_REFRESH_TOKEN': True,
'JWT_REFRESH_EXPIRED_HANDLER': lambda orig_iat, context: False,
}
@receiver(refresh_token_rotated)
def revoke_refresh_token(sender, request, refresh_token, **kwargs):
refresh_token.revoke(request)
optional arguments:
--expired Clears expired tokens
The --expired argument allows the user to remove those refresh tokens whose lifetime is greater than the amount
specified by JWT_REFRESH_EXPIRATION_DELTA setting.
Customizing
If you want to customize the ObtainJSONWebToken behavior, you’ll need to customize the resolve() method
on a subclass of:
class JSONWebTokenMutation(*args, **kwargs)
import graphene
import graphql_jwt
class ObtainJSONWebToken(graphql_jwt.JSONWebTokenMutation):
user = graphene.Field(UserType)
@classmethod
def resolve(cls, root, info, **kwargs):
return cls(user=info.context.user)
Authenticate the user and obtain a JSON Web Token and the user id:
17
Django GraphQL JWT Documentation, Release 0.3.1
18 Chapter 5. Customizing
CHAPTER 6
Relay
6.1 Schema
import graphene
import graphql_jwt
class Mutation(graphene.ObjectType):
token_auth = graphql_jwt.relay.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.relay.Verify.Field()
refresh_token = graphql_jwt.relay.Refresh.Field()
delete_token_cookie = graphql_jwt.relay.DeleteJSONWebTokenCookie.Field()
delete_refresh_token_cookie = \
graphql_jwt.refresh_token.relay.DeleteRefreshTokenCookie.Field()
schema = graphene.Schema(mutation=Mutation)
6.2 Queries
19
Django GraphQL JWT Documentation, Release 0.3.1
• refreshToken to obtain a brand new token with renewed expiration time for non-expired tokens:
• refreshToken to refresh your token, using the refreshToken you already got during authorization:
• revokeToken to revoke a valid refreshToken. The invalidation takes place immediately, and the
refreshToken cannot be used again after the revocation:
6.2.3 Cookies
20 Chapter 6. Relay
Django GraphQL JWT Documentation, Release 0.3.1
mutation {
deleteTokenCookie(input: {}) {
deleted
}
}
mutation {
deleteRefreshTokenCookie(input: {}) {
deleted
}
}
6.3 Customizing
If you want to customize the ObtainJSONWebToken behavior, you’ll need to customize the resolve() method
on a subclass of:
class JSONWebTokenMutation(*args, **kwargs)
import graphene
import graphql_jwt
class ObtainJSONWebToken(graphql_jwt.relay.JSONWebTokenMutation):
user = graphene.Field(UserType)
@classmethod
def resolve(cls, root, info, **kwargs):
return cls(user=info.context.user)
Authenticate the user and obtain a JSON Web Token and the user id:
6.3. Customizing 21
Django GraphQL JWT Documentation, Release 0.3.1
22 Chapter 6. Relay
CHAPTER 7
Signals
7.1 token_issued
7.2 token_refreshed
7.3 refresh_token_rotated
23
Django GraphQL JWT Documentation, Release 0.3.1
7.4 refresh_token_revoked
24 Chapter 7. Signals
CHAPTER 8
Writing tests
class JSONWebTokenTestCase(methodName=’runTest’)
This package includes a subclass of unittest.TestCase and improve support for making GraphQL queries using JSON
Web Token authentication:
class UsersTests(JSONWebTokenTestCase):
def setUp(self):
self.user = get_user_model().objects.create(username='test')
self.client.authenticate(self.user)
def test_get_user(self):
query = '''
query GetUser($username: String!) {
user(username: $username) {
id
}
}'''
variables = {
'username': self.user.username,
}
self.client.execute(query, variables)
25
Django GraphQL JWT Documentation, Release 0.3.1
Settings
Django-graphql-jwt reads your configuration from a single Django setting named GRAPHQL_JWT:
GRAPHQL_JWT = {
'JWT_VERIFY_EXPIRATION': True,
'JWT_EXPIRATION_DELTA': timedelta(minutes=10),
}
9.1 PyJWT
9.1.1 JWT_ALGORITHM
9.1.2 JWT_AUDIENCE
9.1.3 JWT_ISSUER
27
Django GraphQL JWT Documentation, Release 0.3.1
9.1.4 JWT_LEEWAY
Validate an expiration time which is in the past but not very far
Default: timedelta(seconds=0)
9.1.5 JWT_SECRET_KEY
9.1.6 JWT_PUBLIC_KEY
The RSA public key for RS256, RS384 or RS512 asymmetric algorithms. JWT_SECRET_KEY setting
will be ignored
Default: None
9.1.7 JWT_PRIVATE_KEY
The RSA private key for RS256, RS384 or RS512 asymmetric algorithms. JWT_SECRET_KEY setting
will be ignored
Default: None
9.1.8 JWT_VERIFY
9.1.9 JWT_ENCODE_HANDLER
9.1.10 JWT_DECODE_HANDLER
9.1.11 JWT_PAYLOAD_HANDLER
28 Chapter 9. Settings
Django GraphQL JWT Documentation, Release 0.3.1
9.1.12 JWT_PAYLOAD_GET_USERNAME_HANDLER
9.1.13 JWT_GET_USER_BY_NATURAL_KEY_HANDLER
9.2.1 JWT_VERIFY_EXPIRATION
9.2.2 JWT_EXPIRATION_DELTA
9.3.1 JWT_ALLOW_REFRESH
9.3.2 JWT_REFRESH_EXPIRATION_DELTA
9.3.3 JWT_LONG_RUNNING_REFRESH_TOKEN
9.3.4 JWT_REFRESH_TOKEN_MODEL
9.3.5 JWT_REFRESH_TOKEN_N_BYTES
9.3.6 JWT_REUSE_REFRESH_TOKENS
Reuse the long running refreshed token instead of generating a new one
Default: False
9.3.7 JWT_REFRESH_EXPIRED_HANDLER
9.3.8 JWT_GET_REFRESH_TOKEN_HANDLER
9.4 Permissions
9.4.1 JWT_ALLOW_ANY_HANDLER
9.4.2 JWT_ALLOW_ANY_CLASSES
30 Chapter 9. Settings
Django GraphQL JWT Documentation, Release 0.3.1
9.5.1 JWT_AUTH_HEADER_NAME
9.5.2 JWT_AUTH_HEADER_PREFIX
9.6 Per-argument
9.6.1 JWT_ALLOW_ARGUMENT
9.6.2 JWT_ARGUMENT_NAME
9.7.1 JWT_COOKIE_NAME
The name of the cookie when HTTP cookies are used as a valid transport for the token
Default: 'JWT'
9.7.2 JWT_REFRESH_TOKEN_COOKIE_NAME
The name of the cookie when HTTP cookies are used as a valid transport for the refresh token
Default: 'JWT-refresh-token'
9.7.3 JWT_COOKIE_SECURE
Whether to use a secure cookie for the JWT cookie. If this is set to True, the cookie will be marked as
“secure”, which means browsers may ensure that the cookie is only sent under an HTTPS connection
Default: False
9.7.4 JWT_COOKIE_PATH
9.7.5 JWT_COOKIE_DOMAIN
9.7.6 JWT_HIDE_TOKEN_FIELDS
For cookie-based authentications, remove the token fields from the GraphQL schema in order to prevent
XSS exploitation
Default: False
9.8 CSRF
9.8.1 JWT_CSRF_ROTATION
32 Chapter 9. Settings
CHAPTER 10
Changelog
10.1 0.3.1
10.2 0.3.0
10.3 0.2.3
33
Django GraphQL JWT Documentation, Release 0.3.1
10.4 0.2.2
• Removed DjangoMiddleware
• Added dutch and french locales
• Added JWT Refresh token cookie
• Added signals
• Added JWT_GET_USER_BY_NATURAL_KEY_HANDLER
10.5 0.2.1
10.6 0.2.0
10.7 0.1.14
10.8 0.1.13
10.9 0.1.12
10.10 0.1.11
10.11 0.1.10
• Added JWTSettings
• Added jwt-handlers to settings
• Added context argument to jwt-handlers
10.12 0.1.9
10.13 0.1.8
10.14 0.1.7
• Added anonymous-hyperlink
10.8. 0.1.13 35
Django GraphQL JWT Documentation, Release 0.3.1
10.15 0.1.6
10.16 0.1.5
10.17 0.1.4
10.18 0.1.3
10.19 0.1.2
• Shortcuts, get_token
• Modified Refresh output fields
• Updated README, don’t include the token as a UserType field
10.20 0.1.1
10.21 0.1.0
10.22 0.0.2
10.23 0.0.1
• xin chào!
10.22. 0.0.2 37
Django GraphQL JWT Documentation, Release 0.3.1
Contributors
• Dani, @mongkok
• Lennart Kerkvliet, @lennartkerkvliet
• Abdullah Hilson, @abumalick
• Vaibhav Shelke, @vshelke
• Kleber Soares, @klebercode
• @jxltom
• Sultan Iman, @imanhodjaev
• Øyvind Saltvik, @fivethreeo
• William Mai, @wmai
• Víðir Valberg Guðmundsson, @valberg
• Patryk, @patryk-tech
• Christian González, @nerdoc
• @mr-asher
• Florian Schade, @fschade
• Aaron Boman @frenchtoast747
• Colton Hicks @coltonbh
• Jarosław Wygoda, @jwygoda
• Kamil Rykowski, @vintage
• @mtszsobczak
• Lasse Steffen @lassesteffen
• @TitanFighter
39
Django GraphQL JWT Documentation, Release 0.3.1
• @jpadilla / django-rest-framework-jwt
• @jonatasbaldin / howtographql
G U
get_refresh_token_by_model() (in module user_passes_test() (in module
graphql_jwt.refresh_token.utils), 30 graphql_jwt.decorators), 9
get_user_by_natural_key() (in module
graphql_jwt.utils), 29
H
handle() (Command method), 16
J
JSONWebTokenMutation (class in graphql_jwt), 17
JSONWebTokenMutation (class in
graphql_jwt.relay), 21
JSONWebTokenTestCase (class in
graphql_jwt.testcases), 25
jwt_decode() (in module graphql_jwt.utils), 28
jwt_encode() (in module graphql_jwt.utils), 28
jwt_payload() (in module graphql_jwt.utils), 28
L
login_required (in module
graphql_jwt.decorators), 9
P
permission_required() (in module
graphql_jwt.decorators), 10
R
refresh_has_expired() (in module
graphql_jwt.utils), 30
RefreshToken (class in
graphql_jwt.refresh_token.models), 30
S
staff_member_required (in module
graphql_jwt.decorators), 10
41