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.JWTMiddleware',
]
Include the JWT backend in your AUTHENTICATION_BACKENDS
settings:
AUTHENTICATION_BACKENDS = [
'ariadne_jwt.backends.JWTBackend',
'django.contrib.auth.backends.ModelBackend'
]
Login
import ariadne
from ariadne_jwt.shortcuts import get_token
from django.contrib.auth import authenticate, login
type_defs = '''
scalar GenericScalar
type Mutation {
...
LogIn(username:String!, password:String!): GenericScaler
...
}
'''
mutation = ariadne.MutationType()
@mutation.field('LogIn')
def resolve_login(obj, info, **kwargs):
user = authenticate(**kwargs)
if user is None:
raise Exception('Please enter a correct username and password')
if not user.is_active:
raise Exception('It seems your account has been disabled')
login(info.context, user)
return {'token': get_token(user)}
Verify and refresh token
Add mutations to your GraphQL schema
import ariadne
from ariadne_jwt import verify, refresh, genericScalar, jwt_schema
type_defs = '''
type Mutation {
...
verifyToken(token: String!): Verify
refreshToken(token: String!): Refresh
...
}
'''
mutation = ariadne.MutationType()
mutation.set_field('verifyToken', verify)
mutation.set_field('refreshToken', refresh)
schema = ariadne.make_executable_schema([type_defs, jwt_schema], [mutation, genericScalar])
verifyToken
to confirm that the JWT is valid.
mutation {
verifyToken(token: "...") {
payload
}
}
refreshToken
to obtain a brand new token with renewed expiration time for non-expired tokens.
mutation {
refreshToken(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.0.tar.gz
(6.0 kB
view hashes)
Built Distribution
Close
Hashes for ariadne_jwt-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7237e7be2bbca2d5fc68ec460c15994524bd8c0159dbe4f74be12f39c6d524a0 |
|
MD5 | 7ba484de77d312d0bda02397adb0675e |
|
BLAKE2b-256 | 75748bb2076554fee11fff7f526031d650ac1ff69265a681b95abbdcf5b4e60e |