KundaPanda / strawberry-django-jwt

[UNMAINTAINED] JSON Web Token (JWT) authentication for Django with Strawberry GraphQL
MIT License
37 stars 16 forks source link

jwt token generation public api #338

Open AriaMoradi opened 2 years ago

AriaMoradi commented 2 years ago

Hey, I had to write this code for creating the correct token payload, can the library be modified to provide an appropriate public api for usecases like this? Sorry if the language is not polite.

from strawberry_django_jwt.object_types import TokenDataType, TokenPayloadType
from strawberry_django_jwt.decorators import on_token_auth_resolve, refresh_expiration
from strawberry_django_jwt.settings import jwt_settings
from calendar import timegm
from datetime import datetime

def generate_jwt_token_payload(info: Info, user: User):
    """
    This little abomination uses strawberry_django_jwt's cursed internals to generate a token
    compatible with `strawberry_django_jwt.mutations.ObtainJSONWebToken.obtain`
    """
    payload = TokenDataType(payload=TokenPayloadType())
    payload = on_token_auth_resolve((info, user, payload))
    payload.refresh_expires_in = timegm(
        datetime.utcnow().utctimetuple()) + jwt_settings.JWT_REFRESH_EXPIRATION_DELTA.total_seconds()
    return payload
nrbnlulu commented 2 years ago

@AriaMoradi I agree.
Though why won't you just call the resolver?

AriaMoradi commented 2 years ago

Though why won't you just call the resolver?

1- My work is being blocked by https://github.com/nrbnlulu/strawberry-django-auth/issues/46 2- There's a use case which needs to provide jwt token in response to a successful OTP verification

KundaPanda commented 2 years ago

Hi, thanks for creating this issue. I myself do not agree with many design choices taken by the original authors of django-graphql-jwt, but doing a complete rewrite would be very time-consuming.

Does the change in #341 suit your needs?

AriaMoradi commented 2 years ago

Looks like it'll work