utils.py 881 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. if user.is_active:
  20. payload["sub"]["acl"].append("active")
  21. if user.is_superuser:
  22. payload["sub"]["acl"].append("admin")
  23. payload["sub"]["login"] = user.username
  24. payload["sub"]["_id"] = str(user._id)
  25. return payload