-
Notifications
You must be signed in to change notification settings - Fork 670
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
Allow other header claims in tokens #531
Comments
Seems that the first part of my issue can be done using what has been done in !517 Sadly I couldn't find any issue related to this. Any clue when this will be on Pypi? |
Well after digging into the code I have managed to include the I've had to redefine quite a few classes.
|
Could this be included in the base code? I can open a PR if you wish! |
What we’ve done in the past is have a callable or a dotted import string in SIMPLE_JWT settings. In the serializer, we can pass the token to your function. This is similar to the authorization callable. |
@Andrew-Chen-Wang That might be possible but I don't think this is the way to go as it involves Performancewise, adding a header before encoding it would be much better, don't you think? |
Yes, it definitely would be. I just worry about the ordering and people missing something with override classes. But please open a PR and we shall deliberate :) |
For anyone interested, here is the same for sliding tokens import jwt
import rest_framework_simplejwt.views as original_views
from authlib.jose import JsonWebKey
from django.conf import settings
from rest_framework_simplejwt.backends import TokenBackend
from rest_framework_simplejwt.serializers import TokenObtainSlidingSerializer
from rest_framework_simplejwt.settings import api_settings
from rest_framework_simplejwt.tokens import SlidingToken, Token
class TokenBackendWithHeaders(TokenBackend):
def encode(self, payload, headers={}):
"""
Returns an encoded token for the given payload dictionary.
"""
jwt_payload = payload.copy()
if self.audience is not None:
jwt_payload["aud"] = self.audience
if self.issuer is not None:
jwt_payload["iss"] = self.issuer
token = jwt.encode(jwt_payload, self.signing_key,
algorithm=self.algorithm, headers=headers)
if isinstance(token, bytes):
# For PyJWT <= 1.7.1
return token.decode("utf-8")
# For PyJWT >= 2.0.0a1
return token
class TokenWithAnotherTokenBackend(Token):
_token_backend = TokenBackendWithHeaders(
api_settings.ALGORITHM,
api_settings.SIGNING_KEY,
api_settings.VERIFYING_KEY,
api_settings.AUDIENCE,
api_settings.ISSUER,
api_settings.JWK_URL,
api_settings.LEEWAY,
)
def __init__(self, token=None, verify=True):
Token.__init__(self, token, verify)
self.headers = {}
def __str__(self):
"""
Signs and returns a token as a base64 encoded string.
"""
return self.get_token_backend().encode(self.payload, self.headers)
class SlidingokenWithAnotherTokenBackend(SlidingToken, TokenWithAnotherTokenBackend):
pass
class TokenObtainSlidingSerializerDifferentToken(TokenObtainSlidingSerializer):
token_class = SlidingokenWithAnotherTokenBackend
@classmethod
def get_token(cls, user):
key = JsonWebKey.import_key(
settings.SIMPLE_JWT['VERIFYING_KEY'], {'kty': 'RSA'})
token = cls.token_class.for_user(user)
# Add custom header claims
token.headers['kid'] = key.thumbprint()
return token
class TokenObtainSlidingView(original_views.TokenObtainPairView):
serializer_class = TokenObtainSlidingSerializerDifferentToken |
Has this been incorporated or solved in the latest codebase as I am currently facing the exact same issue of trying to add a 'kid' claim into the header of the signed token? So strange that this is not mentioned anywhere in the docs. |
This is not implemented. |
Would a new settings I'm facing this problem where I want to add |
At this point it would be good to just have the |
As defined in RFC7515, section 4.1, tokens can include several more header claims than just
typ
andalg
as allowed from this.I have tried to include a
kid
one as I use signed token but I couldn't.Using pyjwt I was able to add it to the token string but when I called
RefreshToken(token)
constructor it removed all custom headers.I have checked in the doc and nothing seems to cover this use case.
I haven't digged much in the code though.
As for
kid
claim, I suggest to include it by default in header when the token is signed.(AuthLib documentation for reference)
This is somehow related to #491 as
kid
might be useful when combined with JWK endpoint.The text was updated successfully, but these errors were encountered: