2025-12-05 13:05:53 +09:00
|
|
|
from flask_restx import Resource
|
|
|
|
|
from pydantic import BaseModel, Field
|
2024-06-15 02:46:02 +08:00
|
|
|
|
2025-10-14 10:20:37 +09:00
|
|
|
from libs.login import current_account_with_tenant, login_required
|
2024-06-15 02:46:02 +08:00
|
|
|
from services.auth.api_key_auth_service import ApiKeyAuthService
|
|
|
|
|
|
2025-12-05 13:05:53 +09:00
|
|
|
from .. import console_ns
|
|
|
|
|
from ..auth.error import ApiKeyAuthFailedError
|
|
|
|
|
from ..wraps import account_initialization_required, is_admin_or_owner_required, setup_required
|
|
|
|
|
|
|
|
|
|
DEFAULT_REF_TEMPLATE_SWAGGER_2_0 = "#/definitions/{model}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiKeyAuthBindingPayload(BaseModel):
|
|
|
|
|
category: str = Field(...)
|
|
|
|
|
provider: str = Field(...)
|
|
|
|
|
credentials: dict = Field(...)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console_ns.schema_model(
|
|
|
|
|
ApiKeyAuthBindingPayload.__name__,
|
|
|
|
|
ApiKeyAuthBindingPayload.model_json_schema(ref_template=DEFAULT_REF_TEMPLATE_SWAGGER_2_0),
|
|
|
|
|
)
|
2024-06-15 02:46:02 +08:00
|
|
|
|
|
|
|
|
|
2025-09-28 13:37:06 +08:00
|
|
|
@console_ns.route("/api-key-auth/data-source")
|
2024-06-15 02:46:02 +08:00
|
|
|
class ApiKeyAuthDataSource(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
|
|
|
|
data_source_api_key_bindings = ApiKeyAuthService.get_provider_auth_list(current_tenant_id)
|
2024-06-15 02:46:02 +08:00
|
|
|
if data_source_api_key_bindings:
|
|
|
|
|
return {
|
2024-08-26 15:29:10 +08:00
|
|
|
"sources": [
|
|
|
|
|
{
|
|
|
|
|
"id": data_source_api_key_binding.id,
|
|
|
|
|
"category": data_source_api_key_binding.category,
|
|
|
|
|
"provider": data_source_api_key_binding.provider,
|
|
|
|
|
"disabled": data_source_api_key_binding.disabled,
|
|
|
|
|
"created_at": int(data_source_api_key_binding.created_at.timestamp()),
|
|
|
|
|
"updated_at": int(data_source_api_key_binding.updated_at.timestamp()),
|
|
|
|
|
}
|
|
|
|
|
for data_source_api_key_binding in data_source_api_key_bindings
|
|
|
|
|
]
|
2024-06-17 03:06:32 -05:00
|
|
|
}
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"sources": []}
|
2024-06-15 02:46:02 +08:00
|
|
|
|
|
|
|
|
|
2025-09-28 13:37:06 +08:00
|
|
|
@console_ns.route("/api-key-auth/data-source/binding")
|
2024-06-15 02:46:02 +08:00
|
|
|
class ApiKeyAuthDataSourceBinding(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
2025-11-21 15:25:53 +09:00
|
|
|
@is_admin_or_owner_required
|
2025-12-05 13:05:53 +09:00
|
|
|
@console_ns.expect(console_ns.models[ApiKeyAuthBindingPayload.__name__])
|
2024-06-15 02:46:02 +08:00
|
|
|
def post(self):
|
|
|
|
|
# The role of the current user in the table must be admin or owner
|
2025-11-21 15:25:53 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2025-12-05 13:05:53 +09:00
|
|
|
payload = ApiKeyAuthBindingPayload.model_validate(console_ns.payload)
|
|
|
|
|
data = payload.model_dump()
|
|
|
|
|
ApiKeyAuthService.validate_api_key_auth_args(data)
|
2024-06-15 02:46:02 +08:00
|
|
|
try:
|
2025-12-05 13:05:53 +09:00
|
|
|
ApiKeyAuthService.create_provider_auth(current_tenant_id, data)
|
2024-06-15 02:46:02 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
raise ApiKeyAuthFailedError(str(e))
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}, 200
|
2024-06-15 02:46:02 +08:00
|
|
|
|
|
|
|
|
|
2025-09-28 13:37:06 +08:00
|
|
|
@console_ns.route("/api-key-auth/data-source/<uuid:binding_id>")
|
2024-06-15 02:46:02 +08:00
|
|
|
class ApiKeyAuthDataSourceBindingDelete(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
2025-11-21 15:25:53 +09:00
|
|
|
@is_admin_or_owner_required
|
2024-06-15 02:46:02 +08:00
|
|
|
def delete(self, binding_id):
|
|
|
|
|
# The role of the current user in the table must be admin or owner
|
2025-11-21 15:25:53 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2024-06-15 02:46:02 +08:00
|
|
|
|
2025-10-14 10:20:37 +09:00
|
|
|
ApiKeyAuthService.delete_provider_auth(current_tenant_id, binding_id)
|
2024-06-15 02:46:02 +08:00
|
|
|
|
2025-04-27 12:12:46 +08:00
|
|
|
return {"result": "success"}, 204
|