forked from flavors/django-graphql-jwt
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Extend token payload #352
Comments
@KundaPanda can you assist? |
Dirty hack that made this work for me for the time being:
from calendar import timegm
from datetime import datetime
import strawberry
from strawberry_django_jwt.object_types import TokenPayloadType
from strawberry_django_jwt.settings import jwt_settings
def monkey_patch_payload_type():
fields = {
'something': (str, '')
}
for field, data in fields.items():
setattr(TokenPayloadType, field, data[1])
TokenPayloadType.__annotations__[field] = data[0]
strawberry.type(TokenPayloadType)
return TokenPayloadType
def jwt_payload(user, _=None):
username = user.get_username()
if hasattr(username, 'pk'):
username = username.pk
exp = datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA
payload = {
user.USERNAME_FIELD: username,
'exp': timegm(exp.utctimetuple()),
}
if jwt_settings.JWT_ALLOW_REFRESH:
payload['origIat'] = timegm(datetime.utcnow().utctimetuple())
if jwt_settings.JWT_AUDIENCE is not None:
payload['aud'] = jwt_settings.JWT_AUDIENCE
if jwt_settings.JWT_ISSUER is not None:
payload['iss'] = jwt_settings.JWT_ISSUER
payload_data = TokenPayloadType(**payload)
payload_data.something = 'foo bar'
return payload_data
import strawberry
from strawberry_django_jwt.middleware import AsyncJSONWebTokenMiddleware
from app.utils import monkey_patch_payload_type
monkey_patch_payload_type()
...
schema = strawberry.Schema(query=Query, mutation=Mutation, extensions=[
AsyncJSONWebTokenMiddleware,
])
GRAPHQL_JWT = {
...
'JWT_PAYLOAD_HANDLER': 'app.utils.jwt_payload',
} |
Thanks! I also ended up doing it somewhat dirty, by using This can be overwritten in settings as well like
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to get custom fields into the JWT payload to make things work with Hasura. I can set a custom
jwt_payload
function, but I get the problem that there is a hardcoded typeTokenPayloadType
used everywhere which thus throws an error:__init__() got an unexpected keyword argument
.How can I extend tokens to include custom payload?
The text was updated successfully, but these errors were encountered: