I am doing a (more-or-less) custom authentication in a django-rest-framework-based application, where I just need to call another microservice to ask it if a token is valid and what username/userid are associated with it. (I don't have a local table for users).
Having found no out-of-the-box solution, I am overriding the dispatch method (I am using a APIView-based view), where I make the request to the remote service and, if the response is not 200, I want to return a 403 error.
Here's my code:
def dispatch(self, request, *args, **kwargs):
try:
user_info = user_from_token_in_request(request)
return super().dispatch(*args, **kwargs)
except:
return Response(
"No Auth Token provided or bad Auth Token"
status.HTTP_403_FORBIDDEN,
)
However, when I pass an invalid token, I get this error: AssertionError: .accepted_renderer not set on Response, because the response context is not initialised when the dispatch method is processed.
Is there a, sort of, more proper way of doing it?