Files
dify/api/libs/passport.py
T
6f434baf93 ci: enable Ruff B904 (#44121)
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: hjlarry <hjlarry@163.com>
2026-10-19 12:47:53 +00:00

25 lines
911 B
Python

import jwt
from werkzeug.exceptions import Unauthorized
from configs import dify_config
class PassportService:
def __init__(self):
self.sk = dify_config.SECRET_KEY
def issue(self, payload):
return jwt.encode(payload, self.sk, algorithm="HS256")
def verify(self, token):
try:
return jwt.decode(token, self.sk, algorithms=["HS256"])
except jwt.ExpiredSignatureError as caught_error:
raise Unauthorized("Token has expired.") from caught_error
except jwt.InvalidSignatureError as caught_error:
raise Unauthorized("Invalid token signature.") from caught_error
except jwt.DecodeError as caught_error:
raise Unauthorized("Invalid token.") from caught_error
except jwt.PyJWTError as caught_error: # Catch-all for other JWT errors
raise Unauthorized("Invalid token.") from caught_error