From 57fbf325fa01e4ae855559e3fca1bbccdb507767 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 12:30:02 +0000 Subject: [PATCH] feat(sources): sign in to Dropbox, OneDrive and Drive through rclone on the server Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01VrczAC5nKz1zkCUvRKXG5C --- CHANGELOG.md | 6 + docs/processing.md | 19 +- fastapi_backend/app/api/v1/routes/sources.py | 53 ++- fastapi_backend/app/domain/rclone_signin.py | 352 ++++++++++++++++++ fastapi_backend/app/domain/store.py | 3 + fastapi_backend/app/schemas/sources.py | 28 ++ .../tests/api/test_rclone_signin.py | 168 +++++++++ .../tests/fake_rclone_authorize.py | 93 +++++ .../__tests__/rclone-signin.test.tsx | 140 +++++++ .../__tests__/sources-model.test.ts | 8 +- nextjs-frontend/app/openapi-client/index.ts | 19 + nextjs-frontend/app/openapi-client/sdk.gen.ts | 65 ++++ .../app/openapi-client/types.gen.ts | 184 +++++++++ .../components/sources/connection-dialog.tsx | 143 +++++-- .../components/sources/connection-fields.tsx | 84 +++-- .../components/sources/rclone-signin.ts | 45 +++ .../components/sources/rclone-wait.tsx | 124 ++++++ .../components/sources/source-model.ts | 16 +- .../components/sources/sources-page.tsx | 13 +- nextjs-frontend/openapi.json | 276 ++++++++++++++ threat-model.json | 10 + threat-model.md | 1 + threat-model.yaml | 11 +- 23 files changed, 1789 insertions(+), 72 deletions(-) create mode 100644 fastapi_backend/app/domain/rclone_signin.py create mode 100644 fastapi_backend/tests/api/test_rclone_signin.py create mode 100755 fastapi_backend/tests/fake_rclone_authorize.py create mode 100644 nextjs-frontend/__tests__/rclone-signin.test.tsx create mode 100644 nextjs-frontend/components/sources/rclone-signin.ts create mode 100644 nextjs-frontend/components/sources/rclone-wait.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e48a9b1d..f92abd8a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ([Finding notes](docs/notes.md#finding-notes)). - **Paste a Drive folder's link.** A Google Drive connection's Root folder and shared drive take the link from the browser's address bar as well as the bare id, so limiting imports to one folder is a paste, not hunting for the id. +- **Sign in to Dropbox, OneDrive or Google Drive with no app of your own.** Their connections now have **Sign in + with Dropbox** (Microsoft, Google): the service's page opens in a new tab, Lens runs `rclone authorize` on its own + server, and you paste back the address the tab ends on, or nothing at all when the browser runs where Lens does. + No rclone to install on another computer, and no Google Cloud app needed for Drive. Pasting a token still works. + Agents do the same through `POST /api/v1/sources/rclone-signin` + ([Storage sources](docs/processing.md#storage-sources)). ## 0.10.0 October 15, 2026 {id="0.10.0"} diff --git a/docs/processing.md b/docs/processing.md index 07ee979c1..54dd2adb6 100644 --- a/docs/processing.md +++ b/docs/processing.md @@ -300,8 +300,23 @@ Admins can also import chosen files of a source once, without watching their fol files, then Import (`POST /api/v1/import/source`). The listing marks files that are assets already, and where. - Credentials are encrypted in the database (AES-GCM, key from `ARCHIVE_SECRET_KEY` or `data_dir/secret.key`) and given - to rclone in a private temporary config file per call. For Dropbox, Drive and OneDrive, paste the token from - `rclone authorize dropbox` (or drive, onedrive); tokens rclone refreshes are saved back. + to rclone in a private temporary config file per call; tokens rclone refreshes are saved back. +- **Dropbox, OneDrive, and Google Drive as a Google account sign in through rclone on the Lens server**, with nothing + to install elsewhere and no app of your own: Sign in with Dropbox (or Microsoft, Google) opens the service's page in + a new tab, which asks you to allow rclone, the tool Lens connects with. Afterwards the service sends that tab to + `http://127.0.0.1:53682/?state=…&code=…`, where `rclone authorize` waits on the server. When your browser runs on + the same machine as Lens that reaches rclone and the dialog carries on by itself; otherwise the page doesn't load: + copy its address from the address bar and paste it into the dialog. Lens passes only that address's state, code and + error on to rclone, and only when the state is this sign-in's. The connection is then made and tested (OneDrive's + own drive filled in), and the dialog goes on to pick a folder. One sign-in runs at a time (rclone listens on one + port), each for ten minutes. A connection whose token stopped working shows Sign in again. Pasting the token + `rclone authorize dropbox` (or drive, onedrive) prints on another computer still works: Paste a token from rclone + instead. +- Agents sign in through rclone the same way: `POST /api/v1/sources/rclone-signin` with `{"type": "dropbox"}` (or + `{"source": }`, and `params`) answers `{id, url, port}`; the person opens `url`, and the agent posts the address + 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 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/fastapi_backend/app/api/v1/routes/sources.py b/fastapi_backend/app/api/v1/routes/sources.py index 37b940541..9c64648a4 100644 --- a/fastapi_backend/app/api/v1/routes/sources.py +++ b/fastapi_backend/app/api/v1/routes/sources.py @@ -8,13 +8,17 @@ from fastapi import APIRouter, BackgroundTasks, HTTPException, Request from app.api.deps import Acl, AdminReader, AdminWriter, Cfg, CurrentUser, Db -from app.domain import auth, sources, store +from app.domain import auth, rclone_signin, sources, store from app.domain.store import DB from app.schemas.admin import Started from app.schemas.common import Created, Ok from app.schemas.sources import ( Backend, BrowseEntry, + RcloneSignIn, + RcloneSignInFinish, + RcloneSignInStart, + RcloneSignInState, RoutedMessage, RoutePreviewRequest, Source, @@ -80,6 +84,53 @@ def update_source(sid: int, body: SourceUpdate, user: AdminWriter, db: Db, cfg: return SourceUpdated(health=sources.test(db, cfg, sid)) +@router.post("/sources/rclone-signin") +def start_rclone_signin(body: RcloneSignInStart, user: AdminWriter, db: Db, cfg: Cfg) -> RcloneSignIn: + """Connect Google Drive (with no Google app set up here), Dropbox or OneDrive by signing in through rclone on this + server, or sign a connection in again (`source`). Open `url` in a browser and sign in; the service then sends the + browser to http://127.0.0.1:53682/?…, which only loads on the machine Lens runs on. Hand that address to + POST /sources/rclone-signin/{id}, which makes the connection. One at a time: this ends any other.""" + if body.source is None and body.type is None: + raise HTTPException(400, "say which type of connection to make, or which connection to sign in again") + try: + fid, url, port = rclone_signin.start(db, cfg, body.type, body.params, body.name, body.source, by=user.email) + except KeyError: + raise HTTPException(404, "not found") from None + except (ValueError, RuntimeError) as e: + raise HTTPException(400, str(e)) from None + row = db.one("SELECT expires_at FROM $r", r=store.R("rclone_signin", fid)) + return RcloneSignIn(id=fid, url=url, port=port, expires_at=row["expires_at"]) + + +@router.post("/sources/rclone-signin/{fid}") +def finish_rclone_signin(fid: str, body: RcloneSignInFinish, user: AdminWriter, db: Db, cfg: Cfg) -> RcloneSignInState: + """Finish signing in through rclone with the address the browser ended on (`address`), or, without one, look + whether rclone already got it. Answers the connection, made and tested, or none while it waits. 400: that address + isn't this sign-in's, and it's still waiting for the right one; 410: it didn't work (cancelled, refused, ran out), + and it's over; start again. Audited as `source.signin`.""" + try: + sid = rclone_signin.finish(db, cfg, fid, body.address) + except KeyError: + raise HTTPException(404, "no such sign-in: it finished, ran out or was cancelled") from None + except rclone_signin.WrongAddress as e: + raise HTTPException(400, str(e)) from None + except ValueError as e: + raise HTTPException(410, str(e)) from None + if sid is not None: + auth.audit(db, user.as_audit(), "source.signin", f"storage_source:{sid}", ["rclone"]) + return RcloneSignInState(connected=sid) + + +@router.delete("/sources/rclone-signin/{fid}") +def cancel_rclone_signin(fid: str, user: AdminWriter, db: Db) -> Ok: + """Stop a sign-in through rclone.""" + try: + rclone_signin.cancel(db, fid) + except KeyError: + raise HTTPException(404, "no such sign-in") from None + return Ok() + + @router.delete("/sources/{sid}") def delete_source(sid: int, user: AdminWriter, db: Db) -> Ok: """Remove a source and its watched folders.""" diff --git a/fastapi_backend/app/domain/rclone_signin.py b/fastapi_backend/app/domain/rclone_signin.py new file mode 100644 index 000000000..64ba790b5 --- /dev/null +++ b/fastapi_backend/app/domain/rclone_signin.py @@ -0,0 +1,352 @@ +"""Signing a storage source in through rclone on this server, with rclone's own app for the service: Google Drive (as a +Google account), Dropbox and OneDrive. Nothing to install or run on another computer, and no app of your own. + +Lens runs `rclone authorize `, which waits on 127.0.0.1:53682 for the service to send the browser back, and hands +the person the service's sign-in page. After they sign in, the service sends their browser to +http://127.0.0.1:53682/?state=…&code=…. Where the browser runs on the same machine as Lens (a native install) that is +rclone itself, which takes it from there. Anywhere else the page doesn't load, and the person pastes its address into +Lens, which passes it on to rclone here: only its state, code and error, only to rclone's own address, and only when the +state is this sign-in's. + +rclone swaps the code for a token with its own app, prints it and ends. Lens keeps the token sealed as the source's, as +if it had been pasted from `rclone authorize` on another computer, and rclone refreshes it the same way. The service's +page asks the person to allow rclone, not Lens. + +rclone listens on one fixed address, so a server runs one of these sign-ins at a time: starting one ends the one +before. Each lasts ten minutes; rclone is stopped then, or once it's finished or cancelled. +""" + +from __future__ import annotations + +import base64 +import contextlib +import datetime as dt +import json +import os +import re +import secrets +import shutil +import signal +import subprocess +import tempfile +import threading +import time +import urllib.parse + +import httpx + +from . import auth, sources, store + +R = store.R +MINUTES = 10 +HOSTS = ("127.0.0.1", "localhost") # where the service sends the browser back (OneDrive's app says localhost) +LINK = re.compile(r"http://127\.0\.0\.1:(\d+)/auth\?state=([\w-]+)") +EXCHANGE = 45 # seconds for rclone to swap a code for a token at the service +GRAPH_DRIVE = "https://graph.microsoft.com/v1.0/me/drive" # OneDrive: which drive is the account's own +_running: dict[str, subprocess.Popen] = {} # the rclone each sign-in started in this process runs, by sign-in id + + +def _later(minutes): + return (dt.datetime.now(dt.timezone.utc) + dt.timedelta(minutes=minutes)).isoformat(timespec="seconds") + + +def _blob(typ, params): + """The options rclone's sign-in asks the service for (Drive's scope), as rclone reads them on its command line.""" + opts = {"scope": params["scope"]} if typ == "drive" and params.get("scope") else {} + if not opts: + return [] + return [base64.b64encode(json.dumps(opts, separators=(",", ":")).encode()).decode().rstrip("=")] + + +def _read(path): + try: + with open(path, encoding="utf-8", errors="replace") as f: + return f.read() + except OSError: + return "" + + +def _alive(row): + proc = _running.get(row["id"]) + if proc is not None: + return proc.poll() is None + try: + os.kill(row["pid"], 0) + except (ProcessLookupError, PermissionError): + return False + return _is_rclone(row["pid"]) + + +def _is_rclone(pid): + """Whether `pid` is still an rclone sign-in (not a process that got its number since), where the system says.""" + try: + with open(f"/proc/{pid}/cmdline", "rb") as f: + return b"authorize" in f.read() + except OSError: + return os.name != "posix" or not os.path.isdir("/proc") # no /proc (macOS): trust the number + + +def _stop(row): + """End a sign-in's rclone and remove what it wrote.""" + proc = _running.pop(row["id"], None) + if proc is not None: + if proc.poll() is None: + proc.kill() + with contextlib.suppress(subprocess.TimeoutExpired): + proc.wait(5) + elif row.get("pid") and _alive(row): + with contextlib.suppress(OSError): + os.kill(row["pid"], signal.SIGTERM) + if row.get("dir", "").startswith(tempfile.gettempdir()): + shutil.rmtree(row["dir"], ignore_errors=True) + + +def _rows(db): + return db.rows("SELECT record::id(id) AS id, * FROM rclone_signin") + + +def cancel(db, fid): + """End sign-in `fid` (rclone stops waiting). KeyError when there's no such sign-in.""" + rows = db.rows("DELETE $r RETURN BEFORE", r=R("rclone_signin", fid)) + if not rows: + raise KeyError(fid) + _stop({**rows[0], "id": fid}) + + +def cancel_all(db): + for row in _rows(db): + db.q("DELETE $r", r=R("rclone_signin", row["id"])) + _stop(row) + + +def _expire(fid): + proc = _running.pop(fid, None) + if proc is not None and proc.poll() is None: + proc.kill() + with contextlib.suppress(subprocess.TimeoutExpired): + proc.wait(5) + + +def start(db, cfg, typ=None, params=None, name=None, sid=None, by=None): + """Start signing in through rclone, for a new source of type `typ` (with `params`, named `name`) or for signing + source `sid` in again: returns (this sign-in's id, the service's sign-in page, the port rclone waits on). Ends any + other one running. ValueError when the type doesn't sign in in the browser; KeyError for no such source; + RuntimeError when rclone can't run here.""" + if sid is not None: + src = sources.get(db, sid) + typ, params = src["type"], {**(src.get("params") or {}), **(params or {})} + spec = sources.BACKENDS.get(typ or "") + if not spec or not spec.get("oauth"): + raise ValueError(f"{spec['label'] if spec else typ} doesn't sign in in the browser") + sources._check_params(typ, params, None) + params = sources.tidy(typ, {**spec["fields"], **(params or {})}) + binary = sources._bin(cfg) + cancel_all(db) # rclone listens on one address + d = tempfile.mkdtemp(prefix="lens-signin-") + out, err = os.path.join(d, "out"), os.path.join(d, "err") + with open(out, "wb") as o, open(err, "wb") as e: + proc = subprocess.Popen( + # a config of its own that doesn't exist: nothing from the server's rclone setup comes into it + [binary, "--config", os.path.join(d, "rclone.conf"), "authorize", typ, *_blob(typ, params), "--auth-no-open-browser"], + stdin=subprocess.DEVNULL, + stdout=o, + stderr=e, + start_new_session=True, # not stopped with a request, and not left behind as part of one + ) + fid = secrets.token_urlsafe(12) + _running[fid] = proc + timer = threading.Timer(MINUTES * 60, _expire, (fid,)) + timer.daemon = True + timer.start() + deadline = time.monotonic() + 15 + while not (m := LINK.search(_read(err))): + if proc.poll() is not None or time.monotonic() > deadline: + text = _read(err) + _stop({"id": fid, "dir": d}) + if "address already in use" in text: + raise RuntimeError("something else on this server is using rclone's sign-in address (port 53682); try again later") + raise RuntimeError(f"rclone couldn't start signing in: {sources._clean_err(text) or 'no answer'}") + time.sleep(0.1) + port, state = int(m[1]), m[2] + try: # rclone's own page sends the browser on to the service's, with rclone's app and this sign-in's state + r = httpx.get(f"http://127.0.0.1:{port}/auth", params={"state": state}, follow_redirects=False, timeout=10, trust_env=False) + url = r.headers.get("location") or "" + except httpx.HTTPError: + url = "" + if not url.startswith("https://"): + _stop({"id": fid, "dir": d}) + raise RuntimeError("rclone didn't give a sign-in page") + db.q( + "CREATE $r CONTENT $d", + r=R("rclone_signin", fid), + d=store.clean( + { + "type": typ, + "params": params, + "name": (name or "").strip()[:80] or None, + "source": sid, + "by": by, + "state": auth.sha(state), + "port": port, + "pid": proc.pid, + "dir": d, + "expires_at": _later(MINUTES), + } + ), + ) + return fid, url, port + + +def _token(text): + """The token rclone printed between its arrows: the token itself, or (when it was given options) its options with + the token in them, base64-encoded. None when it hasn't printed one.""" + m = re.search(r"--->\s*\n(.*?)\n\s*<---End paste", text, re.S) + if not m: + return None + body = "".join(m[1].split()) + try: + tok = body if body.startswith("{") else json.loads(base64.b64decode(body + "=" * (-len(body) % 4)))["token"] + if json.loads(tok).get("access_token"): + return tok + except (ValueError, KeyError, TypeError, AttributeError): + pass + raise ValueError("rclone printed something that isn't a token; start again") + + +def _why(text): + """Why rclone stopped, from what it printed: its last fatal error (with the service's description, if any).""" + tail = text.rsplit("Fatal error:", 1)[1] if "Fatal error:" in text else sources._clean_err(text) + if "access_denied" in tail: + return "signing in didn't finish: you cancelled" + m = re.search(r"^Description:\s*(\S.*)$", tail, re.M) + msg = m[1] if m else next((l.strip() for l in tail.splitlines() if l.strip()), "") + return f"signing in didn't finish: {msg.replace('config failed to refresh token: ', '')[:300] or 'rclone stopped'}" + + +class WrongAddress(ValueError): + """An address that isn't this sign-in's to finish: the sign-in goes on waiting for the right one.""" + + +def _forward(row, address): + """Hand the address the browser ended on to rclone, which is waiting for it here.""" + u = urllib.parse.urlsplit(address.strip()) + q = dict(urllib.parse.parse_qsl(u.query)) + try: + port = u.port + except ValueError: + port = None + if ( + u.scheme != "http" + or u.hostname not in HOSTS + or port != row["port"] + or u.path not in ("", "/") + or not (q.get("code") or q.get("error")) + ): + raise WrongAddress(f"paste the whole address the browser ended on: it starts with http://127.0.0.1:{row['port']}/?") + if auth.sha(q.get("state", "")) != row["state"]: + raise WrongAddress("that address is from another sign-in; paste the one this sign-in ended on") + keep = {k: q[k] for k in ("state", "code", "scope", "error", "error_description") if k in q} + with contextlib.suppress(httpx.HTTPError): # rclone stops listening once it has the code, at times before it answers + httpx.get(f"http://127.0.0.1:{row['port']}/", params=keep, timeout=EXCHANGE, trust_env=False) + + +def _wait(row, seconds): + """The token rclone printed, waiting up to `seconds` for it; None when it's still waiting for the browser.""" + out, err = os.path.join(row["dir"], "out"), os.path.join(row["dir"], "err") + deadline = time.monotonic() + seconds + while (tok := _token(_read(out))) is None: + if not _alive(row): + time.sleep(0.2) # its last words + if (tok := _token(_read(out))) is not None: + break + raise ValueError(_why(_read(err))) + if time.monotonic() >= deadline: + if seconds: + raise ValueError("the service didn't answer rclone in time; start again") + return None + time.sleep(0.2) + return tok + + +def _row(db, fid): + row = db.one("SELECT record::id(id) AS id, * FROM $r", r=R("rclone_signin", fid)) + if not row: + raise KeyError(fid) + return row + + +def _made(db, fid): + """The connection another request is making from this sign-in's token, once it's made.""" + deadline = time.monotonic() + EXCHANGE + while (sid := _row(db, fid).get("connected")) is None: + if time.monotonic() > deadline: + raise ValueError("connecting took too long; look for it in the list, or start again") + time.sleep(0.2) + return sid + + +def finish(db, cfg, fid, address=None): + """Finish sign-in `fid`: with `address` (the one the browser ended on), hand it to rclone and wait for the token; + without, only look whether rclone already has one (the browser reached it itself). Connects the source and returns + its id (again, if asked again), or None while it's still waiting. KeyError for no such sign-in. ValueError says why + it didn't work, and the sign-in is over then; WrongAddress (a ValueError) leaves it waiting for the right address.""" + row = _row(db, fid) + if row.get("connected") is not None: + return row["connected"] + try: + if (row.get("expires_at") or "") < store.now(): + raise ValueError("that sign-in ran out after ten minutes; start again") + if address and _token(_read(os.path.join(row["dir"], "out"))) is None: + _forward(row, address) + tok = _wait(row, EXCHANGE if address else 0) + except WrongAddress: + raise + except ValueError: + with contextlib.suppress(KeyError): + cancel(db, fid) + raise + if tok is None: + return None + # one request makes the connection (the page asks while a pasted address is on its way); any other gets its id + if not db.rows("UPDATE $r SET taken = true WHERE taken IS NONE RETURN AFTER", r=R("rclone_signin", fid)): + return _made(db, fid) + try: + sid = _connect(db, cfg, row, tok) + except ValueError: + with contextlib.suppress(KeyError): + cancel(db, fid) + raise + db.q("UPDATE $r SET connected = $s", r=R("rclone_signin", fid), s=sid) + _stop(row) # rclone is done; the sign-in stays, saying what it made, until the next one starts + return sid + + +def _my_drive(token): + """The signed-in account's own OneDrive ({drive_id, drive_type}), which rclone needs named; {} when Microsoft + won't say (rclone then asks for it on the connection's test).""" + try: + r = httpx.get(GRAPH_DRIVE, headers={"Authorization": f"Bearer {json.loads(token)['access_token']}"}, timeout=15) + d = r.json() + except (httpx.HTTPError, ValueError, KeyError): + return {} + if r.status_code != 200 or not isinstance(d, dict) or not d.get("id"): + return {} + return {"drive_id": str(d["id"]), "drive_type": str(d.get("driveType") or "personal")} + + +def _connect(db, cfg, row, token): + typ = row["type"] + spec = sources.BACKENDS[typ] + if typ == "onedrive" and not (row.get("params") or {}).get("drive_id"): + row = {**row, "params": {**(row.get("params") or {}), **_my_drive(token)}} + if row.get("source") is not None: + sid = row["source"] + others = {k: None for k in spec["secrets"] if k != "token"} # signed in: no service account + try: + sources.update(db, cfg, sid, params=row.get("params"), secret_values={"token": token, **others}) + except KeyError: + raise ValueError("that connection was deleted while you were signing in") from None + else: + sid = sources.create(db, cfg, row.get("name") or spec["label"], typ, row.get("params"), {"token": token}, row.get("by")) + sources.test(db, cfg, sid) + return sid diff --git a/fastapi_backend/app/domain/store.py b/fastapi_backend/app/domain/store.py index 669133df4..cdc1ee8be 100644 --- a/fastapi_backend/app/domain/store.py +++ b/fastapi_backend/app/domain/store.py @@ -1258,6 +1258,9 @@ def close(self): "DEFINE TABLE IF NOT EXISTS worker SCHEMALESS", # storage "DEFINE TABLE IF NOT EXISTS storage_source SCHEMALESS", + # a sign-in through rclone on this server, waiting for the browser to come back (domain/rclone_signin.py): + # rclone_signin:, one at a time, for ten minutes + "DEFINE TABLE IF NOT EXISTS rclone_signin SCHEMALESS", "DEFINE TABLE IF NOT EXISTS watch_path SCHEMALESS", "DEFINE INDEX IF NOT EXISTS watch_path_source ON watch_path FIELDS source", "DEFINE TABLE IF NOT EXISTS remote_file SCHEMALESS", diff --git a/fastapi_backend/app/schemas/sources.py b/fastapi_backend/app/schemas/sources.py index 57c22d592..cd6907c59 100644 --- a/fastapi_backend/app/schemas/sources.py +++ b/fastapi_backend/app/schemas/sources.py @@ -65,6 +65,34 @@ class SourceUpdate(RequestModel): secrets: dict[str, str | None] | None = Field(default=None, description="null or empty removes a secret") +class RcloneSignInStart(RequestModel): + type: SourceType | None = Field( + default=None, description="the kind of connection to make (drive, dropbox, onedrive); leave out with `source`" + ) + source: int | None = Field(default=None, description="a connection to sign in again, instead of making a new one") + name: str | None = Field(default=None, description="the new connection's name; by default the service's") + params: dict[str, Any] | None = Field(default=None, description="its options, as for POST /sources (scope, root_folder_id...)") + + +class RcloneSignIn(ResponseModel): + id: str = Field(description="this sign-in, to finish or cancel") + url: str = Field(description="the service's sign-in page, to open in a browser (it asks to allow rclone); once, within ten minutes") + port: int = Field(description="where the service sends the browser back afterwards: http://127.0.0.1:/?state=…&code=…") + expires_at: str + + +class RcloneSignInFinish(RequestModel): + address: str | None = Field( + default=None, + description="the address the browser ended on after signing in (http://127.0.0.1:53682/?state=…&code=…); leave " + "out to only look whether rclone already got it (a browser on the machine Lens runs on reaches it itself)", + ) + + +class RcloneSignInState(ResponseModel): + connected: int | None = Field(default=None, description="the connection, made or signed in again, and tested; none while it waits") + + class SourceCreated(Created): health: SourceHealth diff --git a/fastapi_backend/tests/api/test_rclone_signin.py b/fastapi_backend/tests/api/test_rclone_signin.py new file mode 100644 index 000000000..03cf85c8b --- /dev/null +++ b/fastapi_backend/tests/api/test_rclone_signin.py @@ -0,0 +1,168 @@ +"""Connecting Drive (with no Google app), Dropbox and OneDrive by signing in through rclone on the server +(rclone_signin.py), against a stand-in for `rclone authorize` (tests/fake_rclone_authorize.py).""" + +from __future__ import annotations + +import json +import pathlib +import stat +import sys +import urllib.parse + +import httpx +import pytest + +from app.domain import rclone_signin, sources +from tests.helpers import login, make_user + +FAKE = pathlib.Path(__file__).resolve().parent.parent / "fake_rclone_authorize.py" + + +@pytest.fixture +def rclone(tmp_path, monkeypatch, db): + exe = tmp_path / "rclone" + exe.write_text(f'#!/bin/sh\nexec {sys.executable} {FAKE} "$@"\n') + exe.chmod(exe.stat().st_mode | stat.S_IXUSR) + monkeypatch.setenv("RCLONE_BINARY", str(exe)) + monkeypatch.setattr(sources, "test", lambda *a, **k: {"ok": True}) # no service to list in tests + monkeypatch.setattr(sources, "obscure", lambda cfg, v: v) + yield exe + rclone_signin.cancel_all(db) + + +@pytest.fixture +def admin(client, db): + make_user(db, "root@x.io", "root password 1", admin=True) + return login(client, "root@x.io", "root password 1") + + +def _back(r, code="good", **more): + """The address the service sends the browser back to, as the person pastes it.""" + q = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(r["url"]).query)) + back = {"state": q["state"], **({"code": code} if code else {}), "scope": q.get("scope", ""), **more} + return f"http://127.0.0.1:{r['port']}/?{urllib.parse.urlencode(back)}" + + +def _token(cfg, db, sid): + _, conf = sources._config(cfg, sources.get(db, sid)) + return json.loads(next(l for l in conf.splitlines() if l.startswith("token = ")).split(" = ", 1)[1]), conf + + +def test_drive_signs_in_through_rclone_with_no_google_app(client, db, cfg, admin, rclone): + body = {"type": "drive", "params": {"scope": "drive.readonly", "root_folder_id": "https://drive.google.com/drive/folders/1AbC"}} + r = client.post("/api/v1/sources/rclone-signin", json=body, headers=admin) + assert r.status_code == 200, r.text + start = r.json() + page = urllib.parse.urlsplit(start["url"]) + q = dict(urllib.parse.parse_qsl(page.query)) + assert page.scheme == "https" and q["client_id"] == "rclone-drive" and q["scope"] == "drive.readonly" # rclone's app, our scope + # nothing yet: rclone is waiting for the browser + wait = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={}, headers=admin) + assert wait.status_code == 200 and wait.json() == {"connected": None} + # an address that isn't this sign-in's leaves it waiting + for bad in ("http://evil.example/?code=good", f"http://127.0.0.1:{start['port']}/?state=nope&code=good", "not a link"): + r = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": bad}, headers=admin) + assert r.status_code == 400 and "paste" in r.json()["detail"], r.text + r = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": _back(start)}, headers=admin) + assert r.status_code == 200, r.text + sid = r.json()["connected"] + s = next(s for s in client.get("/api/v1/sources", headers=admin).json() if s["id"] == sid) + assert s["name"] == "Google Drive" + assert s["params"]["root_folder_id"] == "1AbC" + tok, conf = _token(cfg, db, sid) + assert tok["refresh_token"] == "1//fake" and "client_id" not in conf + assert "1//fake" not in json.dumps(db.rows("SELECT * FROM storage_source")) # sealed + assert "source.signin" in db.values("SELECT VALUE action FROM audit_log") + # it's over: rclone has stopped, and asking again answers the same connection (the page may ask as it's made) + with pytest.raises(httpx.HTTPError): + httpx.get(f"http://127.0.0.1:{start['port']}/", timeout=2, trust_env=False) + for again in ({}, {"address": _back(start)}): + assert client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json=again, headers=admin).json() == {"connected": sid} + assert len(client.get("/api/v1/sources", headers=admin).json()) == 1 + # the next sign-in clears it away + client.post("/api/v1/sources/rclone-signin", json={"type": "dropbox"}, headers=admin) + assert client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={}, headers=admin).status_code == 404 + + +def test_a_browser_on_the_server_reaches_rclone_itself(client, db, cfg, admin, rclone): + """A native install: the browser runs where Lens does, so the service's redirect reaches rclone and nothing is pasted.""" + start = client.post("/api/v1/sources/rclone-signin", json={"type": "dropbox", "name": "Team Dropbox"}, headers=admin).json() + httpx.get(_back(start), timeout=10, trust_env=False) + for _ in range(50): + r = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={}, headers=admin).json() + if r["connected"]: + break + s = client.get("/api/v1/sources", headers=admin).json() + assert r["connected"] == s[0]["id"] and s[0]["name"] == "Team Dropbox" and s[0]["type"] == "dropbox" + assert _token(cfg, db, s[0]["id"])[0]["access_token"] == "ya29.fake" # printed bare: no options were passed + + +def test_cancelled_failed_and_replaced_sign_ins(client, db, admin, rclone): + start = client.post("/api/v1/sources/rclone-signin", json={"type": "onedrive"}, headers=admin).json() + r = client.post( + f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": _back(start, code="", error="access_denied")}, headers=admin + ) + assert r.status_code == 410 and r.json()["detail"] == "signing in didn't finish: you cancelled" + assert db.rows("SELECT * FROM rclone_signin") == [] + # a code the service refuses + start = client.post("/api/v1/sources/rclone-signin", json={"type": "onedrive"}, headers=admin).json() + r = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": _back(start, code="stale")}, headers=admin) + assert r.status_code == 410 and "invalid_grant" in r.json()["detail"] + # starting another ends the one before: rclone has one address to listen on + first = client.post("/api/v1/sources/rclone-signin", json={"type": "dropbox"}, headers=admin).json() + second = client.post("/api/v1/sources/rclone-signin", json={"type": "dropbox"}, headers=admin).json() + assert client.post(f"/api/v1/sources/rclone-signin/{first['id']}", json={}, headers=admin).status_code == 404 + assert client.delete(f"/api/v1/sources/rclone-signin/{second['id']}", headers=admin).status_code == 200 + assert client.post(f"/api/v1/sources/rclone-signin/{second['id']}", json={"address": _back(second)}, headers=admin).status_code == 404 + # ten minutes on, it has run out + third = client.post("/api/v1/sources/rclone-signin", json={"type": "dropbox"}, headers=admin).json() + db.q("UPDATE rclone_signin SET expires_at = '2000-01-01T00:00:00+00:00'") + r = client.post(f"/api/v1/sources/rclone-signin/{third['id']}", json={"address": _back(third)}, headers=admin) + assert r.status_code == 410 and "ran out" in r.json()["detail"] + assert client.get("/api/v1/sources", headers=admin).json() == [] + # types that don't sign in in the browser, and nothing said + assert client.post("/api/v1/sources/rclone-signin", json={"type": "s3"}, headers=admin).status_code == 400 + assert client.post("/api/v1/sources/rclone-signin", json={}, headers=admin).status_code == 400 + assert client.post("/api/v1/sources/rclone-signin", json={"source": 999}, headers=admin).status_code == 404 + + +def test_signing_in_again_and_agents(client, db, cfg, admin, rclone): + key = {"type": "service_account", "private_key": "k", "client_email": "lens@x.iam.gserviceaccount.com"} + body = {"name": "Team drive", "type": "drive", "secrets": {"service_account_credentials": json.dumps(key)}} + sid = client.post("/api/v1/sources", json=body, headers=admin).json()["id"] + # an agent with a write token starts it and hands its person the link, then posts the address they paste back + token = client.post("/api/v1/tokens", json={"name": "agent", "scope": "write"}, headers=admin).json()["token"] + agent = {"Authorization": f"Bearer {token}"} + start = client.post("/api/v1/sources/rclone-signin", json={"source": sid, "params": {"scope": "drive"}}, headers=agent).json() + assert dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(start["url"]).query))["scope"] == "drive" + r = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": _back(start)}, headers=agent) + assert r.json() == {"connected": sid} + s = client.get("/api/v1/sources", headers=admin).json()[0] + assert s["name"] == "Team drive" and s["params"]["scope"] == "drive" + assert s["secrets"]["token"]["set"] and not s["secrets"]["service_account_credentials"]["set"] # signed in: no service account + assert _token(cfg, db, sid)[0]["refresh_token"] == "1//fake" # rclone's encoded answer, decoded + # a read-only token can't + ro = client.post("/api/v1/tokens", json={"name": "ro", "scope": "read"}, headers=admin).json()["token"] + assert ( + client.post("/api/v1/sources/rclone-signin", json={"type": "drive"}, headers={"Authorization": f"Bearer {ro}"}).status_code == 403 + ) + + +def test_without_rclone_it_says_so(client, db, admin, monkeypatch): + monkeypatch.setenv("RCLONE_BINARY", "") + monkeypatch.setattr(sources.shutil, "which", lambda *a: None) + r = client.post("/api/v1/sources/rclone-signin", json={"type": "dropbox"}, headers=admin) + assert r.status_code == 400 and "rclone isn't installed" in r.json()["detail"] + + +def test_onedrive_finds_the_accounts_own_drive(client, db, admin, rclone, monkeypatch): + """rclone needs OneDrive's drive named; signing in fills it in from Microsoft, unless it was given.""" + asked = [] + monkeypatch.setattr(rclone_signin, "_my_drive", lambda token: asked.append(token) or {"drive_id": "b!abc", "drive_type": "personal"}) + start = client.post("/api/v1/sources/rclone-signin", json={"type": "onedrive"}, headers=admin).json() + sid = client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": _back(start)}, headers=admin).json()["connected"] + assert sources.get(db, sid)["params"] == {"drive_id": "b!abc", "drive_type": "personal"} + assert json.loads(asked[0])["access_token"] == "ya29.fake" + start = client.post("/api/v1/sources/rclone-signin", json={"source": sid, "params": {"drive_id": "b!other"}}, headers=admin).json() + client.post(f"/api/v1/sources/rclone-signin/{start['id']}", json={"address": _back(start)}, headers=admin) + assert sources.get(db, sid)["params"]["drive_id"] == "b!other" and len(asked) == 1 diff --git a/fastapi_backend/tests/fake_rclone_authorize.py b/fastapi_backend/tests/fake_rclone_authorize.py new file mode 100755 index 000000000..4a14c2b9d --- /dev/null +++ b/fastapi_backend/tests/fake_rclone_authorize.py @@ -0,0 +1,93 @@ +"""A stand-in for `rclone authorize [options] --auth-no-open-browser` (rclone_signin.py's tests): it prints the +link to its page, sends that page on to the service, and waits for the browser to come back with a code. Code "good" +gets a token, printed between rclone's arrows (encoded with the options, as rclone does when given some); anything +else, or an error, ends it as rclone would. It listens on a free port rather than rclone's own, and says which. + +Run as a program (RCLONE_BINARY points tests at it); it imports nothing from Lens. +""" + +from __future__ import annotations + +import base64 +import http.server +import json +import secrets +import sys +import urllib.parse + +SERVICE = "https://accounts.example.test/o/oauth2/auth" + + +def main(argv): + if argv[:1] == ["--config"]: + argv = argv[2:] + args = [a for a in argv if not a.startswith("--")] + if args[:1] != ["authorize"] or len(args) < 2: + print("Fatal error: unknown command", file=sys.stderr) + return 1 + typ, blob = args[1], (args[2] if len(args) > 2 else "") + opts = json.loads(base64.b64decode(blob + "=" * (-len(blob) % 4))) if blob else {} + state = secrets.token_urlsafe(16) + done: dict = {} + + class Page(http.server.BaseHTTPRequestHandler): + def log_message(self, *a): + pass + + def _answer(self, status, text, location=None): + self.send_response(status) + if location: + self.send_header("Location", location) + self.send_header("Content-Type", "text/html") + self.end_headers() + self.wfile.write(text.encode()) + + def do_GET(self): + u = urllib.parse.urlsplit(self.path) + q = dict(urllib.parse.parse_qsl(u.query)) + port = self.server.server_address[1] + if u.path == "/auth": + if q.get("state") != state: + return self._answer(403, "Forbidden") + there = {"client_id": f"rclone-{typ}", "redirect_uri": f"http://127.0.0.1:{port}/", "state": state, **opts} + return self._answer(307, "", SERVICE + "?" + urllib.parse.urlencode(there)) + if u.path != "/": + return self._answer(404, "Not found") + if not q.get("code"): + why = "No code returned by remote server: " + q.get("error", "") + done["error"] = f'config failed to refresh token: Error: Auth Error\nCode: ""\nDescription: {why}\nHelp: ' + return self._answer(400, "Failure!") + if q.get("state") != state: + done["error"] = "Auth state doesn't match" + return self._answer(400, "Failure!") + self._answer(200, "Success!") + if q["code"] != "good": + done["error"] = 'config failed to refresh token: failed to get token: oauth2: "invalid_grant" "Bad Request"' + return + tok = {"access_token": "ya29.fake", "token_type": "Bearer", "refresh_token": "1//fake", "expiry": "2030-01-01T00:00:00Z"} + text = json.dumps(tok) + if blob: + text = base64.b64encode(json.dumps({"token": text}).encode()).decode().rstrip("=") + done["token"] = text + + server = http.server.HTTPServer(("127.0.0.1", 0), Page) + port = server.server_address[1] + for line in ( + f'Make sure your Redirect URL is set to "http://127.0.0.1:{port}/" in your custom config.', + f"Please go to the following link: http://127.0.0.1:{port}/auth?state={state}", + "Log in and authorize rclone for access", + "Waiting for code...", + ): + print(f"2026/10/15 11:30:31 NOTICE: {line}", file=sys.stderr, flush=True) + while not done: + server.handle_request() + server.server_close() + if "error" in done: + print(f"2026/10/15 11:30:40 CRITICAL: Fatal error: {done['error']}", file=sys.stderr, flush=True) + return 1 + print(f"Paste the following into your remote machine --->\n{done['token']}\n<---End paste", flush=True) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/nextjs-frontend/__tests__/rclone-signin.test.tsx b/nextjs-frontend/__tests__/rclone-signin.test.tsx new file mode 100644 index 000000000..68ec133c3 --- /dev/null +++ b/nextjs-frontend/__tests__/rclone-signin.test.tsx @@ -0,0 +1,140 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Sources } from "@/app/openapi-client"; +import { ConnectionDialog } from "@/components/sources/connection-dialog"; +import { isRcloneReturn } from "@/components/sources/rclone-signin"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Sources: { + createSource: jest.fn(), + updateSource: jest.fn(), + testSource: jest.fn(), + deleteSource: jest.fn(), + listSources: 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() })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const fail = (status: number, detail: string) => + Promise.resolve({ error: { detail }, response: { ok: false, status } }); +const DROPBOX = { label: "Dropbox", fields: {}, secrets: ["token"], oauth: "rclone authorize dropbox" }; +const BACK = "http://127.0.0.1:53682/?state=s1&code=c1"; + +function show(onSaved: (id: number) => void = jest.fn()) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); + render( + + + undefined} backends={{ dropbox: DROPBOX }} onSaved={onSaved} /> + + , + ); + fireEvent.click(screen.getByRole("radio", { name: /Dropbox/ })); +} + +describe("signing in through rclone on the server", () => { + const tab = { location: { href: "" }, opener: {} as unknown, close: jest.fn() }; + beforeEach(() => { + tab.location.href = ""; + tab.opener = {}; + jest.spyOn(window, "open").mockReturnValue(tab as unknown as Window); + (Sources.startRcloneSignin as jest.Mock) + .mockReset() + .mockImplementation(() => + ok({ id: "f1", url: "https://dropbox.test/authorize", port: 53682, expires_at: "2030-01-01T00:00:00+00:00" }), + ); + (Sources.finishRcloneSignin as jest.Mock).mockReset().mockImplementation(() => ok({ connected: null })); + (Sources.cancelRcloneSignin as jest.Mock).mockReset().mockImplementation(() => ok({ ok: true })); + (Sources.listSources as jest.Mock) + .mockReset() + .mockImplementation(() => + ok([{ id: 9, name: "Dropbox", type: "dropbox", params: {}, secrets: {}, health: { ok: true } }]), + ); + }); + + it("opens the service in a new tab, takes the address it ends on, then tests and names the connection", async () => { + const saved = jest.fn(); + show(saved); + expect(screen.queryByRole("button", { name: "Test connection" })).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Sign in with Dropbox" })); + await waitFor(() => expect(tab.location.href).toBe("https://dropbox.test/authorize")); + expect(window.open).toHaveBeenCalledWith("", "_blank"); + expect(tab.opener).toBeNull(); + expect((Sources.startRcloneSignin as jest.Mock).mock.calls[0][0].body).toEqual({ + type: "dropbox", + params: {}, + name: null, + }); + const paste = screen.getByLabelText(/Paste the address it ends on/); + expect(paste).toHaveFocus(); + // the address goes as soon as it's pasted + (Sources.finishRcloneSignin as jest.Mock).mockImplementation(() => ok({ connected: 9 })); + fireEvent.change(paste, { target: { value: BACK } }); + await waitFor(() => + expect(Sources.finishRcloneSignin).toHaveBeenCalledWith( + expect.objectContaining({ path: { fid: "f1" }, body: { address: BACK } }), + ), + ); + expect(await screen.findByLabelText("Name")).toHaveValue("Dropbox"); + expect(screen.getByText("Listed the top folder")).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: "Done" })); + await waitFor(() => expect(saved).toHaveBeenCalledWith(9)); + expect(Sources.createSource).not.toHaveBeenCalled(); + }); + + it("keeps waiting after a wrong address, and starts over when the sign-in ended", async () => { + show(); + fireEvent.click(screen.getByRole("button", { name: "Sign in with Dropbox" })); + const paste = await screen.findByLabelText(/Paste the address it ends on/); + (Sources.finishRcloneSignin as jest.Mock).mockImplementation(() => + fail(400, "that address is from another sign-in; paste the one this sign-in ended on"), + ); + fireEvent.change(paste, { target: { value: "http://127.0.0.1:53682/?state=old&code=c0" } }); + expect(await screen.findByText(/that address is from another sign-in/)).toBeInTheDocument(); + expect(screen.getByLabelText(/Paste the address it ends on/)).toBeInTheDocument(); + (Sources.finishRcloneSignin as jest.Mock).mockImplementation(() => + fail(410, "signing in didn't finish: you cancelled"), + ); + fireEvent.change(paste, { target: { value: BACK.replace("code=c1", "error=access_denied") } }); + expect(await screen.findByText("signing in didn't finish: you cancelled")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Sign in with Dropbox" })).toBeInTheDocument(); + }); + + it("finishes by itself when the browser reached rclone, and cancelling stops it on the server", async () => { + jest.useFakeTimers(); + try { + show(); + fireEvent.click(screen.getByRole("button", { name: "Sign in with Dropbox" })); + await screen.findByLabelText(/Paste the address it ends on/); + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + expect(Sources.cancelRcloneSignin).toHaveBeenCalledWith(expect.objectContaining({ path: { fid: "f1" } })); + fireEvent.click(screen.getByRole("button", { name: "Sign in with Dropbox" })); + await screen.findByLabelText(/Paste the address it ends on/); + (Sources.finishRcloneSignin as jest.Mock).mockImplementation(() => ok({ connected: 9 })); + await act(async () => { + jest.advanceTimersByTime(2100); + }); + expect(Sources.finishRcloneSignin).toHaveBeenCalledWith(expect.objectContaining({ body: {} })); + expect(await screen.findByLabelText("Name")).toHaveValue("Dropbox"); + } finally { + jest.useRealTimers(); + } + }); + + it("knows the address the service sends the browser back to", () => { + expect(isRcloneReturn(BACK, 53682)).toBe(true); + expect(isRcloneReturn("http://localhost:53682/?code=x&state=y", 53682)).toBe(true); // OneDrive's + expect(isRcloneReturn("http://127.0.0.1:53682/?error=access_denied", 53682)).toBe(true); + expect(isRcloneReturn("http://127.0.0.1:53682/", 53682)).toBe(false); + expect(isRcloneReturn("https://evil.test/?code=x", 53682)).toBe(false); + expect(isRcloneReturn("not a link", 53682)).toBe(false); + }); +}); diff --git a/nextjs-frontend/__tests__/sources-model.test.ts b/nextjs-frontend/__tests__/sources-model.test.ts index 2754e8060..20f4a5161 100644 --- a/nextjs-frontend/__tests__/sources-model.test.ts +++ b/nextjs-frontend/__tests__/sources-model.test.ts @@ -120,7 +120,11 @@ describe("connections", () => { expect(validateForm("s3", S3, ok, false)).toEqual({}); // Editing: a saved secret counts. expect(validateForm("s3", S3, { ...ok, secrets: {} }, true, (k) => k === "secret_access_key")).toEqual({}); - expect(validateForm("dropbox", DROPBOX, emptyForm(DROPBOX), false)).toHaveProperty("token"); + // Dropbox signs in through rclone on the server; a pasted token is checked + expect(validateForm("dropbox", DROPBOX, emptyForm(DROPBOX), false)).toEqual({}); + expect(validateForm("dropbox", DROPBOX, { ...emptyForm(DROPBOX), signInWith: "paste" }, false)).toHaveProperty( + "token", + ); }); it("sends every parameter and only the secrets that change", () => { const f = { @@ -156,6 +160,8 @@ describe("connections", () => { expect(parseServiceAccountKey("nope").ok).toBe(false); expect(parseServiceAccountKey('{"access_token":"a"}').ok).toBe(false); + // a Google account signs in through rclone on the server: nothing to paste + expect(validateForm("drive", DRIVE, emptyForm(DRIVE), false)).toEqual({}); const sa = { ...emptyForm(DRIVE), driveAuth: "service_account" as const }; expect(Object.keys(validateForm("drive", DRIVE, sa, false))).toEqual(["service_account_credentials"]); const withKey = { ...sa, secrets: { service_account_credentials: key } }; diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index c5089f60c..288a959d0 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -238,6 +238,11 @@ export type { CancelJobErrors, CancelJobResponse, CancelJobResponses, + CancelRcloneSigninData, + CancelRcloneSigninError, + CancelRcloneSigninErrors, + CancelRcloneSigninResponse, + CancelRcloneSigninResponses, CancelUploadData, CancelUploadError, CancelUploadErrors, @@ -945,6 +950,11 @@ export type { FinishData, FinishError, FinishErrors, + FinishRcloneSigninData, + FinishRcloneSigninError, + FinishRcloneSigninErrors, + FinishRcloneSigninResponse, + FinishRcloneSigninResponses, FinishResponse, FinishResponses, FinishTourData, @@ -2251,6 +2261,10 @@ export type { QueueStepErrors, QueueStepResponse, QueueStepResponses, + RcloneSignIn, + RcloneSignInFinish, + RcloneSignInStart, + RcloneSignInState, RdfImport, RdfImportItem, RdfImportResult, @@ -2814,6 +2828,11 @@ export type { SplitSegmentResponse, SplitSegmentResponses, Started, + StartRcloneSigninData, + StartRcloneSigninError, + StartRcloneSigninErrors, + StartRcloneSigninResponse, + StartRcloneSigninResponses, StartUpgradeData, StartUpgradeError, StartUpgradeErrors, diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index 8fe7b4f38..bbe2501ad 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -85,6 +85,9 @@ import type { CancelJobData, CancelJobErrors, CancelJobResponses, + CancelRcloneSigninData, + CancelRcloneSigninErrors, + CancelRcloneSigninResponses, CancelUploadData, CancelUploadErrors, CancelUploadResponses, @@ -438,6 +441,9 @@ import type { FindSimilarResponses, FinishData, FinishErrors, + FinishRcloneSigninData, + FinishRcloneSigninErrors, + FinishRcloneSigninResponses, FinishResponses, FinishTourData, FinishTourResponses, @@ -1352,6 +1358,9 @@ import type { SplitSegmentData, SplitSegmentErrors, SplitSegmentResponses, + StartRcloneSigninData, + StartRcloneSigninErrors, + StartRcloneSigninResponses, StartUpgradeData, StartUpgradeErrors, StartUpgradeResponses, @@ -8006,6 +8015,62 @@ export class Sources { }); } + /** + * Start Rclone Signin + * + * Connect Google Drive (with no Google app set up here), Dropbox or OneDrive by signing in through rclone on this + * server, or sign a connection in again (`source`). Open `url` in a browser and sign in; the service then sends the + * browser to http://127.0.0.1:53682/?…, which only loads on the machine Lens runs on. Hand that address to + * POST /sources/rclone-signin/{id}, which makes the connection. One at a time: this ends any other. + */ + public static startRcloneSignin( + options: Options, + ): RequestResult { + return (options.client ?? client).post({ + url: "/api/v1/sources/rclone-signin", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); + } + + /** + * Cancel Rclone Signin + * + * Stop a sign-in through rclone. + */ + public static cancelRcloneSignin( + options: Options, + ): RequestResult { + return (options.client ?? client).delete({ + url: "/api/v1/sources/rclone-signin/{fid}", + ...options, + }); + } + + /** + * Finish Rclone Signin + * + * Finish signing in through rclone with the address the browser ended on (`address`), or, without one, look + * whether rclone already got it. Answers the connection, made and tested, or none while it waits. 400: that address + * isn't this sign-in's, and it's still waiting for the right one; 410: it didn't work (cancelled, refused, ran out), + * and it's over; start again. Audited as `source.signin`. + */ + public static finishRcloneSignin( + options: Options, + ): RequestResult { + return (options.client ?? client).post({ + url: "/api/v1/sources/rclone-signin/{fid}", + ...options, + headers: { + "Content-Type": "application/json", + ...options.headers, + }, + }); + } + /** * Test Source */ diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index b255196d0..887eec9d2 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -13169,6 +13169,105 @@ export type Pushed = { [key: string]: unknown; }; +/** + * RcloneSignIn + */ +export type RcloneSignIn = { + /** + * Id + * + * this sign-in, to finish or cancel + */ + id: string; + /** + * Url + * + * the service's sign-in page, to open in a browser (it asks to allow rclone); once, within ten minutes + */ + url: string; + /** + * Port + * + * where the service sends the browser back afterwards: http://127.0.0.1:/?state=…&code=… + */ + port: number; + /** + * Expires At + */ + expires_at: string; + [key: string]: unknown; +}; + +/** + * RcloneSignInFinish + */ +export type RcloneSignInFinish = { + /** + * Address + * + * the address the browser ended on after signing in (http://127.0.0.1:53682/?state=…&code=…); leave out to only look whether rclone already got it (a browser on the machine Lens runs on reaches it itself) + */ + address?: string | null; +}; + +/** + * RcloneSignInStart + */ +export type RcloneSignInStart = { + /** + * Type + * + * the kind of connection to make (drive, dropbox, onedrive); leave out with `source` + */ + type?: + | "s3" + | "dropbox" + | "drive" + | "onedrive" + | "sftp" + | "smb" + | "webdav" + | "local" + | "imap" + | "gmail" + | "ical" + | "gcal" + | null; + /** + * Source + * + * a connection to sign in again, instead of making a new one + */ + source?: number | null; + /** + * Name + * + * the new connection's name; by default the service's + */ + name?: string | null; + /** + * Params + * + * its options, as for POST /sources (scope, root_folder_id...) + */ + params?: { + [key: string]: unknown; + } | null; +}; + +/** + * RcloneSignInState + */ +export type RcloneSignInState = { + /** + * Connected + * + * the connection, made or signed in again, and tested; none while it waits + */ + connected?: number | null; + [key: string]: unknown; +}; + /** * RdfImport */ @@ -32598,6 +32697,91 @@ export type UpdateSourceResponses = { export type UpdateSourceResponse = UpdateSourceResponses[keyof UpdateSourceResponses]; +export type StartRcloneSigninData = { + body: RcloneSignInStart; + path?: never; + query?: never; + url: "/api/v1/sources/rclone-signin"; +}; + +export type StartRcloneSigninErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type StartRcloneSigninError = StartRcloneSigninErrors[keyof StartRcloneSigninErrors]; + +export type StartRcloneSigninResponses = { + /** + * Successful Response + */ + 200: RcloneSignIn; +}; + +export type StartRcloneSigninResponse = StartRcloneSigninResponses[keyof StartRcloneSigninResponses]; + +export type CancelRcloneSigninData = { + body?: never; + path: { + /** + * Fid + */ + fid: string; + }; + query?: never; + url: "/api/v1/sources/rclone-signin/{fid}"; +}; + +export type CancelRcloneSigninErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type CancelRcloneSigninError = CancelRcloneSigninErrors[keyof CancelRcloneSigninErrors]; + +export type CancelRcloneSigninResponses = { + /** + * Successful Response + */ + 200: Ok; +}; + +export type CancelRcloneSigninResponse = CancelRcloneSigninResponses[keyof CancelRcloneSigninResponses]; + +export type FinishRcloneSigninData = { + body: RcloneSignInFinish; + path: { + /** + * Fid + */ + fid: string; + }; + query?: never; + url: "/api/v1/sources/rclone-signin/{fid}"; +}; + +export type FinishRcloneSigninErrors = { + /** + * Validation Error + */ + 422: HttpValidationError; +}; + +export type FinishRcloneSigninError = FinishRcloneSigninErrors[keyof FinishRcloneSigninErrors]; + +export type FinishRcloneSigninResponses = { + /** + * Successful Response + */ + 200: RcloneSignInState; +}; + +export type FinishRcloneSigninResponse = FinishRcloneSigninResponses[keyof FinishRcloneSigninResponses]; + export type TestSourceData = { body?: never; path: { diff --git a/nextjs-frontend/components/sources/connection-dialog.tsx b/nextjs-frontend/components/sources/connection-dialog.tsx index f292d4f39..77181c332 100644 --- a/nextjs-frontend/components/sources/connection-dialog.tsx +++ b/nextjs-frontend/components/sources/connection-dialog.tsx @@ -1,13 +1,16 @@ "use client"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { useEffect, useState, type ReactNode } from "react"; +import { useEffect, useRef, useState, type ReactNode } from "react"; import { Sources } from "@/app/openapi-client"; import type { Source, SourceHealth } from "@/app/openapi-client/types.gen"; import { ConnectionFields } from "@/components/sources/connection-fields"; +import { startRclone, type RcloneWaiting } from "@/components/sources/rclone-signin"; +import { RcloneWait } from "@/components/sources/rclone-wait"; import { TYPE_ICON } from "@/components/sources/source-icons"; import { + SIGNIN_SERVICE, TYPE_NAME, TYPE_ORDER, buildPayload, @@ -53,20 +56,20 @@ function StepTrail({ step, editing }: { step: Step; editing: boolean }) { /** SO2: add a connection (choose type → details → test, named there), or edit one (details → test). A type with * nothing to fill in (a folder on this machine) is added, tested and named at once: the test step only shows if it - * fails. */ + * fails. Dropbox, OneDrive and Google Drive (as a Google account) sign in at the service in a new tab, through rclone + * on the server (RcloneWait), instead of testing a pasted token: the connection is made and tested when the service + * sends the browser back, and the dialog goes on to the test step. */ export function ConnectionDialog({ open, onOpenChange, backends, source, - replaceToken, onSaved, }: { open: boolean; onOpenChange: (open: boolean) => void; backends: Record; source?: Source | null; - replaceToken?: boolean; onSaved?: (id: number) => void; }) { const client = useApiClient(); @@ -84,16 +87,32 @@ export function ConnectionDialog({ const [createdName, setCreatedName] = useState(""); const [result, setResult] = useState<{ health: SourceHealth; - ms: number; + /** How long the test took; null when the connection was tested as it was made (signing in). */ + ms: number | null; } | null>(null); + /** Waiting for the browser to come back to rclone on the server; `rcloneStarting` while it's asked to. */ + const [rclone, setRclone] = useState(null); + const [rcloneStarting, setRcloneStarting] = useState(false); + const rcloneLive = useRef(null); + const waitRclone = (w: RcloneWaiting | null) => { + rcloneLive.current = w; + setRclone(w); + }; + /** Stop waiting for rclone (cancelling, closing, starting over): it stops on the server too. */ + const dropRclone = () => { + const w = rcloneLive.current; + if (w) void Sources.cancelRcloneSignin({ client, path: { fid: w.id } }); + waitRclone(null); + }; /** Secrets saved during this dialog (the source prop doesn't know about them yet). */ const [savedKeys, setSavedKeys] = useState>(new Set()); const spec = backends[type]; const saved = editing || createdId != null; const isSet = (k: string) => (Boolean(source?.secrets?.[k]?.set) && !savedKeys.has(`-${k}`)) || savedKeys.has(k); - // Start fresh each time the dialog opens. + // Start fresh each time the dialog opens; a sign-in through rclone stops when it closes. useEffect(() => { + dropRclone(); if (!open) return; setStep(editing ? "details" : "type"); setType(source?.type ?? "s3"); @@ -112,6 +131,7 @@ export function ConnectionDialog({ setResult(null); setSavedKeys(new Set()); }, [open]); + useEffect(() => () => dropRclone(), []); const refresh = () => { void qc.invalidateQueries({ queryKey: ["sources"] }); @@ -213,6 +233,45 @@ export function ConnectionDialog({ }, }); + // Dropbox, OneDrive and Google Drive (as a Google account) sign in at the service through rclone on the server, + // unless a token is pasted or a saved connection only changes its options + const via = spec?.oauth && !(type === "drive" && form.driveAuth === "service_account") ? form.signInWith : null; + const needSignIn = via === "rclone" && !(saved && isSet("token")); + const service = SIGNIN_SERVICE[type] ?? spec?.label ?? ""; + + // the connection is made and tested: on to the test step, as if it had been saved here + const connected = async (id: number) => { + const made = (await data(Sources.listSources({ client }))).find((s) => s.id === id); + setCreatedId(id); + setCreatedName(made?.name ?? ""); + setSavedKeys((prev) => new Set([...prev, "token", "-service_account_credentials"])); + setForm((f) => ({ ...f, secrets: {}, tokenText: "", name: editing ? f.name : (made?.name ?? f.name) })); + setResult({ health: made?.health ?? { ok: false, error: "Not tested yet" }, ms: null }); + setStep("test"); + refresh(); + }; + + // the service's page opens in a new tab now, in the click; the dialog then waits for the address it ends on + const startSignIn = async () => { + const e = validateForm(type, spec, form, saved, isSet); + setErrors(e); + if (Object.keys(e).length) return; + setRcloneStarting(true); + try { + const { params } = buildPayload(spec, form, type); + waitRclone( + await startRclone( + client, + createdId != null ? { source: createdId, params } : { type, params, name: form.name.trim() || null }, + ), + ); + } catch (err) { + setErrors({ _: err instanceof Error ? err.message : String(err) }); + } finally { + setRcloneStarting(false); + } + }; + const goTest = () => { const e = validateForm(type, spec, form, saved, isSet); setErrors(e); @@ -230,9 +289,7 @@ export function ConnectionDialog({ ? `Edit “${source?.name}”` : TYPE_NAME[type] === "S3 / compatible" ? "S3 or S3-compatible" - : spec?.oauth && type !== "drive" - ? `${TYPE_NAME[type]} · paste a token` - : (spec?.label ?? TYPE_NAME[type]) + : (spec?.label ?? TYPE_NAME[type]) : step === "test" ? test.isPending ? "Testing…" @@ -297,7 +354,31 @@ export function ConnectionDialog({ ); } else if (step === "details") { - body = ( + const alert = errors._ && ( +

+ {errors._} +

+ ); + body = rclone ? ( +
+ { + waitRclone(null); + void connected(id).catch((e: Error) => setErrors({ _: e.message })); + }} + onOver={(why) => { + waitRclone(null); + setErrors({ _: why }); + }} + /> + {alert} +
+ ) : (
{editing && ( @@ -315,20 +396,23 @@ export function ConnectionDialog({ errors={errors} saved={saved} isSet={isSet} - replaceToken={replaceToken} + onSignIn={() => void startSignIn()} + signingIn={rcloneStarting} /> ) : null} - {errors._ && ( -

- {errors._} -

- )} + {alert}
); - actions = ( + actions = rclone ? ( + <> + + + + ) : ( <> {!editing && ( - + {needSignIn ? ( + + ) : ( + + )} ); } else if (step === "test") { @@ -364,7 +459,7 @@ export function ConnectionDialog({ ? "Listed the top folder" : "Couldn’t list the top folder"} - {result ? `${result.ms} ms` : ""} + {result?.ms != null ? `${result.ms} ms` : ""} {result && !ok && (
diff --git a/nextjs-frontend/components/sources/connection-fields.tsx b/nextjs-frontend/components/sources/connection-fields.tsx index 268962837..16e98ca0d 100644 --- a/nextjs-frontend/components/sources/connection-fields.tsx +++ b/nextjs-frontend/components/sources/connection-fields.tsx @@ -1,15 +1,15 @@ "use client"; -import { useState } from "react"; - import { DRIVE_SCOPES, FIELD_LABEL, IMAP_SECURITY, S3_PROVIDERS, + SIGNIN_SERVICE, WEBDAV_VENDORS, type BackendSpec, type ConnForm, + type SignInWith, type SourceType, imapHost, parseServiceAccountKey, @@ -19,12 +19,6 @@ import { Button } from "@/components/ui/button"; import { Field, Input, SecretField, Select, Textarea } from "@/components/ui/field"; import { Segmented } from "@/components/ui/tabs"; -const SERVICE: Partial> = { - dropbox: "Dropbox", - drive: "Google", - onedrive: "Microsoft", -}; - /** SO2 step 2: the details for one type of connection, from the backend's field list, with type-specific widgets. */ export function ConnectionFields({ type, @@ -34,7 +28,8 @@ export function ConnectionFields({ errors, saved, isSet, - replaceToken, + onSignIn, + signingIn, }: { type: SourceType; spec: BackendSpec; @@ -44,9 +39,10 @@ export function ConnectionFields({ /** The connection exists (editing it, or created earlier in the dialog): saved secrets show as set / replace / clear. */ saved: boolean; isSet: (key: string) => boolean; - replaceToken?: boolean; + /** Sign in again through rclone; shown while a saved connection can also just be saved. */ + onSignIn?: () => void; + signingIn?: boolean; }) { - const [pasting, setPasting] = useState(Boolean(replaceToken)); const editing = saved; const p = form.params; const set = (k: string, v: string) => onChange({ ...form, params: { ...p, [k]: v } }); @@ -136,31 +132,51 @@ export function ConnectionFields({ ); - const token = spec.oauth ? ( - editing && isSet("token") && !pasting ? ( + const service = SIGNIN_SERVICE[type] ?? "your account"; + const how = (label: string, to: SignInWith) => ( + + ); + const saving = editing && isSet("token"); + // an OAuth connection signs in through rclone on the server (the footer's button), or pastes rclone's token + const token = !spec.oauth ? null : form.signInWith === "paste" ? ( +
+ onChange({ ...form, tokenText: v })} + replacing={editing} + /> + {errors.token && form.tokenText.trim() === "" && ( +

+ {errors.token} +

+ )} + {how(`Sign in with ${service} instead`, "rclone")} +
+ ) : ( +
- A token is saved (never shown again). rclone refreshes it while it can. - -
- ) : ( -
- onChange({ ...form, tokenText: v })} - replacing={editing} - /> - {errors.token && form.tokenText.trim() === "" && ( -

- {errors.token} -

+ + {saving + ? "A token is saved (never shown again). rclone renews it while it can; sign in again if it stops." + : `You sign in to ${service} in a new tab. Lens connects through rclone on its server, so ${service} asks you to allow rclone.`} + + {saving && onSignIn && ( + )}
- ) - ) : null; + {how("Paste a token from rclone instead", "paste")} +
+ ); /** A Google service account's .json key file (Google Drive, Google Calendar). */ const serviceAccountKey = (share: string) => { @@ -262,7 +278,7 @@ export function ConnectionFields({ {text("drive_id", { mono: true, optional: true, - hint: "rclone config shows it", + hint: "Filled in when you sign in", })} {text("drive_type", { optional: true, diff --git a/nextjs-frontend/components/sources/rclone-signin.ts b/nextjs-frontend/components/sources/rclone-signin.ts new file mode 100644 index 000000000..b41705bd9 --- /dev/null +++ b/nextjs-frontend/components/sources/rclone-signin.ts @@ -0,0 +1,45 @@ +import { Sources } from "@/app/openapi-client"; +import type { Client } from "@/app/openapi-client/client"; +import type { RcloneSignIn, RcloneSignInStart } from "@/app/openapi-client/types.gen"; +import { data } from "@/lib/api/browser"; + +/** A sign-in through rclone on the server (POST /sources/rclone-signin), waiting for the browser to come back. */ +export type RcloneWaiting = { id: string; url: string; port: number; opened: boolean }; + +/** + * Sign in through rclone on the server: the service's page opens in a new tab (opened now, in the click, or it would + * be blocked), where the person signs in and allows rclone. The service then sends that tab to rclone's address on + * the server, http://127.0.0.1:53682/…, which loads only when the browser runs there too; RcloneWait takes the address + * from there. + */ +export async function startRclone(client: Client, body: RcloneSignInStart): Promise { + const tab = window.open("", "_blank"); + let got: RcloneSignIn; + try { + got = await data(Sources.startRcloneSignin({ client, body })); + } catch (e) { + tab?.close(); + throw e; + } + if (tab) { + tab.opener = null; // the service's page can't reach back into Lens + tab.location.href = got.url; + } + return { id: got.id, url: got.url, port: got.port, opened: Boolean(tab) }; +} + +/** An address the service sent the browser back to, for rclone on the server (its code, or why there's none). */ +export function isRcloneReturn(text: string, port: number): boolean { + let u: URL; + try { + u = new URL(text.trim()); + } catch { + return false; + } + return ( + u.protocol === "http:" && + (u.hostname === "127.0.0.1" || u.hostname === "localhost") && + u.port === String(port) && + (u.searchParams.has("code") || u.searchParams.has("error")) + ); +} diff --git a/nextjs-frontend/components/sources/rclone-wait.tsx b/nextjs-frontend/components/sources/rclone-wait.tsx new file mode 100644 index 000000000..de9dabe0f --- /dev/null +++ b/nextjs-frontend/components/sources/rclone-wait.tsx @@ -0,0 +1,124 @@ +"use client"; + +import { useEffect, useId, useRef, useState } from "react"; + +import { Sources } from "@/app/openapi-client"; +import { isRcloneReturn, type RcloneWaiting } from "@/components/sources/rclone-signin"; +import { Input } from "@/components/ui/field"; +import { ApiError, data, useApiClient } from "@/lib/api/browser"; + +const POLL_MS = 2000; + +/** + * Waiting for a sign-in through rclone on the server (startRclone). The address the service sent the browser to is + * pasted here and handed on as soon as it looks right (Enter sends it too). Meanwhile it asks every two seconds whether + * rclone got there itself, as it does when the browser runs on the server: then there's nothing to paste. + * `onConnected` gets the connection, made and tested; `onOver` why the sign-in ended (cancelled at the service, + * refused, ran out), to start again. + */ +export function RcloneWait({ + waiting, + service, + onConnected, + onOver, +}: { + waiting: RcloneWaiting; + service: string; + onConnected: (id: number) => void; + onOver: (why: string) => void; +}) { + const client = useApiClient(); + const id = useId(); + const [address, setAddress] = useState(""); + const [error, setError] = useState(null); + const [sending, setSending] = useState(false); + const busy = useRef(false); + const done = useRef(false); + + // one request at a time: a look and a pasted address don't race to make the connection + const finish = async (addr?: string) => { + if (busy.current || done.current) return; + busy.current = true; + if (addr) setSending(true); + try { + const r = await data( + Sources.finishRcloneSignin({ client, path: { fid: waiting.id }, body: addr ? { address: addr } : {} }), + ); + if (r.connected != null) { + done.current = true; + onConnected(r.connected); + } + } catch (e) { + if (e instanceof ApiError && e.status === 400) setError(e.message); + else { + done.current = true; + onOver(e instanceof Error ? e.message : String(e)); + } + } finally { + busy.current = false; + setSending(false); + } + }; + + useEffect(() => { + const t = window.setInterval(() => void finish(), POLL_MS); + return () => window.clearInterval(t); + }, [waiting.id]); + + const send = (text: string) => { + setError(null); + void finish(text.trim()); + }; + + return ( +
+

+ 1 · Sign in to {service}{" "} + {waiting.opened ? "in the new tab" : "on its page"} and allow rclone, the tool Lens connects with.{" "} + + {waiting.opened ? "Open it again" : `Open ${service}’s sign-in page`} + +

+
+ + { + setAddress(e.target.value); + if (isRcloneReturn(e.target.value, waiting.port)) send(e.target.value); + }} + onKeyDown={(e) => { + if (e.key === "Enter" && address.trim()) { + e.preventDefault(); + send(address); + } + }} + spellCheck={false} + autoComplete="off" + /> + + {error ?? (sending ? "Connecting…" : "On the machine Lens runs on, this finishes by itself.")} + +
+
+ ); +} diff --git a/nextjs-frontend/components/sources/source-model.ts b/nextjs-frontend/components/sources/source-model.ts index 0f8701e09..1f95745e1 100644 --- a/nextjs-frontend/components/sources/source-model.ts +++ b/nextjs-frontend/components/sources/source-model.ts @@ -434,6 +434,18 @@ export type ConnForm = { /** Google Drive and Google Calendar 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. */ + signInWith: SignInWith; +}; + +export type SignInWith = "rclone" | "paste"; + +/** Whose sign-in page an OAuth connection opens. */ +export const SIGNIN_SERVICE: Partial> = { + dropbox: "Dropbox", + drive: "Google", + onedrive: "Microsoft", }; export type ServiceAccountKey = { ok: true; email: string } | { ok: false; error: string }; @@ -492,6 +504,7 @@ export function emptyForm(spec: BackendSpec, source?: Pick s.id === selected) ?? all.find((s) => bySource(s.id).length) ?? all[0]; const specs = (backends.data ?? {}) as Record; - // Test (or Paste new token) and the More menu, in a table row on a wide screen and on a card on a phone + // Test (or Sign in again) and the More menu, in a table row on a wide screen and on a card on a phone const actions = (s: Source) => { const bad = s.health && !s.health.ok; const oauth = Boolean(s.oauth); return (
e.stopPropagation()}> {bad && oauth && /token|auth|expired|unauthori[sz]ed|401|invalid_grant/i.test(s.health?.error ?? "") ? ( - ) : bad || !s.health ? (