2023-07-11 15:21:20 +08:00
|
|
|
import jwt
|
2024-01-12 12:34:01 +08:00
|
|
|
from werkzeug.exceptions import Unauthorized
|
|
|
|
|
2024-07-30 11:15:26 +08:00
|
|
|
from configs import dify_config
|
|
|
|
|
2024-01-12 12:34:01 +08:00
|
|
|
|
2023-07-11 15:21:20 +08:00
|
|
|
class PassportService:
|
|
|
|
def __init__(self):
|
2024-07-30 11:15:26 +08:00
|
|
|
self.sk = dify_config.SECRET_KEY
|
|
|
|
|
2023-07-11 15:21:20 +08:00
|
|
|
def issue(self, payload):
|
2024-08-15 17:53:12 +08:00
|
|
|
return jwt.encode(payload, self.sk, algorithm="HS256")
|
2024-07-30 11:15:26 +08:00
|
|
|
|
2023-07-11 15:21:20 +08:00
|
|
|
def verify(self, token):
|
|
|
|
try:
|
2024-08-15 17:53:12 +08:00
|
|
|
return jwt.decode(token, self.sk, algorithms=["HS256"])
|
2023-07-11 15:21:20 +08:00
|
|
|
except jwt.exceptions.InvalidSignatureError:
|
2024-08-15 17:53:12 +08:00
|
|
|
raise Unauthorized("Invalid token signature.")
|
2023-07-11 15:21:20 +08:00
|
|
|
except jwt.exceptions.DecodeError:
|
2024-08-15 17:53:12 +08:00
|
|
|
raise Unauthorized("Invalid token.")
|
2023-07-11 15:21:20 +08:00
|
|
|
except jwt.exceptions.ExpiredSignatureError:
|
2024-08-15 17:53:12 +08:00
|
|
|
raise Unauthorized("Token has expired.")
|