utils.py 847 B

123456789101112131415161718192021222324252627282930313233343536
  1. from datetime import datetime
  2. from graphql_jwt.settings import jwt_settings
  3. from store_back.settings import GRAPHQL_JWT
  4. from calendar import timegm
  5. ## JWT payload for Hasura
  6. def jwt_payload(user, context=None):
  7. username = user.get_username()
  8. exp = datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA
  9. payload = {
  10. user.USERNAME_FIELD: username,
  11. "exp": timegm(exp.utctimetuple()),
  12. }
  13. payload['iat'] =timegm(datetime.utcnow().utctimetuple())
  14. payload["sub"] = {
  15. "acl":["anon"],
  16. }
  17. if user._id:
  18. payload["sub"]["acl"].append(str(user._id))
  19. payload["sub"]["acl"].append("user")
  20. if user.is_superuser:
  21. payload["sub"]["acl"].append("admin")
  22. payload["sub"]["login"] = user.username
  23. payload["sub"]["_id"] = str(user._id)
  24. return payload