Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(api): decouple data source API key auth flow (#40452)

Byron.wang891627b2105e
diff --git a/api/controllers/console/__init__.py b/api/controllers/console/__init__.py
index cd87e1f8243..45063841c96 100644
--- a/api/controllers/console/__init__.py
+++ b/api/controllers/console/__init__.py
@@ -1,9 +1,11 @@
+from http import HTTPStatus
from importlib import import_module
-from flask import Blueprint
+from flask import Blueprint, current_app, got_request_exception
from flask_restx import Namespace
from libs.external_api import ExternalApi
+from machinery.errors import ActiveWorkspaceRequiredError
bp = Blueprint("console", __name__, url_prefix="/console/api")
@@ -14,6 +16,19 @@ api = ExternalApi(
description="Console management APIs for app configuration, monitoring, and administration",
)
+
[email protected](ActiveWorkspaceRequiredError)
+def _handle_active_workspace_required_error(error: ActiveWorkspaceRequiredError):
+ """Map a broken Console admission invariant without exposing internal details."""
+ got_request_exception.send(current_app, exception=error)
+ status = HTTPStatus.INTERNAL_SERVER_ERROR
+ return {
+ "code": error.error_code,
+ "message": status.phrase,
+ "status": status.value,
+ }, status.value
+
+
console_ns = Namespace("console", description="Console management API operations", path="/")
RESOURCE_MODULES = (
diff --git a/api/controllers/console/auth/data_source_bearer_auth.py b/api/controllers/console/auth/data_source_bearer_auth.py
index 3f42b7a3ad7..808fae53b91 100644
--- a/api/controllers/console/auth/data_source_bearer_auth.py
+++ b/api/controllers/console/auth/data_source_bearer_auth.py
@@ -1,33 +1,52 @@
+from http import HTTPStatus
from uuid import UUID
from flask_restx import Resource
-from pydantic import BaseModel, Field
+from pydantic import BaseModel, ConfigDict, Field
from controllers.common.fields import SimpleResultResponse
from controllers.common.schema import register_response_schema_models, register_schema_models
-from extensions.ext_database import db
+from controllers.console import console_ns
+from controllers.console.auth.error import (
+ DataSourceApiKeyAuthCredentialsRejectedRequestError,
+ DataSourceApiKeyAuthProviderNotSupportedError,
+ DataSourceApiKeyAuthProviderUnavailableRequestError,
+ InvalidDataSourceApiKeyAuthCredentialsRequestError,
+)
+from controllers.console.flask_admission import console_account_admission
+from controllers.console.wraps import RBACPermission, RBACResourceScope, model_validate
+from extensions.ext_application_services import application_services
from fields.base import ResponseModel
-from libs.login import login_required
-from services.auth.api_key_auth_service import ApiKeyAuthService
-
-from .. import console_ns
-from ..auth.error import ApiKeyAuthFailedError
-from ..wraps import (
- RBACPermission,
- RBACResourceScope,
- account_initialization_required,
- is_admin_or_owner_required,
- model_validate,
- rbac_permission_required,
- setup_required,
- with_current_tenant_id,
+from libs.helper import dump_response
+from machinery.context import RequestContext
+from models.account import TenantAccountRole
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+ UnsupportedDataSourceApiKeyAuthProviderError,
+)
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingCreate,
+ DataSourceApiKeyAuthCredentials,
)
+class ApiKeyAuthConfigPayload(BaseModel):
+ model_config = ConfigDict(extra="allow")
+
+ api_key: str = Field(min_length=1)
+
+
+class ApiKeyAuthCredentialsPayload(BaseModel):
+ auth_type: str = Field(min_length=1)
+ config: ApiKeyAuthConfigPayload
+
+
class ApiKeyAuthBindingPayload(BaseModel):
- category: str = Field(...)
- provider: str = Field(...)
- credentials: dict = Field(...)
+ category: str = Field(min_length=1)
+ provider: str = Field(min_length=1)
+ credentials: ApiKeyAuthCredentialsPayload
class ApiKeyAuthDataSourceItem(ResponseModel):
@@ -52,65 +71,77 @@ register_response_schema_models(
)
+_ADMIN_OR_OWNER_ROLES = frozenset({TenantAccountRole.ADMIN, TenantAccountRole.OWNER})
+
+
@console_ns.route("/api-key-auth/data-source")
class ApiKeyAuthDataSource(Resource):
- @console_ns.response(200, "Success", console_ns.models[ApiKeyAuthDataSourceListResponse.__name__])
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_tenant_id
- def get(self, current_tenant_id: str):
- data_source_api_key_bindings = ApiKeyAuthService.get_provider_auth_list(current_tenant_id, session=db.session())
- if data_source_api_key_bindings:
- return {
+ @console_ns.response(HTTPStatus.OK, "Success", console_ns.models[ApiKeyAuthDataSourceListResponse.__name__])
+ @console_account_admission()
+ def get(self, request_context: RequestContext):
+ bindings = application_services().data_source_api_key_auth.list_bindings(request_context)
+ return dump_response(
+ ApiKeyAuthDataSourceListResponse,
+ {
"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()),
+ "id": binding.id,
+ "category": binding.category,
+ "provider": binding.provider,
+ "disabled": binding.disabled,
+ "created_at": int(binding.created_at.timestamp()),
+ "updated_at": int(binding.updated_at.timestamp()),
}
- for data_source_api_key_binding in data_source_api_key_bindings
+ for binding in bindings
]
- }
- return {"sources": []}
+ },
+ )
@console_ns.route("/api-key-auth/data-source/binding")
class ApiKeyAuthDataSourceBinding(Resource):
- @console_ns.response(200, "Success", console_ns.models[SimpleResultResponse.__name__])
- @setup_required
- @login_required
- @account_initialization_required
- @is_admin_or_owner_required
- @rbac_permission_required(RBACResourceScope.WORKSPACE, RBACPermission.CREDENTIAL_CREATE, resource_required=False)
+ @console_ns.response(HTTPStatus.OK, "Success", console_ns.models[SimpleResultResponse.__name__])
@console_ns.expect(console_ns.models[ApiKeyAuthBindingPayload.__name__])
- @with_current_tenant_id
+ @console_account_admission(
+ allowed_roles=_ADMIN_OR_OWNER_ROLES,
+ rbac_resource_scope=RBACResourceScope.WORKSPACE,
+ rbac_permission=RBACPermission.CREDENTIAL_CREATE,
+ rbac_resource_required=False,
+ )
@model_validate(ApiKeyAuthBindingPayload)
- def post(self, req_data: ApiKeyAuthBindingPayload, current_tenant_id: str):
- # The role of the current user in the table must be admin or owner
- data = req_data.model_dump()
- ApiKeyAuthService.validate_api_key_auth_args(data)
+ def post(self, req_data: ApiKeyAuthBindingPayload, request_context: RequestContext):
+ credential_options = req_data.credentials.config.model_dump(exclude={"api_key"})
+ command = DataSourceApiKeyAuthBindingCreate(
+ category=req_data.category,
+ provider=req_data.provider,
+ credentials=DataSourceApiKeyAuthCredentials(
+ auth_type=req_data.credentials.auth_type,
+ api_key=req_data.credentials.config.api_key,
+ options=credential_options,
+ ),
+ )
try:
- ApiKeyAuthService.create_provider_auth(current_tenant_id, data, session=db.session())
- except Exception as e:
- raise ApiKeyAuthFailedError(str(e))
- return {"result": "success"}, 200
+ application_services().data_source_api_key_auth.create_binding(request_context, command)
+ except UnsupportedDataSourceApiKeyAuthProviderError as exc:
+ raise DataSourceApiKeyAuthProviderNotSupportedError() from exc
+ except InvalidDataSourceApiKeyAuthCredentialsError as exc:
+ raise InvalidDataSourceApiKeyAuthCredentialsRequestError(description=str(exc)) from exc
+ except DataSourceApiKeyAuthCredentialValidationError as exc:
+ raise DataSourceApiKeyAuthCredentialsRejectedRequestError(description=str(exc)) from exc
+ except DataSourceApiKeyAuthProviderUnavailableError as exc:
+ raise DataSourceApiKeyAuthProviderUnavailableRequestError() from exc
+ return dump_response(SimpleResultResponse, {"result": "success"}), HTTPStatus.OK
@console_ns.route("/api-key-auth/data-source/<uuid:binding_id>")
class ApiKeyAuthDataSourceBindingDelete(Resource):
- @setup_required
- @login_required
- @account_initialization_required
- @is_admin_or_owner_required
- @rbac_permission_required(RBACResourceScope.WORKSPACE, RBACPermission.CREDENTIAL_MANAGE, resource_required=False)
- @console_ns.response(204, "Binding deleted successfully")
- @with_current_tenant_id
- def delete(self, current_tenant_id: str, binding_id: UUID):
- # The role of the current user in the table must be admin or owner
- ApiKeyAuthService.delete_provider_auth(current_tenant_id, str(binding_id), session=db.session())
-
- return "", 204
+ @console_ns.response(HTTPStatus.NO_CONTENT, "Binding deleted successfully")
+ @console_account_admission(
+ allowed_roles=_ADMIN_OR_OWNER_ROLES,
+ rbac_resource_scope=RBACResourceScope.WORKSPACE,
+ rbac_permission=RBACPermission.CREDENTIAL_MANAGE,
+ rbac_resource_required=False,
+ )
+ def delete(self, request_context: RequestContext, binding_id: UUID):
+ application_services().data_source_api_key_auth.delete_binding(request_context, str(binding_id))
+ return "", HTTPStatus.NO_CONTENT
diff --git a/api/controllers/console/auth/error.py b/api/controllers/console/auth/error.py
index 36bc34cc406..514450b343d 100644
--- a/api/controllers/console/auth/error.py
+++ b/api/controllers/console/auth/error.py
@@ -1,10 +1,28 @@
from libs.exception import BaseHTTPException
-class ApiKeyAuthFailedError(BaseHTTPException):
- error_code = "auth_failed"
- description = "{message}"
- code = 500
+class DataSourceApiKeyAuthProviderNotSupportedError(BaseHTTPException):
+ error_code = "unsupported_data_source_api_key_auth_provider"
+ description = "The data-source API-key authentication provider is not supported."
+ code = 400
+
+
+class InvalidDataSourceApiKeyAuthCredentialsRequestError(BaseHTTPException):
+ error_code = "invalid_data_source_api_key_auth_credentials"
+ description = "The data-source API-key authentication credentials are invalid."
+ code = 400
+
+
+class DataSourceApiKeyAuthCredentialsRejectedRequestError(BaseHTTPException):
+ error_code = "data_source_api_key_auth_credentials_rejected"
+ description = "The data-source provider rejected the API-key authentication credentials."
+ code = 400
+
+
+class DataSourceApiKeyAuthProviderUnavailableRequestError(BaseHTTPException):
+ error_code = "data_source_api_key_auth_provider_unavailable"
+ description = "The data-source API-key authentication provider is temporarily unavailable."
+ code = 502
class InvalidEmailError(BaseHTTPException):
diff --git a/api/controllers/console/flask_admission.py b/api/controllers/console/flask_admission.py
index d7ce23b67e1..424c966953d 100644
--- a/api/controllers/console/flask_admission.py
+++ b/api/controllers/console/flask_admission.py
@@ -5,19 +5,28 @@ from functools import wraps
from typing import Concatenate
from flask import Response, abort, request
+from werkzeug.exceptions import Forbidden
from configs import dify_config
+from controllers.common.wraps import enforce_rbac_access
from controllers.console.wraps import account_initialization_required, enterprise_license_required, setup_required
from core.logging.context import get_request_id, get_trace_id
+from core.rbac import RBACPermission, RBACResourceScope
from enums import DeploymentEdition
from libs.login import current_account_with_tenant, login_required
from machinery.context import RequestContext
+from machinery.errors import AdmissionConfigurationError
+from models.account import TenantAccountRole
def console_account_admission[T, **P, R](
*,
editions: frozenset[DeploymentEdition] | None = None,
require_valid_enterprise_license: bool = False,
+ allowed_roles: frozenset[TenantAccountRole] | None = None,
+ rbac_resource_scope: RBACResourceScope | None = None,
+ rbac_permission: RBACPermission | None = None,
+ rbac_resource_required: bool = True,
) -> Callable[
[Callable[Concatenate[T, RequestContext, P], R]],
Callable[Concatenate[T, P], R | Response],
@@ -26,18 +35,35 @@ def console_account_admission[T, **P, R](
All combinations use this decorator factory. Requirements are data, while
the execution order stays fixed: edition, setup, login/CSRF, account
- initialization, optional enterprise license, then context construction.
+ initialization, optional enterprise license, role/RBAC checks, then context
+ construction.
"""
+ if (rbac_resource_scope is None) != (rbac_permission is None):
+ raise AdmissionConfigurationError("RBAC resource scope and permission must be configured together")
+
def decorator(
view: Callable[Concatenate[T, RequestContext, P], R],
) -> Callable[Concatenate[T, P], R | Response]:
@wraps(view)
def inject_request_context(self: T, /, *args: P.args, **kwargs: P.kwargs) -> R:
account_with_tenant = current_account_with_tenant()
+ account = account_with_tenant.account
+ tenant_id = account_with_tenant.tenant_id
+ if allowed_roles is not None and not dify_config.RBAC_ENABLED and account.role not in allowed_roles:
+ raise Forbidden()
+ if rbac_resource_scope is not None and rbac_permission is not None:
+ enforce_rbac_access(
+ tenant_id=tenant_id,
+ account_id=account.id,
+ resource_type=rbac_resource_scope,
+ scene=rbac_permission,
+ resource_required=rbac_resource_required,
+ path_args=kwargs,
+ )
request_context = RequestContext(
- account_id=account_with_tenant.account.id,
- active_workspace_id=account_with_tenant.tenant_id,
+ account_id=account.id,
+ active_workspace_id=tenant_id,
request_id=get_request_id(),
trace_id=get_trace_id() or request.headers.get("X-Trace-Id"),
)
diff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.py
index 8db338a8238..43f5989dfce 100644
--- a/api/extensions/ext_application_services.py
+++ b/api/extensions/ext_application_services.py
@@ -13,11 +13,17 @@ from core.schemas.schema_manager import SchemaManager
from enums import DeploymentEdition
from extensions.ext_redis import RedisClientWrapper, redis_client
from repositories.app_definition_query_repository import AppDefinitionQueryRepository
+from repositories.data_source_api_key_auth_repository import SQLAlchemyDataSourceApiKeyAuthBindingRepository
from repositories.explore_banner_query_repository import ExploreBannerQueryRepository
from repositories.installation_state_repository import InstallationStateRepository
from repositories.workspace_member_query_repository import WorkspaceMemberQueryRepository
from repositories.workspace_query_repository import WorkspaceQueryRepository
from services.app_definition_query_service import AppDefinitionQueryService
+from services.auth.data_source_api_key_auth_gateways import (
+ ProviderApiKeyAuthCredentialValidator,
+ TenantApiKeyAuthCredentialEncryptor,
+)
+from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthService
from services.explore_banner_query_service import ExploreBannerQueryService
from services.feature_query_service import FeatureQueryService
from services.feature_service import FeatureService
@@ -37,6 +43,7 @@ _EXTENSION_KEY = "application_services"
@dataclass(frozen=True, slots=True)
class ApplicationServices:
app_definitions: AppDefinitionQueryService
+ data_source_api_key_auth: DataSourceApiKeyAuthService
explore_banner_queries: ExploreBannerQueryService
schema_definitions: SchemaDefinitionService
setup: SetupService
@@ -54,6 +61,7 @@ def build_application_services(
redis: RedisClientWrapper,
) -> ApplicationServices:
installation_state = InstallationStateRepository(client=database_client)
+ data_source_api_key_auth_bindings = SQLAlchemyDataSourceApiKeyAuthBindingRepository(session_factory=database_client)
return ApplicationServices(
app_definitions=AppDefinitionQueryService(
definitions=AppDefinitionQueryRepository(session_factory=database_client),
@@ -61,6 +69,11 @@ def build_application_services(
dify_config.CONSOLE_API_URL + "/console/api/workspaces/current/tool-provider/builtin/"
),
),
+ data_source_api_key_auth=DataSourceApiKeyAuthService(
+ bindings=data_source_api_key_auth_bindings,
+ validator=ProviderApiKeyAuthCredentialValidator(),
+ encryptor=TenantApiKeyAuthCredentialEncryptor(),
+ ),
explore_banner_queries=ExploreBannerQueryService(
banners=ExploreBannerQueryRepository(client=database_client),
is_enabled=FeatureService.is_explore_banner_enabled,
diff --git a/api/machinery/errors.py b/api/machinery/errors.py
index 517d61fcb42..73be1980f91 100644
--- a/api/machinery/errors.py
+++ b/api/machinery/errors.py
@@ -4,16 +4,45 @@ They deliberately do not carry Flask responses, Werkzeug exceptions, HTTP
status codes, or surface-specific wire models.
"""
-from typing import NamedTuple
+from collections.abc import Sequence
+from dataclasses import dataclass
+from typing import ClassVar
-class ErrorDetail(NamedTuple):
+@dataclass(frozen=True, slots=True)
+class ErrorDetail:
+ """Structured, transport-neutral context for one machinery error."""
+
type: str
location: tuple[str | int, ...]
message: str
class MachineryError(Exception):
- """Base class for failures owned by API machinery."""
+ """Base class for failures owned by API machinery.
+
+ ``error_code`` is a stable semantic identifier, not an HTTP status code.
+ Transport adapters decide whether and how a concrete error is exposed.
+ """
+
+ error_code: ClassVar[str] = "machinery_error"
+ default_message: ClassVar[str] = "API machinery operation failed."
+
+ def __init__(self, message: str | None = None, *, details: Sequence[ErrorDetail] = ()) -> None:
+ self.message = message or self.default_message
+ self.details = tuple(details)
+ super().__init__(self.message)
+
+
+class AdmissionConfigurationError(MachineryError):
+ """Raised when an admission declaration contains incompatible requirements."""
+
+ error_code = "invalid_admission_configuration"
+ default_message = "Admission configuration is invalid."
+
+
+class ActiveWorkspaceRequiredError(MachineryError):
+ """Raised when an admitted use case requires an active workspace but none was resolved."""
- code: str = "machinery_error"
+ error_code = "active_workspace_required"
+ default_message = "Admission did not resolve an active workspace."
diff --git a/api/openapi/markdown/console-openapi.md b/api/openapi/markdown/console-openapi.md
index b403b6713da..92db2caf77c 100644
--- a/api/openapi/markdown/console-openapi.md
+++ b/api/openapi/markdown/console-openapi.md
@@ -15288,9 +15288,22 @@ Legacy Chat App model config used only for follow-up question generation.
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| category | string | | Yes |
-| credentials | object | | Yes |
+| credentials | [ApiKeyAuthCredentialsPayload](#apikeyauthcredentialspayload) | | Yes |
| provider | string | | Yes |
+#### ApiKeyAuthConfigPayload
+
+| Name | Type | Description | Required |
+| ---- | ---- | ----------- | -------- |
+| api_key | string | | Yes |
+
+#### ApiKeyAuthCredentialsPayload
+
+| Name | Type | Description | Required |
+| ---- | ---- | ----------- | -------- |
+| auth_type | string | | Yes |
+| config | [ApiKeyAuthConfigPayload](#apikeyauthconfigpayload) | | Yes |
+
#### ApiKeyAuthDataSourceItem
| Name | Type | Description | Required |
diff --git a/api/repositories/data_source_api_key_auth_repository.py b/api/repositories/data_source_api_key_auth_repository.py
new file mode 100644
index 00000000000..a28ebc16204
--- /dev/null
+++ b/api/repositories/data_source_api_key_auth_repository.py
@@ -0,0 +1,87 @@
+"""Persistence adapter for data-source API-key authentication bindings."""
+
+import json
+from copy import deepcopy
+from typing import override
+
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from models.source import DataSourceApiKeyAuthBinding
+from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthBindingRepository
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingRecord,
+ DataSourceApiKeyAuthCredentials,
+)
+
+
+class SQLAlchemyDataSourceApiKeyAuthBindingRepository(DataSourceApiKeyAuthBindingRepository):
+ """Own a short-lived Session for every binding operation."""
+
+ def __init__(self, session_factory: sessionmaker[Session]) -> None:
+ self._session_factory = session_factory
+
+ @override
+ def list_enabled(self, workspace_id: str) -> tuple[DataSourceApiKeyAuthBindingRecord, ...]:
+ stmt = (
+ select(
+ DataSourceApiKeyAuthBinding.id,
+ DataSourceApiKeyAuthBinding.category,
+ DataSourceApiKeyAuthBinding.provider,
+ DataSourceApiKeyAuthBinding.disabled,
+ DataSourceApiKeyAuthBinding.created_at,
+ DataSourceApiKeyAuthBinding.updated_at,
+ )
+ .where(
+ DataSourceApiKeyAuthBinding.tenant_id == workspace_id,
+ DataSourceApiKeyAuthBinding.disabled.is_(False),
+ )
+ .order_by(DataSourceApiKeyAuthBinding.created_at.asc())
+ )
+ with self._session_factory() as session:
+ rows = session.execute(stmt).all()
+
+ return tuple(
+ DataSourceApiKeyAuthBindingRecord(
+ id=binding_id,
+ category=category,
+ provider=provider,
+ disabled=bool(disabled),
+ created_at=created_at,
+ updated_at=updated_at,
+ )
+ for binding_id, category, provider, disabled, created_at, updated_at in rows
+ )
+
+ @override
+ def create(
+ self,
+ workspace_id: str,
+ category: str,
+ provider: str,
+ credentials: DataSourceApiKeyAuthCredentials,
+ ) -> None:
+ config = deepcopy(dict(credentials.options))
+ config["api_key"] = credentials.api_key
+ binding = DataSourceApiKeyAuthBinding(
+ tenant_id=workspace_id,
+ category=category,
+ provider=provider,
+ credentials=json.dumps(
+ {"auth_type": credentials.auth_type, "config": config},
+ ensure_ascii=False,
+ ),
+ )
+ with self._session_factory.begin() as session:
+ session.add(binding)
+
+ @override
+ def delete(self, workspace_id: str, binding_id: str) -> None:
+ stmt = select(DataSourceApiKeyAuthBinding).where(
+ DataSourceApiKeyAuthBinding.tenant_id == workspace_id,
+ DataSourceApiKeyAuthBinding.id == binding_id,
+ )
+ with self._session_factory.begin() as session:
+ binding = session.scalar(stmt)
+ if binding is not None:
+ session.delete(binding)
diff --git a/api/services/auth/api_key_auth_base.py b/api/services/auth/api_key_auth_base.py
deleted file mode 100644
index b2554343336..00000000000
--- a/api/services/auth/api_key_auth_base.py
+++ /dev/null
@@ -1,16 +0,0 @@
-from abc import ABC, abstractmethod
-from typing import Any, TypedDict
-
-
-class AuthCredentials(TypedDict):
- auth_type: str
- config: dict[str, Any]
-
-
-class ApiKeyAuthBase(ABC):
- def __init__(self, credentials: AuthCredentials):
- self.credentials = credentials
-
- @abstractmethod
- def validate_credentials(self):
- raise NotImplementedError
diff --git a/api/services/auth/api_key_auth_factory.py b/api/services/auth/api_key_auth_factory.py
deleted file mode 100644
index 6e183b70e3a..00000000000
--- a/api/services/auth/api_key_auth_factory.py
+++ /dev/null
@@ -1,29 +0,0 @@
-from services.auth.api_key_auth_base import ApiKeyAuthBase, AuthCredentials
-from services.auth.auth_type import AuthType
-
-
-class ApiKeyAuthFactory:
- def __init__(self, provider: str, credentials: AuthCredentials):
- auth_factory = self.get_apikey_auth_factory(provider)
- self.auth = auth_factory(credentials)
-
- def validate_credentials(self):
- return self.auth.validate_credentials()
-
- @staticmethod
- def get_apikey_auth_factory(provider: str) -> type[ApiKeyAuthBase]:
- match provider:
- case AuthType.FIRECRAWL:
- from services.auth.firecrawl.firecrawl import FirecrawlAuth
-
- return FirecrawlAuth
- case AuthType.WATERCRAWL:
- from services.auth.watercrawl.watercrawl import WatercrawlAuth
-
- return WatercrawlAuth
- case AuthType.JINA:
- from services.auth.jina.jina import JinaAuth
-
- return JinaAuth
- case _:
- raise ValueError("Invalid provider")
diff --git a/api/services/auth/api_key_auth_service.py b/api/services/auth/api_key_auth_service.py
deleted file mode 100644
index f9ad7cf27b0..00000000000
--- a/api/services/auth/api_key_auth_service.py
+++ /dev/null
@@ -1,77 +0,0 @@
-import json
-from typing import Any
-
-from sqlalchemy import select
-from sqlalchemy.orm import Session
-
-from core.helper import encrypter
-from models.source import DataSourceApiKeyAuthBinding
-from services.auth.api_key_auth_factory import ApiKeyAuthFactory
-
-
-class ApiKeyAuthService:
- @staticmethod
- def get_provider_auth_list(tenant_id: str, *, session: Session):
- data_source_api_key_bindings = session.scalars(
- select(DataSourceApiKeyAuthBinding).where(
- DataSourceApiKeyAuthBinding.tenant_id == tenant_id, DataSourceApiKeyAuthBinding.disabled.is_(False)
- )
- ).all()
- return data_source_api_key_bindings
-
- @staticmethod
- def create_provider_auth(tenant_id: str, args: dict[str, Any], *, session: Session):
- auth_result = ApiKeyAuthFactory(args["provider"], args["credentials"]).validate_credentials()
- if auth_result:
- # Encrypt the api key
- api_key = encrypter.encrypt_token(tenant_id, args["credentials"]["config"]["api_key"])
- args["credentials"]["config"]["api_key"] = api_key
-
- data_source_api_key_binding = DataSourceApiKeyAuthBinding(
- tenant_id=tenant_id, category=args["category"], provider=args["provider"]
- )
- data_source_api_key_binding.credentials = json.dumps(args["credentials"], ensure_ascii=False)
- session.add(data_source_api_key_binding)
- session.commit()
-
- @staticmethod
- def get_auth_credentials(tenant_id: str, category: str, provider: str, *, session: Session):
- data_source_api_key_bindings = session.scalar(
- select(DataSourceApiKeyAuthBinding).where(
- DataSourceApiKeyAuthBinding.tenant_id == tenant_id,
- DataSourceApiKeyAuthBinding.category == category,
- DataSourceApiKeyAuthBinding.provider == provider,
- DataSourceApiKeyAuthBinding.disabled.is_(False),
- )
- )
- if not data_source_api_key_bindings:
- return None
- if not data_source_api_key_bindings.credentials:
- return None
- credentials = json.loads(data_source_api_key_bindings.credentials)
- return credentials
-
- @staticmethod
- def delete_provider_auth(tenant_id: str, binding_id: str, *, session: Session):
- data_source_api_key_binding = session.scalar(
- select(DataSourceApiKeyAuthBinding).where(
- DataSourceApiKeyAuthBinding.tenant_id == tenant_id,
- DataSourceApiKeyAuthBinding.id == binding_id,
- )
- )
- if data_source_api_key_binding:
- session.delete(data_source_api_key_binding)
- session.commit()
-
- @classmethod
- def validate_api_key_auth_args(cls, args):
- if "category" not in args or not args["category"]:
- raise ValueError("category is required")
- if "provider" not in args or not args["provider"]:
- raise ValueError("provider is required")
- if "credentials" not in args or not args["credentials"]:
- raise ValueError("credentials is required")
- if not isinstance(args["credentials"], dict):
- raise ValueError("credentials must be a dictionary")
- if "auth_type" not in args["credentials"] or not args["credentials"]["auth_type"]:
- raise ValueError("auth_type is required")
diff --git a/api/services/auth/auth_type.py b/api/services/auth/auth_type.py
deleted file mode 100644
index ec7118df270..00000000000
--- a/api/services/auth/auth_type.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from enum import StrEnum
-
-
-class AuthType(StrEnum):
- FIRECRAWL = "firecrawl"
- WATERCRAWL = "watercrawl"
- JINA = "jinareader"
diff --git a/api/services/auth/data_source_api_key_auth_gateways.py b/api/services/auth/data_source_api_key_auth_gateways.py
new file mode 100644
index 00000000000..d40490c731c
--- /dev/null
+++ b/api/services/auth/data_source_api_key_auth_gateways.py
@@ -0,0 +1,58 @@
+"""Outer adapters used by the data-source API-key auth application service."""
+
+from collections.abc import Callable
+from typing import Protocol, override
+
+import httpx
+
+from core.helper import encrypter
+from services.auth.data_source_api_key_auth_service import ApiKeyAuthCredentialEncryptor, ApiKeyAuthCredentialValidator
+from services.auth.errors import (
+ DataSourceApiKeyAuthProviderUnavailableError,
+ UnsupportedDataSourceApiKeyAuthProviderError,
+)
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
+
+
+class _ProviderApiKeyAuthValidator(Protocol):
+ def validate_credentials(self) -> bool: ...
+
+
+type _ProviderApiKeyAuthValidatorFactory = Callable[
+ [DataSourceApiKeyAuthCredentials],
+ _ProviderApiKeyAuthValidator,
+]
+
+
+def _get_provider_validator_factory(provider: str) -> _ProviderApiKeyAuthValidatorFactory:
+ match provider:
+ case "firecrawl":
+ from services.auth.firecrawl.firecrawl import FirecrawlAuth
+
+ return FirecrawlAuth
+ case "watercrawl":
+ from services.auth.watercrawl.watercrawl import WatercrawlAuth
+
+ return WatercrawlAuth
+ case "jinareader":
+ from services.auth.jina.jina import JinaAuth
+
+ return JinaAuth
+ case _:
+ raise UnsupportedDataSourceApiKeyAuthProviderError(provider)
+
+
+class ProviderApiKeyAuthCredentialValidator(ApiKeyAuthCredentialValidator):
+ @override
+ def validate(self, provider: str, credentials: DataSourceApiKeyAuthCredentials) -> bool:
+ validator_factory = _get_provider_validator_factory(provider)
+ try:
+ return validator_factory(credentials).validate_credentials()
+ except httpx.TransportError as exc:
+ raise DataSourceApiKeyAuthProviderUnavailableError(provider) from exc
+
+
+class TenantApiKeyAuthCredentialEncryptor(ApiKeyAuthCredentialEncryptor):
+ @override
+ def encrypt(self, workspace_id: str, token: str) -> str:
+ return encrypter.encrypt_token(workspace_id, token)
diff --git a/api/services/auth/data_source_api_key_auth_service.py b/api/services/auth/data_source_api_key_auth_service.py
new file mode 100644
index 00000000000..8bdad21a30f
--- /dev/null
+++ b/api/services/auth/data_source_api_key_auth_service.py
@@ -0,0 +1,77 @@
+"""Data-source API-key authentication binding application service and ports."""
+
+from collections.abc import Sequence
+from dataclasses import replace
+from typing import Protocol
+
+from machinery.context import RequestContext
+from machinery.errors import ActiveWorkspaceRequiredError
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingCreate,
+ DataSourceApiKeyAuthBindingRecord,
+ DataSourceApiKeyAuthCredentials,
+)
+
+
+class DataSourceApiKeyAuthBindingRepository(Protocol):
+ def list_enabled(self, workspace_id: str) -> Sequence[DataSourceApiKeyAuthBindingRecord]: ...
+
+ def create(
+ self,
+ workspace_id: str,
+ category: str,
+ provider: str,
+ credentials: DataSourceApiKeyAuthCredentials,
+ ) -> None: ...
+
+ def delete(self, workspace_id: str, binding_id: str) -> None: ...
+
+
+class ApiKeyAuthCredentialValidator(Protocol):
+ def validate(self, provider: str, credentials: DataSourceApiKeyAuthCredentials) -> bool: ...
+
+
+class ApiKeyAuthCredentialEncryptor(Protocol):
+ def encrypt(self, workspace_id: str, token: str) -> str: ...
+
+
+class DataSourceApiKeyAuthService:
+ def __init__(
+ self,
+ *,
+ bindings: DataSourceApiKeyAuthBindingRepository,
+ validator: ApiKeyAuthCredentialValidator,
+ encryptor: ApiKeyAuthCredentialEncryptor,
+ ) -> None:
+ self._bindings = bindings
+ self._validator = validator
+ self._encryptor = encryptor
+
+ def list_bindings(self, context: RequestContext) -> tuple[DataSourceApiKeyAuthBindingRecord, ...]:
+ return tuple(self._bindings.list_enabled(self._require_workspace(context)))
+
+ def create_binding(self, context: RequestContext, command: DataSourceApiKeyAuthBindingCreate) -> None:
+ workspace_id = self._require_workspace(context)
+ if not self._validator.validate(command.provider, command.credentials):
+ return
+
+ encrypted_credentials = replace(
+ command.credentials,
+ api_key=self._encryptor.encrypt(workspace_id, command.credentials.api_key),
+ )
+
+ self._bindings.create(
+ workspace_id,
+ command.category,
+ command.provider,
+ encrypted_credentials,
+ )
+
+ def delete_binding(self, context: RequestContext, binding_id: str) -> None:
+ self._bindings.delete(self._require_workspace(context), binding_id)
+
+ @staticmethod
+ def _require_workspace(context: RequestContext) -> str:
+ if context.active_workspace_id is None:
+ raise ActiveWorkspaceRequiredError()
+ return context.active_workspace_id
diff --git a/api/services/auth/errors.py b/api/services/auth/errors.py
new file mode 100644
index 00000000000..ea08416cc1f
--- /dev/null
+++ b/api/services/auth/errors.py
@@ -0,0 +1,40 @@
+"""Errors exposed by the data-source API-key authentication use case."""
+
+from typing import ClassVar
+
+
+class DataSourceApiKeyAuthError(Exception):
+ error_code: ClassVar[str] = "data_source_api_key_auth_error"
+ default_message: ClassVar[str] = "Data-source API-key authentication failed."
+
+ def __init__(self, message: str | None = None) -> None:
+ self.message = message or self.default_message
+ super().__init__(self.message)
+
+
+class InvalidDataSourceApiKeyAuthCredentialsError(DataSourceApiKeyAuthError):
+ error_code = "invalid_data_source_api_key_auth_credentials"
+ default_message = "The data-source API-key credentials are invalid."
+
+
+class DataSourceApiKeyAuthCredentialValidationError(DataSourceApiKeyAuthError):
+ error_code = "data_source_api_key_auth_credentials_rejected"
+ default_message = "The data-source provider rejected the API-key credentials."
+
+
+class DataSourceApiKeyAuthProviderUnavailableError(DataSourceApiKeyAuthError):
+ error_code = "data_source_api_key_auth_provider_unavailable"
+
+ def __init__(self, provider: str, status_code: int | None = None) -> None:
+ self.provider = provider
+ self.status_code = status_code
+ status_suffix = f" (status code: {status_code})" if status_code is not None else ""
+ super().__init__(f"Data-source API-key auth provider is unavailable: {provider}{status_suffix}")
+
+
+class UnsupportedDataSourceApiKeyAuthProviderError(DataSourceApiKeyAuthError):
+ error_code = "unsupported_data_source_api_key_auth_provider"
+
+ def __init__(self, provider: str) -> None:
+ self.provider = provider
+ super().__init__(f"Unsupported data-source API-key auth provider: {provider}")
diff --git a/api/services/auth/firecrawl/firecrawl.py b/api/services/auth/firecrawl/firecrawl.py
index 387f75dee94..2261208bf08 100644
--- a/api/services/auth/firecrawl/firecrawl.py
+++ b/api/services/auth/firecrawl/firecrawl.py
@@ -1,29 +1,32 @@
import json
-from typing import override
+from http import HTTPStatus
+from typing import Never
import httpx
-from services.auth.api_key_auth_base import ApiKeyAuthBase, AuthCredentials
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+)
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
# Explicit bounded timeout for credential-validation requests so a slow or
# hanging Firecrawl endpoint cannot block the worker indefinitely.
_CREDENTIAL_TIMEOUT = httpx.Timeout(10.0)
-class FirecrawlAuth(ApiKeyAuthBase):
- def __init__(self, credentials: AuthCredentials):
- super().__init__(credentials)
- auth_type = credentials.get("auth_type")
- if auth_type != "bearer":
- raise ValueError("Invalid auth type, Firecrawl auth type must be Bearer")
- self.api_key = credentials.get("config", {}).get("api_key", None)
- self.base_url = credentials.get("config", {}).get("base_url", "https://api.firecrawl.dev")
+class FirecrawlAuth:
+ def __init__(self, credentials: DataSourceApiKeyAuthCredentials):
+ if credentials.auth_type != "bearer":
+ raise InvalidDataSourceApiKeyAuthCredentialsError("Invalid auth type, Firecrawl auth type must be Bearer")
+ self.api_key = credentials.api_key
+ self.base_url = credentials.options.get("base_url", "https://api.firecrawl.dev")
if not self.api_key:
- raise ValueError("No API key provided")
+ raise InvalidDataSourceApiKeyAuthCredentialsError("No API key provided")
- @override
- def validate_credentials(self):
+ def validate_credentials(self) -> bool:
headers = self._prepare_headers()
options = {
"url": "https://example.com",
@@ -48,10 +51,18 @@ class FirecrawlAuth(ApiKeyAuthBase):
def _post_request(self, url, data, headers):
return httpx.post(url, headers=headers, json=data, timeout=_CREDENTIAL_TIMEOUT)
- def _handle_error(self, response):
+ def _handle_error(self, response) -> Never:
+ if (
+ response.status_code == HTTPStatus.TOO_MANY_REQUESTS
+ or response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
+ ):
+ raise DataSourceApiKeyAuthProviderUnavailableError("firecrawl", response.status_code)
+
try:
payload = response.json()
except json.JSONDecodeError:
payload = {}
error_message = payload.get("error") or payload.get("message") or (response.text or "Unknown error occurred")
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"
+ )
diff --git a/api/services/auth/jina.py b/api/services/auth/jina.py
deleted file mode 100644
index 125596e1a60..00000000000
--- a/api/services/auth/jina.py
+++ /dev/null
@@ -1,61 +0,0 @@
-import json
-from typing import override
-
-import httpx
-
-from core.helper.http_client_pooling import get_pooled_http_client
-from services.auth.api_key_auth_base import ApiKeyAuthBase, AuthCredentials
-
-_http_client: httpx.Client = get_pooled_http_client(
- "auth:jina_standalone",
- lambda: httpx.Client(
- timeout=httpx.Timeout(10.0),
- limits=httpx.Limits(max_keepalive_connections=50, max_connections=100),
- ),
-)
-
-
-class JinaAuth(ApiKeyAuthBase):
- def __init__(self, credentials: AuthCredentials):
- super().__init__(credentials)
- auth_type = credentials.get("auth_type")
- if auth_type != "bearer":
- raise ValueError("Invalid auth type, Jina Reader auth type must be Bearer")
- self.api_key = credentials.get("config", {}).get("api_key", None)
-
- if not self.api_key:
- raise ValueError("No API key provided")
-
- @override
- def validate_credentials(self):
- headers = self._prepare_headers()
- options = {
- "url": "https://example.com",
- }
- response = self._post_request("https://r.jina.ai", options, headers)
- if response.status_code == 200:
- return True
- else:
- self._handle_error(response)
-
- def _prepare_headers(self):
- return {"Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}"}
-
- def _post_request(self, url, data, headers):
- return _http_client.post(url, headers=headers, json=data)
-
- def _handle_error(self, response):
- if response.status_code in {402, 409, 500}:
- try:
- error_message = response.json().get("error", "Unknown error occurred")
- except ValueError:
- error_message = response.text or "Unknown error occurred"
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
- else:
- if response.text:
- try:
- error_message = json.loads(response.text).get("error", "Unknown error occurred")
- except ValueError:
- error_message = response.text
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
- raise Exception(f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}")
diff --git a/api/services/auth/jina/jina.py b/api/services/auth/jina/jina.py
index da0696b94fa..cf201ae0c38 100644
--- a/api/services/auth/jina/jina.py
+++ b/api/services/auth/jina/jina.py
@@ -1,10 +1,16 @@
import json
-from typing import override
+from http import HTTPStatus
+from typing import Never
import httpx
from core.helper.http_client_pooling import get_pooled_http_client
-from services.auth.api_key_auth_base import ApiKeyAuthBase, AuthCredentials
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+)
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
_http_client: httpx.Client = get_pooled_http_client(
"auth:jina",
@@ -15,19 +21,16 @@ _http_client: httpx.Client = get_pooled_http_client(
)
-class JinaAuth(ApiKeyAuthBase):
- def __init__(self, credentials: AuthCredentials):
- super().__init__(credentials)
- auth_type = credentials.get("auth_type")
- if auth_type != "bearer":
- raise ValueError("Invalid auth type, Jina Reader auth type must be Bearer")
- self.api_key = credentials.get("config", {}).get("api_key", None)
+class JinaAuth:
+ def __init__(self, credentials: DataSourceApiKeyAuthCredentials):
+ if credentials.auth_type != "bearer":
+ raise InvalidDataSourceApiKeyAuthCredentialsError("Invalid auth type, Jina Reader auth type must be Bearer")
+ self.api_key = credentials.api_key
if not self.api_key:
- raise ValueError("No API key provided")
+ raise InvalidDataSourceApiKeyAuthCredentialsError("No API key provided")
- @override
- def validate_credentials(self):
+ def validate_credentials(self) -> bool:
headers = self._prepare_headers()
options = {
"url": "https://example.com",
@@ -44,18 +47,30 @@ class JinaAuth(ApiKeyAuthBase):
def _post_request(self, url, data, headers):
return _http_client.post(url, headers=headers, json=data)
- def _handle_error(self, response):
- if response.status_code in {402, 409, 500}:
+ def _handle_error(self, response) -> Never:
+ if (
+ response.status_code == HTTPStatus.TOO_MANY_REQUESTS
+ or response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
+ ):
+ raise DataSourceApiKeyAuthProviderUnavailableError("jinareader", response.status_code)
+
+ if response.status_code in {402, 409}:
try:
error_message = response.json().get("error", "Unknown error occurred")
- except ValueError:
+ except json.JSONDecodeError:
error_message = response.text or "Unknown error occurred"
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"
+ )
else:
if response.text:
try:
error_message = json.loads(response.text).get("error", "Unknown error occurred")
- except ValueError:
+ except json.JSONDecodeError:
error_message = response.text
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
- raise Exception(f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}")
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"
+ )
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}"
+ )
diff --git a/api/services/auth/watercrawl/watercrawl.py b/api/services/auth/watercrawl/watercrawl.py
index 2b2dd20641c..e4d0951f6df 100644
--- a/api/services/auth/watercrawl/watercrawl.py
+++ b/api/services/auth/watercrawl/watercrawl.py
@@ -1,30 +1,35 @@
import json
-from typing import override
+from http import HTTPStatus
+from typing import Never
from urllib.parse import urljoin
import httpx
-from services.auth.api_key_auth_base import ApiKeyAuthBase, AuthCredentials
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+)
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
# Explicit bounded timeout for credential-validation requests so a slow or
# hanging WaterCrawl endpoint cannot block the worker indefinitely.
_CREDENTIAL_TIMEOUT = httpx.Timeout(10.0)
-class WatercrawlAuth(ApiKeyAuthBase):
- def __init__(self, credentials: AuthCredentials):
- super().__init__(credentials)
- auth_type = credentials.get("auth_type")
- if auth_type != "x-api-key":
- raise ValueError("Invalid auth type, WaterCrawl auth type must be x-api-key")
- self.api_key = credentials.get("config", {}).get("api_key", None)
- self.base_url = credentials.get("config", {}).get("base_url", "https://app.watercrawl.dev")
+class WatercrawlAuth:
+ def __init__(self, credentials: DataSourceApiKeyAuthCredentials):
+ if credentials.auth_type != "x-api-key":
+ raise InvalidDataSourceApiKeyAuthCredentialsError(
+ "Invalid auth type, WaterCrawl auth type must be x-api-key"
+ )
+ self.api_key = credentials.api_key
+ self.base_url = credentials.options.get("base_url", "https://app.watercrawl.dev")
if not self.api_key:
- raise ValueError("No API key provided")
+ raise InvalidDataSourceApiKeyAuthCredentialsError("No API key provided")
- @override
- def validate_credentials(self):
+ def validate_credentials(self) -> bool:
headers = self._prepare_headers()
url = urljoin(self.base_url, "/api/v1/core/crawl-requests/")
response = self._get_request(url, headers)
@@ -39,18 +44,30 @@ class WatercrawlAuth(ApiKeyAuthBase):
def _get_request(self, url, headers):
return httpx.get(url, headers=headers, timeout=_CREDENTIAL_TIMEOUT)
- def _handle_error(self, response):
- if response.status_code in {402, 409, 500}:
+ def _handle_error(self, response) -> Never:
+ if (
+ response.status_code == HTTPStatus.TOO_MANY_REQUESTS
+ or response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
+ ):
+ raise DataSourceApiKeyAuthProviderUnavailableError("watercrawl", response.status_code)
+
+ if response.status_code in {402, 409}:
try:
error_message = response.json().get("error", "Unknown error occurred")
- except ValueError:
+ except json.JSONDecodeError:
error_message = response.text or "Unknown error occurred"
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"
+ )
else:
if response.text:
try:
error_message = json.loads(response.text).get("error", "Unknown error occurred")
- except ValueError:
+ except json.JSONDecodeError:
error_message = response.text
- raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
- raise Exception(f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}")
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"
+ )
+ raise DataSourceApiKeyAuthCredentialValidationError(
+ f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}"
+ )
diff --git a/api/services/entities/data_source_api_key_auth_entities.py b/api/services/entities/data_source_api_key_auth_entities.py
new file mode 100644
index 00000000000..d0eb628e3e6
--- /dev/null
+++ b/api/services/entities/data_source_api_key_auth_entities.py
@@ -0,0 +1,30 @@
+"""Framework- and persistence-independent contracts for data-source API-key auth."""
+
+from collections.abc import Mapping
+from dataclasses import dataclass
+from datetime import datetime
+from typing import Any
+
+
+@dataclass(frozen=True, slots=True)
+class DataSourceApiKeyAuthCredentials:
+ auth_type: str
+ api_key: str
+ options: Mapping[str, Any]
+
+
+@dataclass(frozen=True, slots=True)
+class DataSourceApiKeyAuthBindingCreate:
+ category: str
+ provider: str
+ credentials: DataSourceApiKeyAuthCredentials
+
+
+@dataclass(frozen=True, slots=True)
+class DataSourceApiKeyAuthBindingRecord:
+ id: str
+ category: str
+ provider: str
+ disabled: bool
+ created_at: datetime
+ updated_at: datetime
diff --git a/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_bearer_auth.py b/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_bearer_auth.py
index ef8c0add709..f51bf8f13e1 100644
--- a/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_bearer_auth.py
+++ b/api/tests/test_containers_integration_tests/controllers/console/auth/test_data_source_bearer_auth.py
@@ -8,6 +8,11 @@ from sqlalchemy import select
from sqlalchemy.orm import Session
from models.source import DataSourceApiKeyAuthBinding
+from services.auth.errors import DataSourceApiKeyAuthCredentialValidationError
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingCreate,
+ DataSourceApiKeyAuthCredentials,
+)
from tests.test_containers_integration_tests.controllers.console.helpers import (
authenticate_console_client,
create_console_account_and_tenant,
@@ -71,12 +76,15 @@ def test_create_binding_successful(
) -> None:
account, tenant = create_console_account_and_tenant(db_session_with_containers)
tenant_id = tenant.id
- payload = {"category": "api_key", "provider": "custom", "credentials": {"key": "value"}}
-
- with (
- patch("controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.validate_api_key_auth_args"),
- patch("controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.create_provider_auth") as create_auth,
- ):
+ payload = {
+ "category": "api_key",
+ "provider": "custom",
+ "credentials": {"auth_type": "bearer", "config": {"api_key": "secret"}},
+ }
+
+ with patch(
+ "services.auth.data_source_api_key_auth_service.DataSourceApiKeyAuthService.create_binding"
+ ) as create_auth:
response = test_client_with_containers.post(
"/console/api/api-key-auth/data-source/binding",
json=payload,
@@ -85,7 +93,15 @@ def test_create_binding_successful(
assert response.status_code == 200
assert response.get_json() == {"result": "success"}
- create_auth.assert_called_once_with(tenant_id, payload, session=ANY)
+ create_auth.assert_called_once_with(
+ ANY,
+ DataSourceApiKeyAuthBindingCreate(
+ category=payload["category"],
+ provider=payload["provider"],
+ credentials=DataSourceApiKeyAuthCredentials("bearer", "secret", {}),
+ ),
+ )
+ assert create_auth.call_args.args[0].active_workspace_id == tenant_id
def test_create_binding_failure(
@@ -94,24 +110,25 @@ def test_create_binding_failure(
) -> None:
account, _tenant = create_console_account_and_tenant(db_session_with_containers)
- with (
- patch("controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.validate_api_key_auth_args"),
- patch(
- "controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.create_provider_auth",
- side_effect=ValueError("Invalid structure"),
- ),
+ with patch(
+ "services.auth.data_source_api_key_auth_service.DataSourceApiKeyAuthService.create_binding",
+ side_effect=DataSourceApiKeyAuthCredentialValidationError("Credentials rejected"),
):
response = test_client_with_containers.post(
"/console/api/api-key-auth/data-source/binding",
- json={"category": "api_key", "provider": "custom", "credentials": {"key": "value"}},
+ json={
+ "category": "api_key",
+ "provider": "custom",
+ "credentials": {"auth_type": "bearer", "config": {"api_key": "secret"}},
+ },
headers=authenticate_console_client(test_client_with_containers, account),
)
- assert response.status_code == 500
+ assert response.status_code == 400
payload = response.get_json()
assert payload is not None
- assert payload["code"] == "auth_failed"
- assert payload["message"] == "Invalid structure"
+ assert payload["code"] == "data_source_api_key_auth_credentials_rejected"
+ assert payload["message"] == "Credentials rejected"
def test_delete_binding_successful(
diff --git a/api/tests/test_containers_integration_tests/services/auth/test_auth_integration.py b/api/tests/test_containers_integration_tests/services/auth/test_auth_integration.py
index 2495fa9d49d..ec2c013459e 100644
--- a/api/tests/test_containers_integration_tests/services/auth/test_auth_integration.py
+++ b/api/tests/test_containers_integration_tests/services/auth/test_auth_integration.py
@@ -1,6 +1,4 @@
-"""
-API Key Authentication System Integration Tests
-"""
+"""Integration tests for the data-source API-key auth adapters."""
from __future__ import annotations
@@ -10,13 +8,24 @@ from uuid import uuid4
import httpx
import pytest
-from flask import Flask
-from sqlalchemy.orm import Session
+from sqlalchemy.orm import Session, sessionmaker
-from extensions.ext_database import db
+from machinery.context import RequestContext
from models.source import DataSourceApiKeyAuthBinding
-from services.auth.api_key_auth_service import ApiKeyAuthService
-from services.auth.auth_type import AuthType
+from repositories.data_source_api_key_auth_repository import SQLAlchemyDataSourceApiKeyAuthBindingRepository
+from services.auth.data_source_api_key_auth_gateways import (
+ ProviderApiKeyAuthCredentialValidator,
+ TenantApiKeyAuthCredentialEncryptor,
+)
+from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthService
+from services.auth.errors import DataSourceApiKeyAuthProviderUnavailableError
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingCreate,
+ DataSourceApiKeyAuthCredentials,
+)
+
+_FIRECRAWL_PROVIDER = "firecrawl"
+_JINA_PROVIDER = "jinareader"
class TestAuthIntegration:
@@ -33,20 +42,37 @@ class TestAuthIntegration:
return "search"
@pytest.fixture
- def firecrawl_credentials(self) -> dict:
- return {"auth_type": "bearer", "config": {"api_key": "fc_test_key_123"}}
+ def firecrawl_credentials(self) -> DataSourceApiKeyAuthCredentials:
+ return DataSourceApiKeyAuthCredentials("bearer", "fc_test_key_123", {})
@pytest.fixture
- def jina_credentials(self) -> dict:
- return {"auth_type": "bearer", "config": {"api_key": "jina_test_key_456"}}
+ def jina_credentials(self) -> DataSourceApiKeyAuthCredentials:
+ return DataSourceApiKeyAuthCredentials("bearer", "jina_test_key_456", {})
+
+ @staticmethod
+ def _context(workspace_id: str) -> RequestContext:
+ return RequestContext(
+ request_id="request-1",
+ trace_id=None,
+ account_id="account-1",
+ active_workspace_id=workspace_id,
+ )
+
+ @staticmethod
+ def _service(session: Session) -> DataSourceApiKeyAuthService:
+ session_factory = sessionmaker(bind=session.get_bind(), expire_on_commit=False)
+ return DataSourceApiKeyAuthService(
+ bindings=SQLAlchemyDataSourceApiKeyAuthBindingRepository(session_factory),
+ validator=ProviderApiKeyAuthCredentialValidator(),
+ encryptor=TenantApiKeyAuthCredentialEncryptor(),
+ )
@patch("services.auth.firecrawl.firecrawl.httpx.post")
- @patch("services.auth.api_key_auth_service.encrypter.encrypt_token")
+ @patch("services.auth.data_source_api_key_auth_gateways.encrypter.encrypt_token")
def test_end_to_end_auth_flow(
self,
mock_encrypt,
mock_http,
- flask_app_with_containers: Flask,
db_session_with_containers: Session,
tenant_id_1,
category,
@@ -54,23 +80,23 @@ class TestAuthIntegration:
):
mock_http.return_value = self._create_success_response()
mock_encrypt.return_value = "encrypted_fc_test_key_123"
+ service = self._service(db_session_with_containers)
+ command = DataSourceApiKeyAuthBindingCreate(category, _FIRECRAWL_PROVIDER, firecrawl_credentials)
- args = {"category": category, "provider": AuthType.FIRECRAWL, "credentials": firecrawl_credentials}
- ApiKeyAuthService.create_provider_auth(tenant_id_1, args, session=db_session_with_containers)
+ service.create_binding(self._context(tenant_id_1), command)
mock_http.assert_called_once()
call_args = mock_http.call_args
assert "https://api.firecrawl.dev/v1/crawl" in call_args[0][0]
assert call_args[1]["headers"]["Authorization"] == "Bearer fc_test_key_123"
-
mock_encrypt.assert_called_once_with(tenant_id_1, "fc_test_key_123")
db_session_with_containers.expire_all()
bindings = db_session_with_containers.query(DataSourceApiKeyAuthBinding).filter_by(tenant_id=tenant_id_1).all()
assert len(bindings) == 1
- assert bindings[0].provider == AuthType.FIRECRAWL
+ assert bindings[0].provider == _FIRECRAWL_PROVIDER
- @patch("services.auth.api_key_auth_service.encrypter.encrypt_token")
+ @patch("services.auth.data_source_api_key_auth_gateways.encrypter.encrypt_token")
@patch("services.auth.firecrawl.firecrawl.httpx.post")
@patch("services.auth.jina.jina._http_client.post")
def test_multi_tenant_isolation(
@@ -78,7 +104,6 @@ class TestAuthIntegration:
mock_jina_http,
mock_fc_http,
mock_encrypt,
- flask_app_with_containers: Flask,
db_session_with_containers: Session,
tenant_id_1,
tenant_id_2,
@@ -89,62 +114,51 @@ class TestAuthIntegration:
mock_fc_http.return_value = self._create_success_response()
mock_jina_http.return_value = self._create_success_response()
mock_encrypt.return_value = "encrypted_key"
+ service = self._service(db_session_with_containers)
- args1 = {"category": category, "provider": AuthType.FIRECRAWL, "credentials": firecrawl_credentials}
- ApiKeyAuthService.create_provider_auth(tenant_id_1, args1, session=db_session_with_containers)
-
- args2 = {"category": category, "provider": AuthType.JINA, "credentials": jina_credentials}
- ApiKeyAuthService.create_provider_auth(tenant_id_2, args2, session=db_session_with_containers)
-
- db_session_with_containers.expire_all()
-
- result1 = ApiKeyAuthService.get_provider_auth_list(tenant_id_1, session=db_session_with_containers)
- result2 = ApiKeyAuthService.get_provider_auth_list(tenant_id_2, session=db_session_with_containers)
+ service.create_binding(
+ self._context(tenant_id_1),
+ DataSourceApiKeyAuthBindingCreate(category, _FIRECRAWL_PROVIDER, firecrawl_credentials),
+ )
+ service.create_binding(
+ self._context(tenant_id_2),
+ DataSourceApiKeyAuthBindingCreate(category, _JINA_PROVIDER, jina_credentials),
+ )
+ result1 = service.list_bindings(self._context(tenant_id_1))
+ result2 = service.list_bindings(self._context(tenant_id_2))
assert len(result1) == 1
- assert result1[0].tenant_id == tenant_id_1
+ assert result1[0].provider == _FIRECRAWL_PROVIDER
assert len(result2) == 1
- assert result2[0].tenant_id == tenant_id_2
-
- def test_cross_tenant_access_prevention(
- self, flask_app_with_containers: Flask, db_session_with_containers: Session, tenant_id_2, category
- ):
- result = ApiKeyAuthService.get_auth_credentials(
- tenant_id_2, category, AuthType.FIRECRAWL, session=db_session_with_containers
- )
-
- assert result is None
+ assert result2[0].provider == _JINA_PROVIDER
@patch("services.auth.firecrawl.firecrawl.httpx.post")
- @patch("services.auth.api_key_auth_service.encrypter.encrypt_token", return_value="encrypted_key")
+ @patch("services.auth.data_source_api_key_auth_gateways.encrypter.encrypt_token", return_value="encrypted_key")
def test_concurrent_creation_safety(
self,
mock_encrypt,
mock_http,
- flask_app_with_containers: Flask,
db_session_with_containers: Session,
tenant_id_1,
category,
firecrawl_credentials,
):
- app = flask_app_with_containers
mock_http.return_value = self._create_success_response()
-
+ service = self._service(db_session_with_containers)
results = []
exceptions = []
def create_auth():
try:
- with app.app_context():
- thread_args = {
- "category": category,
- "provider": AuthType.FIRECRAWL,
- "credentials": {"auth_type": "bearer", "config": {"api_key": "fc_test_key_123"}},
- }
- ApiKeyAuthService.create_provider_auth(tenant_id_1, thread_args, session=db.session())
+ command = DataSourceApiKeyAuthBindingCreate(
+ category,
+ _FIRECRAWL_PROVIDER,
+ DataSourceApiKeyAuthCredentials("bearer", "fc_test_key_123", {}),
+ )
+ service.create_binding(self._context(tenant_id_1), command)
results.append("success")
- except Exception as e:
- exceptions.append(e)
+ except Exception as exc:
+ exceptions.append(exc)
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(create_auth) for _ in range(5)]
@@ -158,50 +172,24 @@ class TestAuthIntegration:
def test_network_failure_recovery(
self,
mock_http,
- flask_app_with_containers: Flask,
db_session_with_containers: Session,
tenant_id_1,
category,
firecrawl_credentials,
):
- mock_http.side_effect = httpx.RequestError("Network timeout")
+ mock_http.side_effect = httpx.ConnectError("Network unavailable")
+ service = self._service(db_session_with_containers)
+ command = DataSourceApiKeyAuthBindingCreate(category, _FIRECRAWL_PROVIDER, firecrawl_credentials)
- args = {"category": category, "provider": AuthType.FIRECRAWL, "credentials": firecrawl_credentials}
-
- with pytest.raises(httpx.RequestError):
- ApiKeyAuthService.create_provider_auth(tenant_id_1, args, session=db_session_with_containers)
+ with pytest.raises(DataSourceApiKeyAuthProviderUnavailableError):
+ service.create_binding(self._context(tenant_id_1), command)
db_session_with_containers.expire_all()
bindings = db_session_with_containers.query(DataSourceApiKeyAuthBinding).filter_by(tenant_id=tenant_id_1).all()
assert len(bindings) == 0
- @patch("services.auth.api_key_auth_service.encrypter.encrypt_token")
- @patch("services.auth.firecrawl.firecrawl.httpx.post")
- def test_get_auth_credentials_returns_stored_credentials(
- self,
- mock_http,
- mock_encrypt,
- flask_app_with_containers: Flask,
- db_session_with_containers: Session,
- tenant_id_1,
- category,
- firecrawl_credentials,
- ):
- mock_http.return_value = self._create_success_response()
- mock_encrypt.return_value = "encrypted_key"
-
- args = {"category": category, "provider": AuthType.FIRECRAWL, "credentials": firecrawl_credentials}
- ApiKeyAuthService.create_provider_auth(tenant_id_1, args, session=db_session_with_containers)
-
- db_session_with_containers.expire_all()
-
- result = ApiKeyAuthService.get_auth_credentials(
- tenant_id_1, category, AuthType.FIRECRAWL, session=db_session_with_containers
- )
- assert result is not None
- assert result["config"]["api_key"] == "encrypted_key"
-
- def _create_success_response(self, status_code=200):
+ @staticmethod
+ def _create_success_response(status_code=200):
mock_response = Mock()
mock_response.status_code = status_code
mock_response.json.return_value = {"status": "success"}
diff --git a/api/tests/unit_tests/commands/test_generate_swagger_specs.py b/api/tests/unit_tests/commands/test_generate_swagger_specs.py
index 8726067822b..89023f4b1df 100644
--- a/api/tests/unit_tests/commands/test_generate_swagger_specs.py
+++ b/api/tests/unit_tests/commands/test_generate_swagger_specs.py
@@ -216,6 +216,16 @@ def test_generate_specs_include_console_contract_shapes_for_schema_migration(tmp
assert file_upload_schema["properties"]["file"]["type"] == "string"
assert file_upload_schema["properties"]["source"]["enum"] == ["datasets"]
+ api_key_auth_binding_schema = _request_schema(paths["/api-key-auth/data-source/binding"]["post"])
+ assert api_key_auth_binding_schema["$ref"] == "#/components/schemas/ApiKeyAuthBindingPayload"
+ assert schemas["ApiKeyAuthBindingPayload"]["properties"]["credentials"]["$ref"] == (
+ "#/components/schemas/ApiKeyAuthCredentialsPayload"
+ )
+ assert schemas["ApiKeyAuthCredentialsPayload"]["properties"]["config"]["$ref"] == (
+ "#/components/schemas/ApiKeyAuthConfigPayload"
+ )
+ assert schemas["ApiKeyAuthConfigPayload"]["properties"]["api_key"]["minLength"] == 1
+
invoices_schema_ref = _response_schema(paths["/billing/invoices"]["get"])["$ref"].removeprefix(
"#/components/schemas/"
)
diff --git a/api/tests/unit_tests/controllers/console/auth/test_data_source_bearer_auth.py b/api/tests/unit_tests/controllers/console/auth/test_data_source_bearer_auth.py
index 549a78a6915..7f04e26e0ca 100644
--- a/api/tests/unit_tests/controllers/console/auth/test_data_source_bearer_auth.py
+++ b/api/tests/unit_tests/controllers/console/auth/test_data_source_bearer_auth.py
@@ -3,7 +3,11 @@ from __future__ import annotations
from datetime import UTC, datetime
from inspect import unwrap
from types import SimpleNamespace
-from unittest.mock import ANY, patch
+from unittest.mock import create_autospec, patch
+from uuid import UUID
+
+import pytest
+from pydantic import ValidationError
from controllers.console.auth.data_source_bearer_auth import (
ApiKeyAuthBindingPayload,
@@ -11,68 +15,238 @@ from controllers.console.auth.data_source_bearer_auth import (
ApiKeyAuthDataSourceBinding,
ApiKeyAuthDataSourceBindingDelete,
)
+from controllers.console.auth.error import (
+ DataSourceApiKeyAuthCredentialsRejectedRequestError,
+ DataSourceApiKeyAuthProviderNotSupportedError,
+ DataSourceApiKeyAuthProviderUnavailableRequestError,
+ InvalidDataSourceApiKeyAuthCredentialsRequestError,
+)
+from libs.exception import BaseHTTPException
+from machinery.context import RequestContext
+from machinery.errors import ActiveWorkspaceRequiredError
+from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthService
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+ UnsupportedDataSourceApiKeyAuthProviderError,
+)
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingCreate,
+ DataSourceApiKeyAuthBindingRecord,
+ DataSourceApiKeyAuthCredentials,
+)
+
+
+def _request_context() -> RequestContext:
+ return RequestContext(
+ request_id="request-1",
+ trace_id="trace-1",
+ account_id="account-1",
+ active_workspace_id="tenant-1",
+ )
-def test_list_data_source_auth_uses_injected_tenant_id() -> None:
+def _application_services(service: DataSourceApiKeyAuthService) -> SimpleNamespace:
+ return SimpleNamespace(data_source_api_key_auth=service)
+
+
+def test_list_data_source_auth_passes_context_and_serializes_application_result() -> None:
api = ApiKeyAuthDataSource()
method = unwrap(api.get)
- binding = SimpleNamespace(
- id="binding-1",
- category="api_key",
- provider="custom",
- disabled=False,
- created_at=datetime(2026, 1, 1, tzinfo=UTC),
- updated_at=datetime(2026, 1, 2, tzinfo=UTC),
+ request_context = _request_context()
+ service = create_autospec(DataSourceApiKeyAuthService, instance=True, spec_set=True)
+ service.list_bindings.return_value = (
+ DataSourceApiKeyAuthBindingRecord(
+ id="binding-1",
+ category="api_key",
+ provider="custom",
+ disabled=False,
+ created_at=datetime(2026, 1, 1, tzinfo=UTC),
+ updated_at=datetime(2026, 1, 2, tzinfo=UTC),
+ ),
)
- with (
- patch("controllers.console.auth.data_source_bearer_auth.db"),
- patch(
- "controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.get_provider_auth_list",
- return_value=[binding],
- ) as get_provider_auth_list,
+ with patch(
+ "controllers.console.auth.data_source_bearer_auth.application_services",
+ return_value=_application_services(service),
):
- result = method(api, "tenant-1")
+ result = method(api, request_context)
- get_provider_auth_list.assert_called_once_with("tenant-1", session=ANY)
- assert result["sources"][0]["id"] == "binding-1"
- assert result["sources"][0]["provider"] == "custom"
+ service.list_bindings.assert_called_once_with(request_context)
+ assert result == {
+ "sources": [
+ {
+ "id": "binding-1",
+ "category": "api_key",
+ "provider": "custom",
+ "disabled": False,
+ "created_at": 1767225600,
+ "updated_at": 1767312000,
+ }
+ ]
+ }
-def test_create_data_source_auth_binding_uses_injected_tenant_id() -> None:
+def test_create_data_source_auth_binding_passes_context_and_command() -> None:
api = ApiKeyAuthDataSourceBinding()
method = unwrap(api.post)
payload = {
"category": "api_key",
"provider": "custom",
- "credentials": {"auth_type": "api_key", "config": {"api_key": "secret"}},
+ "credentials": {
+ "auth_type": "api_key",
+ "config": {"api_key": "secret", "base_url": "https://example.com"},
+ },
}
+ request_context = _request_context()
req_data = ApiKeyAuthBindingPayload.model_validate(payload)
+ service = create_autospec(DataSourceApiKeyAuthService, instance=True, spec_set=True)
- with (
- patch("controllers.console.auth.data_source_bearer_auth.db"),
- patch("controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.validate_api_key_auth_args"),
- patch("controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.create_provider_auth") as create_auth,
+ with patch(
+ "controllers.console.auth.data_source_bearer_auth.application_services",
+ return_value=_application_services(service),
):
- result, status = method(api, req_data, "tenant-1")
+ result, status = method(api, req_data, request_context)
- create_auth.assert_called_once_with("tenant-1", payload, session=ANY)
+ service.create_binding.assert_called_once_with(
+ request_context,
+ DataSourceApiKeyAuthBindingCreate(
+ category="api_key",
+ provider="custom",
+ credentials=DataSourceApiKeyAuthCredentials(
+ "api_key",
+ "secret",
+ {"base_url": "https://example.com"},
+ ),
+ ),
+ )
assert result == {"result": "success"}
assert status == 200
-def test_delete_data_source_auth_binding_uses_injected_tenant_id() -> None:
- api = ApiKeyAuthDataSourceBindingDelete()
- method = unwrap(api.delete)
+def test_create_data_source_auth_binding_maps_unsupported_provider_to_bad_request() -> None:
+ api = ApiKeyAuthDataSourceBinding()
+ method = unwrap(api.post)
+ request_context = _request_context()
+ req_data = ApiKeyAuthBindingPayload(
+ category="api_key",
+ provider="unsupported",
+ credentials={"auth_type": "api_key", "config": {"api_key": "secret"}},
+ )
+ service = create_autospec(DataSourceApiKeyAuthService, instance=True, spec_set=True)
+ service.create_binding.side_effect = UnsupportedDataSourceApiKeyAuthProviderError(req_data.provider)
with (
- patch("controllers.console.auth.data_source_bearer_auth.db"),
patch(
- "controllers.console.auth.data_source_bearer_auth.ApiKeyAuthService.delete_provider_auth"
- ) as delete_provider_auth,
+ "controllers.console.auth.data_source_bearer_auth.application_services",
+ return_value=_application_services(service),
+ ),
+ pytest.raises(DataSourceApiKeyAuthProviderNotSupportedError) as exc_info,
+ ):
+ method(api, req_data, request_context)
+
+ assert exc_info.value.code == 400
+
+
+ ("service_error", "request_error", "expected_status"),
+ [
+ (
+ InvalidDataSourceApiKeyAuthCredentialsError("Invalid auth type"),
+ InvalidDataSourceApiKeyAuthCredentialsRequestError,
+ 400,
+ ),
+ (
+ DataSourceApiKeyAuthCredentialValidationError("Credentials rejected"),
+ DataSourceApiKeyAuthCredentialsRejectedRequestError,
+ 400,
+ ),
+ (
+ DataSourceApiKeyAuthProviderUnavailableError("firecrawl"),
+ DataSourceApiKeyAuthProviderUnavailableRequestError,
+ 502,
+ ),
+ ],
+)
+def test_create_data_source_auth_binding_maps_domain_errors(
+ service_error: Exception,
+ request_error: type[BaseHTTPException],
+ expected_status: int,
+) -> None:
+ api = ApiKeyAuthDataSourceBinding()
+ method = unwrap(api.post)
+ request_context = _request_context()
+ req_data = ApiKeyAuthBindingPayload(
+ category="api_key",
+ provider="firecrawl",
+ credentials={"auth_type": "bearer", "config": {"api_key": "secret"}},
+ )
+ service = create_autospec(DataSourceApiKeyAuthService, instance=True, spec_set=True)
+ service.create_binding.side_effect = service_error
+
+ with (
+ patch(
+ "controllers.console.auth.data_source_bearer_auth.application_services",
+ return_value=_application_services(service),
+ ),
+ pytest.raises(request_error) as exc_info,
+ ):
+ method(api, req_data, request_context)
+
+ assert exc_info.value.code == expected_status
+
+
+ "payload",
+ [
+ {"category": "", "provider": "firecrawl", "credentials": {"auth_type": "bearer", "config": {"api_key": "key"}}},
+ {"category": "api_key", "provider": "", "credentials": {"auth_type": "bearer", "config": {"api_key": "key"}}},
+ {"category": "api_key", "provider": "firecrawl", "credentials": {"config": {"api_key": "key"}}},
+ {"category": "api_key", "provider": "firecrawl", "credentials": {"auth_type": "bearer", "config": {}}},
+ {"category": "api_key", "provider": "firecrawl", "credentials": {"auth_type": "bearer", "config": "invalid"}},
+ ],
+)
+def test_api_key_auth_binding_payload_rejects_invalid_structure(payload: dict[str, object]) -> None:
+ with pytest.raises(ValidationError):
+ ApiKeyAuthBindingPayload.model_validate(payload)
+
+
+def test_create_data_source_auth_binding_preserves_admission_invariant_error() -> None:
+ api = ApiKeyAuthDataSourceBinding()
+ method = unwrap(api.post)
+ request_context = _request_context()
+ req_data = ApiKeyAuthBindingPayload(
+ category="api_key",
+ provider="firecrawl",
+ credentials={"auth_type": "bearer", "config": {"api_key": "secret"}},
+ )
+ service = create_autospec(DataSourceApiKeyAuthService, instance=True, spec_set=True)
+ service.create_binding.side_effect = ActiveWorkspaceRequiredError()
+
+ with (
+ patch(
+ "controllers.console.auth.data_source_bearer_auth.application_services",
+ return_value=_application_services(service),
+ ),
+ pytest.raises(ActiveWorkspaceRequiredError),
+ ):
+ method(api, req_data, request_context)
+
+
+def test_delete_data_source_auth_binding_passes_context_and_binding_id() -> None:
+ api = ApiKeyAuthDataSourceBindingDelete()
+ method = unwrap(api.delete)
+ request_context = _request_context()
+ binding_id = UUID("31a00aeb-0865-4dd2-952a-5e0de34c57cc")
+ service = create_autospec(DataSourceApiKeyAuthService, instance=True, spec_set=True)
+
+ with patch(
+ "controllers.console.auth.data_source_bearer_auth.application_services",
+ return_value=_application_services(service),
):
- result, status = method(api, "tenant-1", "binding-1")
+ result, status = method(api, request_context, binding_id)
- delete_provider_auth.assert_called_once_with("tenant-1", "binding-1", session=ANY)
+ service.delete_binding.assert_called_once_with(request_context, str(binding_id))
assert result == ""
assert status == 204
diff --git a/api/tests/unit_tests/controllers/console/test_wraps.py b/api/tests/unit_tests/controllers/console/test_wraps.py
index a1432098416..37b1f0782c2 100644
--- a/api/tests/unit_tests/controllers/console/test_wraps.py
+++ b/api/tests/unit_tests/controllers/console/test_wraps.py
@@ -11,6 +11,7 @@ from sqlalchemy.orm import Session
from werkzeug.exceptions import HTTPException
from controllers.common.wraps import _extract_resource_id
+from controllers.console import api as console_api
from controllers.console import flask_admission
from controllers.console.error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogout
from controllers.console.workspace.error import AccountNotInitializedError
@@ -38,6 +39,7 @@ from controllers.console.wraps import (
from enums import DeploymentEdition
from libs.login import AccountWithTenant
from machinery.context import RequestContext
+from machinery.errors import ActiveWorkspaceRequiredError, AdmissionConfigurationError
from models import Account
from models.account import AccountStatus, TenantAccountRole
from models.dataset import Dataset, RateLimitLog
@@ -192,6 +194,91 @@ class TestCurrentContextInjection:
assert admission_context.active_workspace_id == "tenant-123"
assert route_value == "route-value"
+ def test_console_account_admission_enforces_legacy_workspace_roles(self):
+ current_user = make_account()
+ current_user.role = TenantAccountRole.NORMAL
+
+ with (
+ patch("controllers.console.flask_admission.setup_required", side_effect=lambda view: view),
+ patch("controllers.console.flask_admission.login_required", side_effect=lambda view: view),
+ patch("controllers.console.flask_admission.account_initialization_required", side_effect=lambda view: view),
+ patch("controllers.console.flask_admission.dify_config.RBAC_ENABLED", False),
+ patch(
+ "controllers.console.flask_admission.current_account_with_tenant",
+ return_value=AccountWithTenant(account=current_user, tenant_id="tenant-123"),
+ ),
+ ):
+
+ class Handler:
+ @flask_admission.console_account_admission(
+ allowed_roles=frozenset({TenantAccountRole.ADMIN, TenantAccountRole.OWNER})
+ )
+ def get(self, request_context: RequestContext):
+ return request_context
+
+ with Flask(__name__).test_request_context(), pytest.raises(HTTPException) as exc_info:
+ Handler().get()
+
+ assert exc_info.value.code == 403
+
+ def test_console_account_admission_enforces_declared_rbac_requirement(self):
+ current_user = make_account()
+
+ with (
+ patch("controllers.console.flask_admission.setup_required", side_effect=lambda view: view),
+ patch("controllers.console.flask_admission.login_required", side_effect=lambda view: view),
+ patch("controllers.console.flask_admission.account_initialization_required", side_effect=lambda view: view),
+ patch("controllers.console.flask_admission.dify_config.RBAC_ENABLED", True),
+ patch(
+ "controllers.console.flask_admission.current_account_with_tenant",
+ return_value=AccountWithTenant(account=current_user, tenant_id="tenant-123"),
+ ),
+ patch("controllers.console.flask_admission.get_request_id", return_value="request-1"),
+ patch("controllers.console.flask_admission.get_trace_id", return_value=None),
+ patch("controllers.console.flask_admission.enforce_rbac_access") as enforce_rbac_access,
+ ):
+
+ class Handler:
+ @flask_admission.console_account_admission(
+ allowed_roles=frozenset({TenantAccountRole.ADMIN, TenantAccountRole.OWNER}),
+ rbac_resource_scope=RBACResourceScope.WORKSPACE,
+ rbac_permission=RBACPermission.CREDENTIAL_CREATE,
+ rbac_resource_required=False,
+ )
+ def post(self, request_context: RequestContext):
+ return request_context
+
+ with Flask(__name__).test_request_context(headers={"X-Trace-Id": "trace-1"}):
+ result = Handler().post()
+
+ assert result.active_workspace_id == "tenant-123"
+ assert result.trace_id == "trace-1"
+ enforce_rbac_access.assert_called_once_with(
+ tenant_id="tenant-123",
+ account_id=current_user.id,
+ resource_type=RBACResourceScope.WORKSPACE,
+ scene=RBACPermission.CREDENTIAL_CREATE,
+ resource_required=False,
+ path_args={},
+ )
+
+ def test_console_account_admission_rejects_incomplete_rbac_requirement(self):
+ with pytest.raises(AdmissionConfigurationError, match="configured together"):
+ flask_admission.console_account_admission(rbac_resource_scope=RBACResourceScope.WORKSPACE)
+
+ def test_console_maps_missing_active_workspace_to_safe_internal_error(self):
+ handler = console_api.error_handlers[ActiveWorkspaceRequiredError]
+
+ with Flask(__name__).app_context():
+ body, status = handler(ActiveWorkspaceRequiredError())
+
+ assert status == 500
+ assert body == {
+ "code": "active_workspace_required",
+ "message": "Internal Server Error",
+ "status": 500,
+ }
+
def test_with_current_tenant_id_injects_tenant_id(self):
class Handler:
@with_current_tenant_id
diff --git a/api/tests/unit_tests/extensions/test_ext_application_services.py b/api/tests/unit_tests/extensions/test_ext_application_services.py
index 8122b953c10..d3cc79c9760 100644
--- a/api/tests/unit_tests/extensions/test_ext_application_services.py
+++ b/api/tests/unit_tests/extensions/test_ext_application_services.py
@@ -10,6 +10,7 @@ from enums import DeploymentEdition
from extensions import ext_application_services
from extensions.ext_redis import RedisClientWrapper
from models.model import DifySetup
+from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthService
from services.init_validation_service import InvalidInitializationPasswordError
@@ -134,3 +135,16 @@ def test_build_application_services_does_not_construct_schema_manager(
)
schema_manager.assert_not_called()
+
+
+def test_build_application_services_wires_data_source_api_key_auth(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ services = ext_application_services.build_application_services(
+ database_client=sqlite_session_factory,
+ deployment_edition=DeploymentEdition.COMMUNITY,
+ initialization_password="",
+ redis=MagicMock(spec=RedisClientWrapper),
+ )
+
+ assert isinstance(services.data_source_api_key_auth, DataSourceApiKeyAuthService)
diff --git a/api/tests/unit_tests/machinery/test_errors.py b/api/tests/unit_tests/machinery/test_errors.py
new file mode 100644
index 00000000000..1d3f2269fd1
--- /dev/null
+++ b/api/tests/unit_tests/machinery/test_errors.py
@@ -0,0 +1,33 @@
+from machinery.errors import ActiveWorkspaceRequiredError, AdmissionConfigurationError, ErrorDetail, MachineryError
+
+
+def test_admission_configuration_error_has_transport_neutral_contract() -> None:
+ detail = ErrorDetail(
+ type="incomplete_requirement",
+ location=("rbac", "permission"),
+ message="RBAC resource scope and permission must be configured together",
+ )
+
+ error = AdmissionConfigurationError("Invalid RBAC admission declaration", details=[detail])
+
+ assert isinstance(error, MachineryError)
+ assert not isinstance(error, ValueError)
+ assert error.error_code == "invalid_admission_configuration"
+ assert error.message == "Invalid RBAC admission declaration"
+ assert str(error) == error.message
+ assert error.details == (detail,)
+ assert not hasattr(error, "code")
+
+
+def test_admission_configuration_error_uses_stable_default_message() -> None:
+ error = AdmissionConfigurationError()
+
+ assert error.message == "Admission configuration is invalid."
+ assert error.details == ()
+
+
+def test_active_workspace_required_error_has_stable_contract() -> None:
+ error = ActiveWorkspaceRequiredError()
+
+ assert error.error_code == "active_workspace_required"
+ assert error.message == "Admission did not resolve an active workspace."
diff --git a/api/tests/unit_tests/repositories/test_data_source_api_key_auth_repository.py b/api/tests/unit_tests/repositories/test_data_source_api_key_auth_repository.py
new file mode 100644
index 00000000000..09c23b02898
--- /dev/null
+++ b/api/tests/unit_tests/repositories/test_data_source_api_key_auth_repository.py
@@ -0,0 +1,90 @@
+import json
+from datetime import datetime
+
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from models.source import DataSourceApiKeyAuthBinding
+from repositories.data_source_api_key_auth_repository import SQLAlchemyDataSourceApiKeyAuthBindingRepository
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
+
+
+def _binding(
+ *,
+ workspace_id: str,
+ binding_id: str,
+ disabled: bool = False,
+) -> DataSourceApiKeyAuthBinding:
+ binding = DataSourceApiKeyAuthBinding(
+ tenant_id=workspace_id,
+ category="search",
+ provider="firecrawl",
+ credentials=json.dumps({"auth_type": "bearer", "config": {"api_key": "encrypted"}}),
+ disabled=disabled,
+ )
+ binding.id = binding_id
+ binding.created_at = datetime(2026, 1, 1)
+ binding.updated_at = datetime(2026, 1, 2)
+ return binding
+
+
+def test_list_enabled_is_scoped_and_returns_detached_records(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add_all(
+ [
+ _binding(workspace_id="workspace-1", binding_id="enabled"),
+ _binding(workspace_id="workspace-1", binding_id="disabled", disabled=True),
+ _binding(workspace_id="workspace-2", binding_id="foreign"),
+ ]
+ )
+
+ repository = SQLAlchemyDataSourceApiKeyAuthBindingRepository(sqlite_session_factory)
+
+ assert [binding.id for binding in repository.list_enabled("workspace-1")] == ["enabled"]
+
+
+def test_create_serializes_credentials(sqlite_session_factory: sessionmaker[Session]) -> None:
+ repository = SQLAlchemyDataSourceApiKeyAuthBindingRepository(sqlite_session_factory)
+ credentials = DataSourceApiKeyAuthCredentials(
+ auth_type="bearer",
+ api_key="encrypted",
+ options={"base_url": "https://example.com"},
+ )
+
+ repository.create("workspace-1", "search", "firecrawl", credentials)
+
+ with sqlite_session_factory() as session:
+ binding = session.scalar(
+ select(DataSourceApiKeyAuthBinding).where(DataSourceApiKeyAuthBinding.tenant_id == "workspace-1")
+ )
+
+ assert binding is not None
+ assert binding.credentials is not None
+ assert json.loads(binding.credentials) == {
+ "auth_type": "bearer",
+ "config": {"api_key": "encrypted", "base_url": "https://example.com"},
+ }
+
+
+def test_delete_is_scoped_to_workspace(sqlite_session_factory: sessionmaker[Session]) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add(_binding(workspace_id="workspace-1", binding_id="binding-1"))
+
+ repository = SQLAlchemyDataSourceApiKeyAuthBindingRepository(sqlite_session_factory)
+ repository.delete("workspace-2", "binding-1")
+
+ with sqlite_session_factory() as session:
+ assert (
+ session.scalar(select(DataSourceApiKeyAuthBinding).where(DataSourceApiKeyAuthBinding.id == "binding-1"))
+ is not None
+ )
+
+ repository.delete("workspace-1", "binding-1")
+
+ with sqlite_session_factory() as session:
+ assert (
+ session.scalar(select(DataSourceApiKeyAuthBinding).where(DataSourceApiKeyAuthBinding.id == "binding-1"))
+ is None
+ )
diff --git a/api/tests/unit_tests/services/auth/test_api_key_auth_base.py b/api/tests/unit_tests/services/auth/test_api_key_auth_base.py
deleted file mode 100644
index 388504c07f8..00000000000
--- a/api/tests/unit_tests/services/auth/test_api_key_auth_base.py
+++ /dev/null
@@ -1,49 +0,0 @@
-import pytest
-
-from services.auth.api_key_auth_base import ApiKeyAuthBase
-
-
-class ConcreteApiKeyAuth(ApiKeyAuthBase):
- """Concrete implementation for testing abstract base class"""
-
- def validate_credentials(self):
- return True
-
-
-class TestApiKeyAuthBase:
- def test_should_store_credentials_on_init(self):
- """Test that credentials are properly stored during initialization"""
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_key"}}
- auth = ConcreteApiKeyAuth(credentials)
- assert auth.credentials == credentials
-
- def test_should_not_instantiate_abstract_class(self):
- """Test that ApiKeyAuthBase cannot be instantiated directly"""
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_key"}}
-
- with pytest.raises(TypeError) as exc_info:
- ApiKeyAuthBase(credentials)
-
- assert "Can't instantiate abstract class" in str(exc_info.value)
- assert "validate_credentials" in str(exc_info.value)
-
- def test_should_allow_subclass_implementation(self):
- """Test that subclasses can properly implement the abstract method"""
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_key"}}
- auth = ConcreteApiKeyAuth(credentials)
-
- # Should not raise any exception
- result = auth.validate_credentials()
- assert result is True
-
- def test_should_handle_empty_credentials(self):
- """Test initialization with empty credentials"""
- credentials = {}
- auth = ConcreteApiKeyAuth(credentials)
- assert auth.credentials == {}
-
- def test_should_handle_none_credentials(self):
- """Test initialization with None credentials"""
- credentials = None
- auth = ConcreteApiKeyAuth(credentials)
- assert auth.credentials is None
diff --git a/api/tests/unit_tests/services/auth/test_api_key_auth_factory.py b/api/tests/unit_tests/services/auth/test_api_key_auth_factory.py
deleted file mode 100644
index b1f7cf24f34..00000000000
--- a/api/tests/unit_tests/services/auth/test_api_key_auth_factory.py
+++ /dev/null
@@ -1,81 +0,0 @@
-from unittest.mock import MagicMock, patch
-
-import pytest
-
-from services.auth.api_key_auth_factory import ApiKeyAuthFactory
-from services.auth.auth_type import AuthType
-
-
-class TestApiKeyAuthFactory:
- """Test cases for ApiKeyAuthFactory"""
-
- @pytest.mark.parametrize(
- ("provider", "auth_class_path"),
- [
- (AuthType.FIRECRAWL, "services.auth.firecrawl.firecrawl.FirecrawlAuth"),
- (AuthType.WATERCRAWL, "services.auth.watercrawl.watercrawl.WatercrawlAuth"),
- (AuthType.JINA, "services.auth.jina.jina.JinaAuth"),
- ],
- )
- def test_get_apikey_auth_factory_valid_providers(self, provider, auth_class_path):
- """Test getting auth factory for all valid providers"""
- with patch(auth_class_path, autospec=True) as mock_auth:
- auth_class = ApiKeyAuthFactory.get_apikey_auth_factory(provider)
- assert auth_class == mock_auth
-
- @pytest.mark.parametrize(
- "invalid_provider",
- [
- "invalid_provider",
- "",
- None,
- 123,
- "UNSUPPORTED",
- ],
- )
- def test_get_apikey_auth_factory_invalid_providers(self, invalid_provider):
- """Test getting auth factory with various invalid providers"""
- with pytest.raises(ValueError) as exc_info:
- ApiKeyAuthFactory.get_apikey_auth_factory(invalid_provider)
- assert str(exc_info.value) == "Invalid provider"
-
- @pytest.mark.parametrize(
- ("credentials_return_value", "expected_result"),
- [
- (True, True),
- (False, False),
- ],
- )
- @patch("services.auth.api_key_auth_factory.ApiKeyAuthFactory.get_apikey_auth_factory", autospec=True)
- def test_validate_credentials_delegates_to_auth_instance(
- self, mock_get_factory, credentials_return_value, expected_result
- ):
- """Test that validate_credentials delegates to auth instance correctly"""
- # Arrange
- mock_auth_instance = MagicMock()
- mock_auth_instance.validate_credentials.return_value = credentials_return_value
- mock_auth_class = MagicMock(return_value=mock_auth_instance)
- mock_get_factory.return_value = mock_auth_class
-
- # Act
- factory = ApiKeyAuthFactory(AuthType.FIRECRAWL, {"auth_type": "bearer", "config": {"api_key": "test_key"}})
- result = factory.validate_credentials()
-
- # Assert
- assert result is expected_result
- mock_auth_instance.validate_credentials.assert_called_once()
-
- @patch("services.auth.api_key_auth_factory.ApiKeyAuthFactory.get_apikey_auth_factory", autospec=True)
- def test_validate_credentials_propagates_exceptions(self, mock_get_factory):
- """Test that exceptions from auth instance are propagated"""
- # Arrange
- mock_auth_instance = MagicMock()
- mock_auth_instance.validate_credentials.side_effect = Exception("Authentication error")
- mock_auth_class = MagicMock(return_value=mock_auth_instance)
- mock_get_factory.return_value = mock_auth_class
-
- # Act & Assert
- factory = ApiKeyAuthFactory(AuthType.FIRECRAWL, {"auth_type": "bearer", "config": {"api_key": "test_key"}})
- with pytest.raises(Exception) as exc_info:
- factory.validate_credentials()
- assert str(exc_info.value) == "Authentication error"
diff --git a/api/tests/unit_tests/services/auth/test_api_key_auth_service.py b/api/tests/unit_tests/services/auth/test_api_key_auth_service.py
deleted file mode 100644
index 4bf1913dd5d..00000000000
--- a/api/tests/unit_tests/services/auth/test_api_key_auth_service.py
+++ /dev/null
@@ -1,309 +0,0 @@
-"""Unit tests for API-key authentication using an SQLite binding table."""
-
-from __future__ import annotations
-
-import json
-from unittest.mock import MagicMock, Mock, patch
-from uuid import uuid4
-
-import pytest
-from sqlalchemy import event
-from sqlalchemy.orm import Session
-
-from models.source import DataSourceApiKeyAuthBinding
-from services.auth.api_key_auth_service import ApiKeyAuthService
-
-
[email protected]("sqlite_session", [(DataSourceApiKeyAuthBinding,)], indirect=True)
[email protected]("sqlite_session")
-class TestApiKeyAuthService:
- @pytest.fixture
- def tenant_id(self) -> str:
- return str(uuid4())
-
- @pytest.fixture
- def category(self) -> str:
- return "search"
-
- @pytest.fixture
- def provider(self) -> str:
- return "google"
-
- @pytest.fixture
- def mock_credentials(self) -> dict:
- return {"auth_type": "api_key", "config": {"api_key": "test_secret_key_123"}}
-
- @pytest.fixture
- def mock_args(self, category, provider, mock_credentials) -> dict:
- return {"category": category, "provider": provider, "credentials": mock_credentials}
-
- def _create_binding(self, db_session: Session, *, tenant_id, category, provider, credentials=None, disabled=False):
- binding = DataSourceApiKeyAuthBinding(
- tenant_id=tenant_id,
- category=category,
- provider=provider,
- credentials=json.dumps(credentials, ensure_ascii=False) if credentials else None,
- disabled=disabled,
- )
- db_session.add(binding)
- db_session.commit()
- return binding
-
- def test_get_provider_auth_list_success(self, sqlite_session: Session, tenant_id, category, provider):
- self._create_binding(sqlite_session, tenant_id=tenant_id, category=category, provider=provider)
- sqlite_session.expire_all()
-
- result = ApiKeyAuthService.get_provider_auth_list(tenant_id, session=sqlite_session)
-
- assert len(result) >= 1
- tenant_results = [r for r in result if r.tenant_id == tenant_id]
- assert len(tenant_results) == 1
- assert tenant_results[0].provider == provider
-
- def test_get_provider_auth_list_empty(self, sqlite_session: Session, tenant_id):
- result = ApiKeyAuthService.get_provider_auth_list(tenant_id, session=sqlite_session)
-
- tenant_results = [r for r in result if r.tenant_id == tenant_id]
- assert tenant_results == []
-
- def test_get_provider_auth_list_filters_disabled(self, sqlite_session: Session, tenant_id, category, provider):
- self._create_binding(sqlite_session, tenant_id=tenant_id, category=category, provider=provider, disabled=True)
- sqlite_session.expire_all()
-
- result = ApiKeyAuthService.get_provider_auth_list(tenant_id, session=sqlite_session)
-
- tenant_results = [r for r in result if r.tenant_id == tenant_id]
- assert tenant_results == []
-
- @patch("services.auth.api_key_auth_service.ApiKeyAuthFactory")
- @patch("services.auth.api_key_auth_service.encrypter")
- def test_create_provider_auth_success(
- self,
- mock_encrypter: MagicMock,
- mock_factory: MagicMock,
- sqlite_session: Session,
- tenant_id,
- mock_args,
- ):
- mock_auth_instance = Mock()
- mock_auth_instance.validate_credentials.return_value = True
- mock_factory.return_value = mock_auth_instance
- mock_encrypter.encrypt_token.return_value = "encrypted_test_key_123"
-
- ApiKeyAuthService.create_provider_auth(tenant_id, mock_args, session=sqlite_session)
-
- mock_factory.assert_called_once()
- mock_auth_instance.validate_credentials.assert_called_once()
- mock_encrypter.encrypt_token.assert_called_once_with(tenant_id, "test_secret_key_123")
-
- sqlite_session.expire_all()
- bindings = sqlite_session.query(DataSourceApiKeyAuthBinding).filter_by(tenant_id=tenant_id).all()
- assert len(bindings) == 1
-
- @patch("services.auth.api_key_auth_service.ApiKeyAuthFactory")
- def test_create_provider_auth_validation_failed(
- self,
- mock_factory: MagicMock,
- sqlite_session: Session,
- tenant_id,
- mock_args,
- ):
- mock_auth_instance = Mock()
- mock_auth_instance.validate_credentials.return_value = False
- mock_factory.return_value = mock_auth_instance
-
- ApiKeyAuthService.create_provider_auth(tenant_id, mock_args, session=sqlite_session)
-
- sqlite_session.expire_all()
- bindings = sqlite_session.query(DataSourceApiKeyAuthBinding).filter_by(tenant_id=tenant_id).all()
- assert len(bindings) == 0
-
- @patch("services.auth.api_key_auth_service.ApiKeyAuthFactory")
- @patch("services.auth.api_key_auth_service.encrypter")
- def test_create_provider_auth_encrypts_api_key(
- self,
- mock_encrypter: MagicMock,
- mock_factory: MagicMock,
- sqlite_session: Session,
- tenant_id,
- mock_args,
- ):
- mock_auth_instance = Mock()
- mock_auth_instance.validate_credentials.return_value = True
- mock_factory.return_value = mock_auth_instance
- mock_encrypter.encrypt_token.return_value = "encrypted_test_key_123"
-
- original_key = mock_args["credentials"]["config"]["api_key"]
-
- ApiKeyAuthService.create_provider_auth(tenant_id, mock_args, session=sqlite_session)
-
- assert mock_args["credentials"]["config"]["api_key"] == "encrypted_test_key_123"
- assert mock_args["credentials"]["config"]["api_key"] != original_key
- mock_encrypter.encrypt_token.assert_called_once_with(tenant_id, original_key)
-
- def test_get_auth_credentials_success(
- self,
- sqlite_session: Session,
- tenant_id,
- category,
- provider,
- mock_credentials,
- ):
- self._create_binding(
- sqlite_session,
- tenant_id=tenant_id,
- category=category,
- provider=provider,
- credentials=mock_credentials,
- )
- sqlite_session.expire_all()
-
- result = ApiKeyAuthService.get_auth_credentials(tenant_id, category, provider, session=sqlite_session)
-
- assert result == mock_credentials
-
- def test_get_auth_credentials_not_found(self, sqlite_session: Session, tenant_id, category, provider):
- result = ApiKeyAuthService.get_auth_credentials(tenant_id, category, provider, session=sqlite_session)
-
- assert result is None
-
- def test_get_auth_credentials_json_parsing(self, sqlite_session: Session, tenant_id, category, provider):
- special_credentials = {"auth_type": "api_key", "config": {"api_key": "key_with_中文_and_special_chars_!@#$%"}}
- self._create_binding(
- sqlite_session,
- tenant_id=tenant_id,
- category=category,
- provider=provider,
- credentials=special_credentials,
- )
- sqlite_session.expire_all()
-
- result = ApiKeyAuthService.get_auth_credentials(tenant_id, category, provider, session=sqlite_session)
-
- assert result == special_credentials
- assert result["config"]["api_key"] == "key_with_中文_and_special_chars_!@#$%"
-
- def test_delete_provider_auth_success(self, sqlite_session: Session, tenant_id, category, provider):
- binding = self._create_binding(sqlite_session, tenant_id=tenant_id, category=category, provider=provider)
- binding_id = binding.id
- sqlite_session.expire_all()
-
- ApiKeyAuthService.delete_provider_auth(tenant_id, binding_id, session=sqlite_session)
-
- sqlite_session.expire_all()
- remaining = sqlite_session.query(DataSourceApiKeyAuthBinding).filter_by(id=binding_id).first()
- assert remaining is None
-
- def test_delete_provider_auth_not_found(self, sqlite_session: Session, tenant_id):
- # Should not raise when binding not found
- ApiKeyAuthService.delete_provider_auth(tenant_id, str(uuid4()), session=sqlite_session)
-
- def test_validate_api_key_auth_args_success(self, mock_args):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_missing_category(self, mock_args):
- del mock_args["category"]
- with pytest.raises(ValueError, match="category is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_empty_category(self, mock_args):
- mock_args["category"] = ""
- with pytest.raises(ValueError, match="category is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_missing_provider(self, mock_args):
- del mock_args["provider"]
- with pytest.raises(ValueError, match="provider is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_empty_provider(self, mock_args):
- mock_args["provider"] = ""
- with pytest.raises(ValueError, match="provider is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_missing_credentials(self, mock_args):
- del mock_args["credentials"]
- with pytest.raises(ValueError, match="credentials is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_empty_credentials(self, mock_args):
- mock_args["credentials"] = None
- with pytest.raises(ValueError, match="credentials is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_invalid_credentials_type(self, mock_args):
- mock_args["credentials"] = "not_a_dict"
- with pytest.raises(ValueError, match="credentials must be a dictionary"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_missing_auth_type(self, mock_args):
- del mock_args["credentials"]["auth_type"]
- with pytest.raises(ValueError, match="auth_type is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- def test_validate_api_key_auth_args_empty_auth_type(self, mock_args):
- mock_args["credentials"]["auth_type"] = ""
- with pytest.raises(ValueError, match="auth_type is required"):
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- @pytest.mark.parametrize(
- "malicious_input",
- [
- "<script>alert('xss')</script>",
- "'; DROP TABLE users; --",
- "../../../etc/passwd",
- "\\x00\\x00",
- "A" * 10000,
- ],
- )
- def test_validate_api_key_auth_args_malicious_input(self, malicious_input, mock_args):
- mock_args["category"] = malicious_input
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
-
- @patch("services.auth.api_key_auth_service.ApiKeyAuthFactory")
- @patch("services.auth.api_key_auth_service.encrypter")
- def test_create_provider_auth_database_error_handling(
- self, mock_encrypter, mock_factory, tenant_id, mock_args, sqlite_session: Session
- ) -> None:
- mock_auth_instance = Mock()
- mock_auth_instance.validate_credentials.return_value = True
- mock_factory.return_value = mock_auth_instance
- mock_encrypter.encrypt_token.return_value = "encrypted_key"
-
- def raise_database_error(_session: Session) -> None:
- raise Exception("Database error")
-
- event.listen(sqlite_session, "before_commit", raise_database_error)
- try:
- with pytest.raises(Exception, match="Database error"):
- ApiKeyAuthService.create_provider_auth(tenant_id, mock_args, session=sqlite_session)
- finally:
- event.remove(sqlite_session, "before_commit", raise_database_error)
-
- @patch("services.auth.api_key_auth_service.ApiKeyAuthFactory")
- def test_create_provider_auth_factory_exception(
- self, mock_factory: MagicMock, tenant_id, mock_args, sqlite_session: Session
- ) -> None:
- mock_factory.side_effect = Exception("Factory error")
- with pytest.raises(Exception, match="Factory error"):
- ApiKeyAuthService.create_provider_auth(tenant_id, mock_args, session=sqlite_session)
-
- @patch("services.auth.api_key_auth_service.ApiKeyAuthFactory")
- @patch("services.auth.api_key_auth_service.encrypter")
- def test_create_provider_auth_encryption_exception(
- self, mock_encrypter, mock_factory, tenant_id, mock_args, sqlite_session: Session
- ) -> None:
- mock_auth_instance = Mock()
- mock_auth_instance.validate_credentials.return_value = True
- mock_factory.return_value = mock_auth_instance
- mock_encrypter.encrypt_token.side_effect = Exception("Encryption error")
- with pytest.raises(Exception, match="Encryption error"):
- ApiKeyAuthService.create_provider_auth(tenant_id, mock_args, session=sqlite_session)
-
- def test_validate_api_key_auth_args_none_input(self):
- with pytest.raises(TypeError):
- ApiKeyAuthService.validate_api_key_auth_args(None)
-
- def test_validate_api_key_auth_args_dict_credentials_with_list_auth_type(self, mock_args):
- mock_args["credentials"]["auth_type"] = ["api_key"]
- ApiKeyAuthService.validate_api_key_auth_args(mock_args)
diff --git a/api/tests/unit_tests/services/auth/test_auth_type.py b/api/tests/unit_tests/services/auth/test_auth_type.py
deleted file mode 100644
index fb67dabcc56..00000000000
--- a/api/tests/unit_tests/services/auth/test_auth_type.py
+++ /dev/null
@@ -1,149 +0,0 @@
-import pytest
-
-from services.auth.auth_type import AuthType
-
-
-class TestAuthType:
- """Test cases for AuthType enum"""
-
- def test_auth_type_is_str_enum(self):
- """Test that AuthType is properly a StrEnum"""
- assert issubclass(AuthType, str)
- assert hasattr(AuthType, "__members__")
-
- def test_auth_type_has_expected_values(self):
- """Test that all expected auth types exist with correct values"""
- expected_values = {
- "FIRECRAWL": "firecrawl",
- "WATERCRAWL": "watercrawl",
- "JINA": "jinareader",
- }
-
- # Verify all expected members exist
- for member_name, expected_value in expected_values.items():
- assert hasattr(AuthType, member_name)
- assert getattr(AuthType, member_name).value == expected_value
-
- # Verify no extra members exist
- assert len(AuthType) == len(expected_values)
-
- @pytest.mark.parametrize(
- ("auth_type", "expected_string"),
- [
- (AuthType.FIRECRAWL, "firecrawl"),
- (AuthType.WATERCRAWL, "watercrawl"),
- (AuthType.JINA, "jinareader"),
- ],
- )
- def test_auth_type_string_representation(self, auth_type, expected_string):
- """Test string representation of auth types"""
- assert str(auth_type) == expected_string
- assert auth_type.value == expected_string
-
- @pytest.mark.parametrize(
- ("auth_type", "compare_value", "expected_result"),
- [
- (AuthType.FIRECRAWL, "firecrawl", True),
- (AuthType.WATERCRAWL, "watercrawl", True),
- (AuthType.JINA, "jinareader", True),
- (AuthType.FIRECRAWL, "FIRECRAWL", False), # Case sensitive
- (AuthType.FIRECRAWL, "watercrawl", False),
- (AuthType.JINA, "jina", False), # Full value mismatch
- ],
- )
- def test_auth_type_comparison(self, auth_type, compare_value, expected_result):
- """Test auth type comparison with strings"""
- assert (auth_type == compare_value) is expected_result
-
- def test_auth_type_iteration(self):
- """Test that AuthType can be iterated over"""
- auth_types = list(AuthType)
- assert len(auth_types) == 3
- assert AuthType.FIRECRAWL in auth_types
- assert AuthType.WATERCRAWL in auth_types
- assert AuthType.JINA in auth_types
-
- def test_auth_type_membership(self):
- """Test membership checking for AuthType"""
- assert "firecrawl" in [auth.value for auth in AuthType]
- assert "watercrawl" in [auth.value for auth in AuthType]
- assert "jinareader" in [auth.value for auth in AuthType]
- assert "invalid" not in [auth.value for auth in AuthType]
-
- def test_auth_type_invalid_attribute_access(self):
- """Test accessing non-existent auth type raises AttributeError"""
- with pytest.raises(AttributeError):
- _ = AuthType.INVALID_TYPE
-
- def test_auth_type_immutability(self):
- """Test that enum values cannot be modified"""
- with pytest.raises(AttributeError):
- AuthType.FIRECRAWL = "modified"
-
- def test_auth_type_from_value(self):
- """Test creating AuthType from string value"""
- assert AuthType("firecrawl") == AuthType.FIRECRAWL
- assert AuthType("watercrawl") == AuthType.WATERCRAWL
- assert AuthType("jinareader") == AuthType.JINA
-
- # Test invalid value
- with pytest.raises(ValueError) as exc_info:
- AuthType("invalid_auth_type")
- assert "invalid_auth_type" in str(exc_info.value)
-
- def test_auth_type_name_property(self):
- """Test the name property of enum members"""
- assert AuthType.FIRECRAWL.name == "FIRECRAWL"
- assert AuthType.WATERCRAWL.name == "WATERCRAWL"
- assert AuthType.JINA.name == "JINA"
-
- @pytest.mark.parametrize(
- "auth_type",
- [AuthType.FIRECRAWL, AuthType.WATERCRAWL, AuthType.JINA],
- )
- def test_auth_type_isinstance_checks(self, auth_type):
- """Test isinstance checks for auth types"""
- assert isinstance(auth_type, AuthType)
- assert isinstance(auth_type, str)
- assert isinstance(auth_type.value, str)
-
- def test_auth_type_hash(self):
- """Test that auth types are hashable and can be used in sets/dicts"""
- auth_set = {AuthType.FIRECRAWL, AuthType.WATERCRAWL, AuthType.JINA}
- assert len(auth_set) == 3
-
- auth_dict = {
- AuthType.FIRECRAWL: "firecrawl_handler",
- AuthType.WATERCRAWL: "watercrawl_handler",
- AuthType.JINA: "jina_handler",
- }
- assert auth_dict[AuthType.FIRECRAWL] == "firecrawl_handler"
-
- def test_auth_type_json_serializable(self):
- """Test that auth types can be JSON serialized"""
- import json
-
- auth_data = {
- "provider": AuthType.FIRECRAWL,
- "enabled": True,
- }
-
- # Should serialize to string value
- json_str = json.dumps(auth_data, default=str)
- assert '"provider": "firecrawl"' in json_str
-
- def test_auth_type_matches_factory_usage(self):
- """Test that all AuthType values are handled by ApiKeyAuthFactory"""
- # This test verifies that the enum values match what's expected
- # by the factory implementation
- from services.auth.api_key_auth_factory import ApiKeyAuthFactory
-
- for auth_type in AuthType:
- # Should not raise ValueError for valid auth types
- try:
- auth_class = ApiKeyAuthFactory.get_apikey_auth_factory(auth_type)
- assert auth_class is not None
- except ImportError:
- # It's OK if the actual auth implementation doesn't exist
- # We're just testing that the enum value is recognized
- pass
diff --git a/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_gateways.py b/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_gateways.py
new file mode 100644
index 00000000000..8de8e952484
--- /dev/null
+++ b/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_gateways.py
@@ -0,0 +1,132 @@
+from unittest.mock import MagicMock, patch
+
+import httpx
+import pytest
+
+from services.auth.data_source_api_key_auth_gateways import ProviderApiKeyAuthCredentialValidator
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ UnsupportedDataSourceApiKeyAuthProviderError,
+)
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
+
+
+class TestProviderApiKeyAuthCredentialValidator:
+ @pytest.mark.parametrize(
+ ("provider", "auth_class_path", "credentials"),
+ [
+ (
+ "firecrawl",
+ "services.auth.firecrawl.firecrawl.FirecrawlAuth",
+ DataSourceApiKeyAuthCredentials("bearer", "test_key", {}),
+ ),
+ (
+ "watercrawl",
+ "services.auth.watercrawl.watercrawl.WatercrawlAuth",
+ DataSourceApiKeyAuthCredentials("x-api-key", "test_key", {}),
+ ),
+ (
+ "jinareader",
+ "services.auth.jina.jina.JinaAuth",
+ DataSourceApiKeyAuthCredentials("bearer", "test_key", {}),
+ ),
+ ],
+ )
+ def test_validate_routes_to_provider(
+ self,
+ provider: str,
+ auth_class_path: str,
+ credentials: DataSourceApiKeyAuthCredentials,
+ ) -> None:
+ auth_instance = MagicMock()
+ auth_instance.validate_credentials.return_value = True
+
+ with patch(auth_class_path, autospec=True, return_value=auth_instance) as auth_factory:
+ result = ProviderApiKeyAuthCredentialValidator().validate(provider, credentials)
+
+ assert result is True
+ auth_factory.assert_called_once_with(credentials)
+ auth_instance.validate_credentials.assert_called_once_with()
+
+ @pytest.mark.parametrize("invalid_provider", ["invalid_provider", "", "UNSUPPORTED"])
+ def test_validate_rejects_unknown_provider(self, invalid_provider: str) -> None:
+ credentials = DataSourceApiKeyAuthCredentials("bearer", "test_key", {})
+
+ with pytest.raises(
+ UnsupportedDataSourceApiKeyAuthProviderError,
+ match=f"Unsupported data-source API-key auth provider: {invalid_provider}",
+ ):
+ ProviderApiKeyAuthCredentialValidator().validate(invalid_provider, credentials)
+
+ @pytest.mark.parametrize("validation_result", [True, False])
+ def test_validate_returns_provider_result(self, validation_result: bool) -> None:
+ auth_instance = MagicMock()
+ auth_instance.validate_credentials.return_value = validation_result
+
+ with patch(
+ "services.auth.firecrawl.firecrawl.FirecrawlAuth",
+ autospec=True,
+ return_value=auth_instance,
+ ):
+ result = ProviderApiKeyAuthCredentialValidator().validate(
+ "firecrawl",
+ DataSourceApiKeyAuthCredentials("bearer", "test_key", {}),
+ )
+
+ assert result is validation_result
+
+ def test_validate_propagates_credential_validation_error(self) -> None:
+ auth_instance = MagicMock()
+ auth_instance.validate_credentials.side_effect = DataSourceApiKeyAuthCredentialValidationError(
+ "Authentication error"
+ )
+
+ with patch(
+ "services.auth.firecrawl.firecrawl.FirecrawlAuth",
+ autospec=True,
+ return_value=auth_instance,
+ ):
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError, match="Authentication error"):
+ ProviderApiKeyAuthCredentialValidator().validate(
+ "firecrawl",
+ DataSourceApiKeyAuthCredentials("bearer", "test_key", {}),
+ )
+
+ def test_validate_maps_provider_network_error(self) -> None:
+ auth_instance = MagicMock()
+ auth_instance.validate_credentials.side_effect = httpx.ConnectError("Authentication endpoint unavailable")
+
+ with patch(
+ "services.auth.firecrawl.firecrawl.FirecrawlAuth",
+ autospec=True,
+ return_value=auth_instance,
+ ):
+ with pytest.raises(
+ DataSourceApiKeyAuthProviderUnavailableError,
+ match="Data-source API-key auth provider is unavailable: firecrawl",
+ ):
+ ProviderApiKeyAuthCredentialValidator().validate(
+ "firecrawl",
+ DataSourceApiKeyAuthCredentials("bearer", "test_key", {}),
+ )
+
+ def test_validate_does_not_map_non_transport_http_error(self) -> None:
+ request = httpx.Request("POST", "https://api.firecrawl.dev/v1/crawl")
+ response = httpx.Response(500, request=request)
+ status_error = httpx.HTTPStatusError("Provider returned an error", request=request, response=response)
+ auth_instance = MagicMock()
+ auth_instance.validate_credentials.side_effect = status_error
+
+ with patch(
+ "services.auth.firecrawl.firecrawl.FirecrawlAuth",
+ autospec=True,
+ return_value=auth_instance,
+ ):
+ with pytest.raises(httpx.HTTPStatusError) as exc_info:
+ ProviderApiKeyAuthCredentialValidator().validate(
+ "firecrawl",
+ DataSourceApiKeyAuthCredentials("bearer", "test_key", {}),
+ )
+
+ assert exc_info.value is status_error
diff --git a/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py b/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py
new file mode 100644
index 00000000000..70f3ec8343f
--- /dev/null
+++ b/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py
@@ -0,0 +1,112 @@
+"""Unit tests for the data-source API-key auth application service and ports."""
+
+from datetime import datetime
+from unittest.mock import MagicMock
+
+import pytest
+
+from machinery.context import RequestContext
+from machinery.errors import ActiveWorkspaceRequiredError
+from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthService
+from services.entities.data_source_api_key_auth_entities import (
+ DataSourceApiKeyAuthBindingCreate,
+ DataSourceApiKeyAuthBindingRecord,
+ DataSourceApiKeyAuthCredentials,
+)
+
+
+def _context(workspace_id: str | None = "workspace-1") -> RequestContext:
+ return RequestContext(
+ request_id="request-1",
+ trace_id=None,
+ account_id="account-1",
+ active_workspace_id=workspace_id,
+ )
+
+
+def _command() -> DataSourceApiKeyAuthBindingCreate:
+ return DataSourceApiKeyAuthBindingCreate(
+ category="search",
+ provider="firecrawl",
+ credentials=DataSourceApiKeyAuthCredentials(
+ auth_type="bearer",
+ api_key="secret",
+ options={"base_url": "https://example.com"},
+ ),
+ )
+
+
+def _service() -> tuple[DataSourceApiKeyAuthService, MagicMock, MagicMock, MagicMock]:
+ bindings = MagicMock()
+ validator = MagicMock()
+ encryptor = MagicMock()
+ return (
+ DataSourceApiKeyAuthService(bindings=bindings, validator=validator, encryptor=encryptor),
+ bindings,
+ validator,
+ encryptor,
+ )
+
+
+def test_list_bindings_uses_active_workspace() -> None:
+ service, bindings, _validator, _encryptor = _service()
+ record = DataSourceApiKeyAuthBindingRecord(
+ id="binding-1",
+ category="search",
+ provider="firecrawl",
+ disabled=False,
+ created_at=datetime(2026, 1, 1),
+ updated_at=datetime(2026, 1, 2),
+ )
+ bindings.list_enabled.return_value = [record]
+
+ assert service.list_bindings(_context()) == (record,)
+ bindings.list_enabled.assert_called_once_with("workspace-1")
+
+
+def test_create_binding_validates_before_encrypting_and_persisting() -> None:
+ service, bindings, validator, encryptor = _service()
+ command = _command()
+ validator.validate.return_value = True
+ encryptor.encrypt.return_value = "encrypted-secret"
+
+ service.create_binding(_context(), command)
+
+ validator.validate.assert_called_once_with("firecrawl", command.credentials)
+ encryptor.encrypt.assert_called_once_with("workspace-1", "secret")
+ bindings.create.assert_called_once_with(
+ "workspace-1",
+ "search",
+ "firecrawl",
+ DataSourceApiKeyAuthCredentials(
+ auth_type="bearer",
+ api_key="encrypted-secret",
+ options={"base_url": "https://example.com"},
+ ),
+ )
+ assert command.credentials.api_key == "secret"
+
+
+def test_create_binding_does_not_persist_rejected_credentials() -> None:
+ service, bindings, validator, encryptor = _service()
+ validator.validate.return_value = False
+
+ service.create_binding(_context(), _command())
+
+ encryptor.encrypt.assert_not_called()
+ bindings.create.assert_not_called()
+
+
+def test_delete_binding_scopes_to_active_workspace() -> None:
+ service, bindings, _validator, _encryptor = _service()
+
+ service.delete_binding(_context(), "binding-1")
+
+ bindings.delete.assert_called_once_with("workspace-1", "binding-1")
+
+
+def test_use_cases_require_active_workspace() -> None:
+ service, _bindings, _validator, _encryptor = _service()
+
+ with pytest.raises(ActiveWorkspaceRequiredError, match="active workspace"):
+ service.list_bindings(_context(None))
diff --git a/api/tests/unit_tests/services/auth/test_firecrawl_auth.py b/api/tests/unit_tests/services/auth/test_firecrawl_auth.py
index 8f8435cc59f..476a685f15b 100644
--- a/api/tests/unit_tests/services/auth/test_firecrawl_auth.py
+++ b/api/tests/unit_tests/services/auth/test_firecrawl_auth.py
@@ -4,14 +4,28 @@ from unittest.mock import MagicMock, patch
import httpx
import pytest
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+)
from services.auth.firecrawl.firecrawl import FirecrawlAuth
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
+
+
+def _credentials(
+ auth_type: str = "bearer",
+ api_key: str = "test_api_key_123",
+ **options: str,
+) -> DataSourceApiKeyAuthCredentials:
+ return DataSourceApiKeyAuthCredentials(auth_type, api_key, options)
class TestFirecrawlAuth:
@pytest.fixture
def valid_credentials(self):
"""Fixture for valid bearer credentials"""
- return {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ return _credentials()
@pytest.fixture
def auth_instance(self, valid_credentials):
@@ -23,14 +37,10 @@ class TestFirecrawlAuth:
auth = FirecrawlAuth(valid_credentials)
assert auth.api_key == "test_api_key_123"
assert auth.base_url == "https://api.firecrawl.dev"
- assert auth.credentials == valid_credentials
def test_should_initialize_with_custom_base_url(self):
"""Test initialization with custom base URL"""
- credentials = {
- "auth_type": "bearer",
- "config": {"api_key": "test_api_key_123", "base_url": "https://custom.firecrawl.dev"},
- }
+ credentials = _credentials(base_url="https://custom.firecrawl.dev")
auth = FirecrawlAuth(credentials)
assert auth.api_key == "test_api_key_123"
assert auth.base_url == "https://custom.firecrawl.dev"
@@ -44,26 +54,15 @@ class TestFirecrawlAuth:
],
)
def test_should_raise_error_for_invalid_auth_type(self, auth_type, expected_error):
- """Test that non-bearer auth types raise ValueError"""
- credentials = {"auth_type": auth_type, "config": {"api_key": "test_api_key_123"}}
- with pytest.raises(ValueError) as exc_info:
+ """Test that non-bearer auth types raise a credential error."""
+ credentials = _credentials(auth_type=auth_type)
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
FirecrawlAuth(credentials)
assert str(exc_info.value) == expected_error
- @pytest.mark.parametrize(
- ("credentials", "expected_error"),
- [
- ({"auth_type": "bearer", "config": {}}, "No API key provided"),
- ({"auth_type": "bearer"}, "No API key provided"),
- ({"auth_type": "bearer", "config": {"api_key": ""}}, "No API key provided"),
- ({"auth_type": "bearer", "config": {"api_key": None}}, "No API key provided"),
- ],
- )
- def test_should_raise_error_for_missing_api_key(self, credentials, expected_error):
- """Test that missing or empty API key raises ValueError"""
- with pytest.raises(ValueError) as exc_info:
- FirecrawlAuth(credentials)
- assert str(exc_info.value) == expected_error
+ def test_should_raise_error_for_empty_api_key(self):
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError, match="No API key provided"):
+ FirecrawlAuth(_credentials(api_key=""))
@patch("services.auth.firecrawl.firecrawl.httpx.post", autospec=True)
def test_should_validate_valid_credentials_successfully(self, mock_post: MagicMock, auth_instance: FirecrawlAuth):
@@ -94,7 +93,6 @@ class TestFirecrawlAuth:
[
(402, "Payment required"),
(409, "Conflict error"),
- (500, "Internal server error"),
],
)
@patch("services.auth.firecrawl.firecrawl.httpx.post", autospec=True)
@@ -107,10 +105,27 @@ class TestFirecrawlAuth:
mock_response.json.return_value = {"error": error_message}
mock_post.return_value = mock_response
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth_instance.validate_credentials()
assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {error_message}"
+ @pytest.mark.parametrize("status_code", [429, 500, 502, 503])
+ @patch("services.auth.firecrawl.firecrawl.httpx.post", autospec=True)
+ def test_should_map_upstream_failure_to_provider_unavailable(
+ self,
+ mock_post: MagicMock,
+ status_code: int,
+ auth_instance: FirecrawlAuth,
+ ):
+ mock_response = MagicMock(status_code=status_code)
+ mock_post.return_value = mock_response
+
+ with pytest.raises(DataSourceApiKeyAuthProviderUnavailableError) as exc_info:
+ auth_instance.validate_credentials()
+
+ assert exc_info.value.provider == "firecrawl"
+ assert exc_info.value.status_code == status_code
+
@pytest.mark.parametrize(
("status_code", "response_text", "has_json_error", "expected_error_contains"),
[
@@ -141,7 +156,7 @@ class TestFirecrawlAuth:
mock_response.json.return_value = {"error": "Forbidden"}
mock_post.return_value = mock_response
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth_instance.validate_credentials()
assert str(exc_info.value) == expected_error_contains
@@ -167,15 +182,15 @@ class TestFirecrawlAuth:
def test_should_not_expose_api_key_in_error_messages(self):
"""Test that API key is not exposed in error messages"""
- credentials = {"auth_type": "bearer", "config": {"api_key": "super_secret_key_12345"}}
+ credentials = _credentials(api_key="super_secret_key_12345")
auth = FirecrawlAuth(credentials)
# Verify API key is stored but not in any error message
assert auth.api_key == "super_secret_key_12345"
# Test various error scenarios don't expose the key
- with pytest.raises(ValueError) as exc_info:
- FirecrawlAuth({"auth_type": "basic", "config": {"api_key": "super_secret_key_12345"}})
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
+ FirecrawlAuth(_credentials(auth_type="basic", api_key="super_secret_key_12345"))
assert "super_secret_key_12345" not in str(exc_info.value)
@patch("services.auth.firecrawl.firecrawl.httpx.post", autospec=True)
@@ -186,10 +201,7 @@ class TestFirecrawlAuth:
mock_post.return_value = mock_response
for base in ("https://custom.firecrawl.dev", "https://custom.firecrawl.dev/"):
- credentials = {
- "auth_type": "bearer",
- "config": {"api_key": "test_api_key_123", "base_url": base},
- }
+ credentials = _credentials(base_url=base)
auth = FirecrawlAuth(credentials)
result = auth.validate_credentials()
diff --git a/api/tests/unit_tests/services/auth/test_jina_auth.py b/api/tests/unit_tests/services/auth/test_jina_auth.py
index e9a1035da29..a1e41938e9d 100644
--- a/api/tests/unit_tests/services/auth/test_jina_auth.py
+++ b/api/tests/unit_tests/services/auth/test_jina_auth.py
@@ -1,37 +1,43 @@
+import json
from unittest.mock import MagicMock, patch
import httpx
import pytest
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+)
from services.auth.jina.jina import JinaAuth
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
+
+
+def _credentials(
+ auth_type: str = "bearer",
+ api_key: str = "test_api_key_123",
+) -> DataSourceApiKeyAuthCredentials:
+ return DataSourceApiKeyAuthCredentials(auth_type, api_key, {})
class TestJinaAuth:
def test_should_initialize_with_valid_bearer_credentials(self):
"""Test successful initialization with valid bearer credentials"""
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
assert auth.api_key == "test_api_key_123"
- assert auth.credentials == credentials
def test_should_raise_error_for_invalid_auth_type(self):
- """Test that non-bearer auth type raises ValueError"""
- credentials = {"auth_type": "basic", "config": {"api_key": "test_api_key_123"}}
- with pytest.raises(ValueError) as exc_info:
+ """Test that non-bearer auth type raises a credential error."""
+ credentials = _credentials(auth_type="basic")
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
JinaAuth(credentials)
assert str(exc_info.value) == "Invalid auth type, Jina Reader auth type must be Bearer"
def test_should_raise_error_for_missing_api_key(self):
- """Test that missing API key raises ValueError"""
- credentials = {"auth_type": "bearer", "config": {}}
- with pytest.raises(ValueError) as exc_info:
- JinaAuth(credentials)
- assert str(exc_info.value) == "No API key provided"
-
- def test_should_raise_error_for_missing_config(self):
- """Test that missing config section raises ValueError"""
- credentials = {"auth_type": "bearer"}
- with pytest.raises(ValueError) as exc_info:
+ """Test that an empty API key raises a credential error."""
+ credentials = _credentials(api_key="")
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
JinaAuth(credentials)
assert str(exc_info.value) == "No API key provided"
@@ -42,7 +48,7 @@ class TestJinaAuth:
mock_response.status_code = 200
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
result = auth.validate_credentials()
@@ -61,10 +67,10 @@ class TestJinaAuth:
mock_response.json.return_value = {"error": "Payment required"}
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"
@@ -74,13 +80,13 @@ class TestJinaAuth:
mock_response = MagicMock()
mock_response.status_code = 402
mock_response.text = "Payment required"
- mock_response.json.side_effect = ValueError("Not JSON")
+ mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"
@@ -92,27 +98,30 @@ class TestJinaAuth:
mock_response.json.return_value = {"error": "Conflict error"}
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 409. Error: Conflict error"
+ @pytest.mark.parametrize("status_code", [429, 500, 502, 503])
@patch("services.auth.jina.jina._http_client.post", autospec=True)
- def test_should_handle_http_500_error(self, mock_post: MagicMock):
- """Test handling of 500 Internal Server Error"""
- mock_response = MagicMock()
- mock_response.status_code = 500
- mock_response.json.return_value = {"error": "Internal server error"}
+ def test_should_map_upstream_failure_to_provider_unavailable(
+ self,
+ mock_post: MagicMock,
+ status_code: int,
+ ):
+ mock_response = MagicMock(status_code=status_code)
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
- auth = JinaAuth(credentials)
+ auth = JinaAuth(_credentials())
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthProviderUnavailableError) as exc_info:
auth.validate_credentials()
- assert str(exc_info.value) == "Failed to authorize. Status code: 500. Error: Internal server error"
+
+ assert exc_info.value.provider == "jinareader"
+ assert exc_info.value.status_code == status_code
@patch("services.auth.jina.jina._http_client.post", autospec=True)
def test_should_handle_unexpected_error_with_text_response(self, mock_post: MagicMock):
@@ -120,13 +129,13 @@ class TestJinaAuth:
mock_response = MagicMock()
mock_response.status_code = 403
mock_response.text = '{"error": "Forbidden"}'
- mock_response.json.side_effect = Exception("Not JSON")
+ mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 403. Error: Forbidden"
@@ -136,13 +145,13 @@ class TestJinaAuth:
mock_response = MagicMock()
mock_response.status_code = 403
mock_response.text = "Forbidden"
- mock_response.json.side_effect = Exception("Not JSON")
+ mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 403. Error: Forbidden"
@@ -152,13 +161,13 @@ class TestJinaAuth:
mock_response = MagicMock()
mock_response.status_code = 404
mock_response.text = ""
- mock_response.json.side_effect = Exception("Not JSON")
+ mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
mock_post.return_value = mock_response
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Unexpected error occurred while trying to authorize. Status code: 404"
@@ -167,7 +176,7 @@ class TestJinaAuth:
"""Test handling of network connection errors"""
mock_post.side_effect = httpx.ConnectError("Network error")
- credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
+ credentials = _credentials()
auth = JinaAuth(credentials)
with pytest.raises(httpx.ConnectError):
@@ -175,13 +184,13 @@ class TestJinaAuth:
def test_should_not_expose_api_key_in_error_messages(self):
"""Test that API key is not exposed in error messages"""
- credentials = {"auth_type": "bearer", "config": {"api_key": "super_secret_key_12345"}}
+ credentials = _credentials(api_key="super_secret_key_12345")
auth = JinaAuth(credentials)
# Verify API key is stored but not in any error message
assert auth.api_key == "super_secret_key_12345"
# Test various error scenarios don't expose the key
- with pytest.raises(ValueError) as exc_info:
- JinaAuth({"auth_type": "basic", "config": {"api_key": "super_secret_key_12345"}})
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
+ JinaAuth(_credentials(auth_type="basic", api_key="super_secret_key_12345"))
assert "super_secret_key_12345" not in str(exc_info.value)
diff --git a/api/tests/unit_tests/services/auth/test_jina_auth_standalone_module.py b/api/tests/unit_tests/services/auth/test_jina_auth_standalone_module.py
deleted file mode 100644
index 9e45cbbd944..00000000000
--- a/api/tests/unit_tests/services/auth/test_jina_auth_standalone_module.py
+++ /dev/null
@@ -1,179 +0,0 @@
-from __future__ import annotations
-
-import importlib.util
-import sys
-from pathlib import Path
-from types import ModuleType
-from typing import Any
-from unittest.mock import MagicMock
-
-import httpx
-import pytest
-
-
[email protected](scope="module")
-def jina_module() -> ModuleType:
- """
- Load `api/services/auth/jina.py` as a standalone module.
-
- This repo contains both `services/auth/jina.py` and a package at
- `services/auth/jina/`, so importing `services.auth.jina` can be ambiguous.
- """
-
- module_path = Path(__file__).resolve().parents[4] / "services" / "auth" / "jina.py"
- # Use a stable module name so pytest-cov can target it with `--cov=services.auth.jina_file`.
- spec = importlib.util.spec_from_file_location("services.auth.jina_file", module_path)
- assert spec is not None
- assert spec.loader is not None
- module = importlib.util.module_from_spec(spec)
- sys.modules[spec.name] = module
- spec.loader.exec_module(module)
- return module
-
-
-def _credentials(api_key: str | None = "test_api_key_123", auth_type: str = "bearer") -> dict[str, Any]:
- config: dict[str, Any] = {} if api_key is None else {"api_key": api_key}
- return {"auth_type": auth_type, "config": config}
-
-
-def test_init_valid_bearer_credentials(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials())
- assert auth.api_key == "test_api_key_123"
- assert auth.credentials["auth_type"] == "bearer"
-
-
-def test_init_rejects_invalid_auth_type(jina_module: ModuleType) -> None:
- with pytest.raises(ValueError, match="Invalid auth type.*Bearer"):
- jina_module.JinaAuth(_credentials(auth_type="basic"))
-
-
[email protected]("credentials", [{"auth_type": "bearer", "config": {}}, {"auth_type": "bearer"}])
-def test_init_requires_api_key(jina_module: ModuleType, credentials: dict[str, Any]) -> None:
- with pytest.raises(ValueError, match="No API key provided"):
- jina_module.JinaAuth(credentials)
-
-
-def test_prepare_headers_includes_bearer_api_key(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- assert auth._prepare_headers() == {"Content-Type": "application/json", "Authorization": "Bearer k"}
-
-
-def test_post_request_calls_httpx(jina_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- post_mock = MagicMock(name="httpx.post")
- monkeypatch.setattr(jina_module._http_client, "post", post_mock)
-
- auth._post_request("https://r.jina.ai", {"url": "https://example.com"}, {"h": "v"})
- post_mock.assert_called_once_with("https://r.jina.ai", headers={"h": "v"}, json={"url": "https://example.com"})
-
-
-def test_validate_credentials_success(jina_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
-
- response = MagicMock()
- response.status_code = 200
- post_mock = MagicMock(return_value=response)
- monkeypatch.setattr(jina_module._http_client, "post", post_mock)
-
- assert auth.validate_credentials() is True
- post_mock.assert_called_once_with(
- "https://r.jina.ai",
- headers={"Content-Type": "application/json", "Authorization": "Bearer k"},
- json={"url": "https://example.com"},
- )
-
-
-def test_validate_credentials_non_200_raises_via_handle_error(
- jina_module: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
-
- response = MagicMock()
- response.status_code = 402
- response.json.return_value = {"error": "Payment required"}
- monkeypatch.setattr(jina_module._http_client, "post", MagicMock(return_value=response))
-
- with pytest.raises(Exception, match="Status code: 402.*Payment required"):
- auth.validate_credentials()
-
-
[email protected]("status_code", [402, 409, 500])
-def test_handle_error_statuses_use_response_json(jina_module: ModuleType, status_code: int) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = status_code
- response.json.return_value = {"error": "boom"}
-
- with pytest.raises(Exception, match=f"Status code: {status_code}.*boom"):
- auth._handle_error(response)
-
-
-def test_handle_error_statuses_default_unknown_error(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = 402
- response.json.return_value = {}
-
- with pytest.raises(Exception, match="Unknown error occurred"):
- auth._handle_error(response)
-
-
-def test_handle_error_statuses_fall_back_to_text_body(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = 402
- response.text = "Payment required"
- response.json.side_effect = ValueError("Not JSON")
-
- with pytest.raises(Exception, match="Status code: 402.*Payment required"):
- auth._handle_error(response)
-
-
-def test_handle_error_with_text_json_body(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = 403
- response.text = '{"error": "Forbidden"}'
-
- with pytest.raises(Exception, match="Status code: 403.*Forbidden"):
- auth._handle_error(response)
-
-
-def test_handle_error_with_non_json_text_body(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = 403
- response.text = "Forbidden"
-
- with pytest.raises(Exception, match="Status code: 403.*Forbidden"):
- auth._handle_error(response)
-
-
-def test_handle_error_with_text_json_body_missing_error(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = 403
- response.text = "{}"
-
- with pytest.raises(Exception, match="Unknown error occurred"):
- auth._handle_error(response)
-
-
-def test_handle_error_without_text_raises_unexpected(jina_module: ModuleType) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- response = MagicMock()
- response.status_code = 404
- response.text = ""
-
- with pytest.raises(Exception, match="Unexpected error occurred.*404"):
- auth._handle_error(response)
-
-
-def test_validate_credentials_propagates_network_errors(
- jina_module: ModuleType, monkeypatch: pytest.MonkeyPatch
-) -> None:
- auth = jina_module.JinaAuth(_credentials(api_key="k"))
- monkeypatch.setattr(jina_module._http_client, "post", MagicMock(side_effect=httpx.ConnectError("boom")))
-
- with pytest.raises(httpx.ConnectError, match="boom"):
- auth.validate_credentials()
diff --git a/api/tests/unit_tests/services/auth/test_watercrawl_auth.py b/api/tests/unit_tests/services/auth/test_watercrawl_auth.py
index c8bc8414cd1..09fd6e86a80 100644
--- a/api/tests/unit_tests/services/auth/test_watercrawl_auth.py
+++ b/api/tests/unit_tests/services/auth/test_watercrawl_auth.py
@@ -1,16 +1,31 @@
+import json
from unittest.mock import MagicMock, patch
import httpx
import pytest
+from services.auth.errors import (
+ DataSourceApiKeyAuthCredentialValidationError,
+ DataSourceApiKeyAuthProviderUnavailableError,
+ InvalidDataSourceApiKeyAuthCredentialsError,
+)
from services.auth.watercrawl.watercrawl import WatercrawlAuth
+from services.entities.data_source_api_key_auth_entities import DataSourceApiKeyAuthCredentials
+
+
+def _credentials(
+ auth_type: str = "x-api-key",
+ api_key: str = "test_api_key_123",
+ **options: str,
+) -> DataSourceApiKeyAuthCredentials:
+ return DataSourceApiKeyAuthCredentials(auth_type, api_key, options)
class TestWatercrawlAuth:
@pytest.fixture
def valid_credentials(self):
"""Fixture for valid x-api-key credentials"""
- return {"auth_type": "x-api-key", "config": {"api_key": "test_api_key_123"}}
+ return _credentials()
@pytest.fixture
def auth_instance(self, valid_credentials):
@@ -22,14 +37,10 @@ class TestWatercrawlAuth:
auth = WatercrawlAuth(valid_credentials)
assert auth.api_key == "test_api_key_123"
assert auth.base_url == "https://app.watercrawl.dev"
- assert auth.credentials == valid_credentials
def test_should_initialize_with_custom_base_url(self):
"""Test initialization with custom base URL"""
- credentials = {
- "auth_type": "x-api-key",
- "config": {"api_key": "test_api_key_123", "base_url": "https://custom.watercrawl.dev"},
- }
+ credentials = _credentials(base_url="https://custom.watercrawl.dev")
auth = WatercrawlAuth(credentials)
assert auth.api_key == "test_api_key_123"
assert auth.base_url == "https://custom.watercrawl.dev"
@@ -43,26 +54,15 @@ class TestWatercrawlAuth:
],
)
def test_should_raise_error_for_invalid_auth_type(self, auth_type, expected_error):
- """Test that non-x-api-key auth types raise ValueError"""
- credentials = {"auth_type": auth_type, "config": {"api_key": "test_api_key_123"}}
- with pytest.raises(ValueError) as exc_info:
+ """Test that non-x-api-key auth types raise a credential error."""
+ credentials = _credentials(auth_type=auth_type)
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
WatercrawlAuth(credentials)
assert str(exc_info.value) == expected_error
- @pytest.mark.parametrize(
- ("credentials", "expected_error"),
- [
- ({"auth_type": "x-api-key", "config": {}}, "No API key provided"),
- ({"auth_type": "x-api-key"}, "No API key provided"),
- ({"auth_type": "x-api-key", "config": {"api_key": ""}}, "No API key provided"),
- ({"auth_type": "x-api-key", "config": {"api_key": None}}, "No API key provided"),
- ],
- )
- def test_should_raise_error_for_missing_api_key(self, credentials, expected_error):
- """Test that missing or empty API key raises ValueError"""
- with pytest.raises(ValueError) as exc_info:
- WatercrawlAuth(credentials)
- assert str(exc_info.value) == expected_error
+ def test_should_raise_error_for_empty_api_key(self):
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError, match="No API key provided"):
+ WatercrawlAuth(_credentials(api_key=""))
@patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)
def test_should_validate_valid_credentials_successfully(self, mock_get, auth_instance):
@@ -85,7 +85,6 @@ class TestWatercrawlAuth:
[
(402, "Payment required"),
(409, "Conflict error"),
- (500, "Internal server error"),
],
)
@patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)
@@ -96,20 +95,37 @@ class TestWatercrawlAuth:
mock_response.json.return_value = {"error": error_message}
mock_get.return_value = mock_response
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth_instance.validate_credentials()
assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {error_message}"
+ @pytest.mark.parametrize("status_code", [429, 500, 502, 503])
+ @patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)
+ def test_should_map_upstream_failure_to_provider_unavailable(
+ self,
+ mock_get: MagicMock,
+ status_code: int,
+ auth_instance: WatercrawlAuth,
+ ):
+ mock_response = MagicMock(status_code=status_code)
+ mock_get.return_value = mock_response
+
+ with pytest.raises(DataSourceApiKeyAuthProviderUnavailableError) as exc_info:
+ auth_instance.validate_credentials()
+
+ assert exc_info.value.provider == "watercrawl"
+ assert exc_info.value.status_code == status_code
+
@patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)
def test_should_handle_http_error_with_non_json_text_response(self, mock_get, auth_instance):
"""Test handling of known HTTP errors with non-JSON text response."""
mock_response = MagicMock()
mock_response.status_code = 402
mock_response.text = "Payment required"
- mock_response.json.side_effect = ValueError("Not JSON")
+ mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
mock_get.return_value = mock_response
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth_instance.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"
@@ -130,10 +146,10 @@ class TestWatercrawlAuth:
mock_response.status_code = status_code
mock_response.text = response_text
if has_json_error:
- mock_response.json.side_effect = Exception("Not JSON")
+ mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
mock_get.return_value = mock_response
- with pytest.raises(Exception) as exc_info:
+ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
auth_instance.validate_credentials()
assert expected_error_contains in str(exc_info.value)
@@ -157,15 +173,15 @@ class TestWatercrawlAuth:
def test_should_not_expose_api_key_in_error_messages(self):
"""Test that API key is not exposed in error messages"""
- credentials = {"auth_type": "x-api-key", "config": {"api_key": "super_secret_key_12345"}}
+ credentials = _credentials(api_key="super_secret_key_12345")
auth = WatercrawlAuth(credentials)
# Verify API key is stored but not in any error message
assert auth.api_key == "super_secret_key_12345"
# Test various error scenarios don't expose the key
- with pytest.raises(ValueError) as exc_info:
- WatercrawlAuth({"auth_type": "bearer", "config": {"api_key": "super_secret_key_12345"}})
+ with pytest.raises(InvalidDataSourceApiKeyAuthCredentialsError) as exc_info:
+ WatercrawlAuth(_credentials(auth_type="bearer", api_key="super_secret_key_12345"))
assert "super_secret_key_12345" not in str(exc_info.value)
@patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)
@@ -175,10 +191,7 @@ class TestWatercrawlAuth:
mock_response.status_code = 200
mock_get.return_value = mock_response
- credentials = {
- "auth_type": "x-api-key",
- "config": {"api_key": "test_api_key_123", "base_url": "https://custom.watercrawl.dev"},
- }
+ credentials = _credentials(base_url="https://custom.watercrawl.dev")
auth = WatercrawlAuth(credentials)
result = auth.validate_credentials()
@@ -200,7 +213,7 @@ class TestWatercrawlAuth:
mock_response.status_code = 200
mock_get.return_value = mock_response
- credentials = {"auth_type": "x-api-key", "config": {"api_key": "test_api_key_123", "base_url": base_url}}
+ credentials = _credentials(base_url=base_url)
auth = WatercrawlAuth(credentials)
auth.validate_credentials()
diff --git a/packages/contracts/generated/api/console/api-key-auth/types.gen.ts b/packages/contracts/generated/api/console/api-key-auth/types.gen.ts
index 9932f21d749..2a89c4dd3e6 100644
--- a/packages/contracts/generated/api/console/api-key-auth/types.gen.ts
+++ b/packages/contracts/generated/api/console/api-key-auth/types.gen.ts
@@ -10,9 +10,7 @@ export type ApiKeyAuthDataSourceListResponse = {
export type ApiKeyAuthBindingPayload = {
category: string
- credentials: {
- [key: string]: unknown
- }
+ credentials: ApiKeyAuthCredentialsPayload
provider: string
}
@@ -29,6 +27,16 @@ export type ApiKeyAuthDataSourceItem = {
updated_at: number
}
+export type ApiKeyAuthCredentialsPayload = {
+ auth_type: string
+ config: ApiKeyAuthConfigPayload
+}
+
+export type ApiKeyAuthConfigPayload = {
+ api_key: string
+ [key: string]: unknown
+}
+
export type GetApiKeyAuthDataSourceData = {
body?: never
path?: never
diff --git a/packages/contracts/generated/api/console/api-key-auth/zod.gen.ts b/packages/contracts/generated/api/console/api-key-auth/zod.gen.ts
index a55ba309823..f9517e6dd1b 100644
--- a/packages/contracts/generated/api/console/api-key-auth/zod.gen.ts
+++ b/packages/contracts/generated/api/console/api-key-auth/zod.gen.ts
@@ -2,15 +2,6 @@
import * as z from 'zod'
-/**
- * ApiKeyAuthBindingPayload
- */
-export const zApiKeyAuthBindingPayload = z.object({
- category: z.string(),
- credentials: z.record(z.string(), z.unknown()),
- provider: z.string(),
-})
-
/**
* SimpleResultResponse
*/
@@ -37,6 +28,30 @@ export const zApiKeyAuthDataSourceListResponse = z.object({
sources: z.array(zApiKeyAuthDataSourceItem),
})
+/**
+ * ApiKeyAuthConfigPayload
+ */
+export const zApiKeyAuthConfigPayload = z.object({
+ api_key: z.string().min(1),
+})
+
+/**
+ * ApiKeyAuthCredentialsPayload
+ */
+export const zApiKeyAuthCredentialsPayload = z.object({
+ auth_type: z.string().min(1),
+ config: zApiKeyAuthConfigPayload,
+})
+
+/**
+ * ApiKeyAuthBindingPayload
+ */
+export const zApiKeyAuthBindingPayload = z.object({
+ category: z.string().min(1),
+ credentials: zApiKeyAuthCredentialsPayload,
+ provider: z.string().min(1),
+})
+
/**
* Success
*/