opencode-devin 0.2.11 → 2.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/LICENSE +1 -1
- package/README.md +118 -228
- package/SECURITY.md +81 -0
- package/package.json +52 -58
- package/scripts/dump-log.ts +22 -0
- package/scripts/login.ts +46 -0
- package/src/index.ts +5 -0
- package/src/plugin.ts +293 -0
- package/src/provider/auth.ts +339 -0
- package/src/provider/cascade.ts +469 -0
- package/src/provider/index.ts +321 -0
- package/src/provider/log.ts +99 -0
- package/src/provider/package.json +6 -0
- package/src/provider/paths.ts +33 -0
- package/src/provider/proto.ts +197 -0
- package/dist/catalog.d.ts +0 -22
- package/dist/catalog.js +0 -111
- package/dist/constants.d.ts +0 -19
- package/dist/constants.js +0 -19
- package/dist/credentials.d.ts +0 -23
- package/dist/credentials.js +0 -57
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -135
- package/dist/protocol/model.d.ts +0 -68
- package/dist/protocol/model.js +0 -738
- package/dist/provider.d.ts +0 -43
- package/dist/provider.js +0 -30
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Devin auth — clean-room implementation.
|
|
3
|
+
*
|
|
4
|
+
* Token ladder:
|
|
5
|
+
* PKCE (app.devin.ai/auth/cli/continue)
|
|
6
|
+
* → POST api.devin.ai/auth/cli/token → devin-session-token$<jwt>
|
|
7
|
+
* → POST {cascade}/exa.auth_pb.AuthService/GetUserJwt → user_jwt (~24min)
|
|
8
|
+
*
|
|
9
|
+
* The session token is long-lived and stored by opencode's credential store;
|
|
10
|
+
* user_jwt is short-lived, minted lazily and cached in memory per process.
|
|
11
|
+
*/
|
|
12
|
+
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
13
|
+
import { createServer } from "node:http";
|
|
14
|
+
import { decodeMessage, fMessage, fString, fVarint, getBytes, getString } from "./proto.js";
|
|
15
|
+
import { log, redact, scrubSecrets } from "./log.js";
|
|
16
|
+
|
|
17
|
+
export const APP_ORIGIN = "https://app.devin.ai";
|
|
18
|
+
export const API_ORIGIN = "https://api.devin.ai";
|
|
19
|
+
export const DEFAULT_CASCADE_HOST = "https://server.codeium.com";
|
|
20
|
+
export const SESSION_PREFIX = "devin-session-token$";
|
|
21
|
+
|
|
22
|
+
export interface StoredCredential {
|
|
23
|
+
sessionToken: string; // devin-session-token$<jwt>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface UserJwt {
|
|
27
|
+
jwt: string;
|
|
28
|
+
cascadeHost: string; // tenant override or DEFAULT_CASCADE_HOST
|
|
29
|
+
expiresAt: number; // ms epoch
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ---------- PKCE ----------
|
|
33
|
+
|
|
34
|
+
function b64url(buf: Uint8Array): string {
|
|
35
|
+
return Buffer.from(buf).toString("base64")
|
|
36
|
+
.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function pkcePair(): { verifier: string; challenge: string; state: string } {
|
|
40
|
+
const verifier = b64url(randomBytes(32));
|
|
41
|
+
const challenge = b64url(createHash("sha256").update(verifier).digest());
|
|
42
|
+
const state = b64url(randomBytes(32));
|
|
43
|
+
return { verifier, challenge, state };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function authorizeUrl(challenge: string, state: string, redirectUri?: string): string {
|
|
47
|
+
const u = new URL(`${APP_ORIGIN}/auth/cli/continue`);
|
|
48
|
+
u.searchParams.set("state", state);
|
|
49
|
+
u.searchParams.set("prompt", "select_account");
|
|
50
|
+
u.searchParams.set("code_challenge", challenge);
|
|
51
|
+
u.searchParams.set("code_challenge_method", "S256");
|
|
52
|
+
if (redirectUri) u.searchParams.set("redirect_uri", redirectUri);
|
|
53
|
+
return u.toString();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function exchangeCode(code: string, verifier: string): Promise<StoredCredential> {
|
|
57
|
+
const res = await fetch(`${API_ORIGIN}/auth/cli/token`, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: { "content-type": "application/json" },
|
|
60
|
+
body: JSON.stringify({ code, code_verifier: verifier }),
|
|
61
|
+
});
|
|
62
|
+
if (!res.ok) throw new Error(`token exchange failed: ${res.status} ${await res.text()}`);
|
|
63
|
+
const body = (await res.json()) as { token?: string };
|
|
64
|
+
if (!body.token) throw new Error("token exchange returned no token");
|
|
65
|
+
return { sessionToken: `${SESSION_PREFIX}${body.token}` };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const LOGIN_SUCCESS_HTML = `<!doctype html>
|
|
69
|
+
<html lang="en">
|
|
70
|
+
<head>
|
|
71
|
+
<meta charset="utf-8">
|
|
72
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
73
|
+
<title>Devin · Connected</title>
|
|
74
|
+
<style>
|
|
75
|
+
*{margin:0;box-sizing:border-box}
|
|
76
|
+
body{
|
|
77
|
+
min-height:100vh;display:flex;align-items:center;justify-content:center;
|
|
78
|
+
background:#0c0e12;color:#e8eaf0;
|
|
79
|
+
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Inter,sans-serif;
|
|
80
|
+
}
|
|
81
|
+
.card{
|
|
82
|
+
text-align:center;padding:48px 40px;border-radius:16px;
|
|
83
|
+
background:#12151c;border:1px solid #23283a;
|
|
84
|
+
box-shadow:0 24px 60px rgba(0,0,0,.5);max-width:360px;
|
|
85
|
+
}
|
|
86
|
+
.mark{
|
|
87
|
+
width:56px;height:56px;margin:0 auto 20px;border-radius:14px;
|
|
88
|
+
background:linear-gradient(135deg,#6c5ce7,#3ec6d8);
|
|
89
|
+
display:flex;align-items:center;justify-content:center;
|
|
90
|
+
font-size:28px;font-weight:700;color:#fff;
|
|
91
|
+
}
|
|
92
|
+
.check{
|
|
93
|
+
width:40px;height:40px;margin:0 auto 14px;border-radius:50%;
|
|
94
|
+
background:#1d3a2a;color:#3ddc84;display:flex;
|
|
95
|
+
align-items:center;justify-content:center;font-size:20px;
|
|
96
|
+
}
|
|
97
|
+
h1{font-size:18px;font-weight:600;margin-bottom:8px}
|
|
98
|
+
p{font-size:13px;color:#8b93a7;line-height:1.5}
|
|
99
|
+
.dim{margin-top:14px;font-size:11px;color:#5a6377}
|
|
100
|
+
</style>
|
|
101
|
+
</head>
|
|
102
|
+
<body>
|
|
103
|
+
<div class="card">
|
|
104
|
+
<div class="mark">D</div>
|
|
105
|
+
<div class="check">✓</div>
|
|
106
|
+
<h1>Connected to Devin</h1>
|
|
107
|
+
<p>Your session is linked. Return to opencode — the model catalog is loading.</p>
|
|
108
|
+
<p class="dim">You can close this tab.</p>
|
|
109
|
+
</div>
|
|
110
|
+
<script>setTimeout(()=>window.close(),2500)</script>
|
|
111
|
+
</body>
|
|
112
|
+
</html>`;
|
|
113
|
+
|
|
114
|
+
// ---------- login flows ----------
|
|
115
|
+
|
|
116
|
+
export interface LoginFlow {
|
|
117
|
+
url: string;
|
|
118
|
+
finish: (code?: string) => Promise<StoredCredential>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Browser flow: loopback redirect, no paste needed. */
|
|
122
|
+
export async function loginAuto(): Promise<LoginFlow> {
|
|
123
|
+
const { verifier, challenge, state } = pkcePair();
|
|
124
|
+
|
|
125
|
+
const server = createServer();
|
|
126
|
+
const callback = new Promise<{ code: string; state: string }>((resolve, reject) => {
|
|
127
|
+
server.on("request", (req, res) => {
|
|
128
|
+
const u = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
129
|
+
if (u.pathname !== "/callback") { res.statusCode = 404; res.end(); return; }
|
|
130
|
+
const code = u.searchParams.get("code");
|
|
131
|
+
const st = u.searchParams.get("state");
|
|
132
|
+
res.setHeader("content-type", "text/html; charset=utf-8");
|
|
133
|
+
res.end(LOGIN_SUCCESS_HTML);
|
|
134
|
+
if (code && st) resolve({ code, state: st });
|
|
135
|
+
else reject(new Error("callback missing code/state"));
|
|
136
|
+
});
|
|
137
|
+
server.on("error", reject);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
|
|
141
|
+
const port = (server.address() as { port: number }).port;
|
|
142
|
+
const url = authorizeUrl(challenge, state, `http://127.0.0.1:${port}/callback`);
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
url,
|
|
146
|
+
finish: async () => {
|
|
147
|
+
try {
|
|
148
|
+
const cb = await callback;
|
|
149
|
+
if (cb.state !== state) throw new Error("state mismatch");
|
|
150
|
+
return await exchangeCode(cb.code, verifier);
|
|
151
|
+
} finally {
|
|
152
|
+
server.close();
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Headless flow: webapp shows the code, user pastes it back. */
|
|
159
|
+
export async function loginManual(): Promise<LoginFlow> {
|
|
160
|
+
const { verifier, challenge, state } = pkcePair();
|
|
161
|
+
return {
|
|
162
|
+
url: authorizeUrl(challenge, state),
|
|
163
|
+
finish: async (code) => {
|
|
164
|
+
if (!code) throw new Error("manual login requires the pasted code");
|
|
165
|
+
return exchangeCode(code.trim(), verifier);
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---------- user_jwt mint ----------
|
|
171
|
+
|
|
172
|
+
const JWT_TTL_MS = 20 * 60 * 1000; // refresh slightly before ~24min expiry
|
|
173
|
+
|
|
174
|
+
const VERSION_STRING = "2.0.0";
|
|
175
|
+
|
|
176
|
+
function osName(): string {
|
|
177
|
+
switch (process.platform) {
|
|
178
|
+
case "darwin": return "darwin";
|
|
179
|
+
case "linux": return "linux";
|
|
180
|
+
case "win32": return "windows";
|
|
181
|
+
default: return process.platform;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** google.protobuf.Timestamp body — field 1 = seconds, field 2 = nanos. */
|
|
186
|
+
function encodeTimestampBody(): Uint8Array {
|
|
187
|
+
const ms = Date.now();
|
|
188
|
+
return concatProto([
|
|
189
|
+
fVarint(1, BigInt(Math.floor(ms / 1000))),
|
|
190
|
+
fVarint(2, (ms % 1000) * 1_000_000),
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface MetadataInput {
|
|
195
|
+
apiKey: string;
|
|
196
|
+
sessionId: string;
|
|
197
|
+
requestId: bigint;
|
|
198
|
+
triggerId: string;
|
|
199
|
+
userJwt?: string;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Full `Metadata` proto — matches the Windsurf language_server layout the
|
|
204
|
+
* server validates: ideName/version/apiKey/locale/os/ideVersion/requestId/
|
|
205
|
+
* sessionId/ideName/timestamp/triggerId/"Unset"/ideName (+21 userJwt).
|
|
206
|
+
*/
|
|
207
|
+
export function encodeMetadata(input: MetadataInput): Uint8Array {
|
|
208
|
+
const parts: Uint8Array[] = [
|
|
209
|
+
fString(1, "windsurf"),
|
|
210
|
+
fString(2, VERSION_STRING),
|
|
211
|
+
fString(3, input.apiKey),
|
|
212
|
+
fString(4, "en"),
|
|
213
|
+
fString(5, osName()),
|
|
214
|
+
fString(7, VERSION_STRING),
|
|
215
|
+
fVarint(9, input.requestId),
|
|
216
|
+
fString(10, input.sessionId),
|
|
217
|
+
fString(12, "windsurf"),
|
|
218
|
+
fMessage(16, encodeTimestampBody()),
|
|
219
|
+
fString(25, input.triggerId),
|
|
220
|
+
fString(26, "Unset"),
|
|
221
|
+
fString(28, "windsurf"),
|
|
222
|
+
];
|
|
223
|
+
if (input.userJwt) parts.push(fString(21, input.userJwt));
|
|
224
|
+
return concatProto(parts);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function concatProto(parts: Uint8Array[]): Uint8Array {
|
|
228
|
+
const len = parts.reduce((a, p) => a + p.length, 0);
|
|
229
|
+
const out = new Uint8Array(len);
|
|
230
|
+
let off = 0;
|
|
231
|
+
for (const p of parts) { out.set(p, off); off += p.length; }
|
|
232
|
+
return out;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Server-returned `customApiServerUrl` → trusted cascade host.
|
|
237
|
+
*
|
|
238
|
+
* The minted user_jwt AND the long-lived session token (Metadata.apiKey) are
|
|
239
|
+
* sent to this host, so a hostile or compromised response must not be able to
|
|
240
|
+
* point us at an arbitrary server. Accept only https URLs whose hostname is a
|
|
241
|
+
* plain DNS name (no userinfo, no path/query/fragment, no IP literal) and
|
|
242
|
+
* which sits under the same registrable domain as the trusted host or under
|
|
243
|
+
* `codeium.com` / `devin.ai` / `cognition.ai`. Otherwise keep `fallback`.
|
|
244
|
+
*/
|
|
245
|
+
export function sanitizeCascadeHost(customHost: string | undefined, fallback: string): string {
|
|
246
|
+
if (!customHost) return fallback;
|
|
247
|
+
let raw = customHost.trim();
|
|
248
|
+
// tolerate a bare hostname; refuse anything that already carries a scheme
|
|
249
|
+
// other than https, userinfo, a path, or a query/fragment.
|
|
250
|
+
raw = raw.replace(/^https:\/\//i, "");
|
|
251
|
+
if (raw.includes("/") || raw.includes("?") || raw.includes("#") || raw.includes("@")) return fallback;
|
|
252
|
+
if (raw.includes("://") || raw.includes("\\")) return fallback;
|
|
253
|
+
const [hostname, port] = raw.split(":", 2);
|
|
254
|
+
if (!hostname || !/^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$/i.test(hostname)) return fallback;
|
|
255
|
+
if (hostname.includes("..")) return fallback;
|
|
256
|
+
if (/^[0-9.]+$/.test(hostname)) return fallback; // IP literals
|
|
257
|
+
if (hostname === "localhost" || hostname.endsWith(".localhost")) return fallback;
|
|
258
|
+
if (port && !/^\d{1,5}$/.test(port)) return fallback;
|
|
259
|
+
const trustedRoots = ["codeium.com", "devin.ai", "cognition.ai"];
|
|
260
|
+
let fallbackHost = "";
|
|
261
|
+
try { fallbackHost = new URL(fallback).hostname; } catch { fallbackHost = fallback; }
|
|
262
|
+
const allowed = (h: string, root: string) => h === root || h.endsWith(`.${root}`);
|
|
263
|
+
const roots = new Set([...trustedRoots]);
|
|
264
|
+
// allow subdomains of the trusted host itself (e.g. tenant.codeium.com
|
|
265
|
+
// when trusted host is server.codeium.com) — never a different domain.
|
|
266
|
+
const fallbackParts = fallbackHost.split(".");
|
|
267
|
+
if (fallbackParts.length >= 2) roots.add(fallbackParts.slice(-2).join("."));
|
|
268
|
+
const ok = [...roots].some((r) => allowed(hostname.toLowerCase(), r));
|
|
269
|
+
if (!ok) return fallback;
|
|
270
|
+
const portSuffix = port ? `:${port}` : "";
|
|
271
|
+
return `https://${hostname.toLowerCase()}${portSuffix}`;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Mint a short-lived user_jwt from the session token. */
|
|
275
|
+
export async function mintUserJwt(sessionToken: string, host = DEFAULT_CASCADE_HOST): Promise<UserJwt> {
|
|
276
|
+
const t0 = Date.now();
|
|
277
|
+
log.info("auth.mint.start", { host, token: redact(sessionToken) });
|
|
278
|
+
const body = fMessage(1, encodeMetadata({
|
|
279
|
+
apiKey: sessionToken,
|
|
280
|
+
sessionId: randomUUID(),
|
|
281
|
+
requestId: BigInt(Date.now()),
|
|
282
|
+
triggerId: randomUUID(),
|
|
283
|
+
}));
|
|
284
|
+
const res = await fetch(`${host}/exa.auth_pb.AuthService/GetUserJwt`, {
|
|
285
|
+
method: "POST",
|
|
286
|
+
headers: {
|
|
287
|
+
"content-type": "application/proto",
|
|
288
|
+
"connect-protocol-version": "1",
|
|
289
|
+
},
|
|
290
|
+
body, // unary application/proto = raw proto body, no connect frame
|
|
291
|
+
});
|
|
292
|
+
if (!res.ok) {
|
|
293
|
+
const text = await res.text();
|
|
294
|
+
log.error("auth.mint.failed", { status: res.status, body: scrubSecrets(text.slice(0, 300)), ms: Date.now() - t0 });
|
|
295
|
+
throw new Error(`GetUserJwt failed: ${res.status} ${text}`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const raw = new Uint8Array(await res.arrayBuffer());
|
|
299
|
+
const msg = decodeMessage(raw); // unary response = raw proto body
|
|
300
|
+
const jwt = getString(msg, 1);
|
|
301
|
+
if (!jwt) {
|
|
302
|
+
log.error("auth.mint.nojwt", { host, bytes: raw.length });
|
|
303
|
+
throw new Error("GetUserJwt response missing jwt field");
|
|
304
|
+
}
|
|
305
|
+
const customHost = getString(msg, 2); // customApiServerUrl (tenant override)
|
|
306
|
+
const cascadeHost = sanitizeCascadeHost(customHost, host);
|
|
307
|
+
log.info("auth.mint.ok", { host, cascadeHost, customHost: customHost ?? null, jwt: redact(jwt) });
|
|
308
|
+
return {
|
|
309
|
+
jwt,
|
|
310
|
+
cascadeHost,
|
|
311
|
+
expiresAt: Date.now() + JWT_TTL_MS,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ---------- cached credential helper ----------
|
|
316
|
+
|
|
317
|
+
export class DevinCredentials {
|
|
318
|
+
private cached?: UserJwt;
|
|
319
|
+
|
|
320
|
+
constructor(private sessionToken: string, private host = DEFAULT_CASCADE_HOST) {}
|
|
321
|
+
|
|
322
|
+
/** Stable cache key (in-memory only — never logged). */
|
|
323
|
+
get key(): string {
|
|
324
|
+
return this.sessionToken;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Fresh user_jwt, re-minting on expiry. */
|
|
328
|
+
async jwt(): Promise<UserJwt> {
|
|
329
|
+
if (!this.cached || this.cached.expiresAt < Date.now()) {
|
|
330
|
+
this.cached = await mintUserJwt(this.sessionToken, this.host);
|
|
331
|
+
}
|
|
332
|
+
return this.cached;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Force re-mint (call after a 401). */
|
|
336
|
+
invalidate(): void {
|
|
337
|
+
this.cached = undefined;
|
|
338
|
+
}
|
|
339
|
+
}
|