From 16e32c46f1e82f10c7d0afc2d39f72438be7bc07 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 12:04:58 +0000 Subject: [PATCH 1/2] feat(sources): connect Gmail from the connection dialog, with Connect with Google MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add connection › Gmail connects the account through the same consent flow as Google Calendar (#407): Google asks to read mail (all of Gmail for IMAP), the account's address becomes the source's user, and its labels open. A Workspace service account takes its key and the address it acts for, and says which client id and scope an admin must allow. A Gmail row whose sign-in stopped working offers Connect Google account. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01WzqrXPCMR7aJYyLPqmx41Q --- CHANGELOG.md | 6 ++ docs/processing.md | 17 ++-- fastapi_backend/app/api/v1/routes/sources.py | 6 +- fastapi_backend/app/domain/gmail.py | 13 ++- fastapi_backend/app/domain/google_auth.py | 4 +- .../tests/api/test_gmail_source.py | 39 +++++++++ .../__tests__/add-connection.test.tsx | 28 +++++++ .../__tests__/sources-model.test.ts | 46 ++++++++++- .../components/sources/connection-fields.tsx | 82 +++++++++++++++---- .../components/sources/source-model.ts | 49 +++++++---- .../components/sources/sources-page.tsx | 2 +- threat-model.json | 2 +- threat-model.md | 2 +- threat-model.yaml | 4 +- 14 files changed, 243 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3fc6590a..8e95051f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version was. Deleted notes wait in Recently deleted, at the bottom of the Notes page, for 30 days with their files and history, to put back with one click or delete for good; then they go on their own ([Deleting and putting back](docs/notes.md#deleting-and-putting-back)). +- **Connect Gmail with Google.** Add connection › Gmail now has the Google account and Service account choice and + **Connect with Google**, as Google Calendar does: Google asks to read mail (read only; all of Gmail to read over + IMAP), the account's address becomes the connection's, and its labels open to pick one to watch. A service account + takes its key and the address it reads, and says which client ID and scope a Workspace admin allows. A Gmail + connection whose sign-in stopped working has **Connect Google account** on its row + ([Gmail and Google Workspace](docs/processing.md#gmail-and-google-workspace)). - **Connect with Google.** A Google Calendar connection signs in to a Google account from a button now: Google's consent screen asks for read-only calendar access and sends you back to Sources with the account connected and its calendars open. It uses the Google sign-in provider's OAuth client, an earlier connection's, or one entered in the form, which diff --git a/docs/processing.md b/docs/processing.md index bf4b92306..f10e8ce90 100644 --- a/docs/processing.md +++ b/docs/processing.md @@ -410,13 +410,16 @@ A Gmail or Google Workspace mailbox (type `gmail`) is read through Google's Gmai or over IMAP (`via: imap`). For a personal Gmail account, an email (IMAP) connection with an app password works too, with nothing to set up at Google. A Gmail source signs in one of two ways: -- **Google account** (`token`): the "authorized user" JSON (`client_id`, `client_secret`, `refresh_token`) of an - OAuth client of your own Google Cloud project, with the Gmail API turned on there, kept encrypted like a password. - `gcloud auth application-default login --client-id-file=client.json --scopes=https://www.googleapis.com/auth/gmail.readonly,https://www.googleapis.com/auth/cloud-platform` - writes one (with `https://mail.google.com/` instead to read over IMAP); an agent can give one it already has. Enter - the account's address as `user` to read over IMAP. Google reviews apps that read Gmail before people outside your - Workspace domain can use them without a warning: an app in testing works for its test users, whose sign-ins Google - ends after seven days. Connecting the account from the connection dialog, with no JSON to make, is in progress. +- **Google account** (`token`): **Connect with Google** in the connection form, as for + [Google Calendar](#email-imap-calendar-feeds-ical-and-google-calendar): Google's consent screen asks to read your mail + (read only; all of Gmail to read over IMAP, since Google has no read-only scope for it), then sends you back to + Sources with the account connected, its address as the source's `user`, and its labels open. Turn on the Gmail API in + the OAuth client's Google Cloud project and add the Gmail scope to its consent screen. Google reviews apps that read + Gmail before people outside your Workspace domain can use them without a warning: while the consent screen is in + Testing, it works for its test users and Google ends their sign-ins after seven days. Unticking the mail permission on + Google's page leaves the source unconnected, and says so. An agent saves the `token` directly instead (in the form: + Or paste an authorized user JSON): `gcloud auth application-default login --client-id-file=client.json --scopes=https://www.googleapis.com/auth/gmail.readonly,https://www.googleapis.com/auth/cloud-platform` + writes one; enter the account's address as `user` to read over IMAP. - **A service account** (Google Workspace). Upload the service account's JSON key and enter the address of the person whose mail Lens reads; nobody signs in. A Workspace admin lets the account's client id use `https://www.googleapis.com/auth/gmail.readonly` (or `https://mail.google.com/` to read over IMAP) under Admin diff --git a/fastapi_backend/app/api/v1/routes/sources.py b/fastapi_backend/app/api/v1/routes/sources.py index bb28083d0..565e95005 100644 --- a/fastapi_backend/app/api/v1/routes/sources.py +++ b/fastapi_backend/app/api/v1/routes/sources.py @@ -163,9 +163,9 @@ def google_client(user: AdminReader, request: Request, db: Db, cfg: Cfg) -> Goog def connect_google( sid: int, user: AdminWriter, request: Request, response: Response, db: Db, cfg: Cfg, body: GoogleConnect | None = None ) -> GoogleConnectStart: - """Connect a Google account to a Google source (Google Calendar): open the returned address in this browser, consent - there, and Google sends you back to the Sources page with the account connected. Its refresh token is kept sealed on - the source. Without a client here, the Google sign-in provider's (Settings › Sign-in) or an earlier connection's is + """Connect a Google account to a Google source (Gmail, Google Calendar): open the returned address in this browser, + consent there, and Google sends you back to the Sources page with the account connected. Its refresh token is kept + sealed on the source. Without a client here, the Google sign-in provider's (Settings › Sign-in) or an earlier connection's is used. Only from the web app: the consent comes back to the browser that asked (an agent saves an authorized user JSON it already has as the source's `token` instead). Audited as `source.google_connect`.""" if user.via != "access": diff --git a/fastapi_backend/app/domain/gmail.py b/fastapi_backend/app/domain/gmail.py index 2bf3bed04..373fbc83e 100644 --- a/fastapi_backend/app/domain/gmail.py +++ b/fastapi_backend/app/domain/gmail.py @@ -68,9 +68,14 @@ def __init__(self, files=(), marks=()): self.marks = list(marks) -def scopes(src): - """What a source reads with: Gmail read only, or all of Gmail over IMAP (Google has no read-only scope for it).""" - return [MAIL] if str((src.get("params") or {}).get("via") or "").strip() == "imap" else [GMAIL_READ] +def scopes(params): + """What a source reads with, by its params: Gmail read only, or all of Gmail over IMAP (Google has no read-only scope + for it).""" + return (MAIL,) if str((params or {}).get("via") or "").strip() == "imap" else (GMAIL_READ,) + + +SCOPES = scopes # what Connect with Google asks for (google_auth.scopes_for) +ACCOUNT = "user" # the parameter Connect with Google fills in with the account's address def credentials(cfg, src): @@ -86,7 +91,7 @@ def credentials(cfg, src): def token(cfg, src): """An access token for the source's mailbox (IMAP takes it too).""" - return google_auth.access_token(cfg, credentials(cfg, src), scopes(src)) + return google_auth.access_token(cfg, credentials(cfg, src), scopes(src.get("params"))) class _Expired(Exception): diff --git a/fastapi_backend/app/domain/google_auth.py b/fastapi_backend/app/domain/google_auth.py index d4c6f4fac..8d8df46c5 100644 --- a/fastapi_backend/app/domain/google_auth.py +++ b/fastapi_backend/app/domain/google_auth.py @@ -251,9 +251,9 @@ def get(cfg, creds, scopes, url, params=None, api="api", what=None): def _module(src): - from . import feeds # feeds imports gcal, which imports this + from . import feeds, gmail # they import this - mod = feeds.OWN.get(src["type"]) + mod = feeds.OWN.get(src["type"]) or {"gmail": gmail}.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 diff --git a/fastapi_backend/tests/api/test_gmail_source.py b/fastapi_backend/tests/api/test_gmail_source.py index 59fbeebb0..abe7e73b3 100644 --- a/fastapi_backend/tests/api/test_gmail_source.py +++ b/fastapi_backend/tests/api/test_gmail_source.py @@ -5,6 +5,7 @@ from __future__ import annotations import json +import urllib.parse import pytest from cryptography.hazmat.primitives import serialization @@ -233,6 +234,44 @@ def test_a_connected_google_account(client, db, cfg, admin, google, monkeypatch) assert not h["ok"] and "Connect the Google account again" in h["error"] +def _connect(client, google, sid, headers, **consent): + """Connect with Google from the browser: Google's consent page, then its way back to Sources (the query).""" + r = client.post(f"/api/v1/sources/{sid}/google", headers=headers, json={}) + assert r.status_code == 200, r.text + url = r.json()["url"] + code, state = google.consent(url, **consent) + back = client.get("/api/v1/auth/external/google/callback", params={"code": code, "state": state}, follow_redirects=False) + assert back.status_code == 303, back.text + u = urllib.parse.urlsplit(back.headers["location"]) + assert u.path == "/sources" + return dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(url).query)), dict(urllib.parse.parse_qsl(u.query)) + + +def test_connecting_the_account_in_the_browser(client, db, cfg, admin, google): + sid = client.post("/api/v1/sources", headers=admin, json={"type": "gmail", "name": "My mail"}).json()["id"] + body = {"kind": "google", "client_id": google.client_id, "client_secret": google.client_secret, "enabled": False} + assert client.post("/api/v1/auth/providers", json=body, headers=admin).status_code == 200 + + # Google asks to read mail, read only; someone unticks the box there: not connected + asked, back = _connect(client, google, sid, admin, untick=[GMAIL_READ]) + assert asked["scope"].split() == ["openid", "email", GMAIL_READ] and asked["access_type"] == "offline" + assert "wasn't given access" in back["google_error"] and back["source"] == str(sid) + assert not sources.get(db, sid).get("sealed") + + # connected: the account's address is the source's user, its refresh token kept sealed + _, back = _connect(client, google, sid, admin) + assert back == {"google": "connected", "source": str(sid), "account": MARA} + src = client.get("/api/v1/sources", headers=admin).json()[0] + assert src["params"]["user"] == MARA and src["secrets"]["token"]["set"] and src["health"]["ok"] + assert json.loads(google_auth.secret(cfg, sources.get(db, sid), "token"))["client_id"] == google.client_id + + # read over IMAP instead: Google is asked for all of Gmail + r = client.patch(f"/api/v1/sources/{sid}", headers=admin, json={"params": {"via": "imap"}}) + assert r.json()["health"]["error"].endswith("connect the Google account again") + asked, back = _connect(client, google, sid, admin) + assert MAIL in asked["scope"].split() and back["google"] == "connected" + + def test_an_agent_gives_a_token_it_has(client, admin, google): google.refresh["1//agent"] = {"email": MARA, "scope": GMAIL_READ, "client_id": google.client_id} token = {"refresh_token": "1//agent", "client_id": google.client_id, "client_secret": google.client_secret} diff --git a/nextjs-frontend/__tests__/add-connection.test.tsx b/nextjs-frontend/__tests__/add-connection.test.tsx index f6bb1101f..0d2531a6e 100644 --- a/nextjs-frontend/__tests__/add-connection.test.tsx +++ b/nextjs-frontend/__tests__/add-connection.test.tsx @@ -30,6 +30,11 @@ const BACKENDS = { fields: { calendars: "", impersonate: "", days_back: "365", account: "" }, secrets: ["token", "service_account_credentials"], }, + gmail: { + label: "Gmail or Google Workspace", + fields: { user: "", via: "api" }, + secrets: ["token", "service_account_credentials"], + }, }; const CALLBACK = "https://lens.example/api/v1/auth/external/google/callback"; const CONSENT = "https://accounts.google.com/o/oauth2/v2/auth?client_id=c"; @@ -162,4 +167,27 @@ describe("connecting a Google account", () => { client_secret: "shh", }); }); + + it("connects Gmail the same way, and asks a service account whose mail it reads", async () => { + (Sources.googleClient as jest.Mock).mockImplementation(() => + ok({ origin: "sign-in", client_id: "c", redirect_uri: CALLBACK }), + ); + show(jest.fn()); + fireEvent.click(screen.getByRole("radio", { name: /Gmail/ })); + fireEvent.click(screen.getByRole("radio", { name: "Service account" })); + fireEvent.click(screen.getByRole("button", { name: "Test connection" })); + expect(await screen.findByText("Enter the address of the person whose mail Lens reads.")).toBeInTheDocument(); + expect(Sources.createSource).not.toHaveBeenCalled(); + + fireEvent.click(screen.getByRole("radio", { name: "Google account" })); + fireEvent.click(await screen.findByRole("button", { name: "Connect with Google" })); + await waitFor(() => expect(navigate.assign).toHaveBeenCalledWith(CONSENT)); + expect((Sources.createSource as jest.Mock).mock.calls[0][0].body).toEqual({ + type: "gmail", + name: "Gmail", + params: { user: "", via: "api" }, + secrets: {}, + }); + expect((Sources.connectGoogle as jest.Mock).mock.calls[0][0]).toMatchObject({ path: { sid: 7 }, body: {} }); + }); }); diff --git a/nextjs-frontend/__tests__/sources-model.test.ts b/nextjs-frontend/__tests__/sources-model.test.ts index a65e7483b..8aba1334a 100644 --- a/nextjs-frontend/__tests__/sources-model.test.ts +++ b/nextjs-frontend/__tests__/sources-model.test.ts @@ -3,6 +3,7 @@ import { confirms, emptyForm, fileKind, + gmailScope, healthOf, imapHost, nextScan, @@ -154,9 +155,14 @@ describe("connections", () => { const key = JSON.stringify({ type: "service_account", client_email: "lens@p.iam.gserviceaccount.com", + client_id: "1234567890", private_key: "K", }); - expect(parseServiceAccountKey(key)).toEqual({ ok: true, email: "lens@p.iam.gserviceaccount.com" }); + expect(parseServiceAccountKey(key)).toEqual({ + ok: true, + email: "lens@p.iam.gserviceaccount.com", + clientId: "1234567890", + }); expect(parseServiceAccountKey("nope").ok).toBe(false); expect(parseServiceAccountKey('{"access_token":"a"}').ok).toBe(false); @@ -269,6 +275,44 @@ describe("connections", () => { ); expect(suggestName("gcal", blank.params)).toBe("Google Calendar"); }); + it("signs Gmail in as a Google account or as a service account acting for one person", () => { + const GMAIL: BackendSpec = { + label: "Gmail or Google Workspace", + fields: { user: "", via: "api" }, + secrets: ["token", "service_account_credentials"], + }; + const blank = emptyForm(GMAIL); + expect(Object.keys(validateForm("gmail", GMAIL, blank, false))).toEqual(["token"]); // Connect with Google + expect(validateForm("gmail", GMAIL, blank, true, (k) => k === "token")).toEqual({}); + const account = '{"client_id":"c","client_secret":"s","refresh_token":"r"}'; + const pasted = { ...blank, params: { user: "mara@x.io", via: "imap" }, secrets: { token: account } }; + expect(validateForm("gmail", GMAIL, pasted, false)).toEqual({}); + expect(buildPayload(GMAIL, pasted, "gmail")).toEqual({ + params: { user: "mara@x.io", via: "imap" }, + secrets: { token: account, service_account_credentials: null }, + }); + const key = JSON.stringify({ + type: "service_account", + client_email: "lens@p.iam.gserviceaccount.com", + private_key: "K", + }); + const sa = { ...blank, driveAuth: "service_account" as const, secrets: { service_account_credentials: key } }; + expect(Object.keys(validateForm("gmail", GMAIL, sa, false))).toEqual(["user"]); + const acting = { ...sa, params: { ...sa.params, user: "mara@x.io" } }; + expect(validateForm("gmail", GMAIL, acting, false)).toEqual({}); + expect(buildPayload(GMAIL, acting, "gmail")).toEqual({ + params: { user: "mara@x.io", via: "api" }, + secrets: { service_account_credentials: key, token: null }, + }); + expect(gmailScope("imap")).toBe("https://mail.google.com/"); + expect(sourceSubtitle({ type: "gmail", params: { user: "mara@x.io", via: "api" } })).toBe( + "Gmail · mara@x.io · API", + ); + expect([suggestName("gmail", blank.params), suggestName("gmail", acting.params)]).toEqual([ + "Gmail", + "Gmail · mara@x.io", + ]); + }); it("confirms deletes by name, loosely", () => { expect(confirms(" dropbox · callrecorder", "Dropbox · CallRecorder")).toBe(true); expect(confirms("Dropbox", "Dropbox · CallRecorder")).toBe(false); diff --git a/nextjs-frontend/components/sources/connection-fields.tsx b/nextjs-frontend/components/sources/connection-fields.tsx index 370295399..ae6145e43 100644 --- a/nextjs-frontend/components/sources/connection-fields.tsx +++ b/nextjs-frontend/components/sources/connection-fields.tsx @@ -7,6 +7,7 @@ import { Sources } from "@/app/openapi-client"; import { DRIVE_SCOPES, FIELD_LABEL, + GMAIL_VIA, IMAP_SECURITY, S3_PROVIDERS, SIGNIN_SERVICE, @@ -15,6 +16,7 @@ import { type ConnForm, type SignInWith, type SourceType, + gmailScope, imapHost, parseServiceAccountKey, } from "@/components/sources/source-model"; @@ -61,7 +63,7 @@ export function ConnectionFields({ const googleClient = useQuery({ queryKey: ["google-client"], queryFn: () => data(Sources.googleClient({ client: api })), - enabled: type === "gcal", + enabled: type === "gcal" || type === "gmail", staleTime: 60_000, }); const editing = saved; @@ -232,10 +234,12 @@ export function ConnectionFields({ ); }; - /** Google Calendar as a Google account: Connect with Google (Google's consent screen), or an authorized user JSON - * pasted under the fold. */ + /** 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 = (() => { 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 ?? ""; @@ -243,9 +247,9 @@ export function ConnectionFields({
{connected && (

- {p.account ? ( + {account ? ( <> - Connected as {p.account}. + Connected as {account}. ) : ( "A Google account is connected." @@ -281,9 +285,9 @@ export function ConnectionFields({

- In Google Cloud › APIs & Services: enable the 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 {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.
@@ -325,7 +329,7 @@ export function ConnectionFields({ Or paste an authorized user JSON
{({ id, describedBy, invalid }) => ( @@ -350,6 +354,18 @@ export function ConnectionFields({ ); })(); + const googleAuth = ( + onChange({ ...form, driveAuth: v as ConnForm["driveAuth"] })} + items={[ + { value: "token", label: "Google account" }, + { value: "service_account", label: "Service account" }, + ]} + /> + ); + switch (type) { case "s3": return ( @@ -524,18 +540,48 @@ export function ConnectionFields({

); + case "gmail": { + const keyText = form.secrets.service_account_credentials; + const key = keyText ? parseServiceAccountKey(keyText) : null; + const clientId = key?.ok && key.clientId ? key.clientId : "its client ID"; + return ( +
+ {googleAuth} + {form.driveAuth === "service_account" ? ( + <> + {serviceAccountKey( + `A Workspace admin lets ${clientId} use ${gmailScope(p.via)} (Admin console › Security › API controls › Domain-wide delegation).`, + )} + {text("user", { + label: "Mailbox", + placeholder: "you@company.com", + hint: "The address of the person whose mail Lens reads", + })} + + ) : ( + <> + {googleAccount} + {form.secrets.token?.trim() && + text("user", { + label: "Address", + optional: p.via !== "imap", + placeholder: "you@gmail.com", + hint: "The account’s address, to read over IMAP", + })} + + )} + {select("via", GMAIL_VIA)} +

+ Labels are folders and each message becomes an email document, with its attachments. Lens only reads: mail + stays unread. +

+
+ ); + } case "gcal": return (
- onChange({ ...form, driveAuth: v as ConnForm["driveAuth"] })} - items={[ - { value: "token", label: "Google account" }, - { value: "service_account", label: "Service account" }, - ]} - /> + {googleAuth} {form.driveAuth === "service_account" ? ( <> {serviceAccountKey("Share the calendars Lens should read with that address, and enter their IDs below.")} diff --git a/nextjs-frontend/components/sources/source-model.ts b/nextjs-frontend/components/sources/source-model.ts index 02a9cacb6..d46de89d3 100644 --- a/nextjs-frontend/components/sources/source-model.ts +++ b/nextjs-frontend/components/sources/source-model.ts @@ -17,6 +17,7 @@ export const TYPE_ORDER: SourceType[] = [ "webdav", "local", "imap", + "gmail", "ical", "gcal", ]; @@ -79,6 +80,7 @@ export const FIELD_LABEL: Record = { security: "Security", calendars: "Calendars", days_back: "Days back", + via: "Read through", }; export const S3_PROVIDERS = [ @@ -105,6 +107,16 @@ export const IMAP_SECURITY = [ { value: "none", label: "None (not encrypted)" }, ]; +export const GMAIL_VIA = [ + { value: "api", label: "Gmail API (read only)" }, + { value: "imap", label: "IMAP (Google asks for all of Gmail)" }, +]; + +/** What a Gmail source reads with, the way it reads: what a Workspace admin allows a service account. */ +export function gmailScope(via: string | undefined): string { + return via === "imap" ? "https://mail.google.com/" : "https://www.googleapis.com/auth/gmail.readonly"; +} + const IMAP_HOSTS: Record = { "gmail.com": "imap.gmail.com", "googlemail.com": "imap.gmail.com", @@ -432,8 +444,8 @@ export type ConnForm = { sftpAuth: "pass" | "key_pem"; /** What was pasted from `rclone authorize` (OAuth types). */ tokenText: string; - /** Google Drive and Google Calendar sign in as a Google account (a token), or as a service account (its JSON key - * file). */ + /** 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. */ @@ -449,7 +461,10 @@ export const SIGNIN_SERVICE: Partial> = { onedrive: "Microsoft", }; -export type ServiceAccountKey = { ok: true; email: string } | { ok: false; error: string }; +/** The types that sign in as a Google account or as a service account. */ +export const GOOGLE: SourceType[] = ["drive", "gcal", "gmail"]; + +export type ServiceAccountKey = { ok: true; email: string; clientId: string } | { ok: false; error: string }; export type GoogleAccountCheck = { ok: true; json: string } | { ok: false; error: string } | { ok: null }; @@ -472,7 +487,7 @@ export function parseGoogleAccount(text: string): GoogleAccountCheck { /** A Google Cloud service account key file: what it signs in as, or why it isn't one. */ export function parseServiceAccountKey(text: string): ServiceAccountKey { - let o: { type?: unknown; client_email?: unknown; private_key?: unknown } | null; + let o: { type?: unknown; client_email?: unknown; client_id?: unknown; private_key?: unknown } | null; try { o = JSON.parse(text); } catch { @@ -489,7 +504,7 @@ export function parseServiceAccountKey(text: string): ServiceAccountKey { ok: false, error: "That isn’t a service account key. In Google Cloud: IAM › Service accounts › Keys › Add key › JSON.", }; - return { ok: true, email: o.client_email }; + return { ok: true, email: o.client_email, clientId: typeof o.client_id === "string" ? o.client_id : "" }; } export function emptyForm(spec: BackendSpec, source?: Pick | null): ConnForm { @@ -511,6 +526,7 @@ export function emptyForm(spec: BackendSpec, source?: Pick): string { + if (type === "gmail") return params.user?.trim() ? `Gmail · ${params.user.trim()}` : "Gmail"; const host = (params.host || params.url || "").replace(/^(https?|webcals?):\/\//i, "").split(/[/:]/)[0]; const extra = type === "s3" ? [params.provider !== "AWS" ? params.provider : "", params.region].filter(Boolean).join(" ") : host; @@ -534,7 +550,7 @@ export function validateForm( if (!str(form.params[k])) e[k] = msg; }; const secret = (k: string) => (form.secrets[k] === undefined ? editing && isSet(k) : Boolean(form.secrets[k])); - const serviceAccount = (type === "drive" || type === "gcal") && form.driveAuth === "service_account"; + const serviceAccount = GOOGLE.includes(type) && form.driveAuth === "service_account"; if (serviceAccount) { const k = form.secrets.service_account_credentials; const key = k ? parseServiceAccountKey(k) : null; @@ -570,12 +586,13 @@ export function validateForm( if (form.params.port && !/^\d{1,5}$/.test(form.params.port.trim())) e.port = "A port is a number, e.g. 993."; if (!secret("pass")) e.pass = "Enter the password (an app password, where the provider has them)."; } + if ((type === "gcal" || type === "gmail") && form.driveAuth === "token") { + const t = parseGoogleAccount(form.secrets.token ?? ""); + if (t.ok === false) e.token = t.error; + else if (t.ok === null && !secret("token")) e.token = "Connect a Google account first."; + } + if (type === "gmail" && serviceAccount) need("user", "Enter the address of the person whose mail Lens reads."); if (type === "gcal") { - if (form.driveAuth === "token") { - const t = parseGoogleAccount(form.secrets.token ?? ""); - if (t.ok === false) e.token = t.error; - else if (t.ok === null && !secret("token")) e.token = "Connect a Google account first."; - } if (form.params.days_back && !/^\d{1,5}$/.test(form.params.days_back.trim())) e.days_back = "A number of days, or empty for every event."; } @@ -601,9 +618,7 @@ export function buildPayload( const v = form.secrets[k]; const other = (type === "sftp" && k !== form.sftpAuth) || - ((type === "drive" || type === "gcal") && - k === "service_account_credentials" && - form.driveAuth !== "service_account"); + (GOOGLE.includes(type) && k === "service_account_credentials" && form.driveAuth !== "service_account"); if (other) { // Signing in one way drops the other secret only when it was explicitly cleared. if (v === "") secrets[k] = null; @@ -612,17 +627,17 @@ export function buildPayload( if (v === undefined) continue; secrets[k] = v === "" ? null : type === "sftp" && k === "key_pem" ? v.replace(/\r\n/g, "\n") : v; } - if (type === "gcal") { + if (type === "gcal" || type === "gmail") { const t = parseGoogleAccount(form.secrets.token ?? ""); if (form.driveAuth === "service_account") { if (secrets.service_account_credentials) { secrets.token = null; // a key file signs in before the account would - params.account = ""; + if (type === "gcal") params.account = ""; } } else if (t.ok) { secrets.token = t.json; secrets.service_account_credentials = null; - params.account = ""; // a pasted account doesn't say whose it is + if (type === "gcal") params.account = ""; // a pasted account doesn't say whose it is (Gmail's user is typed) } } else if (type === "drive" && form.driveAuth === "service_account") { if (secrets.service_account_credentials) secrets.token = null; // rclone would sign in as the account anyway diff --git a/nextjs-frontend/components/sources/sources-page.tsx b/nextjs-frontend/components/sources/sources-page.tsx index f7280d1aa..d03a2901b 100644 --- a/nextjs-frontend/components/sources/sources-page.tsx +++ b/nextjs-frontend/components/sources/sources-page.tsx @@ -202,7 +202,7 @@ export function SourcesPage() { const oauth = Boolean(s.oauth); return (
e.stopPropagation()}> - {bad && s.type === "gcal" && /Google account/.test(s.health?.error ?? "") ? ( + {bad && (s.type === "gcal" || s.type === "gmail") && /Google account/.test(s.health?.error ?? "") ? (