JSON Web Token for Ariadne Django
Project description
Ariadne JWT
JSON Web Token for Ariadne Django
Installation
pip install ariadne-jwt
Include the JWT middleware in your MIDDLEWARE
settings:
MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'ariadne_jwt.middleware.JSONWebTokenMiddleware',
]
Include the JWT backend in your AUTHENTICATION_BACKENDS
settings:
AUTHENTICATION_BACKENDS = [
'ariadne_jwt.backends.JSONWebTokenBackend',
'django.contrib.auth.backends.ModelBackend'
]
Schema
Add mutations to your GraphQL schema
import ariadne
from ariadne_jwt import resolve_verify, resolve_refresh, resolve_token_auth, jwt_schema, GenericScalar
type_defs = '''
type Mutation {
...
verifyToken(token: String!): VerifyToken
refreshToken(token: String!): RefreshToken
tokenAuth(username: String!, password:String!): TokenAuth
...
}
'''
mutation = ariadne.MutationType()
mutation.set_field('verifyToken', resolve_verify)
mutation.set_field('refreshToken', resolve_refresh)
mutation.set_field('tokenAuth', resolve_token_auth)
schema = ariadne.make_executable_schema([type_defs, jwt_schema], [mutation, GenericScalar])
tokenAuth
to authenticate the user and obtain the JSON Web Token.
The resolver uses 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.
mutation RefreshToken($token: String!) {
refreshToken(token: $token) {
token
payload
}
}
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
ariadne-jwt-0.1.1.tar.gz
(6.3 kB
view hashes)
Built Distribution
Close
Hashes for ariadne_jwt-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2c302b3c1ceac95a8bb37868d032b9e5285654c698aadb1881b278b540b7aaf |
|
MD5 | 460c550a5b7f6e00a99cbeccb5b3483d |
|
BLAKE2b-256 | 7914a8c33aac3f32866174ec98d33791f27b61425e9a384fe7fcff908eb82789 |