feat(provider): 添加API密钥加密存储

This commit is contained in:
2026-08-30 10:56:57 +08:00
parent 0842413d29
commit 0836807aa2
10 changed files with 455 additions and 5 deletions
+45
View File
@@ -10,6 +10,8 @@ from app.contracts import (
AgentRunCreateRequest,
AgentRunListResponse,
ChatRequest,
CredentialStatus,
CredentialWriteRequest,
ExtensionInstallRequest,
IndexJob,
IndexRebuildRequest,
@@ -54,6 +56,7 @@ from app.extensions import ExtensionError
from app.providers.registry import ProviderNotFoundError
from app.providers.factory import UnsupportedProviderError
from app.providers.base import ProviderError
from app.providers.credentials import CredentialStoreError
from app.retrieval.engine import engine
from app.services import index_service, note_service, task_service, transcription_service
@@ -413,6 +416,47 @@ async def uninstall_plugin(plugin_id: str) -> OperationResponse:
# Providers
@router.get(
"/credentials/{credential_id}",
response_model=CredentialStatus,
tags=["Providers"],
)
async def get_credential_status(credential_id: str) -> CredentialStatus:
try:
configured = container.credentials.has(credential_id)
except CredentialStoreError as exc:
raise ApiError(422, "CREDENTIAL_INVALID", str(exc)) from exc
return CredentialStatus(credential_id=credential_id, configured=configured)
@router.put(
"/credentials/{credential_id}",
response_model=CredentialStatus,
tags=["Providers"],
)
async def put_credential(
credential_id: str, request: CredentialWriteRequest
) -> CredentialStatus:
try:
container.credentials.put(credential_id, request.api_key.get_secret_value())
except CredentialStoreError as exc:
raise ApiError(422, "CREDENTIAL_STORE_ERROR", str(exc)) from exc
return CredentialStatus(credential_id=credential_id, configured=True)
@router.delete(
"/credentials/{credential_id}",
response_model=CredentialStatus,
tags=["Providers"],
)
async def delete_credential(credential_id: str) -> CredentialStatus:
try:
container.credentials.delete(credential_id)
except CredentialStoreError as exc:
raise ApiError(422, "CREDENTIAL_STORE_ERROR", str(exc)) from exc
return CredentialStatus(credential_id=credential_id, configured=False)
@router.get("/providers", response_model=ProviderListResponse, tags=["Providers"])
async def list_providers() -> ProviderListResponse:
return ProviderListResponse(items=container.providers.list_configs())
@@ -518,6 +562,7 @@ async def list_provider_models(provider_id: str) -> ProviderModelsResponse:
except ProviderError as exc:
status_code = {
"PROVIDER_CREDENTIAL_MISSING": 422,
"PROVIDER_CREDENTIAL_UNAVAILABLE": 500,
"PROVIDER_AUTH_FAILED": 401,
"MODEL_NOT_FOUND": 404,
"PROVIDER_RATE_LIMITED": 429,