From 62b12782587b3fd1873688c5f3e1f4361d8e9586 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 13:07:33 +0000 Subject: [PATCH 1/2] feat(sources): connect Google Drive with Connect with Google (backend) Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01VrczAC5nKz1zkCUvRKXG5C --- fastapi_backend/app/domain/gdrive.py | 37 ++++++ fastapi_backend/app/domain/google_auth.py | 25 +++-- fastapi_backend/app/domain/sources.py | 15 ++- .../tests/api/test_drive_google.py | 106 ++++++++++++++++++ 4 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 fastapi_backend/app/domain/gdrive.py create mode 100644 fastapi_backend/tests/api/test_drive_google.py diff --git a/fastapi_backend/app/domain/gdrive.py b/fastapi_backend/app/domain/gdrive.py new file mode 100644 index 000000000..a31e09d34 --- /dev/null +++ b/fastapi_backend/app/domain/gdrive.py @@ -0,0 +1,37 @@ +"""Google Drive connected as a Google account through a Google OAuth client of Lens's (Connect with Google, +google_auth.connect_start and connect_finish), for rclone, which reads Drive: what to ask Google for, and how the +account is kept so rclone can renew it. + +rclone keeps a Google account as its own token JSON ({access_token, token_type, refresh_token, expiry}) and renews it +with the client it was given (`client_id`, `client_secret` in its config), so the source keeps those three: the token +and the client's secret sealed, the client's id as a parameter. A token pasted from `rclone authorize`, or one made by +signing in through rclone on the server (rclone_signin.py), renews with rclone's own client instead, so it comes +without one (sources.update drops a client left from before). +""" + +from __future__ import annotations + +import datetime as dt +import json + +READONLY = "https://www.googleapis.com/auth/drive.readonly" +FULL = "https://www.googleapis.com/auth/drive" +ACCOUNT = "account" # the parameter that names the connected account + + +def SCOPES(params): # noqa: N802 (google_auth reads a module's SCOPES) + """What Google is asked for: the connection's own scope (read only unless it was set to full).""" + return (FULL,) if str(params.get("scope") or "").strip() == "drive" else (READONLY,) + + +def keep(client_id, client_secret, body, email): + """The connected account as the source keeps it, from what Google's token endpoint answered: (params, secrets).""" + lasts = int(body.get("expires_in") or 0) + expiry = dt.datetime.now(dt.timezone.utc) + dt.timedelta(seconds=max(lasts - 60, 0)) + token = { + "access_token": body.get("access_token") or "", + "token_type": body.get("token_type") or "Bearer", + "refresh_token": body["refresh_token"], + "expiry": expiry.isoformat(timespec="seconds"), + } + return {"client_id": client_id, ACCOUNT: email}, {"token": json.dumps(token), "client_secret": client_secret} diff --git a/fastapi_backend/app/domain/google_auth.py b/fastapi_backend/app/domain/google_auth.py index 8d8df46c5..66df5785e 100644 --- a/fastapi_backend/app/domain/google_auth.py +++ b/fastapi_backend/app/domain/google_auth.py @@ -37,6 +37,8 @@ "https://www.googleapis.com/auth/gmail.readonly": "View your email messages and settings", "https://mail.google.com/": "Read, compose, send and permanently delete all your email from Gmail", "https://www.googleapis.com/auth/calendar.readonly": "See and download any calendar you can access using your Google Calendar", + "https://www.googleapis.com/auth/drive.readonly": "See and download all your Google Drive files", + "https://www.googleapis.com/auth/drive": "See, edit, create, and delete all of your Google Drive files", } TIMEOUT = 30 _TOKENS: dict[str, tuple[float, str]] = {} # who and for what -> (when it ends, monotonic; the access token) @@ -251,9 +253,9 @@ def get(cfg, creds, scopes, url, params=None, api="api", what=None): def _module(src): - from . import feeds, gmail # they import this + from . import feeds, gdrive, gmail # they import this - mod = feeds.OWN.get(src["type"]) or {"gmail": gmail}.get(src["type"]) + mod = feeds.OWN.get(src["type"]) or {"gmail": gmail, "drive": gdrive}.get(src["type"]) if mod is None or not getattr(mod, "SCOPES", None): raise ValueError(f"a {src['type']} source doesn't sign in with a Google account") return mod @@ -378,10 +380,17 @@ def connect_finish(db, cfg, flow, code, state): email = str(jwt.decode(body["id_token"], options={"verify_signature": False}).get("email") or "") except jwt.PyJWTError: email = "" - user = json.dumps( - {"type": "authorized_user", "client_id": flow["client_id"], "client_secret": client_secret, "refresh_token": body["refresh_token"]} - ) - field = getattr(_module(src), "ACCOUNT", "account") # the parameter that names the connected account - params = {field: email} if email and field in sources.BACKENDS[src["type"]]["fields"] else None - sources.update(db, cfg, src["id"], params=params, secret_values={"token": user, "service_account_credentials": None}) + mod = _module(src) + if hasattr(mod, "keep"): # kept another way (Drive: as rclone keeps it) + params, kept = mod.keep(flow["client_id"], client_secret, body, email) + else: + user = { + "type": "authorized_user", + "client_id": flow["client_id"], + "client_secret": client_secret, + "refresh_token": body["refresh_token"], + } + field = getattr(mod, "ACCOUNT", "account") # the parameter that names the connected account + params, kept = ({field: email} if email and field in sources.BACKENDS[src["type"]]["fields"] else None), {"token": json.dumps(user)} + sources.update(db, cfg, src["id"], params=params, secret_values={**kept, "service_account_credentials": None}) return src["id"], email diff --git a/fastapi_backend/app/domain/sources.py b/fastapi_backend/app/domain/sources.py index b9bbe36bd..389d4901a 100644 --- a/fastapi_backend/app/domain/sources.py +++ b/fastapi_backend/app/domain/sources.py @@ -34,8 +34,10 @@ "dropbox": {"label": "Dropbox", "fields": {}, "secrets": ["token"], "oauth": "rclone authorize dropbox"}, "drive": { "label": "Google Drive", - "fields": {"scope": "drive.readonly", "root_folder_id": "", "team_drive": "", "impersonate": ""}, - "secrets": ["token", "service_account_credentials"], # signs in with a token, or as a service account + # client_id and account: the Google OAuth client and account connected with Connect with Google (gdrive.py) + "fields": {"scope": "drive.readonly", "root_folder_id": "", "team_drive": "", "impersonate": "", "client_id": "", "account": ""}, + # signs in with a token (renewed with client_secret when connected with Google), or as a service account + "secrets": ["token", "service_account_credentials", "client_secret"], "oauth": "rclone authorize drive", }, "onedrive": { @@ -51,6 +53,7 @@ **feeds.TYPES, } OBSCURED = {"pass"} # rclone wants these obscured in its config file +NOT_RCLONE = {"account"} # parameters Lens keeps for itself, not rclone options SKIPPED = "not audio, video, a document, an image or text" TRANSCRIPT_EXT = {".txt", ".text", ".md", ".markdown", ".mdx", ".docx", ".doc", ".pdf", ".srt", ".vtt", ".json", ".jsonl", ".eml", ".ics"} # what a watched folder picks up (its `kinds`): the first three are from before documents, and read PDFs as transcripts @@ -124,7 +127,7 @@ def _config(cfg, src): name, spec = f"src{src['id']}", BACKENDS[src["type"]] lines = [f"[{name}]", f"type = {src['type']}"] for k, v in (src.get("params") or {}).items(): - if k in spec["fields"] and v not in (None, ""): + if k in spec["fields"] and k not in NOT_RCLONE and v not in (None, ""): lines.append(f"{k} = {_one_line(v)}") for k, sealed in (src.get("sealed") or {}).items(): if k in spec["secrets"]: @@ -403,6 +406,12 @@ def update(db, cfg, sid, name=None, params=None, secret_values=None): src = get(db, sid) _check_params(src["type"], params, secret_values) params = tidy(src["type"], params) + given = secret_values or {} + if "client_secret" in BACKENDS[src["type"]]["secrets"] and given.get("token") and not given.get("client_secret"): + # a token on its own is rclone's (pasted, or signed in through rclone): it renews with rclone's own client, so + # the client and account a Google connection left are dropped + secret_values = {**secret_values, "client_secret": None} + params = {**(params or {}), "client_id": "", "account": ""} sealed = dict(src.get("sealed") or {}) for k, v in (secret_values or {}).items(): if v is None or v == "": diff --git a/fastapi_backend/tests/api/test_drive_google.py b/fastapi_backend/tests/api/test_drive_google.py new file mode 100644 index 000000000..c84243eca --- /dev/null +++ b/fastapi_backend/tests/api/test_drive_google.py @@ -0,0 +1,106 @@ +"""Google Drive connected with Connect with Google (google_auth.connect_start and connect_finish, gdrive.py): the +account is kept as rclone keeps it, with the client that renews it, against tests/fake_google.py, never Google.""" + +from __future__ import annotations + +import json +import urllib.parse + +import pytest + +from app.domain import gdrive, google_auth, sources +from tests import fake_google +from tests.helpers import login, make_user + +MARA = "mara@harbour.example" + + +@pytest.fixture +def google(monkeypatch): + g = fake_google.Google() + monkeypatch.setattr(google_auth, "TOKEN_URL", g.base + "/token") + monkeypatch.setattr(google_auth, "AUTH_URL", g.base + "/auth") + monkeypatch.setattr(sources, "test", lambda *a, **k: {"ok": True}) # rclone isn't run in tests + g.clients.add(("lens-web", "web secret 1")) + yield g + g.close() + + +@pytest.fixture +def admin(client, db): + make_user(db, "root@x.io", "root password 1", admin=True) + h = login(client, "root@x.io", "root password 1") + r = client.post("/api/v1/auth/providers", headers=h, json={"kind": "google", "client_id": "lens-web", "client_secret": "web secret 1"}) + assert r.status_code == 200, r.text + return h + + +def _connect(client, google, h, sid): + url = client.post(f"/api/v1/sources/{sid}/google", headers=h, json={}).json()["url"] + r = client.get(google.consent(url, MARA), follow_redirects=False) + assert r.status_code == 303, r.text + u = urllib.parse.urlsplit(r.headers["location"]) + return url, u.path, dict(urllib.parse.parse_qsl(u.query)) + + +def _conf(cfg, db, sid): + _, conf = sources._config(cfg, sources.get(db, sid)) + return dict(line.split(" = ", 1) for line in conf.splitlines()[1:]) + + +def test_drive_connects_with_google_for_rclone(google, client, db, cfg, admin): + h = admin + body = {"type": "drive", "name": "Mara's Drive", "params": {"root_folder_id": "https://drive.google.com/drive/folders/1AbC"}} + sid = client.post("/api/v1/sources", headers=h, json=body).json()["id"] + url, path, back = _connect(client, google, h, sid) + q = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(url).query)) + assert q["client_id"] == "lens-web" and q["scope"].split() == ["openid", "email", gdrive.READONLY] + assert path == "/sources" and back == {"google": "connected", "source": str(sid), "account": MARA} + + src = next(s for s in client.get("/api/v1/sources", headers=h).json() if s["id"] == sid) + assert src["params"]["account"] == MARA and src["params"]["client_id"] == "lens-web" + assert src["secrets"]["token"]["set"] and src["secrets"]["client_secret"]["set"] + assert "web secret 1" not in json.dumps(db.rows("SELECT * FROM storage_source")) # sealed + # rclone gets its own token shape and the client to renew it with; the account is Lens's, not an rclone option + conf = _conf(cfg, db, sid) + tok = json.loads(conf["token"]) + assert tok["refresh_token"] in google.users and tok["token_type"] == "Bearer" and tok["access_token"] and tok["expiry"] + assert conf["client_id"] == "lens-web" and conf["client_secret"] == "web secret 1" + assert conf["root_folder_id"] == "1AbC" and "account" not in conf + + # full access asks Google for all of Drive + client.patch(f"/api/v1/sources/{sid}", headers=h, json={"params": {"scope": "drive"}}) + url, _, back = _connect(client, google, h, sid) + assert dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(url).query))["scope"].split()[-1] == gdrive.FULL + assert back["google"] == "connected" + + +def test_a_token_from_rclone_renews_with_rclones_own_client(google, client, db, cfg, admin): + """Pasting a token (or signing in through rclone) after connecting with Google: the token is rclone's, so the + Google client and account it was connected with are dropped, or rclone would renew it with the wrong client.""" + h = admin + key = {"type": "service_account", "private_key": "k", "client_email": "lens@x.iam.gserviceaccount.com"} + body = {"type": "drive", "secrets": {"service_account_credentials": json.dumps(key)}} + sid = client.post("/api/v1/sources", headers=h, json=body).json()["id"] + _connect(client, google, h, sid) + src = sources.get(db, sid) + assert set(src["sealed"]) == {"token", "client_secret"} # the service account gave way to the connected account + + pasted = json.dumps({"access_token": "ya29.x", "token_type": "Bearer", "refresh_token": "1//rclone", "expiry": "2030-01-01T00:00:00Z"}) + r = client.patch(f"/api/v1/sources/{sid}", headers=h, json={"secrets": {"token": pasted}}) + assert r.status_code == 200, r.text + src = next(s for s in client.get("/api/v1/sources", headers=h).json() if s["id"] == sid) + assert src["params"]["client_id"] == "" and src["params"]["account"] == "" + assert not src["secrets"]["client_secret"]["set"] + conf = _conf(cfg, db, sid) + assert json.loads(conf["token"])["refresh_token"] == "1//rclone" and "client_id" not in conf and "client_secret" not in conf + # options alone keep the connection as it is + _connect(client, google, h, sid) + client.patch(f"/api/v1/sources/{sid}", headers=h, json={"params": {"root_folder_id": "1Other"}}) + assert _conf(cfg, db, sid)["client_id"] == "lens-web" + + +def test_drive_scopes(): + assert google_auth.scopes_for({"type": "drive", "params": {"scope": "drive.readonly"}}) == (gdrive.READONLY,) + assert google_auth.scopes_for({"type": "drive", "params": {"scope": "drive"}}) == (gdrive.FULL,) + assert google_auth.scopes_for({"type": "drive", "params": {}}) == (gdrive.READONLY,) From 0ad789cf216c4b48f41467e5639ec6cb8e15e37e Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 13:48:16 +0000 Subject: [PATCH 2/2] feat(sources): connect Google Drive with Connect with Google (web app) Drive's Google account mode connects through Lens's Google client in one click when it has one, shows the account it is connected as, and keeps rclone and a Google app of your own a click away. Docs, changelog and threat model follow. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01VrczAC5nKz1zkCUvRKXG5C --- CHANGELOG.md | 4 + docs/processing.md | 10 ++ .../__tests__/drive-google.test.tsx | 144 ++++++++++++++++++ .../components/sources/connection-fields.tsx | 76 ++++++--- .../components/sources/source-model.ts | 14 +- .../components/sources/sources-page.tsx | 10 +- threat-model.json | 2 +- threat-model.md | 2 +- threat-model.yaml | 4 +- 9 files changed, 229 insertions(+), 37 deletions(-) create mode 100644 nextjs-frontend/__tests__/drive-google.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 8213ef298..2360a6f1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version also shows the redirect URI to register. A connection whose sign-in stopped working has **Connect Google account** on its row; pasting the authorized user JSON still works ([Google Calendar](docs/processing.md#email-imap-calendar-feeds-ical-and-google-calendar)). +- **Google Drive connects with Google too.** When Lens has a Google client (Google sign-in's, or an earlier + connection's), a Drive connection signs in with **Connect with Google** in one click and shows the account it is + connected as; Read only asks Google for read-only Drive access. With no client it signs in through rclone, as before, + or through a Google app of your own ([Google Drive](docs/processing.md#storage-sources)). - **Every entity has its own page.** An entity's page is named by its name, as a wiki names pages: `/wiki//` (spaces as underscores), so Ada Lovelace in `work` is `/wiki/work/Ada_Lovelace`. It shows what the entity is, the notes people and the assistant keep on it in the BlockSuite editor, every line that says it, diff --git a/docs/processing.md b/docs/processing.md index f10e8ce90..6d8af85cf 100644 --- a/docs/processing.md +++ b/docs/processing.md @@ -321,6 +321,16 @@ files, then Import (`POST /api/v1/import/source`). The listing marks files that they end on to `POST /api/v1/sources/rclone-signin/{id}` (`{"address": …}`), which answers `{connected: }` (400: not this sign-in's address, still waiting; 410: cancelled, refused or ran out). `DELETE` stops it. Audited as `source.signin`. +- **Google Drive can connect with Google** as Google Calendar does: when Lens has a Google OAuth client (the Google + sign-in provider's, or an earlier Google connection's), a new Google account connection uses it, so **Connect with + Google** saves the connection, opens Google's consent screen in this browser and comes back with the account + connected (its address shows on the connection). Read only asks Google for `drive.readonly`, Full access for all of + Drive. With no client Lens signs in through rclone; Connect through your own Google app instead takes a web + application client of a Google Cloud project with the Drive API turned on (the form shows the redirect URI to + register). The connection keeps that client with its token (`client_id`, and the secret encrypted) so rclone renews + the token with it; a token from rclone (signing in through rclone, or pasted) drops it. A connection whose sign-in + stopped working shows **Connect Google account** on its row. As for Google Calendar, connecting happens in the web + app (`POST /api/v1/sources/{id}/google`). - Google Drive can also sign in as a service account: choose Service account and upload its JSON key file (`service_account_credentials`). Share the folders Lens should read with the account's address, or give a shared drive ID (`team_drive`); in Google Workspace, `impersonate` reads a user's own Drive through domain-wide delegation. diff --git a/nextjs-frontend/__tests__/drive-google.test.tsx b/nextjs-frontend/__tests__/drive-google.test.tsx new file mode 100644 index 000000000..ae57d774a --- /dev/null +++ b/nextjs-frontend/__tests__/drive-google.test.tsx @@ -0,0 +1,144 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Sources } from "@/app/openapi-client"; +import type { Source } from "@/app/openapi-client/types.gen"; +import { ConnectionDialog } from "@/components/sources/connection-dialog"; +import { emptyForm, sourceSubtitle, validateForm } from "@/components/sources/source-model"; +import { TooltipProvider } from "@/components/ui/tooltip"; +import { navigate } from "@/lib/navigate"; + +jest.mock("@/app/openapi-client", () => ({ + Sources: { + createSource: jest.fn(), + updateSource: jest.fn(), + testSource: jest.fn(), + deleteSource: jest.fn(), + listSources: jest.fn(), + googleClient: jest.fn(), + connectGoogle: jest.fn(), + startRcloneSignin: jest.fn(), + finishRcloneSignin: jest.fn(), + cancelRcloneSignin: jest.fn(), + }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); +jest.mock("@/lib/navigate", () => ({ navigate: { assign: jest.fn(), replace: jest.fn() } })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const DRIVE = { + label: "Google Drive", + fields: { scope: "drive.readonly", root_folder_id: "", team_drive: "", impersonate: "", client_id: "", account: "" }, + secrets: ["token", "service_account_credentials", "client_secret"], + oauth: "rclone authorize drive", +}; +const CALLBACK = "https://lens.example/api/v1/auth/external/google/callback"; +const CONSENT = "https://accounts.google.com/o/oauth2/v2/auth?client_id=c"; +const FOLDER = "https://drive.google.com/drive/folders/1AbC"; + +function show(source?: Source) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); + render( + + + undefined} backends={{ drive: DRIVE }} source={source} /> + + , + ); + if (!source) fireEvent.click(screen.getByRole("radio", { name: /Google Drive/ })); +} + +describe("Google Drive with Connect with Google", () => { + beforeEach(() => { + (Sources.createSource as jest.Mock) + .mockReset() + .mockImplementation(() => ok({ id: 7, health: { ok: false, error: "empty token" } })); + (Sources.updateSource as jest.Mock).mockReset().mockImplementation(() => ok({ ok: true, health: { ok: true } })); + (Sources.connectGoogle as jest.Mock).mockReset().mockImplementation(() => ok({ url: CONSENT })); + (navigate.assign as jest.Mock).mockReset(); + }); + + it("connects through the Google client Lens has, in one click", async () => { + (Sources.googleClient as jest.Mock).mockImplementation(() => + ok({ origin: "sign-in", client_id: "c", redirect_uri: CALLBACK }), + ); + show(); + const connect = await screen.findByRole("button", { name: "Connect with Google" }); + expect(screen.getByText(/Through Google sign-in’s OAuth client/)).toBeInTheDocument(); + fireEvent.change(screen.getByLabelText(/Root folder/), { target: { value: FOLDER } }); + fireEvent.click(connect); + await waitFor(() => expect(navigate.assign).toHaveBeenCalledWith(CONSENT)); + expect((Sources.createSource as jest.Mock).mock.calls[0][0].body).toEqual({ + type: "drive", + name: "Google Drive", + params: { + scope: "drive.readonly", + root_folder_id: FOLDER, + team_drive: "", + impersonate: "", + client_id: "", + account: "", + }, + secrets: {}, + }); + expect((Sources.connectGoogle as jest.Mock).mock.calls[0][0]).toMatchObject({ path: { sid: 7 }, body: {} }); + // rclone is still a click away + expect(screen.getByRole("button", { name: /sign in through rclone/ })).toBeInTheDocument(); + }); + + it("signs in through rclone when Lens has no Google client, with your own app a click away", async () => { + (Sources.googleClient as jest.Mock).mockImplementation(() => + ok({ origin: null, client_id: null, redirect_uri: CALLBACK }), + ); + show(); + expect(await screen.findByRole("button", { name: "Sign in with Google" })).toBeInTheDocument(); // rclone's + fireEvent.click(await screen.findByRole("button", { name: "Connect through your own Google app instead" })); + expect(screen.getByLabelText("OAuth client ID")).toBeInTheDocument(); + expect(screen.getByText(/enable the Google Drive API/)).toBeInTheDocument(); + expect(screen.getByText(CALLBACK)).toBeInTheDocument(); + fireEvent.change(screen.getByLabelText("OAuth client ID"), { target: { value: "123.apps.googleusercontent.com" } }); + fireEvent.change(screen.getByLabelText("Client secret"), { target: { value: "shh" } }); + fireEvent.click(screen.getByRole("button", { name: "Connect with Google" })); + await waitFor(() => expect(navigate.assign).toHaveBeenCalledWith(CONSENT)); + expect((Sources.connectGoogle as jest.Mock).mock.calls[0][0].body).toEqual({ + client_id: "123.apps.googleusercontent.com", + client_secret: "shh", + }); + }); + + it("shows the connected account, and connects again from there", async () => { + (Sources.googleClient as jest.Mock).mockImplementation(() => + ok({ origin: "connection", client_id: "c", redirect_uri: CALLBACK }), + ); + const set = { secret: true, set: true }; + show({ + id: 7, + name: "Mara's Drive", + type: "drive", + params: { ...DRIVE.fields, client_id: "c", account: "mara@harbour.example" }, + secrets: { token: set, client_secret: set, service_account_credentials: { secret: true, set: false } }, + health: { ok: true }, + } as unknown as Source); + expect(await screen.findByText("mara@harbour.example")).toBeInTheDocument(); + const again = screen.getByRole("button", { name: "Connect again" }); + await waitFor(() => expect(again).toBeEnabled()); // once Lens knows its Google client + fireEvent.click(again); + await waitFor(() => expect(navigate.assign).toHaveBeenCalledWith(CONSENT)); + expect(Sources.createSource).not.toHaveBeenCalled(); + }); + + it("knows which way a Drive connection signed in", () => { + const set = { secret: true, set: true }; + const google = { name: "d", params: { client_id: "c", account: "mara@harbour.example" }, secrets: { token: set } }; + expect(emptyForm(DRIVE, google).signInWith).toBe("google"); + expect(emptyForm(DRIVE, { ...google, params: {} }).signInWith).toBe("rclone"); + expect(validateForm("drive", DRIVE, { ...emptyForm(DRIVE), signInWith: "google" }, false)).toEqual({ + token: "Connect a Google account first.", + }); + expect(sourceSubtitle({ type: "drive", params: { ...google.params, root_folder_id: "1AbC" } })).toBe( + "Drive · as mara@harbour.example · read only · one folder", + ); + }); +}); diff --git a/nextjs-frontend/components/sources/connection-fields.tsx b/nextjs-frontend/components/sources/connection-fields.tsx index ae6145e43..128c2152a 100644 --- a/nextjs-frontend/components/sources/connection-fields.tsx +++ b/nextjs-frontend/components/sources/connection-fields.tsx @@ -1,7 +1,7 @@ "use client"; import { useQuery } from "@tanstack/react-query"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { Sources } from "@/app/openapi-client"; import { @@ -52,8 +52,8 @@ export function ConnectionFields({ /** Sign in again through rclone; shown while a saved connection can also just be saved. */ onSignIn?: () => void; signingIn?: boolean; - /** Connect with Google (Google Calendar): save the connection and go to Google's consent screen, through this OAuth - * client when one is given, else the one Lens has. */ + /** Connect with Google (Google Calendar, Google Drive): save the connection and go to Google's consent screen, + * through this OAuth client when one is given, else the one Lens has. */ onConnectGoogle?: (client: { client_id?: string; client_secret?: string }) => void; connectingGoogle?: boolean; }) { @@ -63,9 +63,15 @@ export function ConnectionFields({ const googleClient = useQuery({ queryKey: ["google-client"], queryFn: () => data(Sources.googleClient({ client: api })), - enabled: type === "gcal" || type === "gmail", + enabled: type === "gcal" || type === "gmail" || type === "drive", staleTime: 60_000, }); + // a new Drive connection connects with Google when Lens has a Google OAuth client already, else through rclone + const hasClient = Boolean(googleClient.data?.origin); + useEffect(() => { + if (type === "drive" && !saved && hasClient && form.signInWith === "rclone") + onChange({ ...form, signInWith: "google" }); + }, [hasClient]); const editing = saved; const p = form.params; const set = (k: string, v: string) => onChange({ ...form, params: { ...p, [k]: v } }); @@ -234,17 +240,13 @@ export function ConnectionFields({ ); }; - /** Google Calendar or Gmail as a Google account: Connect with Google (Google's consent screen), or an authorized user - * JSON pasted under the fold. */ - const googleAccount = (() => { + /** Connect with Google (Google's consent screen) for a Google account, through the OAuth client Lens has or one + * entered here; `api` is the Google API that client's project turns on, `account` the address connected. */ + const connectWithGoogle = (api: string, connected: boolean, account?: string) => { const g = googleClient.data; - const gmail = type === "gmail"; - const account = gmail ? p.user : p.account; - const connected = editing && isSet("token") && form.secrets.token === undefined; const needClient = Boolean(g) && (g?.origin == null || ownClient); - const pasted = form.secrets.token ?? ""; return ( -
+ <> {connected && (

{account ? ( @@ -285,9 +287,8 @@ export function ConnectionFields({

- In Google Cloud › APIs & Services: enable the {gmail ? "Gmail" : "Google Calendar"} API, then under - Credentials create an OAuth client ID (a web application) with this authorized redirect URI. Lens keeps - the client with the connection. + In Google Cloud › APIs & Services: enable the {api}, then under Credentials create an OAuth client + ID (a web application) with this authorized redirect URI. Lens keeps the client with the connection.
@@ -320,6 +321,22 @@ export function ConnectionFields({ )} + + ); + }; + + /** Google Calendar or Gmail as a Google account: Connect with Google, or an authorized user JSON pasted under the + * fold. */ + const googleAccount = (() => { + const gmail = type === "gmail"; + const pasted = form.secrets.token ?? ""; + return ( +
+ {connectWithGoogle( + gmail ? "Gmail API" : "Google Calendar API", + editing && isSet("token") && form.secrets.token === undefined, + gmail ? p.user : p.account, + )} {errors.token && !pasted.trim() && (

{errors.token} @@ -389,16 +406,25 @@ export function ConnectionFields({ const serviceAccount = serviceAccountKey("Share the folders Lens should read with that address."); return (

- onChange({ ...form, driveAuth: v as ConnForm["driveAuth"] })} - items={[ - { value: "token", label: "Google account" }, - { value: "service_account", label: "Service account" }, - ]} - /> - {form.driveAuth === "service_account" ? serviceAccount : token} + {googleAuth} + {form.driveAuth === "service_account" ? ( + serviceAccount + ) : form.signInWith === "google" ? ( +
+ {connectWithGoogle("Google Drive API", editing && isSet("token") && Boolean(p.client_id), p.account)} + {errors.token && ( +

+ {errors.token} +

+ )} + {how("Or sign in through rclone, with no Google app of your own", "rclone")} +
+ ) : ( +
+ {token} + {how(hasClient ? "Connect with Google instead" : "Connect through your own Google app instead", "google")} +
+ )} {select("scope", DRIVE_SCOPES)} {text("root_folder_id", { mono: true, diff --git a/nextjs-frontend/components/sources/source-model.ts b/nextjs-frontend/components/sources/source-model.ts index d46de89d3..27e109d1d 100644 --- a/nextjs-frontend/components/sources/source-model.ts +++ b/nextjs-frontend/components/sources/source-model.ts @@ -155,7 +155,7 @@ export function sourceSubtitle(s: Pick): string { case "dropbox": return "Dropbox · token"; case "drive": - return `Drive · ${str(p.scope) === "drive" ? "full access" : "read only"}${str(p.root_folder_id) ? " · one folder" : ""}`; + return `Drive${str(p.account) ? ` · as ${str(p.account)}` : ""} · ${str(p.scope) === "drive" ? "full access" : "read only"}${str(p.root_folder_id) ? " · one folder" : ""}`; case "onedrive": return `OneDrive · token${str(p.drive_type) ? ` · ${str(p.drive_type)}` : ""}`; case "sftp": @@ -447,12 +447,13 @@ export type ConnForm = { /** Google Drive, Google Calendar and Gmail sign in as a Google account (a token), or as a service account (its JSON * key file). */ driveAuth: "token" | "service_account"; - /** How an OAuth connection (Dropbox, Google Drive, OneDrive) gets its token: signing in through rclone on the - * server, or pasting what `rclone authorize` printed on another computer. */ + /** How an OAuth connection (Dropbox, Google Drive, OneDrive) gets its token: Connect with Google through Lens's + * Google OAuth client (Drive), signing in through rclone on the server, or pasting what `rclone authorize` printed + * on another computer. */ signInWith: SignInWith; }; -export type SignInWith = "rclone" | "paste"; +export type SignInWith = "google" | "rclone" | "paste"; /** Whose sign-in page an OAuth connection opens. */ export const SIGNIN_SERVICE: Partial> = { @@ -520,7 +521,8 @@ export function emptyForm(spec: BackendSpec, source?: Pick | undefined)?.client_id) ? "google" : "rclone", }; } @@ -557,6 +559,8 @@ export function validateForm( if (key?.ok === false) e.service_account_credentials = key.error; else if (!secret("service_account_credentials")) e.service_account_credentials = "Choose the service account’s JSON key file."; + } else if (spec.oauth && form.signInWith === "google") { + if (!(editing && isSet("token"))) e.token = "Connect a Google account first."; } else if (spec.oauth && form.signInWith === "paste") { // (signing in makes the token instead: nothing to check here) const t = parseRcloneToken(form.tokenText); diff --git a/nextjs-frontend/components/sources/sources-page.tsx b/nextjs-frontend/components/sources/sources-page.tsx index d03a2901b..3a9d0bffd 100644 --- a/nextjs-frontend/components/sources/sources-page.tsx +++ b/nextjs-frontend/components/sources/sources-page.tsx @@ -126,8 +126,8 @@ export function SourcesPage() { router.replace(pathname); }, [adding, admin, backends.data, router, pathname]); - // Back from Google's consent screen (Connect with Google): the account's calendars open next to pick one to watch, or - // the connection opens again with what went wrong + // Back from Google's consent screen (Connect with Google): the account's calendars (or Drive's folders) open next to + // pick one to watch, or the connection opens again with what went wrong const fromGoogle = params.get("google") === "connected" ? "connected" : params.has("google_error") ? "error" : null; const askedGoogle = useRef(false); useEffect(() => { @@ -202,7 +202,11 @@ export function SourcesPage() { const oauth = Boolean(s.oauth); return (
e.stopPropagation()}> - {bad && (s.type === "gcal" || s.type === "gmail") && /Google account/.test(s.health?.error ?? "") ? ( + {bad && + (((s.type === "gcal" || s.type === "gmail") && /Google account/.test(s.health?.error ?? "")) || + (s.type === "drive" && + Boolean((s.params as Record | null)?.client_id) && + /token|auth|expired|unauthori[sz]ed|401|invalid_grant/i.test(s.health?.error ?? ""))) ? (