Skip to content

feat(sources): connect Google Drive by signing in with Google - #404

Closed
adeelahmad wants to merge 1 commit into
mainfrom
claude/project-thread-kkr74u
Closed

adeelahmad wants to merge 1 commit into
mainfrom
claude/project-thread-kkr74u

Conversation

@adeelahmad

Copy link
Copy Markdown
Owner

Requested by Adeel · project thread

Description

Before: connecting Google Drive with a Google account meant installing rclone on another computer, running rclone authorize drive, and pasting the JSON it printed into Lens.

After: Sensors › Add connection › Google Drive › Google account has Sign in with Google. Google's consent page opens in a small window; when it comes back the connection is made, tested and named after the account ("Google Drive · you@gmail.com"), and the dialog goes straight on to picking a folder whose files Lens imports (the existing watch flow). A connection whose sign-in ran out shows Sign in again.

It uses the admin's own Google app, the same one people can sign in to Lens with (Settings › Sign-in, login_provider:google), and the same callback (/api/v1/auth/external/google/callback), so nothing extra is registered at Google. If no Google app exists, the Drive dialog asks for its client ID and secret once, shows the redirect URI, and saves it with signing in to Lens left off. Pasting an rclone token and service accounts (#266) work as before.

How: a new source_oauth.py adds a mode: "source" flow to the existing external_flow rows (PKCE, ten minutes, used once, tied to the browser by the same cookie). The callback swaps the code (asking access_type=offline, prompt=consent), keeps the token sealed as the source's token in rclone's JSON shape, and records oauth: {provider, email} on the source; sources._config hands rclone that app's client_id/client_secret, because Google renews a token only for the app that issued it. A pasted token or a service account key drops oauth. The popup reports back over a same-site BroadcastChannel from a new public /connected page; where popups are blocked the whole page goes and comes back with ?connected=<id>.

Agents: POST /api/v1/sources/signin {type: "drive", params} (or {source: id}) returns {url}. Started from an API token it isn't tied to a browser, so an agent can hand the link to its person; it works once within ten minutes and ends on /connected. source_oauth.fresh_token(db, cfg, src) gives code that calls Google directly (the Gmail thread) a renewed access token.

Motivation and Context

Adeel asked for Google Drive "both oauth and via service account, with optional root folder, files get imported in Lens". Service accounts, the root folder ID and watched-folder import already existed; the OAuth path was the gap.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change; ! in the title)

Checklist:

  • The title is a Conventional Commit.
  • My code follows the code style of this project (ruff, mypy, eslint, prettier, tsc clean locally).
  • I added or updated tests: tests/api/test_source_signin.py (5, against the fake OIDC provider, now with refresh tokens), __tests__/drive-signin.test.tsx (3), sources-model.test.ts. Full backend suite 1088 passed; jest 870 passed.
  • My change requires documentation updates, and I have updated docs/ accordingly (processing.md Storage sources, authentication.md; mkdocs build --strict clean).
  • Users need more than the title to understand it, so I wrote it under ## Unreleased in CHANGELOG.md.

Done questions (AGENTS.md)

  • Goal: merges on green CI. Follow-ups for the rest of the ask are separate PRs (below).
  • Agents: POST /sources/signin returns a link an agent can hand to its person; POST /watches then imports.
  • Zero setup: no new prerequisite. Google requires the admin's own OAuth client; the dialog asks for it in place.
  • Fewest clicks: one button, then a folder; the Google app is two fields with the redirect URI shown; Enter saves.
  • Configurable: the Google app is editable in Settings › Sign-in; nothing new in the wizard.
  • Light: no new dependency (httpx, rclone as before).
  • Private and secure: tokens sealed per source; PKCE, single-use state, browser-bound cookie; threat model has the new entry point (an API-token-started link isn't browser-bound).
  • Cost: n/a, no model calls.
  • User decides: nothing connects without the person consenting at Google; signing in to Lens with Google stays off when the app is added from the Drive dialog; defaults unchanged.
  • Upgrades: n/a, one optional field (storage_source.oauth) that existing rows simply lack.
  • Honest: docs say what's merged; the Testing-mode 7-day sign-out is called out.
  • Tests, docs and generated files updated (make openapi).
  • Open standards: OAuth 2.0 with PKCE; no new dependencies.
  • Reported in the project thread; refine later: OneDrive could sign in through a Microsoft app the same way; a Drive folder picker for the root folder.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VrczAC5nKz1zkCUvRKXG5C


Generated by Claude Code

@adeelahmad adeelahmad self-assigned this Oct 15, 2026
@adeelahmad
adeelahmad force-pushed the claude/project-thread-kkr74u branch from 9fa99de to a9c3218 Compare October 15, 2026 12:10
Google Drive connections no longer need rclone run on another computer and a
token pasted back. Sign in with Google opens Google's consent page in a small
window; when it comes back the connection is made, tested and named after the
account, and the dialog goes on to pick a folder whose files Lens imports.

It uses the admin's own Google app, the one people can sign in to Lens with
(login_provider google), through the same external_flow rows and callback, so
nothing more is registered at Google and signing in to Lens can stay off. The
app can be set up from the Drive dialog. Tokens are sealed as the source's
token in rclone's shape, and rclone gets that app's client id and secret to
renew them. POST /sources/signin hands agents a link for their person (not
tied to a browser when started from an API token); source_oauth.fresh_token
gives code that calls Google itself a working access token.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VrczAC5nKz1zkCUvRKXG5C
@adeelahmad
adeelahmad force-pushed the claude/project-thread-kkr74u branch from a9c3218 to f4dab72 Compare October 15, 2026 12:17

Copy link
Copy Markdown
Owner Author

Closing as superseded. #407 adds the shared Connect with Google flow (google_auth.connect_start / connect_finish) that Calendar and Gmail use, so Drive will join that flow instead of adding a second one here (source_oauth.py).

The rest of this branch ships separately as smaller PRs:


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants