您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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.")