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
}
}
Customizing
If you want to customize the tokenAuth
behavior, you'll need to extend the TokenAuth
type and write a resolver with @token_auth decorator.
from ariadne_jwt.decorators import token_auth
extended_type_defs='''
type UserNode {
id
username
email
}
extend type TokenAuth {
user: UserNode
}
'''
@token_auth
def resolve_token_auth(obj, info, **kwargs):
return { 'user':info.context.get('request').user }
mutation TokenAuth($username: String!, $password: String!) {
tokenAuth(username: $username, password: $password) {
token
user {
id
}
}
}
Authentication in GraphQL queries
Now in order to access protected API you must include the Authorization: JWT <token>
header.
you can use the login_required()
decorator for your resolvers:
from ariadne import QueryType
from ariadne_jwt.decorators import login_required
type_defs='''
type UserNode {
username:String
email: String
}
type Query {
me: UserNode
}
'''
query=QueryType()
@query.field('me')
@login_required
def resolve_viewer(self, info, **kwargs):
return info.context.get('request').user
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
Built Distribution
Hashes for ariadne_jwt-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2ed66289a55069608bf15524a0a2f6c47129166cfec693ba71f9d2eec1552be |
|
MD5 | 50db384b66119056f887a98851ce7da9 |
|
BLAKE2b-256 | c1a98b9dd81d21b40f6bdfcda8247ed40b860b1ce2d4481913cf49bcce54c91e |