-
Notifications
You must be signed in to change notification settings - Fork 177
Description
Hi! I've integrated django-graphql-jwt into my Django project, and I'm encountering an issue when attempting to query data. Below is a summary of my setup:
settings.py:
GRAPHENE = {
"SCHEMA": "core.schema.schema",
"MIDDLEWARE": [
"graphql_jwt.middleware.JSONWebTokenMiddleware",
],
}
AUTHENTICATION_BACKENDS = (
"graphql_jwt.backends.JSONWebTokenBackend", # Added this line
"django_auth_ldap.backend.LDAPBackend",
"django.contrib.auth.backends.ModelBackend",
)
GRAPHQL_JWT = {
"JWT_PAYLOAD_HANDLER": "core.utils.jwt_payload_handler",
"JWT_DECODE_HANDLER": "graphql_jwt.utils.jwt_decode",
"JWT_ENCODE_HANDLER": "graphql_jwt.utils.jwt_encode",
}The query in my schema:
from graphql_jwt.decorators import login_required
class Query(graphene.ObjectType):
colaboradores = graphene.List(ColaboradorType)
@login_required
def resolve_colaboradores(self, info):
return Colaborador.objects.all()When I execute the colaboradores query, I encounter the following error:
{
"errors": [
{
"message": "Invalid payload",
"locations": [
{
"line": 2,
"column": 2
}
],
"path": [
"colaboradores"
]
}
],
"data": {
"colaboradores": null
}
}
Additional Details:
I have LDAP configured in the project, and I'm not sure if this might be contributing to the issue.
To troubleshoot, I created a custom JWT payload handler to replace the user ID with the username, but this hasn't resolved the issue. Below is the code for the custom payload handler:
from django.contrib.auth import get_user_model
import jwt
User = get_user_model()
def jwt_payload_handler(request):
"""
Custom payload handler for JWT.
"""
try:
token_jwt = request.data.get("token")
decoded_payload = jwt.decode(token_jwt)
user_id = decoded_payload.get("user_id")
user = User.objects.get(pk=user_id)
custom_payload = {
"token_type": decoded_payload.get("token_type"),
"exp": decoded_payload.get("exp"),
"iat": decoded_payload.get("iat"),
"jti": decoded_payload.get("jti"),
"username": user.username,
}
return custom_payload
except jwt.ExpiredSignatureError:
raise ValueError("Token expired")
except jwt.DecodeError:
raise ValueError("Token decode error")
except jwt.InvalidTokenError:
raise ValueError("Invalid token")
except User.DoesNotExist:
raise ValueError("User not found")Despite these efforts, the issue persists. I would appreciate any guidance on how to resolve this "Invalid payload" error or any insights into whether the LDAP configuration could be affecting the JWT handling.
Thank you for your help!