Remote DRF Auth and handle payload with JWT
I have auth_service, tpm_api and frontend enviroments. All services use the same secret_key. My users and permissions are on the auth_service. I am using jwt_simple for Authentication on auth_service. On the frontend service, I get token from auth_service with username and password. I am sending requests to endpoints in my tpm_api service with this token. I'm parsing the response and displaying it in my frontend service. So far, no problem.
However, I am not getting the token.payload data within the tpm_api service.
I added REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": ( "rest_framework_simplejwt.authentication.JWTAuthentication", ), } in settings.py.
When I send request to tpm_api service, under self.request.authenticators there is <rest_framework.authentication.BasicAuthentication object 0x000001668986C430> and <rest_framework.authentication.SessionAuthentication object at 0x000001668986C6A0>.
I need <rest_framework_simplejwt.authentication.JWTAuthentication object at 0x000001FFDCD23790>.
I don't have user model anywhere except auth_service.
##### auth_service model.py #####
from django.db import models
from django.contrib.auth.models import AbstractUser
perm_parent_choices = [
("app", "app"),
("factory", "factory"),
("department", "department"),
("role", "role"),
]
class User(AbstractUser):
perms = models.ManyToManyField("login.Perm", related_name="user_perms", blank=True)
gsm = models.CharField(max_length=15, null=True)
class Perm(models.Model):
parent = models.CharField(max_length=50, choices=perm_parent_choices, null=True)
name = models.CharField(max_length=50)
short_code = models.CharField(max_length=5)
def __str__(self):
return self.short_code
##### auth_service views.py #####
class UserViewSet(viewsets.ModelViewSet):
serializer_class = serializers.UserSerializer
queryset = models.User.objects.all()
def list(self, request, *args, **kwargs):
##############################
from rest_framework_simplejwt.authentication import JWTAuthentication
JWT_authenticator = JWTAuthentication()
response = JWT_authenticator.authenticate(request)
if response is not None:
# unpacking
user , token = response
request.session["perms"] = token.payload["perms"]
print(request.session["perms"])
# print("this is decoded token claims", token.payload)
else:
print("no token is provided in the header or the header is missing")
##############################
return super().list(request, *args, **kwargs)
##### tpm_api_service views.py #####
class MachineGroupViewSet(viewsets.ModelViewSet):
queryset = models.MachineGroup.objects.all()
serializer_class = serializers.MachineGroupSerializer
# authentication_classes = []
def get_queryset(self):
r = self.request.authenticators
print(r)
from rest_framework_simplejwt.authentication import JWTAuthentication
JWT_authenticator = JWTAuthentication()
response = JWT_authenticator.authenticate(self.request)
return super().get_queryset()
Here I am not seeing the simple_jwt object.