2024-08-30 21:25:58 +08:00
|
|
|
from core.plugin.entities.request import RequestInvokeEncrypt
|
|
|
|
from core.tools.utils.configuration import ProviderConfigEncrypter
|
|
|
|
from models.account import Tenant
|
|
|
|
|
|
|
|
|
|
|
|
class PluginEncrypter:
|
|
|
|
@classmethod
|
2024-09-26 17:22:39 +08:00
|
|
|
def invoke_encrypt(cls, tenant: Tenant, payload: RequestInvokeEncrypt) -> dict:
|
2024-08-30 21:25:58 +08:00
|
|
|
encrypter = ProviderConfigEncrypter(
|
|
|
|
tenant_id=tenant.id,
|
2024-09-30 17:39:13 +08:00
|
|
|
config=payload.config,
|
2024-08-30 23:29:04 +08:00
|
|
|
provider_type=payload.namespace,
|
2024-08-30 21:25:58 +08:00
|
|
|
provider_identity=payload.identity,
|
|
|
|
)
|
|
|
|
|
2024-09-26 17:22:39 +08:00
|
|
|
if payload.opt == "encrypt":
|
2024-09-26 17:51:13 +08:00
|
|
|
return {
|
|
|
|
"data": encrypter.encrypt(payload.data),
|
|
|
|
}
|
2024-09-27 21:48:48 +08:00
|
|
|
elif payload.opt == "decrypt":
|
2024-09-26 17:51:13 +08:00
|
|
|
return {
|
|
|
|
"data": encrypter.decrypt(payload.data),
|
|
|
|
}
|
2024-09-27 21:48:48 +08:00
|
|
|
elif payload.opt == "clear":
|
2024-10-09 22:33:18 +08:00
|
|
|
encrypter.delete_tool_credentials_cache()
|
2024-09-27 21:48:48 +08:00
|
|
|
return {
|
2024-10-09 22:33:18 +08:00
|
|
|
"data": {},
|
2024-09-27 21:48:48 +08:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Invalid opt: {payload.opt}")
|