Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

Feat/watch task external auth main (#5310)

zihengli94ea3fda1bcc
diff --git a/openviking/connector/auth.py b/openviking/connector/auth.py
new file mode 100644
index 000000000..77296b39f
--- /dev/null
+++ b/openviking/connector/auth.py
@@ -0,0 +1,193 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""Task-scoped access to centrally managed Feishu OAuth credentials."""
+
+import asyncio
+import math
+import time
+from contextlib import asynccontextmanager, contextmanager
+from contextvars import ContextVar
+from threading import Lock
+from typing import TYPE_CHECKING, Any, Dict, Optional
+
+from openviking.connector.client import ConnectorClient
+from openviking.server.identity import RequestContext
+from openviking_cli.exceptions import InternalError, InvalidArgumentError
+
+if TYPE_CHECKING:
+ from openviking.parse.accessors.base import LocalResource
+
+OAUTH_REF_ARG = "openviking_oauth_ref"
+EXTERNAL_FEISHU_PROVIDER = "feishu_external"
+current_feishu_token: ContextVar[Optional["ExternalFeishuToken"]] = ContextVar(
+ "external_feishu_token", default=None
+)
+
+
+def external_auth_url() -> str:
+ from openviking_cli.utils.config.open_viking_config import get_openviking_config
+
+ return get_openviking_config().connector.auth
+
+
+def validate_feishu_auth_args(args: Dict[str, Any]) -> None:
+ if OAUTH_REF_ARG in args and any(
+ key in args
+ for key in (
+ "feishu_access_token",
+ "feishu_refresh_token",
+ "feishu_app_id",
+ "feishu_app_secret",
+ "lark_file",
+ )
+ ):
+ raise InvalidArgumentError(
+ "OAuth references cannot be combined with explicit Feishu credentials."
+ )
+
+
+@asynccontextmanager
+async def feishu_auth_scope(
+ connector,
+ *,
+ path: str,
+ ctx: RequestContext,
+ args: Dict[str, Any],
+ state: Optional[Dict[str, Any]] = None,
+ prepared: bool = False,
+):
+ """Bind external auth for source preparation or a restored task; leave local auth alone."""
+ validate_feishu_auth_args(args)
+ provider = None
+ if is_external_feishu_auth(state):
+ if not prepared:
+ api_key, restored_args = await restore_feishu_request(
+ connector, state, path=path, ctx=ctx
+ )
+ provider = ExternalFeishuToken(restored_args[OAUTH_REF_ARG], api_key)
+ elif OAUTH_REF_ARG in args:
+ from openviking.parse.accessors.feishu_accessor import FeishuAccessor
+
+ if not FeishuAccessor._is_feishu_url(path):
+ raise InvalidArgumentError("OAuth references require a Feishu document URL.")
+ reference = validate_oauth_ref(args.pop(OAUTH_REF_ARG), ctx.user.user_id)
+ provider = ExternalFeishuToken(reference, ctx.api_key or "")
+ state = {
+ "provider": EXTERNAL_FEISHU_PROVIDER,
+ "credentials": await connector.create_watch_auth_state(
+ api_key=ctx.api_key,
+ account_id=ctx.account_id,
+ add_type="feishu_doc",
+ path=path,
+ connector_args={OAUTH_REF_ARG: reference},
+ ),
+ }
+ if provider is not None:
+ args["feishu_access_token"] = await asyncio.to_thread(provider.get_token)
+ with feishu_token_scope(provider):
+ yield state
+
+
+async def restore_feishu_request(
+ connector, state: Dict[str, Any], *, path: str, ctx: RequestContext
+) -> tuple[str, Dict[str, Any]]:
+ api_key, add_type, args = await connector.restore_watch_request(
+ state.get("credentials", {}), account_id=ctx.account_id, path=path
+ )
+ if add_type != "feishu_doc":
+ raise InvalidArgumentError("Stored external Feishu credentials are invalid.")
+ reference = validate_oauth_ref(args.get(OAUTH_REF_ARG), ctx.user.user_id)
+ return api_key, {OAUTH_REF_ARG: reference}
+
+
+def validate_oauth_ref(value: Any, ov_user_id: str) -> Dict[str, str]:
+ keys = {"account_id", "user_id", "ov_user_id", "platform", "type"}
+ if (
+ not isinstance(value, dict)
+ or set(value) != keys
+ or any(not isinstance(value[key], str) or not value[key].strip() for key in keys)
+ ):
+ raise InvalidArgumentError("args.openviking_oauth_ref contains an invalid OAuth reference.")
+ ref = {key: value[key].strip() for key in keys}
+ if ref["type"] != "oauth" or ref["platform"] != "feishu_doc":
+ raise InvalidArgumentError("Native Feishu imports require a feishu_doc OAuth reference.")
+ if ref["ov_user_id"] != ov_user_id:
+ raise InvalidArgumentError(
+ "OAuth reference does not belong to the current OpenViking user."
+ )
+ return ref
+
+
+def is_external_feishu_auth(state: Optional[Dict[str, Any]]) -> bool:
+ return isinstance(state, dict) and state.get("provider") == EXTERNAL_FEISHU_PROVIDER
+
+
+class ExternalFeishuToken:
+ """Cache only access tokens, within one execution; never refresh OAuth locally."""
+
+ def __init__(self, reference: Dict[str, str], api_key: str):
+ self._url = external_auth_url()
+ if not self._url:
+ raise InvalidArgumentError(
+ "connector.auth is required for external Feishu authorization."
+ )
+ if not api_key:
+ raise InvalidArgumentError("External Feishu authorization requires an API key.")
+ self._reference = reference
+ self._api_key = api_key
+ self._client = ConnectorClient("", "", account_id=reference["account_id"])
+ self._lock = Lock()
+ self._token = ""
+ self._valid_until = 0.0
+ self._error: Optional[InternalError] = None
+
+ def get_token(self) -> str:
+ with self._lock:
+ if self._error is not None:
+ raise self._error
+ if self._token and time.time() < self._valid_until:
+ return self._token
+ try:
+ data = self._client.get_oauth_access_token(
+ self._url, self._api_key, self._reference
+ )
+ except InternalError as exc:
+ self._error = exc
+ raise
+ token = data.get("access_token")
+ expires_at = data.get("expires_at", 0)
+ now = time.time()
+ if (
+ not isinstance(token, str)
+ or not token.strip()
+ or type(expires_at) not in (int, float)
+ or not math.isfinite(expires_at)
+ or expires_at < 0
+ or (expires_at > 0 and expires_at <= now)
+ ):
+ self._error = InternalError(
+ "External OAuth returned an invalid or expired access token."
+ )
+ raise self._error
+ self._token = token.strip()
+ self._valid_until = min(now + 60, expires_at - 30) if expires_at else now + 60
+ return self._token
+
+
+def check_feishu_auth(resource: "LocalResource") -> "LocalResource":
+ """Reject and clean partial downloads before parsing, including missing embedded media."""
+ provider = current_feishu_token.get()
+ if provider is not None and provider._error is not None:
+ resource.cleanup()
+ raise provider._error
+ return resource
+
+
+@contextmanager
+def feishu_token_scope(token: Optional[ExternalFeishuToken]):
+ """Context variables also follow asyncio.to_thread into the synchronous SDK."""
+ handle = current_feishu_token.set(token)
+ try:
+ yield
+ finally:
+ current_feishu_token.reset(handle)
diff --git a/openviking/connector/client.py b/openviking/connector/client.py
index 375d70d22..991785a7c 100644
--- a/openviking/connector/client.py
+++ b/openviking/connector/client.py
@@ -54,6 +54,19 @@ class ConnectorClient:
headers["Authorization"] = token
return headers
+ def get_oauth_access_token(
+ self, auth_url: str, api_key: str, reference: Dict[str, str]
+ ) -> Dict[str, Any]:
+ """Read a centrally managed token from synchronous Feishu worker threads."""
+ try:
+ with httpx.Client(timeout=10.0) as client:
+ rsp = client.post(auth_url, json=reference, headers={"X-API-Key": api_key})
+ rsp.raise_for_status()
+ return _unwrap_connector_response(rsp.json())
+ except (httpx.HTTPError, ValueError, InternalError):
+ # Neither remote business messages nor response bodies may expose credentials.
+ raise InternalError("External OAuth access token request failed.") from None
+
async def submit_doc_add(
self,
add_type: str,
diff --git a/openviking/parse/accessors/feishu_accessor.py b/openviking/parse/accessors/feishu_accessor.py
index 5d4837acb..f849a3b71 100644
--- a/openviking/parse/accessors/feishu_accessor.py
+++ b/openviking/parse/accessors/feishu_accessor.py
@@ -452,7 +452,9 @@ class FeishuAccessor(DataAccessor):
source,
config=kwargs.get("feishu_config"),
)
- return await worker._access(source, **kwargs)
+ from openviking.connector.auth import check_feishu_auth
+
+ return check_feishu_auth(await worker._access(source, **kwargs))
async def _access(self, source: Union[str, Path], **kwargs) -> LocalResource:
"""
@@ -1664,6 +1666,11 @@ class FeishuAccessor(DataAccessor):
def _user_request_option(feishu_access_token: Optional[str]):
if not feishu_access_token:
return None
+ from openviking.connector.auth import current_feishu_token
+
+ token_provider = current_feishu_token.get()
+ if token_provider is not None:
+ feishu_access_token = token_provider.get_token()
from lark_oapi.core.model import RequestOption
return RequestOption.builder().user_access_token(feishu_access_token).build()
diff --git a/openviking/parse/understanding_api.py b/openviking/parse/understanding_api.py
index f35fe8a15..e5cedf32d 100644
--- a/openviking/parse/understanding_api.py
+++ b/openviking/parse/understanding_api.py
@@ -587,6 +587,11 @@ class UnderstandingAPI(BaseParser):
return auth or None
async def _resolve_lark_file(self, kwargs: Dict[str, Any]) -> Dict[str, str]:
+ from openviking.connector.auth import current_feishu_token
+
+ token_provider = current_feishu_token.get()
+ if token_provider is not None:
+ return {"user_access_token": await asyncio.to_thread(token_provider.get_token)}
auth = self._normalize_lark_file(kwargs)
if auth:
return auth
diff --git a/openviking/resource/watch_scheduler.py b/openviking/resource/watch_scheduler.py
index 6e24ac095..364c208ba 100644
--- a/openviking/resource/watch_scheduler.py
+++ b/openviking/resource/watch_scheduler.py
@@ -11,6 +11,10 @@ import threading
from datetime import datetime
from typing import Any, Dict, Optional, Set
+from openviking.connector.auth import (
+ is_external_feishu_auth,
+ restore_feishu_request,
+)
from openviking.connector.delegate import ConnectorDelegate
from openviking.resource.feishu_watch_auth import (
FeishuOAuthClient,
@@ -371,7 +375,11 @@ class WatchScheduler:
processor_kwargs = dict(getattr(task, "processor_kwargs", {}) or {})
processor_kwargs.pop("build_index", None)
processor_kwargs.pop("summarize", None)
- if is_feishu_auth_state(auth_state):
+ if is_external_feishu_auth(auth_state):
+ ctx.api_key, processor_kwargs["args"] = await restore_feishu_request(
+ self._resource_service._connector, auth_state, path=task.path, ctx=ctx
+ )
+ elif is_feishu_auth_state(auth_state):
try:
auth_state = await self._prepare_feishu_auth_state(task, auth_state)
processor_kwargs["feishu_access_token"] = auth_state["access_token"]
diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.py
index 5ceba7756..205abf725 100644
--- a/openviking/service/resource_service.py
+++ b/openviking/service/resource_service.py
@@ -20,6 +20,12 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional
from urllib.parse import urlparse
from uuid import uuid4
+from openviking.connector.auth import (
+ OAUTH_REF_ARG,
+ feishu_auth_scope,
+ is_external_feishu_auth,
+ validate_feishu_auth_args,
+)
from openviking.core.namespace import is_content_root_uri
from openviking.observability.http_error_context import sanitize_public_http_error
from openviking.parse.backend import ParserBackend, normalize_parser_backend
@@ -295,6 +301,7 @@ class ResourceService:
"lark_file",
FEISHU_ACCESS_TOKEN_ARG,
FEISHU_REFRESH_TOKEN_ARG,
+ OAUTH_REF_ARG,
"parser_backend",
"resolved_extension",
"understanding_response_id",
@@ -471,6 +478,7 @@ class ResourceService:
)
normalized = dict(args)
+ validate_feishu_auth_args(normalized)
raw_parse_mode = normalized.pop("parse_mode", ParseMode.DEFAULT)
try:
parse_mode = normalize_parse_mode(raw_parse_mode)
@@ -770,11 +778,6 @@ class ResourceService:
internal_kwargs["create_parent"] = True
if msg.source_name is not None:
internal_kwargs["source_name"] = msg.source_name
- auth_kwargs, watch_auth_state = self._restore_source_task_auth(
- msg,
- task_auth or {},
- )
- internal_kwargs.update(auth_kwargs)
if feishu_prepared:
from openviking.service.task_tracker import get_task_tracker
@@ -823,35 +826,50 @@ class ResourceService:
internal_kwargs[PREPARED_FILE_ID_ARG] = msg.understanding_file_id
try:
- result = await self._execute_resource_ingestion(
+ async with feishu_auth_scope(
+ self._connector,
path=msg.path,
ctx=ctx,
- to=target_uri,
- parent=parent_uri,
- to_is_directory=msg.to_is_directory,
- reason=msg.reason,
- instruction=msg.instruction,
- defer_post_processing=False,
- timeout=msg.timeout,
- build_index=msg.build_index,
- summarize=msg.summarize,
- processing_mode=msg.processing_mode,
- parse_mode=msg.parse_mode,
- watch_interval=msg.watch_interval,
- is_active=msg.is_active,
- manage_watch=not msg.skip_watch_management,
- tags=msg.tags,
- tag_mode=msg.tag_mode,
- allow_local_path_resolution=msg.allow_local_path_resolution,
- enforce_public_remote_targets=msg.enforce_public_remote_targets,
- resource_lock=resource_lock,
- stage_callback=stage_callback,
- watch_auth_state=watch_auth_state,
- prepared_resource=prepared_resource,
- internal_task=msg.internal_task,
- on_watch_ready=lambda task_id: setattr(msg, "watch_task_id", task_id),
- **internal_kwargs,
- )
+ args=internal_kwargs,
+ state=task_auth,
+ prepared=(
+ msg.understanding_response_id is not None
+ or msg.understanding_file_id is not None
+ ),
+ ) as auth_state:
+ auth_kwargs, watch_auth_state = self._restore_source_task_auth(
+ msg, auth_state or {}
+ )
+ internal_kwargs.update(auth_kwargs)
+ result = await self._execute_resource_ingestion(
+ path=msg.path,
+ ctx=ctx,
+ to=target_uri,
+ parent=parent_uri,
+ to_is_directory=msg.to_is_directory,
+ reason=msg.reason,
+ instruction=msg.instruction,
+ defer_post_processing=False,
+ timeout=msg.timeout,
+ build_index=msg.build_index,
+ summarize=msg.summarize,
+ processing_mode=msg.processing_mode,
+ parse_mode=msg.parse_mode,
+ watch_interval=msg.watch_interval,
+ is_active=msg.is_active,
+ manage_watch=not msg.skip_watch_management,
+ tags=msg.tags,
+ tag_mode=msg.tag_mode,
+ allow_local_path_resolution=msg.allow_local_path_resolution,
+ enforce_public_remote_targets=msg.enforce_public_remote_targets,
+ resource_lock=resource_lock,
+ stage_callback=stage_callback,
+ watch_auth_state=watch_auth_state,
+ prepared_resource=prepared_resource,
+ internal_task=msg.internal_task,
+ on_watch_ready=lambda task_id: setattr(msg, "watch_task_id", task_id),
+ **internal_kwargs,
+ )
except BaseException:
if msg.cleanup_empty_target_on_failure and resource_lock is not None:
await self._cleanup_reserved_target_if_empty(
@@ -902,6 +920,8 @@ class ResourceService:
if not task_auth:
return {}, None
creating_watch = msg.watch_interval > 0 and not msg.skip_watch_management
+ if is_external_feishu_auth(task_auth):
+ return {}, task_auth if msg.watch_interval > 0 else None
if is_git_http_auth_state(task_auth):
auth_config = git_http_auth_config_from_state(task_auth, msg.path)
watch_auth_state = dict(task_auth) if creating_watch else None
@@ -1915,15 +1935,19 @@ class ResourceService:
path = require_remote_resource_source(path)
kwargs.setdefault("request_validator", ensure_public_remote_target)
- source_plan = await self._prepare_standard_source_plan(
- path=path,
- ctx=ctx,
- mode=mode,
- allow_local_path_resolution=allow_local_path_resolution,
- processor_kwargs=kwargs,
- watch_auth_state=normalized_args.watch_auth_state,
- shared_source=shared_source,
- )
+ async with feishu_auth_scope(
+ connector, path=path, ctx=ctx, args=kwargs, state=normalized_args.watch_auth_state
+ ) as watch_auth_state:
+ normalized_args.watch_auth_state = watch_auth_state
+ source_plan = await self._prepare_standard_source_plan(
+ path=path,
+ ctx=ctx,
+ mode=mode,
+ allow_local_path_resolution=allow_local_path_resolution,
+ processor_kwargs=kwargs,
+ watch_auth_state=normalized_args.watch_auth_state,
+ shared_source=shared_source,
+ )
if source_plan is not None:
result = await self._enqueue_source_plan(
source_plan,
diff --git a/openviking_cli/utils/config/open_viking_config.py b/openviking_cli/utils/config/open_viking_config.py
index d71396b54..6e829ceed 100644
--- a/openviking_cli/utils/config/open_viking_config.py
+++ b/openviking_cli/utils/config/open_viking_config.py
@@ -6,6 +6,7 @@ import os
from pathlib import Path
from threading import Lock
from typing import Any, Dict, List, Optional
+from urllib.parse import urlsplit
from pydantic import BaseModel, Field, ValidationError, model_validator
@@ -71,12 +72,26 @@ class ConnectorConfig(BaseModel):
enable: bool = False
connector: str = ""
tracker: str = ""
+ auth: str = ""
timeout_seconds: int = 3600
poll_interval_ms: int = 5000
allowed_add_types: List[str] = Field(default_factory=lambda: ["tos"])
@model_validator(mode="after")
def _validate(self) -> "ConnectorConfig":
+ self.auth = self.auth.strip()
+ if self.auth:
+ url = urlsplit(self.auth)
+ if (
+ url.scheme not in {"http", "https"}
+ or not url.hostname
+ or url.username
+ or url.password
+ or url.fragment
+ ):
+ raise ValueError(
+ "connector.auth must be an HTTP(S) endpoint URL without credentials or fragment"
+ )
if self.enable:
for name, url in (("connector", self.connector), ("tracker", self.tracker)):
if not url.strip():
diff --git a/tests/connector/test_auth.py b/tests/connector/test_auth.py
new file mode 100644
index 000000000..84926debc
--- /dev/null
+++ b/tests/connector/test_auth.py
@@ -0,0 +1,262 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""External OAuth contract, lifetime and identity regression checks."""
+
+import asyncio
+import json
+from types import SimpleNamespace
+from unittest.mock import Mock
+
+import httpx
+import pytest
+
+from openviking.connector import auth
+from openviking.connector.client import ConnectorClient
+from openviking.parse.accessors.feishu_accessor import FeishuAccessor
+from openviking.parse.understanding_api import UnderstandingAPI
+from openviking.resource.feishu_watch_auth import (
+ FeishuAppCredentials,
+ FeishuOAuthClient,
+ FeishuRefreshedToken,
+)
+from openviking_cli.exceptions import InternalError, InvalidArgumentError
+
+REFERENCE = {
+ "account_id": "cloud-account",
+ "user_id": "cloud-user",
+ "ov_user_id": "alice",
+ "platform": "feishu_doc",
+ "type": "oauth",
+}
+
+
[email protected](autouse=True)
+def external_endpoint(monkeypatch):
+ monkeypatch.setattr(
+ auth, "external_auth_url", lambda: "https://connector.example/oauth/access_token"
+ )
+
+
+def test_http_contract_and_secret_safe_errors(monkeypatch):
+ responses = iter(
+ [
+ {"access_token": "user-token", "expires_at": 4102444800, "version": 2},
+ {"code": 123, "message": "must-not-expose-secret"},
+ ]
+ )
+
+ def handle(request):
+ assert request.method == "POST"
+ assert request.headers["x-api-key"] == "key"
+ assert "authorization" not in request.headers
+ assert json.loads(request.content) == REFERENCE
+ return httpx.Response(200, json=next(responses))
+
+ client_type = httpx.Client
+ monkeypatch.setattr(
+ httpx,
+ "Client",
+ lambda **kwargs: client_type(transport=httpx.MockTransport(handle), **kwargs),
+ )
+ provider = auth.ExternalFeishuToken(REFERENCE, "key")
+ assert provider.get_token() == "user-token"
+ provider._valid_until = 0
+ with pytest.raises(InternalError) as error:
+ provider.get_token()
+ assert "must-not-expose-secret" not in str(error.value)
+
+
+ "data",
+ [
+ {},
+ {"access_token": ""},
+ {"access_token": "x", "expires_at": 1},
+ {"access_token": "x", "expires_at": "4102444800"},
+ {"access_token": "x", "expires_at": float("nan")},
+ {"access_token": "x", "expires_at": True},
+ ],
+)
+def test_invalid_or_expired_tokens_never_fall_back(monkeypatch, data):
+ monkeypatch.setattr(ConnectorClient, "get_oauth_access_token", lambda *_args: data)
+ with pytest.raises(InternalError, match="invalid or expired"):
+ auth.ExternalFeishuToken(REFERENCE, "key").get_token()
+
+
+async def test_long_import_reloads_token_and_scopes_are_isolated(monkeypatch):
+ now = [1000]
+ monkeypatch.setattr(auth, "time", SimpleNamespace(time=lambda: now[0]))
+ fetch = Mock(
+ side_effect=[
+ {"access_token": "old", "expires_at": 1060},
+ {"access_token": "new", "expires_at": 2000},
+ {"access_token": "other-user", "expires_at": 2000},
+ ]
+ )
+ monkeypatch.setattr(ConnectorClient, "get_oauth_access_token", fetch)
+ provider = auth.ExternalFeishuToken(REFERENCE, "key")
+ with auth.feishu_token_scope(provider):
+ option = await asyncio.to_thread(FeishuAccessor._user_request_option, "snapshot")
+ assert option.user_access_token == "old"
+ assert provider.get_token() == "old"
+ assert fetch.call_count == 1
+ now[0] = 1031
+ option = await asyncio.to_thread(FeishuAccessor._user_request_option, "snapshot")
+ assert option.user_access_token == "new"
+ api = object.__new__(UnderstandingAPI)
+ assert await api._resolve_lark_file({"feishu_access_token": "snapshot"}) == {
+ "user_access_token": "new"
+ }
+
+ async def other_task():
+ with auth.feishu_token_scope(auth.ExternalFeishuToken(REFERENCE, "other-key")):
+ return await asyncio.to_thread(FeishuAccessor._user_request_option, "snapshot")
+
+ assert (await asyncio.create_task(other_task())).user_access_token == "other-user"
+ assert auth.current_feishu_token.get() is provider
+ assert auth.current_feishu_token.get() is None
+ assert FeishuAccessor._user_request_option("explicit").user_access_token == "explicit"
+
+
+ "reference", [None, {}, {**REFERENCE, "ov_user_id": "bob"}, {**REFERENCE, "platform": "git"}]
+)
+def test_reference_identity_is_checked(reference):
+ with pytest.raises(InvalidArgumentError):
+ auth.validate_oauth_ref(reference, "alice")
+
+
+async def test_external_endpoint_does_not_disable_local_refresh(monkeypatch):
+ credentials = FeishuAppCredentials("app", "secret", "https://open.feishu.cn", 30)
+ client = FeishuOAuthClient(credentials)
+ refreshed = FeishuRefreshedToken("new-access", "new-refresh", 7200)
+ refresh = Mock(return_value=refreshed)
+ monkeypatch.setattr(client, "_refresh_user_access_token_sync", refresh)
+ assert await client.refresh_user_access_token("local-refresh-token") == refreshed
+ refresh.assert_called_once_with("local-refresh-token")
+
+
[email protected]("source_kind", ["folder", "image"])
+async def test_partial_download_auth_failure_is_cleaned_before_parsing(
+ monkeypatch, tmp_path, source_kind
+):
+ from unittest.mock import AsyncMock
+
+ from openviking.parse.accessors.base import LocalResource, SourceType
+ from openviking.parse.feishu_import import FeishuImportPlan
+ from openviking_cli.utils.config.parser_config import FeishuConfig
+
+ url = "https://example.feishu.cn/drive/folder/test"
+ accessor = FeishuAccessor()._new_operation(url, config=FeishuConfig())
+ monkeypatch.setattr(accessor, "_new_operation", lambda *_args, **_kwargs: accessor)
+ monkeypatch.setattr(
+ accessor,
+ "_list_drive_folder_children",
+ Mock(
+ return_value=[
+ {"type": "file", "token": name, "name": name} for name in ("A.txt", "B.txt")
+ ]
+ ),
+ )
+ fetch = Mock(side_effect=[{"access_token": "valid"}, InternalError("auth unavailable")])
+ monkeypatch.setattr(ConnectorClient, "get_oauth_access_token", fetch)
+ provider = auth.ExternalFeishuToken(REFERENCE, "key")
+
+ def download(token, **kwargs):
+ accessor._user_request_option(kwargs["feishu_access_token"])
+ provider._valid_until = 0
+ return b"content", "text/plain", token
+
+ monkeypatch.setattr(accessor, "_download_drive_file", download)
+
+ async def access(*_args, **_kwargs):
+ if source_kind == "folder":
+ skipped = []
+ await accessor._materialize_drive_folder(
+ "folder",
+ tmp_path,
+ feishu_access_token="snapshot",
+ skipped_items=skipped,
+ plan=FeishuImportPlan(tmp_path),
+ )
+ assert [p.name for p in tmp_path.iterdir()] == ["A.txt"]
+ assert len(skipped) == 1
+ else:
+ assert provider.get_token() == "valid"
+ provider._valid_until = 0
+ monkeypatch.setattr(accessor, "_get_client", Mock(return_value=Mock()))
+ # Image download normally swallows errors and returns incomplete content.
+ assert accessor._download_image("image", feishu_access_token="snapshot") is None
+ (tmp_path / "document.md").write_text("partial content")
+ return LocalResource(tmp_path, SourceType.FEISHU, url)
+
+ monkeypatch.setattr(accessor, "_access", AsyncMock(side_effect=access))
+ with auth.feishu_token_scope(provider), pytest.raises(InternalError, match="auth unavailable"):
+ await accessor.access(url, feishu_access_token="snapshot")
+ assert not tmp_path.exists()
+ assert auth.current_feishu_token.get() is None
+
+
[email protected]("prepared", [None, "understanding_response_id", "understanding_file_id"])
[email protected]("watch_interval", [0, 60])
+async def test_queue_auth_failure_cleanup_and_prepared_results(
+ monkeypatch, prepared, watch_interval
+):
+ from unittest.mock import AsyncMock
+
+ from openviking.server.identity import RequestContext, Role
+ from openviking.service.resource_service import ResourceService
+ from openviking.storage.queuefs.add_resource_msg import AddResourceMsg
+ from openviking_cli.session.user_id import UserIdentifier
+
+ ctx = RequestContext(user=UserIdentifier("ov", "alice"), role=Role.USER)
+ state = {"provider": auth.EXTERNAL_FEISHU_PROVIDER, "credentials": {}}
+ service = ResourceService()
+ service._connector_delegate = SimpleNamespace(
+ restore_watch_request=AsyncMock(
+ return_value=("key", "feishu_doc", {auth.OAUTH_REF_ARG: REFERENCE})
+ )
+ )
+ service._execute_resource_ingestion = AsyncMock(return_value={"status": "success"})
+ service._cleanup_reserved_target_if_empty = AsyncMock()
+ fetch = Mock(side_effect=InternalError("auth unavailable"))
+ monkeypatch.setattr(ConnectorClient, "get_oauth_access_token", fetch)
+ msg = AddResourceMsg(
+ task_id="task",
+ path="https://example.feishu.cn/docx/doc",
+ root_uri="viking://resources/reserved",
+ account_id="ov",
+ user_id="alice",
+ role="user",
+ cleanup_empty_target_on_failure=True,
+ watch_interval=watch_interval,
+ **({prepared: "submitted"} if prepared else {}),
+ )
+ lock = {"lease": "reserved"}
+ job = service.execute_add_resource_job(
+ msg,
+ ctx=ctx,
+ resource_lock=lock,
+ stage_callback=AsyncMock(),
+ task_auth=state,
+ )
+ if prepared:
+ assert await job == {"status": "success"}
+ fetch.assert_not_called()
+ call = service._execute_resource_ingestion.await_args.kwargs
+ assert "feishu_access_token" not in call
+ assert call["watch_auth_state"] == (state if watch_interval else None)
+ service._cleanup_reserved_target_if_empty.assert_not_awaited()
+ else:
+ with pytest.raises(InternalError, match="auth unavailable"):
+ await job
+ service._execute_resource_ingestion.assert_not_awaited()
+ service._cleanup_reserved_target_if_empty.assert_awaited_once_with(
+ root_uri=msg.root_uri,
+ ctx=ctx,
+ resource_lock=lock,
+ )
diff --git a/tests/service/test_resource_service_watch.py b/tests/service/test_resource_service_watch.py
index 845249c0d..b9d213bf8 100644
--- a/tests/service/test_resource_service_watch.py
+++ b/tests/service/test_resource_service_watch.py
@@ -486,6 +486,14 @@ class TestAddResourceArgs:
resource_service: ResourceService,
request_context: RequestContext,
):
+ from openviking.resource.feishu_watch_auth import FeishuRefreshedToken
+ from openviking.resource.watch_scheduler import WatchScheduler
+ from openviking_cli.utils.config import open_viking_config as config_module
+
+ config = config_module.ConnectorConfig(auth="https://connector.example/oauth/access_token")
+ monkeypatch.setattr(
+ config_module, "get_openviking_config", lambda: SimpleNamespace(connector=config)
+ )
monkeypatch.setattr(
resource_service_module,
"load_feishu_app_credentials",
@@ -496,14 +504,20 @@ class TestAddResourceArgs:
request_timeout=30,
),
)
+ monkeypatch.setattr(
+ "openviking.resource.feishu_watch_auth.load_feishu_app_credentials",
+ resource_service_module.load_feishu_app_credentials,
+ )
disable_task_tracker(monkeypatch)
to_uri = "viking://resources/feishu_user_watch"
resource_service._plan_source_job_target = AsyncMock(
return_value=(to_uri, None, False, False)
)
+ expected_token = "u-test"
+
async def preflight(_self, _source, *, feishu_access_token=None, **kwargs):
- assert feishu_access_token == "u-test"
+ assert feishu_access_token == expected_token
assert kwargs["feishu_config"].domain == "https://open.feishu.cn"
return SimpleNamespace(source_name=None, source_format="file")
@@ -566,6 +580,27 @@ class TestAddResourceArgs:
}
assert "auth_state" not in task.to_dict()
+ # A stored local watch still refreshes and queues work with connector.auth enabled.
+ refresh = Mock(return_value=FeishuRefreshedToken("u-new", "r-new", 7200))
+ monkeypatch.setattr(
+ "openviking.resource.feishu_watch_auth.FeishuOAuthClient._refresh_user_access_token_sync",
+ refresh,
+ )
+ expected_token = "u-new"
+ scheduler = WatchScheduler(
+ resource_service, runtime_config_manager=resource_service._runtime_config_manager
+ )
+ scheduler._watch_manager = resource_service._get_watch_manager()
+ await scheduler._execute_task(task)
+ refresh.assert_called_once_with("r-test")
+ updated = await get_task_by_uri(resource_service, to_uri, request_context)
+ assert updated.task_id == task.task_id
+ assert updated.is_active is True
+ assert updated.auth_state["provider"] == "feishu"
+ assert updated.auth_state["refresh_token"] == "r-new"
+ queued_auth = resource_service._enqueue_add_resource_job.await_args.kwargs["task_auth"]
+ assert queued_auth["access_token"] == "u-new"
+
@pytest.mark.asyncio
async def test_feishu_account_default_watch_does_not_store_app_secret(
self,
@@ -1575,3 +1610,135 @@ class TestResourceProcessingIndependence:
assert result is not None
assert "root_uri" in result
+
+
[email protected]("encrypted", [False, True])
+async def test_external_feishu_auth_survives_queue_and_multiple_watches(
+ monkeypatch, resource_service, request_context, encrypted
+):
+ from openviking.connector import auth
+ from openviking.connector.client import ConnectorClient
+ from openviking.resource.watch_scheduler import WatchScheduler
+ from openviking_cli.exceptions import InternalError
+ from openviking_cli.utils.config import open_viking_config as config_module
+
+ config = config_module.ConnectorConfig(auth="https://connector.example/oauth/access_token")
+ monkeypatch.setattr(
+ config_module, "get_openviking_config", lambda: SimpleNamespace(connector=config)
+ )
+ request_context.api_key = "external-api-key"
+ if encrypted:
+ resource_service._viking_fs._encryptor = SimpleNamespace(
+ encrypt=AsyncMock(side_effect=lambda _account, data: b"OVE1" + data[::-1]),
+ decrypt=AsyncMock(side_effect=lambda _account, data: data[4:][::-1]),
+ )
+ worker_context = RequestContext(user=request_context.user, role=request_context.role)
+ reference = {
+ "account_id": "cloud-account",
+ "user_id": "cloud-user",
+ "ov_user_id": request_context.user.user_id,
+ "platform": "feishu_doc",
+ "type": "oauth",
+ }
+ local_refresh = AsyncMock(
+ side_effect=AssertionError("external watches must not refresh locally")
+ )
+ monkeypatch.setattr(
+ "openviking.resource.feishu_watch_auth.FeishuOAuthClient.refresh_user_access_token",
+ local_refresh,
+ )
+ fetched = []
+
+ def fetch(_client, _url, api_key, ref):
+ assert api_key == "external-api-key"
+ assert ref == reference
+ token = f"external-token-{len(fetched)}"
+ fetched.append(token)
+ return {"access_token": token, "expires_at": 4102444800}
+
+ monkeypatch.setattr(ConnectorClient, "get_oauth_access_token", fetch)
+
+ async def preflight(_self, _source, *, feishu_access_token=None, **kwargs):
+ assert feishu_access_token == fetched[-1]
+ assert kwargs["feishu_config"].domain == "https://open.feishu.cn"
+ assert auth.current_feishu_token.get() is not None
+ return SimpleNamespace(source_name=None, source_format="file")
+
+ monkeypatch.setattr(
+ "openviking.parse.accessors.feishu_accessor.FeishuAccessor.preflight_source", preflight
+ )
+ tasks = []
+ for index in range(2):
+ to = f"viking://resources/external-{index}"
+ resource_service._plan_source_job_target = AsyncMock(return_value=(to, None, False, False))
+ await resource_service.add_resource(
+ path=f"https://example.feishu.cn/docx/doc-{index}",
+ ctx=request_context,
+ to=to,
+ watch_interval=30,
+ args={auth.OAUTH_REF_ARG: reference},
+ )
+ assert len(fetched) == 2 * index + 1
+ call = resource_service._enqueue_add_resource_job.await_args
+ message, state = call.args[0], call.kwargs["task_auth"]
+ assert auth.is_external_feishu_auth(state)
+ assert "external-api-key" not in str(message.to_dict())
+ assert "external-token" not in str(message.to_dict())
+ assert "external-token" not in str(state)
+ assert "refresh_token" not in str(state)
+ if encrypted:
+ assert "external-api-key" not in str(state)
+ await resource_service.execute_add_resource_job(
+ message,
+ ctx=worker_context,
+ resource_lock=None,
+ stage_callback=AsyncMock(),
+ task_auth=state,
+ )
+ assert len(fetched) == 2 * index + 2
+ assert resource_service._resource_processor.calls[-1]["feishu_access_token"] == fetched[-1]
+ task = await get_task_by_uri(resource_service, to, request_context)
+ assert task.auth_state == state
+ assert task.processor_kwargs == {}
+ assert "external-api-key" not in str(task.to_dict())
+ tasks.append(task)
+ assert auth.current_feishu_token.get() is None
+
+ scheduler = WatchScheduler(resource_service)
+ scheduler._watch_manager = resource_service._get_watch_manager()
+ await scheduler._execute_task(tasks[0])
+ assert len(fetched) == 5
+ next_call = resource_service._enqueue_add_resource_job.await_args
+ assert auth.is_external_feishu_auth(next_call.kwargs["task_auth"])
+ assert next_call.args[0].skip_watch_management is True
+
+ monkeypatch.setattr(
+ ConnectorClient, "get_oauth_access_token", Mock(side_effect=InternalError("unavailable"))
+ )
+ await scheduler._execute_task(tasks[0])
+ task = await get_task_by_uri(resource_service, tasks[0].to_uri, request_context)
+ assert task.is_active is True
+ assert task.last_status == "failed"
+ local_refresh.assert_not_awaited()
+
+
+async def test_external_feishu_requires_endpoint(monkeypatch, resource_service, request_context):
+ from openviking.connector import auth
+
+ reference = {
+ "account_id": "account",
+ "user_id": "user",
+ "ov_user_id": request_context.user.user_id,
+ "platform": "feishu_doc",
+ "type": "oauth",
+ }
+ monkeypatch.setattr(auth, "external_auth_url", lambda: "")
+ with pytest.raises(InvalidArgumentError, match="connector.auth is required"):
+ await resource_service.add_resource(
+ path="https://example.feishu.cn/docx/doc",
+ ctx=request_context,
+ to="viking://resources/test",
+ args={auth.OAUTH_REF_ARG: reference},
+ )
diff --git a/tests/unit/test_connector_config.py b/tests/unit/test_connector_config.py
index 2db12b51f..ff096e58d 100644
--- a/tests/unit/test_connector_config.py
+++ b/tests/unit/test_connector_config.py
@@ -38,6 +38,7 @@ def test_openviking_config_parses_connector_section():
"enable": True,
"connector": "https://connector.example/doc/add",
"tracker": "https://connector.example/task/info",
+ "auth": "",
"timeout_seconds": 120,
"poll_interval_ms": 250,
"allowed_add_types": ["tos"],
@@ -58,8 +59,16 @@ def test_openviking_config_parses_connector_section():
),
({"timeout_seconds": 0}, "timeout_seconds must be > 0"),
({"poll_interval_ms": 0}, "poll_interval_ms must be > 0"),
+ ({"auth": "file:///tmp/auth"}, "connector.auth must be"),
+ ({"auth": "https://user:[email protected]/auth"}, "connector.auth must be"),
],
)
def test_connector_config_rejects_invalid_runtime_settings(kwargs, match):
with pytest.raises(ValueError, match=match):
ConnectorConfig(**kwargs)
+
+
+def test_connector_auth_does_not_enable_ingestion_delegation():
+ config = ConnectorConfig(auth=" https://connector.example/oauth/access_token ")
+ assert config.auth == "https://connector.example/oauth/access_token"
+ assert config.enable is False