djangorestframework-simplejwt
djangorestframework-simplejwt copied to clipboard
custom token for other entity
In my project, there are several entities that must be able to authorize. How can I write a custom token generation for each entity. p.s. there was an option to use RefreshToken.for_user but it only uses the user model
You're definitely on the right track! I highly recommend overriding for_user
such that you can decide which model to override. Unfortunately, I think this is a complex case that is out of scope for this library's implementation, but we're happy to guide!
I believe a good solution would be to extend the RefreshToken class' constructor such that it can accept a new value that you determine in the view/serializer. In the view/serializer, you can determine which entity to select, then pass that entity. For example (the below example may be slightly wrong as I answer a ton of issues, but it'll get you on the right track):
class RefreshToken(rest_framework_simplejwt):
def __init__(self, *args, **kwargs):
self.entity_to_use = kwargs.get("entity")
def for_user(self, *args, **kwargs):
# Override this portion to define your routing
# In settings.py
"refresh_token_class": "path.to.RefreshToken"