serializers.py 700 B

12345678910111213141516171819202122
  1. from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
  2. from datetime import datetime
  3. class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
  4. @classmethod
  5. def get_token(cls, user):
  6. token = super().get_token(user)
  7. # Add custom claims
  8. token['iat'] = str(datetime.timestamp(datetime.now()))
  9. token["sub"] = {
  10. "acl":["anon"],
  11. }
  12. if user._id:
  13. token["sub"]["acl"].append(str(user._id))
  14. if user.is_superuser:
  15. token["sub"]["acl"].append("admin")
  16. token["sub"]["login"] = user.username
  17. token["sub"]["_id"] = str(user._id)
  18. return token