@clinebot/core 0.0.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/README.md +88 -0
- package/dist/account/cline-account-service.d.ts +34 -0
- package/dist/account/index.d.ts +3 -0
- package/dist/account/rpc.d.ts +38 -0
- package/dist/account/types.d.ts +74 -0
- package/dist/agents/agent-config-loader.d.ts +18 -0
- package/dist/agents/agent-config-parser.d.ts +25 -0
- package/dist/agents/hooks-config-loader.d.ts +23 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/plugin-config-loader.d.ts +22 -0
- package/dist/agents/plugin-loader.d.ts +9 -0
- package/dist/agents/plugin-sandbox.d.ts +12 -0
- package/dist/agents/unified-config-file-watcher.d.ts +77 -0
- package/dist/agents/user-instruction-config-loader.d.ts +63 -0
- package/dist/auth/client.d.ts +11 -0
- package/dist/auth/cline.d.ts +41 -0
- package/dist/auth/codex.d.ts +39 -0
- package/dist/auth/oca.d.ts +22 -0
- package/dist/auth/server.d.ts +22 -0
- package/dist/auth/types.d.ts +72 -0
- package/dist/auth/utils.d.ts +32 -0
- package/dist/chat/chat-schema.d.ts +145 -0
- package/dist/default-tools/constants.d.ts +23 -0
- package/dist/default-tools/definitions.d.ts +96 -0
- package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
- package/dist/default-tools/executors/apply-patch.d.ts +26 -0
- package/dist/default-tools/executors/bash.d.ts +49 -0
- package/dist/default-tools/executors/editor.d.ts +31 -0
- package/dist/default-tools/executors/file-read.d.ts +40 -0
- package/dist/default-tools/executors/index.d.ts +44 -0
- package/dist/default-tools/executors/search.d.ts +50 -0
- package/dist/default-tools/executors/web-fetch.d.ts +58 -0
- package/dist/default-tools/index.d.ts +57 -0
- package/dist/default-tools/presets.d.ts +124 -0
- package/dist/default-tools/schemas.d.ts +121 -0
- package/dist/default-tools/types.d.ts +237 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +220 -0
- package/dist/input/file-indexer.d.ts +5 -0
- package/dist/input/index.d.ts +4 -0
- package/dist/input/mention-enricher.d.ts +12 -0
- package/dist/mcp/config-loader.d.ts +15 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/manager.d.ts +24 -0
- package/dist/mcp/types.d.ts +66 -0
- package/dist/runtime/hook-file-hooks.d.ts +18 -0
- package/dist/runtime/rules.d.ts +5 -0
- package/dist/runtime/runtime-builder.d.ts +5 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
- package/dist/runtime/session-runtime.d.ts +36 -0
- package/dist/runtime/tool-approval.d.ts +9 -0
- package/dist/runtime/workflows.d.ts +13 -0
- package/dist/server/index.d.ts +47 -0
- package/dist/server/index.js +641 -0
- package/dist/session/default-session-manager.d.ts +77 -0
- package/dist/session/rpc-session-service.d.ts +12 -0
- package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
- package/dist/session/session-artifacts.d.ts +19 -0
- package/dist/session/session-graph.d.ts +15 -0
- package/dist/session/session-host.d.ts +21 -0
- package/dist/session/session-manager.d.ts +50 -0
- package/dist/session/session-manifest.d.ts +30 -0
- package/dist/session/session-service.d.ts +113 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
- package/dist/session/unified-session-persistence-service.d.ts +93 -0
- package/dist/session/workspace-manager.d.ts +28 -0
- package/dist/session/workspace-manifest.d.ts +25 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
- package/dist/storage/provider-settings-manager.d.ts +20 -0
- package/dist/storage/sqlite-session-store.d.ts +29 -0
- package/dist/storage/sqlite-team-store.d.ts +31 -0
- package/dist/storage/team-store.d.ts +2 -0
- package/dist/team/index.d.ts +1 -0
- package/dist/team/projections.d.ts +8 -0
- package/dist/types/common.d.ts +10 -0
- package/dist/types/config.d.ts +37 -0
- package/dist/types/events.d.ts +54 -0
- package/dist/types/provider-settings.d.ts +20 -0
- package/dist/types/sessions.d.ts +9 -0
- package/dist/types/storage.d.ts +37 -0
- package/dist/types/workspace.d.ts +7 -0
- package/dist/types.d.ts +26 -0
- package/package.json +63 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +267 -0
- package/src/account/index.ts +20 -0
- package/src/account/rpc.test.ts +62 -0
- package/src/account/rpc.ts +172 -0
- package/src/account/types.ts +80 -0
- package/src/agents/agent-config-loader.test.ts +234 -0
- package/src/agents/agent-config-loader.ts +107 -0
- package/src/agents/agent-config-parser.ts +191 -0
- package/src/agents/hooks-config-loader.ts +97 -0
- package/src/agents/index.ts +84 -0
- package/src/agents/plugin-config-loader.test.ts +91 -0
- package/src/agents/plugin-config-loader.ts +160 -0
- package/src/agents/plugin-loader.test.ts +102 -0
- package/src/agents/plugin-loader.ts +105 -0
- package/src/agents/plugin-sandbox.test.ts +120 -0
- package/src/agents/plugin-sandbox.ts +471 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +414 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +466 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +546 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +78 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/default-tools/constants.ts +35 -0
- package/src/default-tools/definitions.test.ts +233 -0
- package/src/default-tools/definitions.ts +632 -0
- package/src/default-tools/executors/apply-patch-parser.ts +520 -0
- package/src/default-tools/executors/apply-patch.ts +359 -0
- package/src/default-tools/executors/bash.ts +205 -0
- package/src/default-tools/executors/editor.ts +231 -0
- package/src/default-tools/executors/file-read.test.ts +25 -0
- package/src/default-tools/executors/file-read.ts +94 -0
- package/src/default-tools/executors/index.ts +75 -0
- package/src/default-tools/executors/search.ts +278 -0
- package/src/default-tools/executors/web-fetch.ts +259 -0
- package/src/default-tools/index.ts +161 -0
- package/src/default-tools/presets.test.ts +63 -0
- package/src/default-tools/presets.ts +168 -0
- package/src/default-tools/schemas.ts +228 -0
- package/src/default-tools/types.ts +324 -0
- package/src/index.ts +119 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +87 -0
- package/src/input/file-indexer.ts +280 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +82 -0
- package/src/input/mention-enricher.ts +119 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/runtime/hook-file-hooks.test.ts +106 -0
- package/src/runtime/hook-file-hooks.ts +736 -0
- package/src/runtime/index.ts +27 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
- package/src/runtime/runtime-builder.test.ts +215 -0
- package/src/runtime/runtime-builder.ts +515 -0
- package/src/runtime/runtime-parity.test.ts +132 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
- package/src/runtime/session-runtime.ts +44 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +54 -0
- package/src/server/index.ts +282 -0
- package/src/session/default-session-manager.e2e.test.ts +354 -0
- package/src/session/default-session-manager.test.ts +816 -0
- package/src/session/default-session-manager.ts +1286 -0
- package/src/session/index.ts +37 -0
- package/src/session/rpc-session-service.ts +189 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +265 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-graph.ts +90 -0
- package/src/session/session-host.ts +190 -0
- package/src/session/session-manager.ts +56 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +610 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.ts +781 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
- package/src/storage/provider-settings-legacy-migration.ts +637 -0
- package/src/storage/provider-settings-manager.test.ts +111 -0
- package/src/storage/provider-settings-manager.ts +129 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +270 -0
- package/src/storage/sqlite-team-store.ts +443 -0
- package/src/storage/team-store.ts +5 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/types/common.ts +14 -0
- package/src/types/config.ts +64 -0
- package/src/types/events.ts +46 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +127 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
export interface OAuthCallbackPayload {
|
|
2
|
+
url: URL;
|
|
3
|
+
code?: string;
|
|
4
|
+
state?: string;
|
|
5
|
+
provider?: string;
|
|
6
|
+
error?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type Deferred<T> = {
|
|
10
|
+
promise: Promise<T>;
|
|
11
|
+
resolve: (value: T) => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function createDeferred<T>(): Deferred<T> {
|
|
15
|
+
let resolve!: (value: T) => void;
|
|
16
|
+
const promise = new Promise<T>((res) => {
|
|
17
|
+
resolve = res;
|
|
18
|
+
});
|
|
19
|
+
return { promise, resolve };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface LocalOAuthServerOptions {
|
|
23
|
+
host?: string;
|
|
24
|
+
ports: number[];
|
|
25
|
+
callbackPath: string;
|
|
26
|
+
timeoutMs?: number;
|
|
27
|
+
expectedState?: string;
|
|
28
|
+
successHtml?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface LocalOAuthServer {
|
|
32
|
+
callbackUrl: string;
|
|
33
|
+
waitForCallback: () => Promise<OAuthCallbackPayload | null>;
|
|
34
|
+
cancelWait: () => void;
|
|
35
|
+
close: () => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function startLocalOAuthServer(
|
|
39
|
+
options: LocalOAuthServerOptions,
|
|
40
|
+
): Promise<LocalOAuthServer> {
|
|
41
|
+
const http = await import("node:http");
|
|
42
|
+
|
|
43
|
+
const host = options.host ?? "127.0.0.1";
|
|
44
|
+
const timeoutMs = options.timeoutMs ?? 5 * 60 * 1000;
|
|
45
|
+
const successHtml = options.successHtml ?? HTML_CONTENT;
|
|
46
|
+
|
|
47
|
+
const deferred = createDeferred<OAuthCallbackPayload | null>();
|
|
48
|
+
let settled = false;
|
|
49
|
+
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
50
|
+
let activeServer: import("node:http").Server | null = null;
|
|
51
|
+
|
|
52
|
+
const settle = (value: OAuthCallbackPayload | null) => {
|
|
53
|
+
if (settled) return;
|
|
54
|
+
settled = true;
|
|
55
|
+
deferred.resolve(value);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const close = () => {
|
|
59
|
+
if (timeout) {
|
|
60
|
+
clearTimeout(timeout);
|
|
61
|
+
timeout = null;
|
|
62
|
+
}
|
|
63
|
+
if (activeServer) {
|
|
64
|
+
activeServer.close();
|
|
65
|
+
activeServer = null;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const waitForCallback = async () => {
|
|
70
|
+
timeout = setTimeout(() => {
|
|
71
|
+
close();
|
|
72
|
+
settle(null);
|
|
73
|
+
}, timeoutMs);
|
|
74
|
+
return deferred.promise;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
for (const port of options.ports) {
|
|
78
|
+
const server = http.createServer((req, res) => {
|
|
79
|
+
try {
|
|
80
|
+
const requestUrl = new URL(req.url || "", `http://${host}:${port}`);
|
|
81
|
+
if (requestUrl.pathname !== options.callbackPath) {
|
|
82
|
+
res.statusCode = 404;
|
|
83
|
+
res.end("Not found");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const payload: OAuthCallbackPayload = {
|
|
88
|
+
url: requestUrl,
|
|
89
|
+
code: requestUrl.searchParams.get("code") ?? undefined,
|
|
90
|
+
state: requestUrl.searchParams.get("state") ?? undefined,
|
|
91
|
+
provider: requestUrl.searchParams.get("provider") ?? undefined,
|
|
92
|
+
error: requestUrl.searchParams.get("error") ?? undefined,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
if (payload.error) {
|
|
96
|
+
res.statusCode = 400;
|
|
97
|
+
res.end(`Authentication failed: ${payload.error}`);
|
|
98
|
+
close();
|
|
99
|
+
settle(payload);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!payload.code) {
|
|
104
|
+
res.statusCode = 400;
|
|
105
|
+
res.end("Missing authorization code");
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (options.expectedState && payload.state !== options.expectedState) {
|
|
110
|
+
res.statusCode = 400;
|
|
111
|
+
res.end("State mismatch");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
res.statusCode = 200;
|
|
116
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
117
|
+
res.end(successHtml);
|
|
118
|
+
close();
|
|
119
|
+
settle(payload);
|
|
120
|
+
} catch {
|
|
121
|
+
res.statusCode = 500;
|
|
122
|
+
res.end("Internal error");
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const bindResult = await new Promise<{
|
|
127
|
+
bound: boolean;
|
|
128
|
+
error?: NodeJS.ErrnoException;
|
|
129
|
+
}>((resolve) => {
|
|
130
|
+
const onError = (error: NodeJS.ErrnoException) => {
|
|
131
|
+
server.off("error", onError);
|
|
132
|
+
resolve({ bound: false, error });
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
server.once("error", onError);
|
|
136
|
+
server.listen(port, host, () => {
|
|
137
|
+
server.off("error", onError);
|
|
138
|
+
activeServer = server;
|
|
139
|
+
resolve({ bound: true });
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
if (bindResult.error) {
|
|
144
|
+
if (bindResult.error.code === "EADDRINUSE") {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
close();
|
|
148
|
+
throw bindResult.error;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (bindResult.bound) {
|
|
152
|
+
return {
|
|
153
|
+
callbackUrl: `http://${host}:${port}${options.callbackPath}`,
|
|
154
|
+
waitForCallback,
|
|
155
|
+
cancelWait: () => {
|
|
156
|
+
close();
|
|
157
|
+
settle(null);
|
|
158
|
+
},
|
|
159
|
+
close: () => {
|
|
160
|
+
close();
|
|
161
|
+
settle(null);
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
callbackUrl: "",
|
|
169
|
+
waitForCallback: async () => null,
|
|
170
|
+
cancelWait: () => {},
|
|
171
|
+
close: () => {},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const HTML_CONTENT = `<!DOCTYPE html>
|
|
176
|
+
<html lang="en">
|
|
177
|
+
<head>
|
|
178
|
+
<meta charset="utf-8">
|
|
179
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
180
|
+
<title>Authentication Successful</title>
|
|
181
|
+
<style>
|
|
182
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
183
|
+
body {
|
|
184
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
|
185
|
+
min-height: 100vh;
|
|
186
|
+
display: flex;
|
|
187
|
+
align-items: center;
|
|
188
|
+
justify-content: center;
|
|
189
|
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
|
190
|
+
color: #fff;
|
|
191
|
+
}
|
|
192
|
+
.container { text-align: center; padding: 48px; max-width: 420px; }
|
|
193
|
+
.icon {
|
|
194
|
+
width: 72px; height: 72px; margin: 0 auto 24px;
|
|
195
|
+
background: linear-gradient(135deg, #10a37f 0%, #1a7f64 100%);
|
|
196
|
+
border-radius: 50%;
|
|
197
|
+
display: flex; align-items: center; justify-content: center;
|
|
198
|
+
}
|
|
199
|
+
.icon svg { width: 36px; height: 36px; stroke: #fff; stroke-width: 3; fill: none; }
|
|
200
|
+
h1 { font-size: 24px; font-weight: 600; margin-bottom: 12px; }
|
|
201
|
+
p { font-size: 15px; color: rgba(255,255,255,0.7); line-height: 1.5; }
|
|
202
|
+
.closing { margin-top: 32px; font-size: 13px; color: rgba(255,255,255,0.5); }
|
|
203
|
+
</style>
|
|
204
|
+
</head>
|
|
205
|
+
<body>
|
|
206
|
+
<div class="container">
|
|
207
|
+
<div class="icon">
|
|
208
|
+
<svg viewBox="0 0 24 24"><polyline points="20 6 9 17 4 12"></polyline></svg>
|
|
209
|
+
</div>
|
|
210
|
+
<h1>Authentication Successful</h1>
|
|
211
|
+
<p>You're now signed in. You can close this window.</p>
|
|
212
|
+
<p class="closing">This window will close automatically...</p>
|
|
213
|
+
</div>
|
|
214
|
+
<script>setTimeout(() => window.close(), 3000);</script>
|
|
215
|
+
</body>
|
|
216
|
+
</html>`;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export interface OAuthPrompt {
|
|
2
|
+
message: string;
|
|
3
|
+
defaultValue?: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface OAuthCredentials {
|
|
7
|
+
access: string;
|
|
8
|
+
refresh: string;
|
|
9
|
+
/**
|
|
10
|
+
* Expiration timestamp in milliseconds since epoch.
|
|
11
|
+
*/
|
|
12
|
+
expires: number;
|
|
13
|
+
/**
|
|
14
|
+
* Optional provider-specific account identifier.
|
|
15
|
+
*/
|
|
16
|
+
accountId?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Optional email for display/telemetry.
|
|
19
|
+
*/
|
|
20
|
+
email?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Provider-specific metadata (user info, provider hint, etc).
|
|
23
|
+
*/
|
|
24
|
+
metadata?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface OAuthLoginCallbacks {
|
|
28
|
+
onAuth: (info: { url: string; instructions?: string }) => void;
|
|
29
|
+
onPrompt: (prompt: OAuthPrompt) => Promise<string>;
|
|
30
|
+
onProgress?: (message: string) => void;
|
|
31
|
+
onManualCodeInput?: () => Promise<string>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface OAuthProviderInterface {
|
|
35
|
+
id: string;
|
|
36
|
+
name: string;
|
|
37
|
+
usesCallbackServer: boolean;
|
|
38
|
+
login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials>;
|
|
39
|
+
refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials>;
|
|
40
|
+
getApiKey(credentials: OAuthCredentials): string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type OcaMode = "internal" | "external";
|
|
44
|
+
|
|
45
|
+
export interface OcaOAuthEnvironmentConfig {
|
|
46
|
+
clientId: string;
|
|
47
|
+
idcsUrl: string;
|
|
48
|
+
scopes: string;
|
|
49
|
+
baseUrl: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface OcaOAuthConfig {
|
|
53
|
+
internal: OcaOAuthEnvironmentConfig;
|
|
54
|
+
external: OcaOAuthEnvironmentConfig;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface OcaClientMetadata {
|
|
58
|
+
client?: string;
|
|
59
|
+
clientVersion?: string;
|
|
60
|
+
clientIde?: string;
|
|
61
|
+
clientIdeVersion?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface OcaOAuthProviderOptions {
|
|
65
|
+
config?: Partial<OcaOAuthConfig>;
|
|
66
|
+
mode?: OcaMode | (() => OcaMode);
|
|
67
|
+
callbackPath?: string;
|
|
68
|
+
callbackPorts?: number[];
|
|
69
|
+
requestTimeoutMs?: number;
|
|
70
|
+
refreshBufferMs?: number;
|
|
71
|
+
retryableTokenGraceMs?: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface OcaTokenResolution {
|
|
75
|
+
forceRefresh?: boolean;
|
|
76
|
+
refreshBufferMs?: number;
|
|
77
|
+
retryableTokenGraceMs?: number;
|
|
78
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
decodeJwtPayload,
|
|
4
|
+
isCredentialLikelyExpired,
|
|
5
|
+
parseAuthorizationInput,
|
|
6
|
+
parseOAuthError,
|
|
7
|
+
resolveAuthorizationCodeInput,
|
|
8
|
+
resolveUrl,
|
|
9
|
+
} from "./utils.js";
|
|
10
|
+
|
|
11
|
+
function toBase64Url(value: string): string {
|
|
12
|
+
return Buffer.from(value, "utf8").toString("base64url");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function createJwt(payload: Record<string, unknown>): string {
|
|
16
|
+
return `${toBase64Url(JSON.stringify({ alg: "none", typ: "JWT" }))}.${toBase64Url(JSON.stringify(payload))}.sig`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("auth/utils", () => {
|
|
20
|
+
it("parses auth input from full URL with provider", () => {
|
|
21
|
+
const parsed = parseAuthorizationInput(
|
|
22
|
+
"http://localhost/callback?code=test-code&state=s1&provider=google",
|
|
23
|
+
{
|
|
24
|
+
includeProvider: true,
|
|
25
|
+
},
|
|
26
|
+
);
|
|
27
|
+
expect(parsed).toEqual({
|
|
28
|
+
code: "test-code",
|
|
29
|
+
state: "s1",
|
|
30
|
+
provider: "google",
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("parses auth input from hash format when enabled", () => {
|
|
35
|
+
const parsed = parseAuthorizationInput("abc123#state1", {
|
|
36
|
+
allowHashCodeState: true,
|
|
37
|
+
});
|
|
38
|
+
expect(parsed).toEqual({ code: "abc123", state: "state1" });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("builds resolved URLs from base + path", () => {
|
|
42
|
+
expect(resolveUrl("https://example.com/", "/token")).toBe(
|
|
43
|
+
"https://example.com/token",
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("decodes JWT payload", () => {
|
|
48
|
+
const token = createJwt({ sub: "account-1", exp: 123 });
|
|
49
|
+
expect(decodeJwtPayload(token)).toMatchObject({
|
|
50
|
+
sub: "account-1",
|
|
51
|
+
exp: 123,
|
|
52
|
+
});
|
|
53
|
+
expect(decodeJwtPayload("invalid")).toBeNull();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("parses OAuth error payloads from string and object forms", () => {
|
|
57
|
+
expect(
|
|
58
|
+
parseOAuthError(
|
|
59
|
+
JSON.stringify({
|
|
60
|
+
error: "invalid_grant",
|
|
61
|
+
error_description: "expired",
|
|
62
|
+
}),
|
|
63
|
+
),
|
|
64
|
+
).toEqual({
|
|
65
|
+
code: "invalid_grant",
|
|
66
|
+
message: "expired",
|
|
67
|
+
});
|
|
68
|
+
expect(
|
|
69
|
+
parseOAuthError(
|
|
70
|
+
JSON.stringify({ error: { type: "unauthorized", message: "denied" } }),
|
|
71
|
+
),
|
|
72
|
+
).toEqual({
|
|
73
|
+
code: "unauthorized",
|
|
74
|
+
message: "denied",
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("resolves code from callback result", async () => {
|
|
79
|
+
const result = await resolveAuthorizationCodeInput({
|
|
80
|
+
waitForCallback: async () => ({
|
|
81
|
+
url: new URL("http://localhost"),
|
|
82
|
+
code: "from-callback",
|
|
83
|
+
state: "s1",
|
|
84
|
+
}),
|
|
85
|
+
cancelWait: () => {},
|
|
86
|
+
});
|
|
87
|
+
expect(result).toEqual({
|
|
88
|
+
code: "from-callback",
|
|
89
|
+
state: "s1",
|
|
90
|
+
provider: undefined,
|
|
91
|
+
error: undefined,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("resolves code from manual input when callback does not provide one", async () => {
|
|
96
|
+
const cancelWait = vi.fn();
|
|
97
|
+
const result = await resolveAuthorizationCodeInput({
|
|
98
|
+
waitForCallback: async () => null,
|
|
99
|
+
cancelWait,
|
|
100
|
+
onManualCodeInput: async () => "code=manual&state=manual-state",
|
|
101
|
+
});
|
|
102
|
+
expect(cancelWait).toHaveBeenCalled();
|
|
103
|
+
expect(result).toEqual({
|
|
104
|
+
code: "manual",
|
|
105
|
+
state: "manual-state",
|
|
106
|
+
provider: undefined,
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("throws when manual input rejects", async () => {
|
|
111
|
+
await expect(
|
|
112
|
+
resolveAuthorizationCodeInput({
|
|
113
|
+
waitForCallback: async () => null,
|
|
114
|
+
cancelWait: () => {},
|
|
115
|
+
onManualCodeInput: async () => {
|
|
116
|
+
throw new Error("cancelled");
|
|
117
|
+
},
|
|
118
|
+
}),
|
|
119
|
+
).rejects.toThrow("cancelled");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("checks token expiry with configurable buffer", () => {
|
|
123
|
+
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(1_000);
|
|
124
|
+
expect(isCredentialLikelyExpired({ expires: 1_500 }, 600)).toBe(true);
|
|
125
|
+
expect(isCredentialLikelyExpired({ expires: 3_000 }, 600)).toBe(false);
|
|
126
|
+
nowSpy.mockRestore();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import type { OAuthCallbackPayload } from "./server.js";
|
|
2
|
+
import type { OAuthCredentials } from "./types.js";
|
|
3
|
+
|
|
4
|
+
function base64UrlEncode(input: Uint8Array): string {
|
|
5
|
+
let binary = "";
|
|
6
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
7
|
+
binary += String.fromCharCode(input[i] ?? 0);
|
|
8
|
+
}
|
|
9
|
+
return btoa(binary)
|
|
10
|
+
.replace(/\+/g, "-")
|
|
11
|
+
.replace(/\//g, "_")
|
|
12
|
+
.replace(/=+$/g, "");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function sha256(value: string): Promise<Uint8Array> {
|
|
16
|
+
const encoder = new TextEncoder();
|
|
17
|
+
const data = encoder.encode(value);
|
|
18
|
+
const digest = await crypto.subtle.digest("SHA-256", data);
|
|
19
|
+
return new Uint8Array(digest);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function createVerifier(byteLength = 32): string {
|
|
23
|
+
const randomBytes = new Uint8Array(byteLength);
|
|
24
|
+
crypto.getRandomValues(randomBytes);
|
|
25
|
+
return base64UrlEncode(randomBytes);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function getProofKey(): Promise<{
|
|
29
|
+
verifier: string;
|
|
30
|
+
challenge: string;
|
|
31
|
+
}> {
|
|
32
|
+
const verifier = createVerifier();
|
|
33
|
+
const challenge = base64UrlEncode(await sha256(verifier));
|
|
34
|
+
return { verifier, challenge };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function normalizeBaseUrl(value: string): string {
|
|
38
|
+
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function resolveUrl(baseUrl: string, path: string): string {
|
|
42
|
+
return new URL(path, `${normalizeBaseUrl(baseUrl)}/`).toString();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ParsedAuthorizationInput = {
|
|
46
|
+
code?: string;
|
|
47
|
+
state?: string;
|
|
48
|
+
provider?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type ParseAuthorizationInputOptions = {
|
|
52
|
+
includeProvider?: boolean;
|
|
53
|
+
allowHashCodeState?: boolean;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export function parseAuthorizationInput(
|
|
57
|
+
input: string,
|
|
58
|
+
options: ParseAuthorizationInputOptions = {},
|
|
59
|
+
): ParsedAuthorizationInput {
|
|
60
|
+
const value = input.trim();
|
|
61
|
+
if (!value) {
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(value);
|
|
67
|
+
return {
|
|
68
|
+
code: url.searchParams.get("code") ?? undefined,
|
|
69
|
+
state: url.searchParams.get("state") ?? undefined,
|
|
70
|
+
provider: options.includeProvider
|
|
71
|
+
? (url.searchParams.get("provider") ?? undefined)
|
|
72
|
+
: undefined,
|
|
73
|
+
};
|
|
74
|
+
} catch {
|
|
75
|
+
// not a URL
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (options.allowHashCodeState && value.includes("#")) {
|
|
79
|
+
const [code, state] = value.split("#", 2);
|
|
80
|
+
return {
|
|
81
|
+
code: code || undefined,
|
|
82
|
+
state: state || undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (value.includes("code=")) {
|
|
87
|
+
const params = new URLSearchParams(value);
|
|
88
|
+
return {
|
|
89
|
+
code: params.get("code") ?? undefined,
|
|
90
|
+
state: params.get("state") ?? undefined,
|
|
91
|
+
provider: options.includeProvider
|
|
92
|
+
? (params.get("provider") ?? undefined)
|
|
93
|
+
: undefined,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return { code: value };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function decodeBase64(value: string): string | null {
|
|
101
|
+
if (typeof atob === "function") {
|
|
102
|
+
try {
|
|
103
|
+
return atob(value);
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof Buffer !== "undefined") {
|
|
110
|
+
try {
|
|
111
|
+
return Buffer.from(value, "base64").toString("utf8");
|
|
112
|
+
} catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function decodeJwtPayload(
|
|
121
|
+
token?: string,
|
|
122
|
+
): Record<string, unknown> | null {
|
|
123
|
+
if (!token) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
const parts = token.split(".");
|
|
129
|
+
if (parts.length !== 3) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const payload = parts[1];
|
|
134
|
+
if (!payload) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const normalized = payload.replace(/-/g, "+").replace(/_/g, "/");
|
|
139
|
+
const padded = normalized.padEnd(
|
|
140
|
+
normalized.length + ((4 - (normalized.length % 4)) % 4),
|
|
141
|
+
"=",
|
|
142
|
+
);
|
|
143
|
+
const decoded = decodeBase64(padded);
|
|
144
|
+
if (!decoded) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
return JSON.parse(decoded) as Record<string, unknown>;
|
|
148
|
+
} catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function parseOAuthError(text: string): {
|
|
154
|
+
code?: string;
|
|
155
|
+
message?: string;
|
|
156
|
+
} {
|
|
157
|
+
try {
|
|
158
|
+
const json = JSON.parse(text) as Record<string, unknown>;
|
|
159
|
+
const error = json.error;
|
|
160
|
+
const code =
|
|
161
|
+
typeof error === "string"
|
|
162
|
+
? error
|
|
163
|
+
: error &&
|
|
164
|
+
typeof error === "object" &&
|
|
165
|
+
typeof (error as Record<string, unknown>).type === "string"
|
|
166
|
+
? ((error as Record<string, unknown>).type as string)
|
|
167
|
+
: undefined;
|
|
168
|
+
const message =
|
|
169
|
+
typeof json.error_description === "string"
|
|
170
|
+
? json.error_description
|
|
171
|
+
: typeof json.message === "string"
|
|
172
|
+
? json.message
|
|
173
|
+
: error &&
|
|
174
|
+
typeof error === "object" &&
|
|
175
|
+
typeof (error as Record<string, unknown>).message === "string"
|
|
176
|
+
? ((error as Record<string, unknown>).message as string)
|
|
177
|
+
: undefined;
|
|
178
|
+
return { code, message };
|
|
179
|
+
} catch {
|
|
180
|
+
return {};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function isCredentialLikelyExpired(
|
|
185
|
+
credentials: Pick<OAuthCredentials, "expires">,
|
|
186
|
+
refreshBufferMs: number,
|
|
187
|
+
): boolean {
|
|
188
|
+
return Date.now() >= credentials.expires - refreshBufferMs;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function resolveAuthorizationCodeInput(input: {
|
|
192
|
+
waitForCallback: () => Promise<OAuthCallbackPayload | null>;
|
|
193
|
+
cancelWait: () => void;
|
|
194
|
+
onManualCodeInput?: () => Promise<string>;
|
|
195
|
+
parseOptions?: ParseAuthorizationInputOptions;
|
|
196
|
+
}): Promise<ParsedAuthorizationInput & { error?: string }> {
|
|
197
|
+
if (!input.onManualCodeInput) {
|
|
198
|
+
const callbackResult = await input.waitForCallback();
|
|
199
|
+
return {
|
|
200
|
+
code: callbackResult?.code,
|
|
201
|
+
state: callbackResult?.state,
|
|
202
|
+
provider: callbackResult?.provider,
|
|
203
|
+
error: callbackResult?.error,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let manualInput: string | undefined;
|
|
208
|
+
let manualError: Error | undefined;
|
|
209
|
+
const manualPromise = input
|
|
210
|
+
.onManualCodeInput()
|
|
211
|
+
.then((value) => {
|
|
212
|
+
manualInput = value;
|
|
213
|
+
input.cancelWait();
|
|
214
|
+
})
|
|
215
|
+
.catch((error: unknown) => {
|
|
216
|
+
manualError = error instanceof Error ? error : new Error(String(error));
|
|
217
|
+
input.cancelWait();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const callbackResult = await input.waitForCallback();
|
|
221
|
+
if (manualError) {
|
|
222
|
+
throw manualError;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (callbackResult?.code || callbackResult?.error) {
|
|
226
|
+
return {
|
|
227
|
+
code: callbackResult.code,
|
|
228
|
+
state: callbackResult.state,
|
|
229
|
+
provider: callbackResult.provider,
|
|
230
|
+
error: callbackResult.error,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (manualInput) {
|
|
235
|
+
return parseAuthorizationInput(manualInput, input.parseOptions);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
await manualPromise;
|
|
239
|
+
if (manualError) {
|
|
240
|
+
throw manualError;
|
|
241
|
+
}
|
|
242
|
+
if (manualInput) {
|
|
243
|
+
return parseAuthorizationInput(manualInput, input.parseOptions);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {};
|
|
247
|
+
}
|