You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324
  1. import jwt
  2. from werkzeug.exceptions import Unauthorized
  3. from configs import dify_config
  4. class PassportService:
  5. def __init__(self):
  6. self.sk = dify_config.SECRET_KEY
  7. def issue(self, payload):
  8. return jwt.encode(payload, self.sk, algorithm="HS256")
  9. def verify(self, token):
  10. try:
  11. return jwt.decode(token, self.sk, algorithms=["HS256"])
  12. except jwt.exceptions.ExpiredSignatureError:
  13. raise Unauthorized("Token has expired.")
  14. except jwt.exceptions.InvalidSignatureError:
  15. raise Unauthorized("Invalid token signature.")
  16. except jwt.exceptions.DecodeError:
  17. raise Unauthorized("Invalid token.")
  18. except jwt.exceptions.PyJWTError: # Catch-all for other JWT errors
  19. raise Unauthorized("Invalid token.")