@claude-flow/security 3.0.0-alpha.1 → 3.0.0-alpha.11
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 +292 -234
- package/__tests__/acceptance/security-compliance.test.ts +674 -674
- package/__tests__/authorization-propagator.test.ts +155 -0
- package/__tests__/credential-generator.test.ts +310 -310
- package/__tests__/fixtures/configurations.ts +425 -419
- package/__tests__/fixtures/index.ts +21 -21
- package/__tests__/helpers/create-mock.ts +469 -469
- package/__tests__/helpers/index.ts +32 -32
- package/__tests__/input-validator.test.ts +382 -381
- package/__tests__/integration/security-flow.test.ts +606 -606
- package/__tests__/keychain-adapter.test.ts +60 -0
- package/__tests__/oauth-browser.test.ts +13 -0
- package/__tests__/oauth-export-surface.test.ts +21 -0
- package/__tests__/oauth-pkce.test.ts +37 -0
- package/__tests__/password-hasher.test.ts +239 -239
- package/__tests__/path-validator.test.ts +302 -302
- package/__tests__/plugin-integrity-verifier.test.ts +199 -0
- package/__tests__/safe-executor.test.ts +292 -292
- package/__tests__/token-generator.test.ts +371 -371
- package/__tests__/tool-output-guardrail.test.ts +256 -0
- package/__tests__/unit/credential-generator.test.ts +182 -182
- package/__tests__/unit/password-hasher.test.ts +359 -359
- package/__tests__/unit/path-validator.test.ts +509 -509
- package/__tests__/unit/safe-executor.test.ts +667 -667
- package/__tests__/unit/token-generator.test.ts +310 -310
- package/dist/CVE-REMEDIATION.d.ts +17 -6
- package/dist/CVE-REMEDIATION.d.ts.map +1 -1
- package/dist/CVE-REMEDIATION.js +328 -12
- package/dist/CVE-REMEDIATION.js.map +1 -1
- package/dist/authorization/propagator.d.ts +148 -0
- package/dist/authorization/propagator.d.ts.map +1 -0
- package/dist/authorization/propagator.js +182 -0
- package/dist/authorization/propagator.js.map +1 -0
- package/dist/credential-generator.js +9 -9
- package/dist/domain/services/security-domain-service.js +1 -1
- package/dist/domain/services/security-domain-service.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -1
- package/dist/index.js.map +1 -1
- package/dist/keychain-adapter.d.ts +48 -0
- package/dist/keychain-adapter.d.ts.map +1 -0
- package/dist/keychain-adapter.js +0 -0
- package/dist/keychain-adapter.js.map +1 -0
- package/dist/oauth/browser.d.ts +31 -0
- package/dist/oauth/browser.d.ts.map +1 -0
- package/dist/oauth/browser.js +55 -0
- package/dist/oauth/browser.js.map +1 -0
- package/dist/oauth/callback-server.d.ts +37 -0
- package/dist/oauth/callback-server.d.ts.map +1 -0
- package/dist/oauth/callback-server.js +86 -0
- package/dist/oauth/callback-server.js.map +1 -0
- package/dist/oauth/client.d.ts +57 -0
- package/dist/oauth/client.d.ts.map +1 -0
- package/dist/oauth/client.js +120 -0
- package/dist/oauth/client.js.map +1 -0
- package/dist/oauth/pkce.d.ts +25 -0
- package/dist/oauth/pkce.d.ts.map +1 -0
- package/dist/oauth/pkce.js +36 -0
- package/dist/oauth/pkce.js.map +1 -0
- package/dist/password-hasher.d.ts.map +1 -1
- package/dist/password-hasher.js +8 -1
- package/dist/password-hasher.js.map +1 -1
- package/dist/plugins/integrity-verifier.d.ts +125 -0
- package/dist/plugins/integrity-verifier.d.ts.map +1 -0
- package/dist/plugins/integrity-verifier.js +186 -0
- package/dist/plugins/integrity-verifier.js.map +1 -0
- package/dist/safe-executor.d.ts +7 -0
- package/dist/safe-executor.d.ts.map +1 -1
- package/dist/safe-executor.js +21 -1
- package/dist/safe-executor.js.map +1 -1
- package/dist/tool-output-guardrail.d.ts +126 -0
- package/dist/tool-output-guardrail.d.ts.map +1 -0
- package/dist/tool-output-guardrail.js +257 -0
- package/dist/tool-output-guardrail.js.map +1 -0
- package/node_modules-incomplete-release-test/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
- package/package.json +33 -28
- package/scripts/verify-oauth-exports.mjs +29 -0
- package/src/CVE-REMEDIATION.ts +591 -251
- package/src/application/index.ts +10 -10
- package/src/application/services/security-application-service.ts +193 -193
- package/src/authorization/propagator.ts +279 -0
- package/src/credential-generator.ts +368 -368
- package/src/domain/entities/security-context.ts +173 -173
- package/src/domain/index.ts +17 -17
- package/src/domain/services/security-domain-service.ts +296 -296
- package/src/index.ts +340 -271
- package/src/input-validator.ts +466 -466
- package/src/keychain-adapter.ts +0 -0
- package/src/oauth/browser.ts +58 -0
- package/src/oauth/callback-server.ts +97 -0
- package/src/oauth/client.ts +153 -0
- package/src/oauth/pkce.ts +45 -0
- package/src/password-hasher.ts +277 -270
- package/src/path-validator.ts +525 -525
- package/src/plugins/integrity-verifier.ts +265 -0
- package/src/safe-executor.ts +546 -525
- package/src/token-generator.ts +463 -463
- package/src/tool-output-guardrail.ts +360 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -9
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +20 -0
- package/.agentic-flow/intelligence.json +0 -16
- package/__tests__/coverage/base.css +0 -224
- package/__tests__/coverage/block-navigation.js +0 -87
- package/__tests__/coverage/coverage-final.json +0 -11
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +0 -161
- package/__tests__/coverage/lcov-report/base.css +0 -224
- package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +0 -161
- package/__tests__/coverage/lcov-report/prettify.css +0 -1
- package/__tests__/coverage/lcov-report/prettify.js +0 -2
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +0 -210
- package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/lcov-report/src/index.html +0 -206
- package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
- package/__tests__/coverage/lcov.info +0 -3650
- package/__tests__/coverage/prettify.css +0 -1
- package/__tests__/coverage/prettify.js +0 -2
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +0 -210
- package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
- package/__tests__/coverage/src/application/services/index.html +0 -116
- package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
- package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
- package/__tests__/coverage/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
- package/__tests__/coverage/src/domain/services/index.html +0 -116
- package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
- package/__tests__/coverage/src/index.html +0 -206
- package/__tests__/coverage/src/input-validator.ts.html +0 -1483
- package/__tests__/coverage/src/password-hasher.ts.html +0 -895
- package/__tests__/coverage/src/path-validator.ts.html +0 -1660
- package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
- package/__tests__/coverage/src/token-generator.ts.html +0 -1474
|
Binary file
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opens the system default browser for the `ruflo auth login` flow. A
|
|
3
|
+
* TypeScript port of meta-proxy's `src/oauth/browser.rs`.
|
|
4
|
+
*
|
|
5
|
+
* Deliberately does NOT go through `SafeExecutor.execute()`: `SafeExecutor`'s
|
|
6
|
+
* blocked-argument patterns (`safe-executor.ts`'s `DEFAULT_BLOCKED_PATTERNS`)
|
|
7
|
+
* include a bare `&`, which is meant to catch shell-metacharacter injection in
|
|
8
|
+
* command-line-style arguments — but it also appears in every real OAuth
|
|
9
|
+
* authorize URL's query string (`?a=1&b=2&...`), so passing one through would
|
|
10
|
+
* false-positive-reject on every real invocation. That blocklist is the wrong
|
|
11
|
+
* tool for a URL argument: with `shell: false` (used here, same as
|
|
12
|
+
* `SafeExecutor`), a single argv element containing `&` is inert — there's no
|
|
13
|
+
* shell to interpret it. The actual safety property that matters is "the URL
|
|
14
|
+
* was constructed by us from validated components, never from raw external
|
|
15
|
+
* input" (this module's `authorizeUrl()` in `client.ts` is the only caller),
|
|
16
|
+
* which `assertSafeUrl` below checks directly instead.
|
|
17
|
+
*
|
|
18
|
+
* @module v3/security/oauth/browser
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { execFile } from 'node:child_process';
|
|
22
|
+
|
|
23
|
+
/** Throws if `url` isn't a well-formed https/http URL — the one check that matters here. */
|
|
24
|
+
function assertSafeUrl(url: string): void {
|
|
25
|
+
const parsed = new URL(url); // throws on malformed input
|
|
26
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
27
|
+
throw new Error(`refusing to open non-http(s) URL scheme: ${parsed.protocol}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Attempts to open `url` in the system default browser. Resolves whether or
|
|
33
|
+
* not a browser window actually appeared — this cannot be confirmed in
|
|
34
|
+
* general, which is why the caller always also prints the URL as a fallback.
|
|
35
|
+
*/
|
|
36
|
+
export function openBrowser(url: string): Promise<void> {
|
|
37
|
+
assertSafeUrl(url);
|
|
38
|
+
|
|
39
|
+
const { cmd, args } = browserCommand(url);
|
|
40
|
+
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
execFile(cmd, args, { shell: false, windowsHide: true }, () => resolve());
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Pure command selection, exported for platform regression tests. */
|
|
47
|
+
export function browserCommand(url: string, platform: NodeJS.Platform = process.platform): { cmd: string; args: string[] } {
|
|
48
|
+
assertSafeUrl(url);
|
|
49
|
+
if (platform === 'darwin') {
|
|
50
|
+
return { cmd: 'open', args: [url] };
|
|
51
|
+
} else if (platform === 'win32') {
|
|
52
|
+
// cmd.exe treats OAuth query-string `&` separators as command operators,
|
|
53
|
+
// truncating the URL before client_id. Use the URL handler without a shell.
|
|
54
|
+
return { cmd: 'rundll32.exe', args: ['url.dll,FileProtocolHandler', url] };
|
|
55
|
+
} else {
|
|
56
|
+
return { cmd: 'xdg-open', args: [url] };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ephemeral-port localhost callback server for the browser-based OAuth flow
|
|
3
|
+
* (ADR-306). A minimal single-request HTTP server — deliberately not a full
|
|
4
|
+
* framework for one GET route that lives for one request and then shuts
|
|
5
|
+
* down. A TypeScript port of meta-proxy's `src/oauth/callback_server.rs`.
|
|
6
|
+
*
|
|
7
|
+
* @module v3/security/oauth/callback-server
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createServer, type Server } from 'node:http';
|
|
11
|
+
import type { AddressInfo } from 'node:net';
|
|
12
|
+
|
|
13
|
+
export interface CallbackResult {
|
|
14
|
+
code: string | null;
|
|
15
|
+
state: string | null;
|
|
16
|
+
error: string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const SUCCESS_PAGE = `<html>
|
|
20
|
+
<body style="background:#0a0a0a; color:#f5f5f5; font-family:sans-serif;
|
|
21
|
+
display:flex; align-items:center; justify-content:center;
|
|
22
|
+
height:100vh; margin:0;">
|
|
23
|
+
<div style="text-align:center;">
|
|
24
|
+
<h1>✓ ruflo login successful</h1>
|
|
25
|
+
<p>You can close this tab and return to your terminal.</p>
|
|
26
|
+
</div>
|
|
27
|
+
</body>
|
|
28
|
+
</html>`;
|
|
29
|
+
|
|
30
|
+
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes, matches meta-proxy's CALLBACK_TIMEOUT
|
|
31
|
+
|
|
32
|
+
export class CallbackTimeoutError extends Error {
|
|
33
|
+
constructor() {
|
|
34
|
+
super('timed out waiting for OAuth callback');
|
|
35
|
+
this.name = 'CallbackTimeoutError';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class CallbackServer {
|
|
40
|
+
private constructor(
|
|
41
|
+
private readonly server: Server,
|
|
42
|
+
public readonly redirectUri: string,
|
|
43
|
+
public readonly port: number,
|
|
44
|
+
) {}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Binds `127.0.0.1:0` (OS-assigned ephemeral port) and returns the server
|
|
48
|
+
* plus the exact `redirect_uri` to send in the authorize request — must be
|
|
49
|
+
* the `http://127.0.0.1:<port>/oauth/callback` shape identity's
|
|
50
|
+
* `validate_redirect_uri` accepts.
|
|
51
|
+
*/
|
|
52
|
+
static async bind(): Promise<CallbackServer> {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
const server = createServer();
|
|
55
|
+
server.once('error', reject);
|
|
56
|
+
server.listen(0, '127.0.0.1', () => {
|
|
57
|
+
server.removeListener('error', reject);
|
|
58
|
+
const addr = server.address() as AddressInfo;
|
|
59
|
+
const redirectUri = `http://127.0.0.1:${addr.port}/oauth/callback`;
|
|
60
|
+
resolve(new CallbackServer(server, redirectUri, addr.port));
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Waits for exactly one `GET /oauth/callback?...` request, replies with the
|
|
67
|
+
* browser success page, closes the server, and returns the parsed query
|
|
68
|
+
* parameters. Times out after `waitForMs` so a login attempt the user
|
|
69
|
+
* abandons in the browser doesn't hang the CLI forever.
|
|
70
|
+
*/
|
|
71
|
+
async awaitCallback(waitForMs = DEFAULT_TIMEOUT_MS): Promise<CallbackResult> {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
const timer = setTimeout(() => {
|
|
74
|
+
this.server.close();
|
|
75
|
+
reject(new CallbackTimeoutError());
|
|
76
|
+
}, waitForMs);
|
|
77
|
+
|
|
78
|
+
this.server.once('request', (req, res) => {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
const url = new URL(req.url ?? '/oauth/callback', 'http://127.0.0.1');
|
|
81
|
+
const result: CallbackResult = {
|
|
82
|
+
code: url.searchParams.get('code'),
|
|
83
|
+
state: url.searchParams.get('state'),
|
|
84
|
+
error: url.searchParams.get('error'),
|
|
85
|
+
};
|
|
86
|
+
res.writeHead(200, {
|
|
87
|
+
'content-type': 'text/html; charset=utf-8',
|
|
88
|
+
connection: 'close',
|
|
89
|
+
});
|
|
90
|
+
res.end(SUCCESS_PAGE, () => {
|
|
91
|
+
this.server.close();
|
|
92
|
+
resolve(result);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for `auth.cognitum.one`'s OAuth surface (ADR-306): authorize
|
|
3
|
+
* URL construction, `POST /oauth/token` (`authorization_code` and
|
|
4
|
+
* `refresh_token` grants), and `POST /v1/oauth/code-exchange` (OOB fallback).
|
|
5
|
+
*
|
|
6
|
+
* A TypeScript port of meta-proxy's `src/oauth/client.rs`
|
|
7
|
+
* (cognitum-one/meta-proxy) — same base URL, same endpoints, same
|
|
8
|
+
* form/query parameter names, confirmed live 2026-07-16 (a real
|
|
9
|
+
* `GET /oauth/authorize?...&client_id=meta-proxy&redirect_uri=<ruflo-controlled
|
|
10
|
+
* loopback>` returns a working consent page, not a redirect_uri-mismatch
|
|
11
|
+
* error — so this client reuses meta-proxy's registered `client_id` rather
|
|
12
|
+
* than requiring a new one).
|
|
13
|
+
*
|
|
14
|
+
* Deliberately NOT `api.cognitum.one/v1/auth/*` — that's what ruflo's own
|
|
15
|
+
* checked-in OpenAPI spec (v3/docs/api/cognitum-v1.openapi.yaml, ADR-308)
|
|
16
|
+
* describes, but it does not match what the real identity server serves.
|
|
17
|
+
* This client targets the proven, live surface instead.
|
|
18
|
+
*
|
|
19
|
+
* @module v3/security/oauth/client
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export const CLIENT_ID = 'meta-proxy';
|
|
23
|
+
export const SCOPE = 'inference';
|
|
24
|
+
/** RFC 8252 out-of-band sentinel — the OOB/manual-paste flow's `redirect_uri`. */
|
|
25
|
+
export const OOB_REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
|
|
26
|
+
|
|
27
|
+
function authBaseUrl(): string {
|
|
28
|
+
const override = process.env.COGNITUM_AUTH_URL;
|
|
29
|
+
return override && override.trim() ? override : 'https://auth.cognitum.one';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface TokenResponse {
|
|
33
|
+
access_token: string;
|
|
34
|
+
token_type: string;
|
|
35
|
+
account_email?: string;
|
|
36
|
+
refresh_token?: string;
|
|
37
|
+
expires_in?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface OAuthErrorBody {
|
|
41
|
+
error: string;
|
|
42
|
+
error_description: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class OAuthError extends Error {
|
|
46
|
+
constructor(
|
|
47
|
+
message: string,
|
|
48
|
+
public readonly code: 'network' | 'protocol' | 'unexpected_shape',
|
|
49
|
+
public readonly oauthError?: string,
|
|
50
|
+
public readonly oauthDescription?: string,
|
|
51
|
+
) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = 'OAuthError';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Builds the `/oauth/authorize` URL for the standard loopback-redirect flow. */
|
|
58
|
+
export function authorizeUrl(redirectUri: string, state: string, codeChallenge: string): string {
|
|
59
|
+
const url = new URL(`${authBaseUrl()}/oauth/authorize`);
|
|
60
|
+
url.searchParams.set('response_type', 'code');
|
|
61
|
+
url.searchParams.set('client_id', CLIENT_ID);
|
|
62
|
+
url.searchParams.set('redirect_uri', redirectUri);
|
|
63
|
+
url.searchParams.set('scope', SCOPE);
|
|
64
|
+
url.searchParams.set('state', state);
|
|
65
|
+
url.searchParams.set('code_challenge', codeChallenge);
|
|
66
|
+
url.searchParams.set('code_challenge_method', 'S256');
|
|
67
|
+
return url.toString();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function parseTokenResponse(resp: Response): Promise<TokenResponse> {
|
|
71
|
+
if (resp.ok) {
|
|
72
|
+
try {
|
|
73
|
+
return (await resp.json()) as TokenResponse;
|
|
74
|
+
} catch {
|
|
75
|
+
throw new OAuthError('unexpected response shape from the server', 'unexpected_shape');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const body = (await resp.json()) as OAuthErrorBody;
|
|
80
|
+
throw new OAuthError(
|
|
81
|
+
`oauth error: ${body.error} — ${body.error_description}`,
|
|
82
|
+
'protocol',
|
|
83
|
+
body.error,
|
|
84
|
+
body.error_description,
|
|
85
|
+
);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if (e instanceof OAuthError) throw e;
|
|
88
|
+
throw new OAuthError('unexpected response shape from the server', 'unexpected_shape');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function postForm(path: string, form: Record<string, string>, base = authBaseUrl()): Promise<TokenResponse> {
|
|
93
|
+
let resp: Response;
|
|
94
|
+
try {
|
|
95
|
+
resp = await fetch(`${base}${path}`, {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
98
|
+
body: new URLSearchParams(form).toString(),
|
|
99
|
+
});
|
|
100
|
+
} catch (e) {
|
|
101
|
+
throw new OAuthError(`network error: ${e instanceof Error ? e.message : String(e)}`, 'network');
|
|
102
|
+
}
|
|
103
|
+
return parseTokenResponse(resp);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** `POST /oauth/token` with `grant_type=authorization_code`. */
|
|
107
|
+
export async function exchangeCode(
|
|
108
|
+
code: string,
|
|
109
|
+
codeVerifier: string,
|
|
110
|
+
redirectUri: string,
|
|
111
|
+
base = authBaseUrl(),
|
|
112
|
+
): Promise<TokenResponse> {
|
|
113
|
+
return postForm(
|
|
114
|
+
'/oauth/token',
|
|
115
|
+
{ grant_type: 'authorization_code', code, code_verifier: codeVerifier, client_id: CLIENT_ID, redirect_uri: redirectUri },
|
|
116
|
+
base,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* `POST /oauth/token` with `grant_type=refresh_token`. identity rotates
|
|
122
|
+
* refresh tokens with reuse detection: presenting a refresh token returns a
|
|
123
|
+
* NEW refresh token and revokes the old one, and re-presenting a spent one
|
|
124
|
+
* revokes the whole session family. Callers MUST persist the returned
|
|
125
|
+
* `refresh_token` atomically before using the new access token, and must
|
|
126
|
+
* never retry a failed refresh with the same token.
|
|
127
|
+
*/
|
|
128
|
+
export async function refreshToken(refreshTokenValue: string, base = authBaseUrl()): Promise<TokenResponse> {
|
|
129
|
+
return postForm('/oauth/token', { grant_type: 'refresh_token', refresh_token: refreshTokenValue, client_id: CLIENT_ID }, base);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* `POST /v1/oauth/code-exchange` — the OOB manual-entry fallback for
|
|
134
|
+
* headless/SSH/container environments where no browser round-trip to
|
|
135
|
+
* `127.0.0.1` is reachable.
|
|
136
|
+
*/
|
|
137
|
+
export async function exchangeManualCode(
|
|
138
|
+
code: string,
|
|
139
|
+
codeVerifier: string,
|
|
140
|
+
base = authBaseUrl(),
|
|
141
|
+
): Promise<TokenResponse> {
|
|
142
|
+
let resp: Response;
|
|
143
|
+
try {
|
|
144
|
+
resp = await fetch(`${base}/v1/oauth/code-exchange`, {
|
|
145
|
+
method: 'POST',
|
|
146
|
+
headers: { 'content-type': 'application/json' },
|
|
147
|
+
body: JSON.stringify({ code, code_verifier: codeVerifier, client_id: CLIENT_ID }),
|
|
148
|
+
});
|
|
149
|
+
} catch (e) {
|
|
150
|
+
throw new OAuthError(`network error: ${e instanceof Error ? e.message : String(e)}`, 'network');
|
|
151
|
+
}
|
|
152
|
+
return parseTokenResponse(resp);
|
|
153
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth 2.0 + PKCE (RFC 7636) client-side generation (ADR-306).
|
|
3
|
+
*
|
|
4
|
+
* A TypeScript port of meta-proxy's `src/oauth/pkce.rs` (cognitum-one/meta-proxy,
|
|
5
|
+
* itself ported from `dashboard/apps/cli/src/pkce.rs`) so a verifier generated
|
|
6
|
+
* here validates against the same `services/identity` authorization server
|
|
7
|
+
* meta-proxy already talks to successfully — same byte lengths, same encoding,
|
|
8
|
+
* same hash. Intentionally duplicated rather than imported from
|
|
9
|
+
* `@claude-flow/mcp/src/oauth.ts` (a different, unrelated OAuth client in this
|
|
10
|
+
* monorepo) — ADR-306 assigns PKCE ownership to this package specifically.
|
|
11
|
+
*
|
|
12
|
+
* @module v3/security/oauth/pkce
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { randomBytes, createHash } from 'crypto';
|
|
16
|
+
|
|
17
|
+
export interface PkceRequest {
|
|
18
|
+
state: string;
|
|
19
|
+
codeVerifier: string;
|
|
20
|
+
codeChallenge: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 32 random bytes, base64url-encoded (no padding) — comfortably within RFC
|
|
25
|
+
* 7636's 43-128 character verifier length requirement (32 bytes -> 43 chars).
|
|
26
|
+
* Matches the Rust port's `random_url_safe_token(32)` exactly.
|
|
27
|
+
*/
|
|
28
|
+
function randomUrlSafeToken(byteLen: number): string {
|
|
29
|
+
return randomBytes(byteLen).toString('base64url');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function challengeFromVerifier(verifier: string): string {
|
|
33
|
+
return createHash('sha256').update(verifier).digest('base64url');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Generates a fresh `state` + PKCE verifier/challenge pair for one login
|
|
38
|
+
* attempt.
|
|
39
|
+
*/
|
|
40
|
+
export function generate(): PkceRequest {
|
|
41
|
+
const state = randomUrlSafeToken(32);
|
|
42
|
+
const codeVerifier = randomUrlSafeToken(32);
|
|
43
|
+
const codeChallenge = challengeFromVerifier(codeVerifier);
|
|
44
|
+
return { state, codeVerifier, codeChallenge };
|
|
45
|
+
}
|