Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Invalid payload" error #335

Open
IliasVilux opened this issue Sep 4, 2024 · 0 comments
Open

"Invalid payload" error #335

IliasVilux opened this issue Sep 4, 2024 · 0 comments

Comments

@IliasVilux
Copy link

IliasVilux commented Sep 4, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant