Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

t3-connect-remote-setup.html

257 lines17.1 KB

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Plan: seamless `npx t3 connect` for remote boxes</title>
7<style>
8 :root {
9 --bg: #000;
10 --fg: #fff;
11 --muted: #a3a3a3;
12 --surface: #121212;
13 --surface2: #1a1a1a;
14 --border: #2a2a2a;
15 --accent: #7c9cff;
16 --green: #4ade80;
17 --amber: #fbbf24;
18 --red: #f87171;
19 }
20 * { box-sizing: border-box; }
21 body {
22 margin: 0;
23 background: var(--bg);
24 color: var(--fg);
25 font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
26 }
27 main { max-width: 880px; margin: 0 auto; padding: 48px 24px 96px; }
28 h1 { font-size: 30px; line-height: 1.25; margin: 0 0 8px; }
29 h2 { font-size: 21px; margin: 48px 0 12px; padding-top: 24px; border-top: 1px solid var(--border); }
30 h3 { font-size: 16px; margin: 24px 0 8px; }
31 p, li { color: #e5e5e5; }
32 .sub { color: var(--muted); margin: 0 0 24px; }
33 code, pre {
34 font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
35 font-size: 13.5px;
36 }
37 code { background: var(--surface2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; }
38 pre {
39 background: var(--surface);
40 border: 1px solid var(--border);
41 border-radius: 8px;
42 padding: 16px;
43 overflow-x: auto;
44 line-height: 1.5;
45 }
46 pre code { background: none; border: none; padding: 0; }
47 .card {
48 background: var(--surface);
49 border: 1px solid var(--border);
50 border-radius: 10px;
51 padding: 18px 20px;
52 margin: 16px 0;
53 }
54 .card h3 { margin-top: 0; }
55 table { border-collapse: collapse; width: 100%; margin: 16px 0; font-size: 14.5px; }
56 th, td { text-align: left; padding: 8px 12px; border: 1px solid var(--border); vertical-align: top; }
57 th { background: var(--surface2); font-weight: 600; }
58 td code { white-space: nowrap; }
59 .pill {
60 display: inline-block; font-size: 12px; font-weight: 600; border-radius: 999px;
61 padding: 2px 10px; margin-right: 6px; vertical-align: 2px;
62 }
63 .pill.core { background: #1e2a4a; color: var(--accent); }
64 .pill.new { background: #14301e; color: var(--green); }
65 .pill.later { background: #33270e; color: var(--amber); }
66 .ok { color: var(--green); }
67 .warn { color: var(--amber); }
68 .no { color: var(--red); }
69 ul { padding-left: 22px; }
70 li { margin: 4px 0; }
71 .terminal {
72 background: #0a0a0a; border: 1px solid var(--border); border-radius: 8px;
73 padding: 16px; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
74 font-size: 13.5px; line-height: 1.65; color: #d4d4d4; overflow-x: auto;
75 }
76 .terminal .dim { color: #737373; }
77 .terminal .g { color: var(--green); }
78 .terminal .b { color: var(--accent); }
79 figure { margin: 24px 0; }
80 figcaption { color: var(--muted); font-size: 13px; margin-top: 8px; text-align: center; }
81 svg text { font-family: -apple-system, "Segoe UI", sans-serif; }
82 a { color: var(--accent); }
83 .kv { color: var(--muted); font-size: 13.5px; }
84</style>
85</head>
86<body>
87<main>
88​
89<h1>Seamless <code>npx t3 connect</code> for remote boxes</h1>
90<p class="sub">Design principle: <strong>the smallest diff that ships the UX.</strong> No relay/infra changes, no new backend surface, no new auth primitives &mdash; every step reuses code that already exists. One PR, built as four phases with clear commit boundaries &mdash; each phase compiles, passes tests, and leaves the product working, so the PR reviews commit-by-commit. (Phase 4, web-triggered update, is an optional follow-up PR.)</p>
91​
92<div class="terminal">
93$ npx t3 connect<br><br>
94To set up T3 Connect, open this URL and sign in:<br>
95&nbsp;&nbsp;<span class="b">https://app.t3.codes/connect#B64URL_STATE_AND_CHALLENGE</span><br><br>
96Enter your authentication code: <span class="dim">[code]</span><br><br>
97<span class="g">Connected as theo@t3.gg!</span><br><br>
98Run T3 Code in the background whenever this machine boots? (y/n): y<br><br>
99<span class="g">T3 Code is set up and ready to go.</span>
100</div>
101​
102<h2>Why this is a small change</h2>
103<p>The entire <code>t3 connect</code> data plane already works: Clerk PKCE token exchange, encrypted secret store, cloudflared relay-client install, relay environment linking, DPoP tokens. The <em>only</em> broken piece on an SSH box is the redirect: <code>CliTokenManager.login()</code> hardcodes a loopback callback (<code>http://127.0.0.1:34338/callback</code>) that requires a browser on the same machine.</p>
104<p>We swap that one leg for a hosted out-of-band authorization page and keep everything else. Because PKCE's <code>code_verifier</code> never leaves the box, the displayed one-time code is useless to anyone who sees it &mdash; no new token-minting or storage is needed anywhere.</p>
105​
106<div class="card">
107<h3>Reused as-is (zero changes)</h3>
108<ul>
109 <li><code>exchangeToken()</code> PKCE exchange &mdash; <code>apps/server/src/cloud/CliTokenManager.ts:147</code></li>
110 <li>Token persistence in <code>ServerSecretStore</code> (<code>cloud-cli-oauth-token</code>)</li>
111 <li><code>acquireRelayClientForLink()</code> cloudflared install + progress &mdash; <code>cli/connect.ts:146</code></li>
112 <li><code>CliState.setCliDesiredCloudLink()</code> + server-side provisioning on start</li>
113 <li>All relay endpoints (<code>infra/relay</code>) and contracts &mdash; <strong>untouched</strong></li>
114 <li>Existing subcommands <code>login/link/status/unlink/logout</code> &mdash; semantics unchanged</li>
115 <li>Web app Clerk session + hosted-page precedent (<code>routes/pair.tsx</code>, <code>hostedPairing.ts</code>)</li>
116</ul>
117</div>
118​
119<h2>Auth flow (hosted out-of-band OAuth, Clerk PKCE)</h2>
120​
121<figure>
122<svg viewBox="0 0 820 470" width="100%" role="img" aria-label="Sequence diagram of the out-of-band OAuth flow">
123 <!-- lanes -->
124 <rect x="20" y="16" width="220" height="44" rx="8" fill="#121212" stroke="#2a2a2a"/>
125 <rect x="300" y="16" width="220" height="44" rx="8" fill="#121212" stroke="#2a2a2a"/>
126 <rect x="580" y="16" width="220" height="44" rx="8" fill="#121212" stroke="#2a2a2a"/>
127 <text x="130" y="43" fill="#fff" font-size="14" font-weight="600" text-anchor="middle">Remote box &#8212; t3 CLI</text>
128 <text x="410" y="43" fill="#fff" font-size="14" font-weight="600" text-anchor="middle">Laptop &#8212; app.t3.codes</text>
129 <text x="690" y="43" fill="#fff" font-size="14" font-weight="600" text-anchor="middle">Clerk</text>
130 <line x1="130" y1="60" x2="130" y2="450" stroke="#2a2a2a"/>
131 <line x1="410" y1="60" x2="410" y2="450" stroke="#2a2a2a"/>
132 <line x1="690" y1="60" x2="690" y2="450" stroke="#2a2a2a"/>
133​
134 <!-- 1: generate + print -->
135 <rect x="30" y="80" width="200" height="34" rx="6" fill="#1e2a4a"/>
136 <text x="130" y="101" fill="#7c9cff" font-size="12.5" text-anchor="middle">1. gen verifier + challenge + state</text>
137​
138 <!-- 2: user opens URL -->
139 <line x1="130" y1="140" x2="404" y2="140" stroke="#7c9cff" stroke-dasharray="5 4"/>
140 <polygon points="404,140 394,135 394,145" fill="#7c9cff"/>
141 <text x="267" y="132" fill="#a3a3a3" font-size="12.5" text-anchor="middle">2. user opens /connect#&#123;state,challenge&#125;</text>
142​
143 <!-- 3: redirect to clerk -->
144 <line x1="410" y1="185" x2="684" y2="185" stroke="#e5e5e5"/>
145 <polygon points="684,185 674,180 674,190" fill="#e5e5e5"/>
146 <text x="547" y="177" fill="#a3a3a3" font-size="12.5" text-anchor="middle">3. sign in &#8594; /oauth/authorize (PKCE)</text>
147​
148 <!-- 4: redirect back with code -->
149 <line x1="690" y1="230" x2="416" y2="230" stroke="#e5e5e5"/>
150 <polygon points="416,230 426,225 426,235" fill="#e5e5e5"/>
151 <text x="553" y="222" fill="#a3a3a3" font-size="12.5" text-anchor="middle">4. redirect /connect/callback?code&amp;state</text>
152​
153 <!-- 5: page shows code -->
154 <rect x="310" y="252" width="200" height="34" rx="6" fill="#14301e"/>
155 <text x="410" y="273" fill="#4ade80" font-size="12.5" text-anchor="middle">5. shows account + authorization code</text>
156​
157 <!-- 6: user enters code -->
158 <line x1="410" y1="316" x2="136" y2="316" stroke="#7c9cff" stroke-dasharray="5 4"/>
159 <polygon points="136,316 146,311 146,321" fill="#7c9cff"/>
160 <text x="273" y="308" fill="#a3a3a3" font-size="12.5" text-anchor="middle">6. user enters code in terminal</text>
161​
162 <!-- 7: token exchange -->
163 <line x1="130" y1="360" x2="684" y2="360" stroke="#e5e5e5"/>
164 <polygon points="684,360 674,355 674,365" fill="#e5e5e5"/>
165 <text x="407" y="352" fill="#a3a3a3" font-size="12.5" text-anchor="middle">7. POST /oauth/token &#123;code + verifier&#125; &#8594; access/refresh tokens</text>
166​
167 <!-- 8: done -->
168 <rect x="30" y="392" width="200" height="46" rx="6" fill="#14301e"/>
169 <text x="130" y="411" fill="#4ade80" font-size="12.5" text-anchor="middle">8. store token, set desired link,</text>
170 <text x="130" y="427" fill="#4ade80" font-size="12.5" text-anchor="middle">install relay client &#8594; Connected!</text>
171</svg>
172<figcaption>The verifier never leaves the box (steps 1&#8594;7), so the authorization code is worthless if observed. state/challenge ride the URL fragment &mdash; they are not secrets.</figcaption>
173</figure>
174​
175<div class="card">
176<h3>Details that keep it simple</h3>
177<ul>
178 <li><strong>Stateless URL, no short-link service.</strong> The <code>/connect</code> page reads <code>state</code> + <code>code_challenge</code> from the URL fragment and builds the Clerk authorize URL client-side. ~100-char URL &mdash; fine to transfer into an SSH session.</li>
179 <li><strong>State check without a backend:</strong> the callback page displays one authorization blob of <code>code.state</code>; the CLI splits it and verifies <code>state</code> matches what it generated. One line on each side, preserves the loopback flow's CSRF check.</li>
180 <li><strong>Phishing is addressed with copy, not code:</strong> the callback page shows <em>which account</em> is being connected ("Connecting as theo@&hellip;") and warns: <em>"Only enter this code in a terminal session you started yourself."</em> No mechanism needed.</li>
181 <li><strong>Code expiry is a non-issue:</strong> Clerk auth codes live 10 minutes &mdash; the same timeout the existing loopback flow already uses. Wrong/expired code &#8594; friendly retry that reprints the URL.</li>
182 <li><strong>One external config step:</strong> register <code>https://app.t3.codes/connect/callback</code> as an allowed redirect URI on the existing Clerk CLI OAuth client. No new client, no new scopes.</li>
183</ul>
184</div>
185​
186<h2>The phases (one PR, one commit each)</h2>
187<p class="kv">Ordering is dependency order: each phase is independently revertable and the tree is green at every boundary. Phases 1&ndash;3 are the PR; phase 4 ships separately later.</p>
188​
189<table>
190 <tr><th style="width:1%">Phase</th><th>Scope</th><th>Files</th><th style="width:1%">~LOC</th></tr>
191 <tr>
192 <td><span class="pill core">1</span></td>
193 <td><strong>Hosted code page</strong> (web-only, purely additive, zero risk). Two static routes modeled on <code>pair.tsx</code>: <code>/connect</code> (ensure Clerk session, then client-side redirect to authorize &mdash; it's a static SPA, no server 302) and <code>/connect/callback</code> (validate params, show account + copyable code + safety warning). Both routes guard against non-hosted deployments &mdash; redirect to <code>/</code> unless <code>isHostedStaticApp()</code>, same pattern as <code>pair.tsx</code>, since this bundle also ships in local instances. Plus the Clerk dashboard redirect-URI entry.</td>
194 <td><code>apps/web/src/routes/connect.tsx</code><br><code>apps/web/src/routes/connect.callback.tsx</code></td>
195 <td>~200</td>
196 </tr>
197 <tr>
198 <td><span class="pill core">2</span></td>
199 <td><strong>CLI out-of-band OAuth flow + single command.</strong> Add an out-of-band OAuth login path to <code>CliTokenManager</code> (print URL, <code>Prompt.text</code> for the code, reuse <code>exchangeToken</code>). Make bare <code>t3 connect</code> a handler = login + link (subcommands untouched). Auto-pick headless mode inside SSH sessions (<code>SSH_CONNECTION</code>/<code>SSH_TTY</code> &mdash; nothing else); <code>--headless</code> flag as manual override. Loopback stays the default on desktop &mdash; no regression.</td>
200 <td><code>cloud/CliTokenManager.ts</code> (+60)<br><code>cloud/publicConfig.ts</code> (+10)<br><code>cli/connect.ts</code> (+60)</td>
201 <td>~150</td>
202 </tr>
203 <tr>
204 <td><span class="pill new">3</span></td>
205 <td><strong>Background on boot &mdash; Linux first</strong> (the SSH case). One new module: pinned runtime install to <code>~/.t3/runtime/versions/&lt;v&gt;</code> + <code>current</code> symlink, systemd <em>user</em> unit with absolute node/t3 paths, <code>enable-linger</code>. y/n prompt at the end of <code>connect</code>; teardown in <code>logout</code>. Install and service-start failures must land in a log file (under <code>~/.t3/userdata/logs/</code>) whose path is printed at connect time &mdash; systemd user units fail invisibly otherwise. Unit-file generation is pure string-building &#8594; trivially testable. macOS launchd / Windows follow as 3b/3c only if wanted.</td>
206 <td><code>cloud/bootService.ts</code> (new)<br><code>cli/connect.ts</code> (+prompt/teardown)</td>
207 <td>~250</td>
208 </tr>
209 <tr>
210 <td><span class="pill later">4</span></td>
211 <td><strong>Web-triggered update</strong> (optional follow-up PR; not part of this one, not needed for the core UX). Web detects <code>daemon version &lt; latest-on-channel</code> using the existing version-skew surface + hosted manifest; one authenticated "update" command &mdash; client says <em>update</em>, daemon resolves/verifies the version itself (never client-specified &mdash; that would be RCE). Stage install &#8594; verify &#8594; atomic symlink swap &#8594; <code>systemctl --user restart</code>. Progress streams reuse the <code>RelayClientInstallProgressEvent</code> pattern.</td>
212 <td>web banner + one control command + daemon update routine</td>
213 <td>later</td>
214 </tr>
215</table>
216​
217<h3>Runtime layout (phase 3)</h3>
218<pre><code>~/.t3/runtime/
219├── versions/0.0.27/ &larr; npm install --prefix (gets native deps right: node-pty etc.)
220└── current -> versions/0.0.27
221​
222~/.config/systemd/user/t3code.service &larr; ExecStart=/abs/path/node .../current/.../t3 serve
223loginctl enable-linger $USER &larr; survives SSH logout / reboot</code></pre>
224<p class="kv">Why a real <code>npm install</code> and not "reuse the npx binary": the npx cache is ephemeral and <code>t3</code> ships native deps (<code>node-pty</code>, <code>@ff-labs/fff-node</code>) that need per-platform prebuilds. Why pinned and not <code>npx t3@latest</code> in the unit: a boot-time registry fetch means the box may simply not come up (network down, PATH-less systemd env, nvm). Deterministic boot; updates happen out-of-band (phase 4 follow-up) or by re-running <code>npx t3 connect</code>.</p>
225​
226<h2>Explicitly not doing</h2>
227<ul>
228 <li><span class="no">&#10007;</span> Relay / <code>infra</code> / contracts changes &mdash; none, in any phase</li>
229 <li><span class="no">&#10007;</span> Short-link service (<code>app.t3.codes/c/AB7K</code>) &mdash; only matters for hand-typing; revisit if ever needed</li>
230 <li><span class="no">&#10007;</span> RFC 8628 device grant &mdash; wrong UX direction, unverified Clerk support</li>
231 <li><span class="no">&#10007;</span> Auto-update loop in the daemon &mdash; web-triggered only (phase 4 follow-up), user stays in control</li>
232 <li><span class="no">&#10007;</span> Project auto-registration &mdash; workspace assumed set up; the web UI handles the rest</li>
233 <li><span class="no">&#10007;</span> Changing existing loopback flow, subcommands, or desktop behavior</li>
234</ul>
235​
236<h2>Risks &amp; checks</h2>
237<ul>
238 <li><span class="warn">&#9888;</span> <strong>Clerk redirect URI:</strong> confirm the CLI OAuth client accepts the hosted redirect and that the token endpoint honors PKCE exchange for codes issued to it. Verify in staging before the phase 2 commit. <em>(Only external dependency in the plan.)</em></li>
239 <li><span class="warn">&#9888;</span> <strong>systemd user env is minimal:</strong> always write absolute paths for node + t3 into the unit; never rely on PATH. Service failures are invisible by default &mdash; hence the phase 3 requirement to log to a printed file path.</li>
240 <li><span class="warn">&#9888;</span> <strong>Linger prompt honesty:</strong> the y/n prompt should say the machine becomes reachable via T3 Connect whenever powered on &mdash; that's the feature, but say it.</li>
241 <li><span class="ok">&#10003;</span> Re-running <code>connect</code> when linked &#8594; idempotent: refresh token, re-confirm service, done.</li>
242</ul>
243​
244<h2>Decision log</h2>
245<ul class="kv">
246 <li>Auth: hosted out-of-band OAuth redirect on Clerk PKCE (not relay-brokered pairing, not device grant) &mdash; chosen for minimal new surface.</li>
247 <li>URL: stateless static page, no backend short-link.</li>
248 <li>Service: real per-user login service (systemd user + linger first); detect + offer install, never silent.</li>
249 <li>Binary: pinned managed runtime under <code>~/.t3</code>; interactive <code>npx</code> usage untouched.</li>
250 <li>Updates: not always-latest; web UI surfaces available updates with one-click trigger (phase 4 follow-up).</li>
251 <li>Delivery: one PR with a commit per phase (green tree at every boundary), not separate PRs.</li>
252 <li>Workspace: assumed already set up; no auto-registration.</li>
253</ul>
254​
255</main>
256</body>
257</html>