Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
feat(google_drive): the sign-in says how CocoIndex uses Google user data (#2791)
Jiangzhou58b11e1807be
diff --git a/docs/src/content/docs/connectors/google_drive.mdx b/docs/src/content/docs/connectors/google_drive.mdxindex f109e8f0..a4e20234 100644--- a/docs/src/content/docs/connectors/google_drive.mdx+++ b/docs/src/content/docs/connectors/google_drive.mdx@@ -319,7 +319,7 @@ pub async fn list_files(spec: GoogleDriveSourceSpec) -> Result<Vec<DriveFile>>`UserAccount` and the `cocoindex google-drive` commands are experimental. Experimental APIs are outside CocoIndex's compatibility promise: a later release, including a minor one, may change their API or semantics, or remove them.:::-A service account only sees folders shared with it. To read your own Drive, sign in with your Google account instead. The sign-in is a one-time interactive step, run from the shell, outside any CocoIndex run; it uses the OAuth flow Google recommends for command-line applications (an authorization code with PKCE, redirected to a listener on this machine's loopback interface), from a Desktop app OAuth client. CocoIndex asks for read-only access to Drive (`drive.readonly`) and nothing else.+A service account only sees folders shared with it. To read your own Drive, sign in with your Google account instead. The sign-in is a one-time interactive step, run from the shell, outside any CocoIndex run; it uses the OAuth flow Google recommends for command-line applications (an authorization code with PKCE, redirected to a listener on this machine's loopback interface), from a Desktop app OAuth client. CocoIndex asks for read-only access to Drive (`drive.readonly`) and nothing else. CocoIndex's use and transfer of information received from Google APIs adheres to the [Google API Services User Data Policy](https://developers.google.com/terms/api-services-user-data-policy), including the Limited Use requirements; see [how CocoIndex handles Google user data](https://cocoindex.io/privacy-policy/#google-user-data).CocoIndex's own OAuth client is not in a release yet: Google has to verify it first. Until a release includes it, sign in with an OAuth client you create in your own Google Cloud project, as below. A release that includes it needs nothing but your Google account: `cocoindex google-drive sign-in` without a client file.diff --git a/python/cocoindex/connectors/google_drive/_oauth.py b/python/cocoindex/connectors/google_drive/_oauth.pyindex 915c3ebd..69680fc6 100644--- a/python/cocoindex/connectors/google_drive/_oauth.py+++ b/python/cocoindex/connectors/google_drive/_oauth.py@@ -76,6 +76,9 @@ OWN_CLIENT_DOCS = ("https://cocoindex.io/docs/connectors/google_drive#setting-up-the-oauth-client")+# The privacy policy's section on Google user data, which the sign-in links.+PRIVACY_POLICY = "https://cocoindex.io/privacy-policy/#google-user-data"+class SignInError(Exception):"""A sign-in, sign-out or token refresh failed; the message says what to do."""@@ -384,7 +387,7 @@ def _revoke(client: httpx.Client, token: str) -> bool:def _say(message: str) -> None:- """Print a line the person must act on, flushed: the flow is blocking while they read it."""+ """Print what the person reads, flushed: the flow is blocking while they read it."""print(message, flush=True)@@ -521,6 +524,16 @@ def _await_redirect(raise SignInError("The sign-in timed out before Google redirected back.")+def _data_notice(token_path: Path) -> str:+ """What the sign-in asks Google for, and where it keeps what Google returns."""+ return (+ "CocoIndex asks Google for read-only access to your Google Drive.\n"+ f"The sign-in is stored at {token_path}, on this computer only; "+ "nothing is sent to CocoIndex.\n"+ f"How CocoIndex handles Google user data: {PRIVACY_POLICY}\n"+ )++def _instructions(url: str, port: int, browser_opened: bool) -> str:if browser_opened:return (@@ -671,6 +684,10 @@ def run_sign_in(the person reads, and ``browser_available`` to force the no-browser path."""client_cfg = _resolve_client(client_secrets_path)+ # Said before the browser opens or the link prints: Google's verification+ # of a restricted scope wants the app itself to disclose how it uses the+ # data, not only the privacy policy.+ say(_data_notice(token_path))if browser_available is None:browser_available = has_browser()owned = http_client is Nonediff --git a/python/tests/connectors/test_google_drive_oauth.py b/python/tests/connectors/test_google_drive_oauth.pyindex a1ef6aad..50a63066 100644--- a/python/tests/connectors/test_google_drive_oauth.py+++ b/python/tests/connectors/test_google_drive_oauth.py@@ -248,7 +248,8 @@ def _mode(path: pathlib.Path) -> int:def test_sign_in_round_trip_stores_a_private_token_file(flow: Flow) -> None:assert flow.sign_in() == EMAILassert len(flow.opened) == 1- assert flow.said and flow.opened[0] in flow.said[0]+ _notice, instructions = flow.said+ assert flow.opened[0] in instructionsstored = flow.stored()assert stored["email"] == EMAIL@@ -326,6 +327,53 @@ def test_a_web_client_or_a_missing_client_file_is_refused()+# --- the notice on Google user data ------------------------------------------+++def _assert_notice(text: str, token_path: pathlib.Path) -> None:+ assert "CocoIndex asks Google for read-only access to your Google Drive." in text+ assert f"The sign-in is stored at {token_path}, on this computer only" in text+ assert "nothing is sent to CocoIndex" in text+ assert "https://cocoindex.io/privacy-policy/#google-user-data" in text+++def test_the_sign_in_discloses_its_use_of_google_data_before_the_consent(+ flow: Flow, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]+) -> None:+ """Google's verification of a restricted scope wants the app itself to say this."""+ from cocoindex.cli import cli++ said_when_the_browser_opened: list[str] = []++ def browser(url: str) -> None:+ said_when_the_browser_opened.extend(flow.said)+ flow.browser(url)++ flow.sign_in(open_browser=browser)+ (notice,) = said_when_the_browser_opened+ _assert_notice(notice, flow.token_path)++ # Printed to the terminal, from Python and from the CLI.+ _script_sign_in(flow, monkeypatch)+ flow.token_path.unlink()+ assert flow.account().sign_in(flow.client_secrets) == EMAIL+ _assert_notice(capsys.readouterr().out, flow.token_path)++ flow.token_path.unlink()+ result = CliRunner().invoke(+ cli,+ [+ "google-drive",+ "sign-in",+ str(flow.client_secrets),+ "--token-path",+ str(flow.token_path),+ ],+ )+ assert result.exit_code == 0, result.output+ _assert_notice(result.output, flow.token_path)++# --- the no-browser path -----------------------------------------------------@@ -335,18 +383,19 @@ def test_without_a_browser_the_link_is_printed_and_the_redirect_pasted_back(lines: list[str | None] = []def read_line() -> str | None:- # Only after the link was printed can a person paste anything.- while not flow.said:+ # Only after the link was printed (after the notice) can a person paste anything.+ while len(flow.said) < 2:passif not lines:- url = flow.said[0].split("\n\n ")[1].split("\n")[0].strip()+ url = flow.said[1].split("\n\n ")[1].split("\n")[0].strip()lines.extend(["not an address\n", flow.google.consent(url) + "\n", None])return lines.pop(0)assert flow.sign_in(browser_available=False, read_line=read_line) == EMAILassert flow.opened == [] # no browser was opened- assert "another machine" in flow.said[0]- assert any("Paste the whole address" in s for s in flow.said[1:])+ _assert_notice(flow.said[0], flow.token_path) # before the link+ assert "another machine" in flow.said[1]+ assert any("Paste the whole address" in s for s in flow.said[2:])assert flow.stored()["email"] == EMAIL@@ -588,7 +637,10 @@ def test_the_service_builds_on_user_credentials(def _script_sign_in(flow: Flow, monkeypatch: pytest.MonkeyPatch) -> None:- """Make every interactive sign-in, the CLI's included, run against the fake."""+ """Make every interactive sign-in, the CLI's included, run against the fake.++ What the sign-in says still goes to stdout, as a real one's does.+ """interactive = _oauth.run_sign_indef scripted(client: pathlib.Path | None, token: pathlib.Path, **kw: object) -> str:@@ -598,7 +650,6 @@ def _script_sign_in(flow: Flow, monkeypatch: pytest.MonkeyPatch) -> None:http_client=flow.http,open_browser=flow.browser,read_line=lambda: None,- say=flow.said.append,browser_available=True,timeout=10,)