offrouter-core 0.2.1 → 0.2.2
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/dist/audit.d.ts +137 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/audit.js +320 -0
- package/dist/audit.js.map +1 -0
- package/dist/auth/credential-chain.d.ts +84 -0
- package/dist/auth/credential-chain.d.ts.map +1 -0
- package/dist/auth/credential-chain.js +150 -0
- package/dist/auth/credential-chain.js.map +1 -0
- package/dist/auth/index.d.ts +10 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +7 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/keychain.d.ts +60 -0
- package/dist/auth/keychain.d.ts.map +1 -0
- package/dist/auth/keychain.js +104 -0
- package/dist/auth/keychain.js.map +1 -0
- package/dist/auth/oauth-pkce.d.ts +138 -0
- package/dist/auth/oauth-pkce.d.ts.map +1 -0
- package/dist/auth/oauth-pkce.js +375 -0
- package/dist/auth/oauth-pkce.js.map +1 -0
- package/dist/auth/oauth-server.d.ts +36 -0
- package/dist/auth/oauth-server.d.ts.map +1 -0
- package/dist/auth/oauth-server.js +210 -0
- package/dist/auth/oauth-server.js.map +1 -0
- package/dist/config.d.ts +76 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +355 -0
- package/dist/config.js.map +1 -0
- package/dist/delegation.d.ts +123 -0
- package/dist/delegation.d.ts.map +1 -0
- package/dist/delegation.js +455 -0
- package/dist/delegation.js.map +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/policy.d.ts +45 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +318 -0
- package/dist/policy.js.map +1 -0
- package/{src/protocol.ts → dist/protocol.d.ts} +2 -2
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +6 -0
- package/dist/protocol.js.map +1 -0
- package/dist/providers/adapter.d.ts +137 -0
- package/dist/providers/adapter.d.ts.map +1 -0
- package/dist/providers/adapter.js +163 -0
- package/dist/providers/adapter.js.map +1 -0
- package/dist/providers/catalog-data.d.ts +128 -0
- package/dist/providers/catalog-data.d.ts.map +1 -0
- package/dist/providers/catalog-data.js +397 -0
- package/dist/providers/catalog-data.js.map +1 -0
- package/dist/providers/catalog.d.ts +63 -0
- package/dist/providers/catalog.d.ts.map +1 -0
- package/dist/providers/catalog.js +394 -0
- package/dist/providers/catalog.js.map +1 -0
- package/dist/providers/fake.d.ts +46 -0
- package/dist/providers/fake.d.ts.map +1 -0
- package/dist/providers/fake.js +234 -0
- package/dist/providers/fake.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +65 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +434 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/proxy/responses-mapper.d.ts +366 -0
- package/dist/proxy/responses-mapper.d.ts.map +1 -0
- package/dist/proxy/responses-mapper.js +517 -0
- package/dist/proxy/responses-mapper.js.map +1 -0
- package/dist/proxy/responses-server.d.ts +29 -0
- package/dist/proxy/responses-server.d.ts.map +1 -0
- package/dist/proxy/responses-server.js +360 -0
- package/dist/proxy/responses-server.js.map +1 -0
- package/dist/router.d.ts +18 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +296 -0
- package/dist/router.js.map +1 -0
- package/dist/schemas.d.ts +560 -0
- package/dist/schemas.d.ts.map +1 -0
- package/{src/schemas.ts → dist/schemas.js} +83 -103
- package/dist/schemas.js.map +1 -0
- package/dist/secrets.d.ts +112 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +326 -0
- package/dist/secrets.js.map +1 -0
- package/dist/types.d.ts +117 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/usage-file.d.ts +59 -0
- package/dist/usage-file.d.ts.map +1 -0
- package/dist/usage-file.js +316 -0
- package/dist/usage-file.js.map +1 -0
- package/dist/usage.d.ts +235 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +517 -0
- package/dist/usage.js.map +1 -0
- package/package.json +7 -2
- package/src/audit.test.ts +0 -302
- package/src/audit.ts +0 -553
- package/src/auth/credential-chain.test.ts +0 -326
- package/src/auth/credential-chain.ts +0 -235
- package/src/auth/index.ts +0 -45
- package/src/auth/keychain.test.ts +0 -265
- package/src/auth/keychain.ts +0 -168
- package/src/auth/oauth-pkce.test.ts +0 -329
- package/src/auth/oauth-pkce.ts +0 -571
- package/src/auth/oauth-server.test.ts +0 -83
- package/src/auth/oauth-server.ts +0 -296
- package/src/config.test.ts +0 -479
- package/src/config.ts +0 -505
- package/src/delegation.test.ts +0 -368
- package/src/delegation.ts +0 -605
- package/src/index.ts +0 -280
- package/src/policy.test.ts +0 -634
- package/src/policy.ts +0 -420
- package/src/providers/adapter.test.ts +0 -293
- package/src/providers/adapter.ts +0 -328
- package/src/providers/catalog-data.test.ts +0 -258
- package/src/providers/catalog-data.ts +0 -498
- package/src/providers/catalog.test.ts +0 -84
- package/src/providers/catalog.ts +0 -483
- package/src/providers/fake.ts +0 -312
- package/src/providers/openai-compatible.test.ts +0 -366
- package/src/providers/openai-compatible.ts +0 -554
- package/src/proxy/responses-mapper.test.ts +0 -290
- package/src/proxy/responses-mapper.ts +0 -736
- package/src/proxy/responses-server.test.ts +0 -322
- package/src/proxy/responses-server.ts +0 -469
- package/src/router.test.ts +0 -699
- package/src/router.ts +0 -352
- package/src/schemas.test.ts +0 -291
- package/src/secrets.test.ts +0 -271
- package/src/secrets.ts +0 -461
- package/src/types.ts +0 -173
- package/src/usage-file.test.ts +0 -243
- package/src/usage-file.ts +0 -435
- package/src/usage.test.ts +0 -335
- package/src/usage.ts +0 -859
- package/tsconfig.json +0 -9
package/src/auth/oauth-server.ts
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local OAuth callback server for OffRouter's live PKCE login flow.
|
|
3
|
-
*
|
|
4
|
-
* Binds to 127.0.0.1 ONLY (never 0.0.0.0) so the provider can redirect back
|
|
5
|
-
* to this machine without exposing the callback to the network. The server
|
|
6
|
-
* captures the authorization `code` + `state` from the redirect and hands them
|
|
7
|
-
* to the caller; it never touches tokens, secrets, or the network beyond the
|
|
8
|
-
* single loopback HTTP socket.
|
|
9
|
-
*/
|
|
10
|
-
import { createServer, type Server, type IncomingMessage, type ServerResponse } from "node:http";
|
|
11
|
-
import { URL } from "node:url";
|
|
12
|
-
|
|
13
|
-
/** Loopback-only hostname. Hardcoded to prevent accidental 0.0.0.0 binds. */
|
|
14
|
-
const LOOPBACK_HOST = "127.0.0.1";
|
|
15
|
-
|
|
16
|
-
/** Path the provider redirects to (http://127.0.0.1:PORT/callback). */
|
|
17
|
-
const CALLBACK_PATH = "/callback";
|
|
18
|
-
|
|
19
|
-
export interface CallbackServerOptions {
|
|
20
|
-
/**
|
|
21
|
-
* State value expected from the provider. Validated in waitForCode against
|
|
22
|
-
* the state returned by the redirect; a mismatch rejects the promise.
|
|
23
|
-
* May also be supplied per-call to waitForCode.
|
|
24
|
-
*/
|
|
25
|
-
expectedState?: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface WaitForCodeOptions {
|
|
29
|
-
/** Overrides the server-level expectedState for this call. */
|
|
30
|
-
expectedState?: string;
|
|
31
|
-
/** How long to wait for a callback before rejecting (default 5 minutes). */
|
|
32
|
-
timeoutMs?: number;
|
|
33
|
-
/** Optional abort signal to cancel the wait early. */
|
|
34
|
-
signal?: AbortSignal;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface CallbackCode {
|
|
38
|
-
code: string;
|
|
39
|
-
state: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface CallbackServerHandle {
|
|
43
|
-
/** Full redirect_uri to register with the provider (http://127.0.0.1:PORT/callback). */
|
|
44
|
-
url: string;
|
|
45
|
-
/** Actual bound port (useful when 0 was requested). */
|
|
46
|
-
port: number;
|
|
47
|
-
/** Resolve with {code, state} on a valid callback; reject on timeout/state mismatch. */
|
|
48
|
-
waitForCode(options?: WaitForCodeOptions): Promise<CallbackCode>;
|
|
49
|
-
/** Stop listening and free the port. Idempotent. */
|
|
50
|
-
close(): Promise<void>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Start a loopback HTTP server that captures an OAuth authorization code.
|
|
55
|
-
* Pass port 0 to let the OS choose a free port (read handle.port afterwards).
|
|
56
|
-
*/
|
|
57
|
-
export function startCallbackServer(
|
|
58
|
-
port: number,
|
|
59
|
-
options: CallbackServerOptions = {},
|
|
60
|
-
): Promise<CallbackServerHandle> {
|
|
61
|
-
return new Promise((resolve, reject) => {
|
|
62
|
-
const serverLevelState = options.expectedState;
|
|
63
|
-
|
|
64
|
-
// Deferred for the first valid callback (has a `code` param).
|
|
65
|
-
let captured: CallbackCode | null = null;
|
|
66
|
-
const waiters: Array<{
|
|
67
|
-
resolve: (value: CallbackCode) => void;
|
|
68
|
-
reject: (error: Error) => void;
|
|
69
|
-
}> = [];
|
|
70
|
-
|
|
71
|
-
const httpServer: Server = createServer(
|
|
72
|
-
(req: IncomingMessage, res: ServerResponse) => {
|
|
73
|
-
handleCallback(req, res);
|
|
74
|
-
},
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
function handleCallback(req: IncomingMessage, res: ServerResponse): void {
|
|
78
|
-
// Only respond to GET on the callback path; everything else 404s.
|
|
79
|
-
if (req.method !== "GET") {
|
|
80
|
-
res.writeHead(405, { "content-type": "text/plain; charset=utf-8" });
|
|
81
|
-
res.end("Method Not Allowed");
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let parsedUrl: URL;
|
|
86
|
-
try {
|
|
87
|
-
parsedUrl = new URL(req.url ?? "/", `http://${LOOPBACK_HOST}`);
|
|
88
|
-
} catch {
|
|
89
|
-
res.writeHead(400, { "content-type": "text/plain; charset=utf-8" });
|
|
90
|
-
res.end("Bad Request");
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const pathname = parsedUrl.pathname;
|
|
95
|
-
const code = parsedUrl.searchParams.get("code");
|
|
96
|
-
const state = parsedUrl.searchParams.get("state");
|
|
97
|
-
const error = parsedUrl.searchParams.get("error");
|
|
98
|
-
|
|
99
|
-
// OAuth error from the provider (e.g. user denied consent).
|
|
100
|
-
if (error) {
|
|
101
|
-
res.writeHead(400, { "content-type": "text/html; charset=utf-8" });
|
|
102
|
-
res.end(authorizationDeniedPage(error));
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (pathname !== CALLBACK_PATH) {
|
|
107
|
-
res.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
|
|
108
|
-
res.end("Not Found");
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Missing code -> provider returned nothing usable.
|
|
113
|
-
if (!code) {
|
|
114
|
-
res.writeHead(400, { "content-type": "text/html; charset=utf-8" });
|
|
115
|
-
res.end(missingCodePage());
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Capture the first valid callback exactly once.
|
|
120
|
-
const callback: CallbackCode = { code, state: state ?? "" };
|
|
121
|
-
if (!captured) {
|
|
122
|
-
captured = callback;
|
|
123
|
-
// Resolve any pending waiters that accept this callback.
|
|
124
|
-
const pending = waiters.splice(0, waiters.length);
|
|
125
|
-
for (const w of pending) {
|
|
126
|
-
w.resolve(callback);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Always show the user a friendly page; security validation happens in
|
|
131
|
-
// waitForCode (state may not be known at request time).
|
|
132
|
-
res.writeHead(200, { "content-type": "text/html; charset=utf-8" });
|
|
133
|
-
res.end(successPage());
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
httpServer.on("error", (err: NodeJS.ErrnoException) => {
|
|
137
|
-
reject(
|
|
138
|
-
new Error(
|
|
139
|
-
`Failed to start OAuth callback server on 127.0.0.1:${port}: ${err.message}`,
|
|
140
|
-
{ cause: err },
|
|
141
|
-
),
|
|
142
|
-
);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
httpServer.listen(port, LOOPBACK_HOST, () => {
|
|
146
|
-
const address = httpServer.address();
|
|
147
|
-
const boundPort =
|
|
148
|
-
address && typeof address === "object" ? address.port : port;
|
|
149
|
-
const redirectUrl = `http://${LOOPBACK_HOST}:${boundPort}${CALLBACK_PATH}`;
|
|
150
|
-
|
|
151
|
-
const handle: CallbackServerHandle = {
|
|
152
|
-
url: redirectUrl,
|
|
153
|
-
port: boundPort,
|
|
154
|
-
waitForCode(waitOpts: WaitForCodeOptions = {}): Promise<CallbackCode> {
|
|
155
|
-
const expected =
|
|
156
|
-
waitOpts.expectedState !== undefined
|
|
157
|
-
? waitOpts.expectedState
|
|
158
|
-
: serverLevelState;
|
|
159
|
-
const timeoutMs = waitOpts.timeoutMs ?? 5 * 60 * 1000;
|
|
160
|
-
|
|
161
|
-
return new Promise<CallbackCode>((resolveWait, rejectWait) => {
|
|
162
|
-
let settled = false;
|
|
163
|
-
|
|
164
|
-
const settle = (
|
|
165
|
-
outcome: CallbackCode | Error,
|
|
166
|
-
isReject: boolean,
|
|
167
|
-
): void => {
|
|
168
|
-
if (settled) return;
|
|
169
|
-
settled = true;
|
|
170
|
-
clearTimeout(timer);
|
|
171
|
-
waitOpts.signal?.removeEventListener("abort", onAbort);
|
|
172
|
-
if (isReject) {
|
|
173
|
-
rejectWait(outcome as Error);
|
|
174
|
-
} else {
|
|
175
|
-
resolveWait(outcome as CallbackCode);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const onAbort = (): void => {
|
|
180
|
-
settle(
|
|
181
|
-
new Error("OAuth callback wait was aborted."),
|
|
182
|
-
true,
|
|
183
|
-
);
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
const timer = setTimeout(() => {
|
|
187
|
-
settle(new Error("OAuth login timed out waiting for callback."), true);
|
|
188
|
-
}, timeoutMs);
|
|
189
|
-
|
|
190
|
-
if (waitOpts.signal?.aborted) {
|
|
191
|
-
settle(new Error("OAuth callback wait was aborted."), true);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
waitOpts.signal?.addEventListener("abort", onAbort);
|
|
195
|
-
|
|
196
|
-
const resolveWith = (callback: CallbackCode): void => {
|
|
197
|
-
if (expected !== undefined && callback.state !== expected) {
|
|
198
|
-
settle(
|
|
199
|
-
new Error(
|
|
200
|
-
`OAuth state mismatch: expected "${redactState(expected)}" ` +
|
|
201
|
-
`but received "${redactState(callback.state)}". ` +
|
|
202
|
-
`This may indicate a CSRF attempt or a stale login tab.`,
|
|
203
|
-
),
|
|
204
|
-
true,
|
|
205
|
-
);
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
settle(callback, false);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
if (captured) {
|
|
212
|
-
resolveWith(captured);
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
waiters.push({ resolve: resolveWith, reject: rejectWait });
|
|
217
|
-
});
|
|
218
|
-
},
|
|
219
|
-
close(): Promise<void> {
|
|
220
|
-
return new Promise<void>((resolveClose) => {
|
|
221
|
-
httpServer.close(() => resolveClose());
|
|
222
|
-
// close() waits for in-flight connections; closeAllConnections is
|
|
223
|
-
// available on Node 18+ and speeds shutdown without dropping the
|
|
224
|
-
// response the user's browser is mid-receiving.
|
|
225
|
-
// (Not called here to keep the success page delivery intact.)
|
|
226
|
-
});
|
|
227
|
-
},
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
resolve(handle);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// ---------------------------------------------------------------------------
|
|
236
|
-
// Static HTML pages (ASCII-only, no secret material)
|
|
237
|
-
// ---------------------------------------------------------------------------
|
|
238
|
-
|
|
239
|
-
function successPage(): string {
|
|
240
|
-
return [
|
|
241
|
-
"<!doctype html>",
|
|
242
|
-
"<html lang=\"en\">",
|
|
243
|
-
"<head><meta charset=\"utf-8\"><title>OffRouter login</title></head>",
|
|
244
|
-
"<body>",
|
|
245
|
-
"<h1>OffRouter login authorized</h1>",
|
|
246
|
-
"<p>You can close this tab and return to your terminal.</p>",
|
|
247
|
-
"</body>",
|
|
248
|
-
"</html>",
|
|
249
|
-
"",
|
|
250
|
-
].join("\n");
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function authorizationDeniedPage(error: string): string {
|
|
254
|
-
const safe = escapeHtml(error);
|
|
255
|
-
return [
|
|
256
|
-
"<!doctype html>",
|
|
257
|
-
"<html lang=\"en\">",
|
|
258
|
-
"<head><meta charset=\"utf-8\"><title>OffRouter login</title></head>",
|
|
259
|
-
"<body>",
|
|
260
|
-
"<h1>OffRouter login not completed</h1>",
|
|
261
|
-
`<p>The provider reported: ${safe}</p>`,
|
|
262
|
-
"<p>You can close this tab and try again from your terminal.</p>",
|
|
263
|
-
"</body>",
|
|
264
|
-
"</html>",
|
|
265
|
-
"",
|
|
266
|
-
].join("\n");
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function missingCodePage(): string {
|
|
270
|
-
return [
|
|
271
|
-
"<!doctype html>",
|
|
272
|
-
"<html lang=\"en\">",
|
|
273
|
-
"<head><meta charset=\"utf-8\"><title>OffRouter login</title></head>",
|
|
274
|
-
"<body>",
|
|
275
|
-
"<h1>OffRouter login incomplete</h1>",
|
|
276
|
-
"<p>The callback did not include an authorization code.</p>",
|
|
277
|
-
"</body>",
|
|
278
|
-
"</html>",
|
|
279
|
-
"",
|
|
280
|
-
].join("\n");
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function escapeHtml(value: string): string {
|
|
284
|
-
return value
|
|
285
|
-
.replace(/&/g, "&")
|
|
286
|
-
.replace(/</g, "<")
|
|
287
|
-
.replace(/>/g, ">")
|
|
288
|
-
.replace(/"/g, """)
|
|
289
|
-
.replace(/'/g, "'");
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/** Truncate a state token for error messages so it is never echoed in full. */
|
|
293
|
-
function redactState(state: string): string {
|
|
294
|
-
if (state.length <= 8) return "***";
|
|
295
|
-
return `${state.slice(0, 4)}...${state.slice(-4)}`;
|
|
296
|
-
}
|