@3ngram/server 0.7.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/LICENSE +202 -0
- package/dist/app.d.ts +72 -0
- package/dist/app.js +273 -0
- package/dist/app.js.map +1 -0
- package/dist/boot.d.ts +37 -0
- package/dist/boot.js +53 -0
- package/dist/boot.js.map +1 -0
- package/dist/composition/extension.d.ts +50 -0
- package/dist/composition/extension.js +16 -0
- package/dist/composition/extension.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/links.d.ts +17 -0
- package/dist/links.js +46 -0
- package/dist/links.js.map +1 -0
- package/dist/mailer.d.ts +58 -0
- package/dist/mailer.js +187 -0
- package/dist/mailer.js.map +1 -0
- package/dist/mcp/errors.d.ts +14 -0
- package/dist/mcp/errors.js +175 -0
- package/dist/mcp/errors.js.map +1 -0
- package/dist/mcp/prompts.d.ts +29 -0
- package/dist/mcp/prompts.js +180 -0
- package/dist/mcp/prompts.js.map +1 -0
- package/dist/mcp/server.d.ts +14 -0
- package/dist/mcp/server.js +34 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools-admin.d.ts +8 -0
- package/dist/mcp/tools-admin.js +260 -0
- package/dist/mcp/tools-admin.js.map +1 -0
- package/dist/mcp/tools-orient.d.ts +6 -0
- package/dist/mcp/tools-orient.js +110 -0
- package/dist/mcp/tools-orient.js.map +1 -0
- package/dist/mcp/tools.d.ts +88 -0
- package/dist/mcp/tools.js +375 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/middleware/api-key.d.ts +13 -0
- package/dist/middleware/api-key.js +83 -0
- package/dist/middleware/api-key.js.map +1 -0
- package/dist/middleware/api-or-session.d.ts +9 -0
- package/dist/middleware/api-or-session.js +22 -0
- package/dist/middleware/api-or-session.js.map +1 -0
- package/dist/middleware/authenticate.d.ts +21 -0
- package/dist/middleware/authenticate.js +51 -0
- package/dist/middleware/authenticate.js.map +1 -0
- package/dist/middleware/index.d.ts +1 -0
- package/dist/middleware/index.js +9 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/oauth-bearer.d.ts +8 -0
- package/dist/middleware/oauth-bearer.js +117 -0
- package/dist/middleware/oauth-bearer.js.map +1 -0
- package/dist/middleware/rate-limit.d.ts +58 -0
- package/dist/middleware/rate-limit.js +139 -0
- package/dist/middleware/rate-limit.js.map +1 -0
- package/dist/middleware/request-context.d.ts +7 -0
- package/dist/middleware/request-context.js +14 -0
- package/dist/middleware/request-context.js.map +1 -0
- package/dist/output-validation.d.ts +18 -0
- package/dist/output-validation.js +30 -0
- package/dist/output-validation.js.map +1 -0
- package/dist/redis.d.ts +9 -0
- package/dist/redis.js +37 -0
- package/dist/redis.js.map +1 -0
- package/dist/rest/cursor.d.ts +14 -0
- package/dist/rest/cursor.js +38 -0
- package/dist/rest/cursor.js.map +1 -0
- package/dist/rest/errors.d.ts +14 -0
- package/dist/rest/errors.js +119 -0
- package/dist/rest/errors.js.map +1 -0
- package/dist/rest/router.d.ts +33 -0
- package/dist/rest/router.js +846 -0
- package/dist/rest/router.js.map +1 -0
- package/dist/rest/scope.d.ts +14 -0
- package/dist/rest/scope.js +45 -0
- package/dist/rest/scope.js.map +1 -0
- package/dist/routes/api-keys.d.ts +2 -0
- package/dist/routes/api-keys.js +77 -0
- package/dist/routes/api-keys.js.map +1 -0
- package/dist/routes/auth.d.ts +20 -0
- package/dist/routes/auth.js +325 -0
- package/dist/routes/auth.js.map +1 -0
- package/dist/routes/capabilities.d.ts +10 -0
- package/dist/routes/capabilities.js +17 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/health.d.ts +7 -0
- package/dist/routes/health.js +12 -0
- package/dist/routes/health.js.map +1 -0
- package/dist/routes/mcp.d.ts +27 -0
- package/dist/routes/mcp.js +81 -0
- package/dist/routes/mcp.js.map +1 -0
- package/dist/routes/oauth-authorize.d.ts +8 -0
- package/dist/routes/oauth-authorize.js +245 -0
- package/dist/routes/oauth-authorize.js.map +1 -0
- package/dist/routes/oauth-clients.d.ts +2 -0
- package/dist/routes/oauth-clients.js +59 -0
- package/dist/routes/oauth-clients.js.map +1 -0
- package/dist/routes/oauth-register.d.ts +8 -0
- package/dist/routes/oauth-register.js +59 -0
- package/dist/routes/oauth-register.js.map +1 -0
- package/dist/routes/oauth-token.d.ts +30 -0
- package/dist/routes/oauth-token.js +451 -0
- package/dist/routes/oauth-token.js.map +1 -0
- package/dist/routes/onboarding.d.ts +2 -0
- package/dist/routes/onboarding.js +47 -0
- package/dist/routes/onboarding.js.map +1 -0
- package/dist/routes/profile.d.ts +2 -0
- package/dist/routes/profile.js +35 -0
- package/dist/routes/profile.js.map +1 -0
- package/dist/routes/well-known.d.ts +2 -0
- package/dist/routes/well-known.js +77 -0
- package/dist/routes/well-known.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +17 -0
- package/dist/version.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
/**
|
|
4
|
+
* Liveness only — no DB hit (the S5 capture hook polls this on every tool
|
|
5
|
+
* call; its latency budget rules out a round-trip). Readiness with a DB ping
|
|
6
|
+
* arrives with the first DB-backed route (Phase 1C2).
|
|
7
|
+
*/
|
|
8
|
+
export const healthRouter = Router();
|
|
9
|
+
healthRouter.get('/health', (_req, res) => {
|
|
10
|
+
res.json({ status: 'ok' });
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=health.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../src/routes/health.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEhC;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAW,MAAM,EAAE,CAAA;AAE5C,YAAY,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;IACxC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA;AAC5B,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AccessGate, BudgetEnforcement } from '@3ngram/core';
|
|
2
|
+
import type { Gateway } from '@3ngram/llm';
|
|
3
|
+
import { Router } from 'express';
|
|
4
|
+
import type { RateLimiterMiddleware } from '../middleware/rate-limit.js';
|
|
5
|
+
/** Options the boot wiring injects: the embedding gateway, undefined when not configured. */
|
|
6
|
+
export interface McpRouterOptions {
|
|
7
|
+
gateway: Gateway | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Per-user rate limiter. Runs AFTER oauthBearerAuth so it keys on the
|
|
10
|
+
* authenticated principal (req.userId). Injected by the app factory; tests drive
|
|
11
|
+
* it with an in-memory limiter so NO real Redis is required in CI.
|
|
12
|
+
*/
|
|
13
|
+
limiter: RateLimiterMiddleware;
|
|
14
|
+
/** Budget enforcement — gates metered MCP tools (remember/
|
|
15
|
+
* revise/search). The second of the two enforcement points. */
|
|
16
|
+
budget?: BudgetEnforcement | undefined;
|
|
17
|
+
/** Access gate — asserts read/write access on every MCP tool. */
|
|
18
|
+
access?: AccessGate | undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Build the /mcp router. Streamable HTTP handles POST (JSON-RPC), GET (SSE
|
|
22
|
+
* stream) and DELETE (session close) on one path; in stateless mode each is
|
|
23
|
+
* served by a per-request transport with no shared state. The SDK's
|
|
24
|
+
* handleRequest writes the response; on an unexpected pre-response failure we
|
|
25
|
+
* emit a generic 500 (no content) and never leak the error to the client.
|
|
26
|
+
*/
|
|
27
|
+
export declare function mcpRouter(options: McpRouterOptions): Router;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// MCP Streamable HTTP transport at /mcp.
|
|
3
|
+
//
|
|
4
|
+
// STATELESS by design: a fresh McpServer + StreamableHTTPServerTransport per
|
|
5
|
+
// request (sessionIdGenerator: undefined). No in-process session state — any
|
|
6
|
+
// instance serves any request (Railway redeploy = non-event).
|
|
7
|
+
//
|
|
8
|
+
// AUTH: BEARER-ONLY (strict RS). The route mounts BEHIND the
|
|
9
|
+
// existing oauthBearerAuth middleware ONLY — NOT the apiKeyAuth chain — so
|
|
10
|
+
// an X-API-Key alone never reaches a tool; a 401 without a valid Bearer carries
|
|
11
|
+
// the RFC 9728 WWW-Authenticate challenge the discovery doc already advertises.
|
|
12
|
+
// req.userId is the strict-aud-verified token subject by the time a handler runs.
|
|
13
|
+
//
|
|
14
|
+
// LAYERING: zero business logic here — the route wires the SDK
|
|
15
|
+
// transport to the per-request server and threads the authenticated tenant +
|
|
16
|
+
// optional gateway into the tools. Observability: nothing logged
|
|
17
|
+
// here echoes request content.
|
|
18
|
+
import { log } from '@3ngram/config';
|
|
19
|
+
import { crashSafeError } from '@3ngram/config/otel';
|
|
20
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
21
|
+
import { Router } from 'express';
|
|
22
|
+
import { createMcpServer } from '../mcp/server.js';
|
|
23
|
+
import { oauthBearerAuth } from '../middleware/oauth-bearer.js';
|
|
24
|
+
/**
|
|
25
|
+
* Build the /mcp router. Streamable HTTP handles POST (JSON-RPC), GET (SSE
|
|
26
|
+
* stream) and DELETE (session close) on one path; in stateless mode each is
|
|
27
|
+
* served by a per-request transport with no shared state. The SDK's
|
|
28
|
+
* handleRequest writes the response; on an unexpected pre-response failure we
|
|
29
|
+
* emit a generic 500 (no content) and never leak the error to the client.
|
|
30
|
+
*/
|
|
31
|
+
export function mcpRouter(options) {
|
|
32
|
+
const router = Router();
|
|
33
|
+
const handle = async (req, res) => {
|
|
34
|
+
// oauthBearerAuth guarantees req.userId + req.oauthScopes are bound before
|
|
35
|
+
// this runs. Scopes thread into the per-request context so each tool enforces
|
|
36
|
+
// its required scope (ADR-0009 §RS); fail-closed when the claim was absent
|
|
37
|
+
// (req.oauthScopes is then an empty array).
|
|
38
|
+
const server = createMcpServer({
|
|
39
|
+
userId: req.userId,
|
|
40
|
+
scopes: req.oauthScopes ?? [],
|
|
41
|
+
gateway: options.gateway,
|
|
42
|
+
budget: options.budget,
|
|
43
|
+
access: options.access,
|
|
44
|
+
});
|
|
45
|
+
// Stateless: OMIT sessionIdGenerator (optional) — its absence is what selects
|
|
46
|
+
// stateless mode, so no in-process session map is ever created.
|
|
47
|
+
const transport = new StreamableHTTPServerTransport({});
|
|
48
|
+
// Stateless: tear the per-request server + transport down when the response
|
|
49
|
+
// closes so nothing lingers in-process between requests.
|
|
50
|
+
res.on('close', () => {
|
|
51
|
+
transport.close().catch(() => undefined);
|
|
52
|
+
server.close().catch(() => undefined);
|
|
53
|
+
});
|
|
54
|
+
try {
|
|
55
|
+
// The SDK transport declares its optional callbacks as `T | undefined`,
|
|
56
|
+
// which trips exactOptionalPropertyTypes against the Transport interface's
|
|
57
|
+
// `?:` optionals; the shapes are otherwise identical (an SDK type quirk).
|
|
58
|
+
await server.connect(transport);
|
|
59
|
+
await transport.handleRequest(req, res, req.body);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
log().error(crashSafeError(err), 'mcp: transport error');
|
|
63
|
+
if (!res.headersSent) {
|
|
64
|
+
res.status(500).json({ error: 'internal_error' });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
// Bearer-only guard on every method (POST/GET/DELETE). The 401 challenge +
|
|
69
|
+
// WWW-Authenticate is the RFC 9728 client bootstrap. The per-user limiter runs
|
|
70
|
+
// AFTER auth so it keys on the verified req.userId (mcp-design.md per-user
|
|
71
|
+
// bucket); an unauthenticated request is rejected before it can consume a point.
|
|
72
|
+
router.all('/mcp', oauthBearerAuth, options.limiter, (req, res) => {
|
|
73
|
+
handle(req, res).catch((err) => {
|
|
74
|
+
log().error(crashSafeError(err), 'mcp: unhandled handler rejection');
|
|
75
|
+
if (!res.headersSent)
|
|
76
|
+
res.status(500).json({ error: 'internal_error' });
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
return router;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=mcp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../../src/routes/mcp.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,yCAAyC;AACzC,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,8DAA8D;AAC9D,EAAE;AACF,6DAA6D;AAC7D,2EAA2E;AAC3E,gFAAgF;AAChF,gFAAgF;AAChF,kFAAkF;AAClF,EAAE;AACF,+DAA+D;AAC/D,6EAA6E;AAC7E,iEAAiE;AACjE,+BAA+B;AAC/B,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAA;AAElG,OAAO,EAA+B,MAAM,EAAE,MAAM,SAAS,CAAA;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAmB/D;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,OAAyB;IACjD,MAAM,MAAM,GAAG,MAAM,EAAE,CAAA;IAEvB,MAAM,MAAM,GAAG,KAAK,EAAE,GAAY,EAAE,GAAa,EAAiB,EAAE;QAClE,2EAA2E;QAC3E,8EAA8E;QAC9E,2EAA2E;QAC3E,4CAA4C;QAC5C,MAAM,MAAM,GAAG,eAAe,CAAC;YAC7B,MAAM,EAAE,GAAG,CAAC,MAAgB;YAC5B,MAAM,EAAE,GAAG,CAAC,WAAW,IAAI,EAAE;YA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,CAAC,CAAC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE;YACtC,GAAG,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,kCAAkC,CAAC,CAAA;YACpE,IAAI,CAAC,GAAG,CAAC,WAAW;gBAAE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,CAAC,CAAA;QACzE,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import type { RateLimiterMiddleware } from '../middleware/rate-limit.js';
|
|
3
|
+
/** Options the boot wiring injects: the per-IP rate limiter seam. */
|
|
4
|
+
export interface OAuthAuthorizeRouterOptions {
|
|
5
|
+
limiter: RateLimiterMiddleware;
|
|
6
|
+
}
|
|
7
|
+
/** Build the /oauth/authorize router behind the injected per-IP limiter. */
|
|
8
|
+
export declare function oauthAuthorizeRouter(options: OAuthAuthorizeRouterOptions): Router;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// /oauth/authorize transport. Thin by contract: validate at the one boundary
|
|
3
|
+
// (authorizeRequestSchema / consentSubmissionSchema), delegate redirect-URI
|
|
4
|
+
// matching + code issuance to packages/core, render the combined
|
|
5
|
+
// credentials+consent form (ONE server-rendered form, no separate
|
|
6
|
+
// session/cookie infra; consent persistence is deferred).
|
|
7
|
+
//
|
|
8
|
+
// Status contract:
|
|
9
|
+
// GET 200 the consent form; 400 for a schema-invalid request, an unknown
|
|
10
|
+
// client_id, or a redirect_uri that does not BYTE-EXACTLY match a
|
|
11
|
+
// registered one (never a redirect to an unvetted URI — ADR-0009 impl
|
|
12
|
+
// req 3: exact match, no path/query/port relaxation).
|
|
13
|
+
// POST 302 to the registered redirect_uri with code+state on success;
|
|
14
|
+
// 400/403 (schema / CSRF) as above; 401 re-renders the form on wrong
|
|
15
|
+
// credentials (uniform — no user enumeration, login() handles timing).
|
|
16
|
+
//
|
|
17
|
+
// CSRF (ADR-0009 §4: same-site + token): a double-submit pair — the GET mints
|
|
18
|
+
// a CSPRNG token into BOTH a SameSite=Lax HttpOnly cookie and a hidden field;
|
|
19
|
+
// the POST requires them to match (timing-safe). Threat model per the ADR:
|
|
20
|
+
// the form shows the redirect HOST next to the client name (look-alike-name
|
|
21
|
+
// phishing) and only the two v1 scopes exist (consent fatigue).
|
|
22
|
+
//
|
|
23
|
+
// Never log credentials, codes, or CSRF tokens.
|
|
24
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
25
|
+
import { loadOAuthConfig } from '@3ngram/config';
|
|
26
|
+
import { createOAuthServerProvider, EmailNotVerifiedError, MEMORY_READ_SCOPE, MEMORY_WRITE_SCOPE, oauthClientsStore, resolveRegisteredRedirectUri, verifyCredentials, } from '@3ngram/core/auth';
|
|
27
|
+
import { authorizeRequestSchema, consentSubmissionSchema, } from '@3ngram/schema';
|
|
28
|
+
import { Router, urlencoded } from 'express';
|
|
29
|
+
const CSRF_COOKIE = 'oauth_csrf';
|
|
30
|
+
const CSRF_TTL_MS = 600_000;
|
|
31
|
+
/** Minimal cookie read for the one CSRF cookie — no cookie-parser dependency. */
|
|
32
|
+
function readCsrfCookie(header) {
|
|
33
|
+
if (header === undefined)
|
|
34
|
+
return undefined;
|
|
35
|
+
for (const part of header.split(';')) {
|
|
36
|
+
const eq = part.indexOf('=');
|
|
37
|
+
if (eq > 0 && part.slice(0, eq).trim() === CSRF_COOKIE)
|
|
38
|
+
return part.slice(eq + 1).trim();
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
/** Timing-safe equality over the double-submit pair. */
|
|
43
|
+
function csrfMatches(cookieValue, fieldValue) {
|
|
44
|
+
if (cookieValue === undefined)
|
|
45
|
+
return false;
|
|
46
|
+
const a = Buffer.from(cookieValue);
|
|
47
|
+
const b = Buffer.from(fieldValue);
|
|
48
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
49
|
+
}
|
|
50
|
+
/** Mint the CSRF pair: sets the same-site cookie, returns the hidden-field value. */
|
|
51
|
+
function issueCsrfToken(req, res) {
|
|
52
|
+
const token = randomBytes(32).toString('base64url');
|
|
53
|
+
res.cookie(CSRF_COOKIE, token, {
|
|
54
|
+
httpOnly: true,
|
|
55
|
+
sameSite: 'lax',
|
|
56
|
+
secure: req.secure,
|
|
57
|
+
maxAge: CSRF_TTL_MS,
|
|
58
|
+
path: '/oauth/authorize',
|
|
59
|
+
});
|
|
60
|
+
return token;
|
|
61
|
+
}
|
|
62
|
+
/** Escape a value for interpolation into HTML text or a double-quoted attribute. */
|
|
63
|
+
function escapeHtml(value) {
|
|
64
|
+
const map = {
|
|
65
|
+
'&': '&',
|
|
66
|
+
'<': '<',
|
|
67
|
+
'>': '>',
|
|
68
|
+
'"': '"',
|
|
69
|
+
"'": ''',
|
|
70
|
+
};
|
|
71
|
+
return value.replace(/[&<>"']/g, (c) => map[c]);
|
|
72
|
+
}
|
|
73
|
+
/** Serialize the OAuth request params as hidden fields (escaped; absent = omitted). */
|
|
74
|
+
function hiddenFields(params, redirectUri, redirectUriSupplied, csrfToken) {
|
|
75
|
+
const fields = {
|
|
76
|
+
client_id: params.client_id,
|
|
77
|
+
redirect_uri: redirectUri,
|
|
78
|
+
// RFC 6749 §4.1.3: the redirect_uri field above carries the
|
|
79
|
+
// RESOLVED URI, so its presence in the POST cannot reveal whether the client
|
|
80
|
+
// SUPPLIED redirect_uri at /authorize. Mint that fact here, GET-time, into a
|
|
81
|
+
// distinct field the POST reads back (see resolveConsentTarget).
|
|
82
|
+
redirect_uri_was_supplied: redirectUriSupplied ? '1' : undefined,
|
|
83
|
+
response_type: params.response_type,
|
|
84
|
+
code_challenge: params.code_challenge,
|
|
85
|
+
code_challenge_method: params.code_challenge_method,
|
|
86
|
+
scope: params.scope,
|
|
87
|
+
state: params.state,
|
|
88
|
+
resource: params.resource,
|
|
89
|
+
csrf_token: csrfToken,
|
|
90
|
+
};
|
|
91
|
+
return Object.entries(fields)
|
|
92
|
+
.filter((entry) => entry[1] !== undefined)
|
|
93
|
+
.map(([name, value]) => `<input type="hidden" name="${name}" value="${escapeHtml(value)}">`)
|
|
94
|
+
.join('\n ');
|
|
95
|
+
}
|
|
96
|
+
/** Render the combined credentials + consent page (client name, redirect HOST, scopes). */
|
|
97
|
+
function renderConsentPage(view) {
|
|
98
|
+
const scopes = view.params.scope ?? `${MEMORY_READ_SCOPE} ${MEMORY_WRITE_SCOPE}`;
|
|
99
|
+
const scopeItems = scopes
|
|
100
|
+
.split(' ')
|
|
101
|
+
.map((s) => `<li><code>${escapeHtml(s)}</code></li>`)
|
|
102
|
+
.join('');
|
|
103
|
+
const errorLine = view.error === undefined ? '' : `<p class="error">${escapeHtml(view.error)}</p>`;
|
|
104
|
+
return `<!doctype html>
|
|
105
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
|
106
|
+
<title>Authorize ${escapeHtml(view.client.client_name)} — 3ngram</title>
|
|
107
|
+
<style>body{font-family:system-ui,sans-serif;max-width:26rem;margin:4rem auto;padding:0 1rem;color:#1a1a1a}
|
|
108
|
+
label{display:block;margin:.75rem 0 .25rem}input{width:100%;padding:.5rem;box-sizing:border-box}
|
|
109
|
+
button{margin-top:1rem;padding:.6rem 1.2rem;cursor:pointer}.host{color:#555}.error{color:#b00020}</style></head>
|
|
110
|
+
<body>
|
|
111
|
+
<h1>Authorize access</h1>
|
|
112
|
+
<p><strong>${escapeHtml(view.client.client_name)}</strong>
|
|
113
|
+
<span class="host">(redirects to ${escapeHtml(new URL(view.redirectUri).host)})</span>
|
|
114
|
+
is requesting access to your 3ngram memory:</p>
|
|
115
|
+
<ul>${scopeItems}</ul>
|
|
116
|
+
${errorLine}
|
|
117
|
+
<form method="post" action="/oauth/authorize">
|
|
118
|
+
${hiddenFields(view.params, view.redirectUri, view.redirectUriSupplied, view.csrfToken)}
|
|
119
|
+
<label for="email">Email</label>
|
|
120
|
+
<input id="email" name="email" type="email" autocomplete="username" required>
|
|
121
|
+
<label for="password">Password</label>
|
|
122
|
+
<input id="password" name="password" type="password" autocomplete="current-password" required>
|
|
123
|
+
<button type="submit">Sign in & approve</button>
|
|
124
|
+
</form>
|
|
125
|
+
</body></html>`;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Resolve client + redirect URI for a validated request, or undefined when the
|
|
129
|
+
* client is unknown / the redirect_uri is not registered. The 400 is emitted by
|
|
130
|
+
* the caller — NEVER a redirect (the URI is unvetted by definition here).
|
|
131
|
+
*/
|
|
132
|
+
async function resolveConsentTarget(params, redirectUriSupplied) {
|
|
133
|
+
const client = await oauthClientsStore.getClient(params.client_id);
|
|
134
|
+
if (client === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
const redirectUri = resolveRegisteredRedirectUri(client, params.redirect_uri);
|
|
137
|
+
if (redirectUri === undefined)
|
|
138
|
+
return undefined;
|
|
139
|
+
// RFC 6749 §4.1.3: supplied-ness is captured at the INITIAL GET
|
|
140
|
+
// /authorize (whether the CLIENT's request carried redirect_uri) and threaded
|
|
141
|
+
// through the consent form — NOT derived from the POST's redirect_uri, which
|
|
142
|
+
// always carries the RESOLVED URI as a hidden field. Pure pass-through of the
|
|
143
|
+
// caller-supplied flag; the RFC decision stays in core.
|
|
144
|
+
return { client, redirectUri, redirectUriSupplied };
|
|
145
|
+
}
|
|
146
|
+
/** GET: validate, then serve the consent form with a fresh CSRF pair. */
|
|
147
|
+
async function handleAuthorizeGet(req, res) {
|
|
148
|
+
const parsed = authorizeRequestSchema.safeParse(req.query);
|
|
149
|
+
if (!parsed.success) {
|
|
150
|
+
res.status(400).json({ error: 'invalid_request' });
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Capture supplied-ness from the CLIENT's authorize request here, GET-time —
|
|
154
|
+
// this is the only point that sees whether redirect_uri was actually sent.
|
|
155
|
+
const target = await resolveConsentTarget(parsed.data, parsed.data.redirect_uri !== undefined);
|
|
156
|
+
if (target === undefined) {
|
|
157
|
+
res.status(400).json({ error: 'invalid_client' });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const csrfToken = issueCsrfToken(req, res);
|
|
161
|
+
res
|
|
162
|
+
.status(200)
|
|
163
|
+
.type('html')
|
|
164
|
+
.send(renderConsentPage({ ...target, params: parsed.data, csrfToken }));
|
|
165
|
+
}
|
|
166
|
+
/** POST: CSRF + credentials, then issue the code and 302 back with state. */
|
|
167
|
+
async function handleConsentPost(req, res) {
|
|
168
|
+
const parsed = consentSubmissionSchema.safeParse(req.body);
|
|
169
|
+
if (!parsed.success) {
|
|
170
|
+
res.status(400).json({ error: 'invalid_request' });
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
// Read supplied-ness from the GET-time hidden field — NOT from the POST's
|
|
174
|
+
// redirect_uri, which always carries the resolved URI.
|
|
175
|
+
const target = await resolveConsentTarget(parsed.data, parsed.data.redirect_uri_was_supplied === '1');
|
|
176
|
+
if (target === undefined) {
|
|
177
|
+
res.status(400).json({ error: 'invalid_client' });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (!csrfMatches(readCsrfCookie(req.header('cookie')), parsed.data.csrf_token)) {
|
|
181
|
+
res.status(403).json({ error: 'invalid_request' });
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const config = loadOAuthConfig();
|
|
185
|
+
if (parsed.data.resource !== undefined &&
|
|
186
|
+
new URL(parsed.data.resource).href !== config.resource) {
|
|
187
|
+
res.status(400).json({ error: 'invalid_target' });
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
// Consent only needs to confirm identity, not establish a session — calling
|
|
191
|
+
// verifyCredentials (not login) avoids leaving an orphan sessions row whose
|
|
192
|
+
// plaintext token would be discarded.
|
|
193
|
+
let userId;
|
|
194
|
+
try {
|
|
195
|
+
userId = await verifyCredentials(parsed.data.email, parsed.data.password);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (!(error instanceof EmailNotVerifiedError))
|
|
199
|
+
throw error;
|
|
200
|
+
const csrfToken = issueCsrfToken(req, res);
|
|
201
|
+
res
|
|
202
|
+
.status(403)
|
|
203
|
+
.type('html')
|
|
204
|
+
.send(renderConsentPage({
|
|
205
|
+
...target,
|
|
206
|
+
params: parsed.data,
|
|
207
|
+
csrfToken,
|
|
208
|
+
error: 'Verify your email before connecting a client.',
|
|
209
|
+
}));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (userId === undefined) {
|
|
213
|
+
const csrfToken = issueCsrfToken(req, res);
|
|
214
|
+
res
|
|
215
|
+
.status(401)
|
|
216
|
+
.type('html')
|
|
217
|
+
.send(renderConsentPage({
|
|
218
|
+
...target,
|
|
219
|
+
params: parsed.data,
|
|
220
|
+
csrfToken,
|
|
221
|
+
error: 'Invalid email or password.',
|
|
222
|
+
}));
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
await createOAuthServerProvider(config).authorize(target.client, {
|
|
226
|
+
userId,
|
|
227
|
+
codeChallenge: parsed.data.code_challenge,
|
|
228
|
+
redirectUri: target.redirectUri,
|
|
229
|
+
redirectUriSupplied: target.redirectUriSupplied,
|
|
230
|
+
...(parsed.data.scope === undefined ? {} : { scopes: parsed.data.scope.split(' ') }),
|
|
231
|
+
...(parsed.data.state === undefined ? {} : { state: parsed.data.state }),
|
|
232
|
+
}, res);
|
|
233
|
+
}
|
|
234
|
+
/** Build the /oauth/authorize router behind the injected per-IP limiter. */
|
|
235
|
+
export function oauthAuthorizeRouter(options) {
|
|
236
|
+
const router = Router();
|
|
237
|
+
router.get('/oauth/authorize', options.limiter, (req, res, next) => {
|
|
238
|
+
handleAuthorizeGet(req, res).catch(next);
|
|
239
|
+
});
|
|
240
|
+
router.post('/oauth/authorize', options.limiter, urlencoded({ extended: false }), (req, res, next) => {
|
|
241
|
+
handleConsentPost(req, res).catch(next);
|
|
242
|
+
});
|
|
243
|
+
return router;
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=oauth-authorize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-authorize.js","sourceRoot":"","sources":["../../src/routes/oauth-authorize.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,6EAA6E;AAC7E,4EAA4E;AAC5E,iEAAiE;AACjE,kEAAkE;AAClE,0DAA0D;AAC1D,EAAE;AACF,mBAAmB;AACnB,4EAA4E;AAC5E,yEAAyE;AACzE,6EAA6E;AAC7E,6DAA6D;AAC7D,wEAAwE;AACxE,4EAA4E;AAC5E,8EAA8E;AAC9E,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,2EAA2E;AAC3E,4EAA4E;AAC5E,gEAAgE;AAChE,EAAE;AACF,gDAAgD;AAChD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAChD,OAAO,EACL,yBAAyB,EACzB,qBAAqB,EACrB,iBAAiB,EACjB,kBAAkB,EAElB,iBAAiB,EACjB,4BAA4B,EAC5B,iBAAiB,GAClB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAEL,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAA+B,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAQzE,MAAM,WAAW,GAAG,YAAY,CAAA;AAChC,MAAM,WAAW,GAAG,OAAO,CAAA;AAE3B,iFAAiF;AACjF,SAAS,cAAc,CAAC,MAA0B;IAChD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IAC1C,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACrC,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;AAC5E,MAAM,UAAU,oBAAoB,CAAC,OAAoC;IACvE,MAAM,MAAM,GAAG,MAAM,EAAE,CAAA;IACvB,MAAM,CAAC,GAAG,CAAC,kBAAkB,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACjE,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC1C,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,IAAI,CACT,kBAAkB,EAClB,OAAO,CAAC,OAAO,EACf,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,EAC/B,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACjB,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IACzC,CAAC,CACF,CAAA;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// OAuth grant-management transport (consent UI). Thin by
|
|
3
|
+
// contract: validate at the one boundary (oauthClientIdParamSchema),
|
|
4
|
+
// delegate listing / revocation to packages/core, shape the HTTP response. No
|
|
5
|
+
// business logic here. Both routes sit behind the C2 `authenticate` middleware,
|
|
6
|
+
// so req.userId is the session-authenticated owner and consent stays
|
|
7
|
+
// grant-scoped — a user can only ever see/revoke the clients THEY authorized
|
|
8
|
+
// (the db helpers RLS-scope the read/write to the caller's oauth_tokens).
|
|
9
|
+
//
|
|
10
|
+
// Status contract:
|
|
11
|
+
// GET /auth/oauth-clients 200 { clients: [...metadata] } — the
|
|
12
|
+
// caller's authorized clients; name + redirect hosts
|
|
13
|
+
// + authorizedAt only, NEVER any secret material.
|
|
14
|
+
// DELETE /auth/oauth-clients/:clientId 204 on revoke (caller's tokens for the
|
|
15
|
+
// client killed) AND when nothing matched (unknown,
|
|
16
|
+
// not-authorized, already revoked) — idempotent, so a
|
|
17
|
+
// double-click is never a spurious error; the global
|
|
18
|
+
// client row is never touched.
|
|
19
|
+
import { listAuthorizedClients, revokeAuthorizedClient } from '@3ngram/core/auth';
|
|
20
|
+
import { oauthClientIdParamSchema } from '@3ngram/schema';
|
|
21
|
+
import { Router } from 'express';
|
|
22
|
+
import { authenticate } from '../middleware/authenticate.js';
|
|
23
|
+
export const oauthClientsRouter = Router();
|
|
24
|
+
// authenticate guarantees req.userId is bound before each handler runs; the
|
|
25
|
+
// non-null assertion is safe because an unauthenticated request never reaches
|
|
26
|
+
// here (the middleware 401s first).
|
|
27
|
+
oauthClientsRouter.get('/auth/oauth-clients', authenticate, (req, res, next) => {
|
|
28
|
+
listAuthorizedClients(req.userId)
|
|
29
|
+
.then((clients) => {
|
|
30
|
+
res.status(200).json({
|
|
31
|
+
clients: clients.map((c) => ({
|
|
32
|
+
clientId: c.clientId,
|
|
33
|
+
clientName: c.clientName,
|
|
34
|
+
redirectHosts: c.redirectHosts,
|
|
35
|
+
authorizedAt: c.authorizedAt.toISOString(),
|
|
36
|
+
})),
|
|
37
|
+
});
|
|
38
|
+
})
|
|
39
|
+
.catch(next);
|
|
40
|
+
});
|
|
41
|
+
oauthClientsRouter.delete('/auth/oauth-clients/:clientId', authenticate, (req, res, next) => {
|
|
42
|
+
// A malformed (non-uuid) client_id can never match a stored grant; treat it
|
|
43
|
+
// as the same idempotent no-op as an unknown/already-revoked grant (204)
|
|
44
|
+
// rather than forwarding it where a uuid cast would raise a generic 500.
|
|
45
|
+
const clientId = oauthClientIdParamSchema.safeParse(req.params.clientId);
|
|
46
|
+
if (!clientId.success) {
|
|
47
|
+
res.status(204).end();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
revokeAuthorizedClient(req.userId, clientId.data)
|
|
51
|
+
.then(() => {
|
|
52
|
+
// Revoke is idempotent: whether a live grant was closed or none matched,
|
|
53
|
+
// the desired end state (the caller holds no grant for this client) now
|
|
54
|
+
// holds, so both map to 204.
|
|
55
|
+
res.status(204).end();
|
|
56
|
+
})
|
|
57
|
+
.catch(next);
|
|
58
|
+
});
|
|
59
|
+
//# sourceMappingURL=oauth-clients.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-clients.js","sourceRoot":"","sources":["../../src/routes/oauth-clients.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,yDAAyD;AACzD,qEAAqE;AACrE,8EAA8E;AAC9E,gFAAgF;AAChF,qEAAqE;AACrE,6EAA6E;AAC7E,0EAA0E;AAC1E,EAAE;AACF,mBAAmB;AACnB,+EAA+E;AAC/E,iFAAiF;AACjF,8EAA8E;AAC9E,iFAAiF;AACjF,gFAAgF;AAChF,kFAAkF;AAClF,iFAAiF;AACjF,2DAA2D;AAC3D,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AACjF,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AACzD,OAAO,EAAkD,MAAM,EAAE,MAAM,SAAS,CAAA;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AAE5D,MAAM,CAAC,MAAM,kBAAkB,GAAW,MAAM,EAAE,CAAA;AAElD,4EAA4E;AAC5E,8EAA8E;AAC9E,oCAAoC;AACpC,kBAAkB,CAAC,GAAG,CACpB,qBAAqB,EACrB,YAAY,EACZ,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;IAClD,qBAAqB,CAAC,GAAG,CAAC,MAAgB,CAAC;SACxC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;QAChB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC3B,QAAQ,EAAE,CAAC,CAAC,QAAQ;gBACpB,UAAU,EAAE,CAAC,CAAC,UAAU;gBACxB,aAAa,EAAE,CAAC,CAAC,aAAa;gBAC9B,YAAY,EAAE,CAAC,CAAC,YAAY,CAAC,WAAW,EAAE;aAC3C,CAAC,CAAC;SACJ,CAAC,CAAA;IACJ,CAAC,CAAC;SACD,KAAK,CAAC,IAAI,CAAC,CAAA;AAChB,CAAC,CACF,CAAA;AAED,kBAAkB,CAAC,MAAM,CACvB,+BAA+B,EAC/B,YAAY,EACZ,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;IAClD,4EAA4E;IAC5E,yEAAyE;IACzE,yEAAyE;IACzE,MAAM,QAAQ,GAAG,wBAAwB,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IACxE,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;QACtB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAA;QACrB,OAAM;IACR,CAAC;IACD,sBAAsB,CAAC,GAAG,CAAC,MAAgB,EAAE,QAAQ,CAAC,IAAI,CAAC;SACxD,IAAI,CAAC,GAAG,EAAE;QACT,yEAAyE;QACzE,wEAAwE;QACxE,6BAA6B;QAC7B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAA;IACvB,CAAC,CAAC;SACD,KAAK,CAAC,IAAI,CAAC,CAAA;AAChB,CAAC,CACF,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import type { RateLimiterMiddleware } from '../middleware/rate-limit.js';
|
|
3
|
+
/** Options the boot wiring injects: the per-IP rate limiter seam. */
|
|
4
|
+
export interface OAuthRegisterRouterOptions {
|
|
5
|
+
limiter: RateLimiterMiddleware;
|
|
6
|
+
}
|
|
7
|
+
/** Build the /oauth/register router with the injected per-IP limiter gating it. */
|
|
8
|
+
export declare function oauthRegisterRouter(options: OAuthRegisterRouterOptions): Router;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// RFC 7591 dynamic client registration transport. Thin by contract:
|
|
3
|
+
// validate at the one boundary
|
|
4
|
+
// (clientRegistrationInputSchema), delegate policy (id/secret minting, hashing,
|
|
5
|
+
// persistence) to packages/core, shape the RFC 7591 response.
|
|
6
|
+
//
|
|
7
|
+
// Status contract (RFC 7591 §3.2):
|
|
8
|
+
// 201 — the registered metadata. A confidential client's client_secret is
|
|
9
|
+
// returned ONCE here, under Cache-Control: no-store, and is never
|
|
10
|
+
// logged or retrievable again (only its SHA-256 hash is at rest).
|
|
11
|
+
// Public clients ('none') get client_id only.
|
|
12
|
+
// 400 — { error: 'invalid_redirect_uri' } for redirect_uris failures
|
|
13
|
+
// (non-https outside localhost/127.0.0.1, fragments, empty array),
|
|
14
|
+
// { error: 'invalid_client_metadata' } for everything else (unknown
|
|
15
|
+
// token_endpoint_auth_method included) — the schema enum pre-empts the
|
|
16
|
+
// 0005 DB CHECKs so a violation never surfaces as a raw pg error.
|
|
17
|
+
//
|
|
18
|
+
// RATE LIMIT: a per-IP limiter seam runs before the handler (resolveLimiters,
|
|
19
|
+
// app.ts). The default is a real per-IP bucket (ADR-0009 §6 — this endpoint is
|
|
20
|
+
// unauthenticated); the injection seam owns the final policy.
|
|
21
|
+
import { log } from '@3ngram/config';
|
|
22
|
+
import { insertAuditLog, registerOAuthClient } from '@3ngram/core/auth';
|
|
23
|
+
import { clientRegistrationInputSchema } from '@3ngram/schema';
|
|
24
|
+
import { Router } from 'express';
|
|
25
|
+
/** Map a Zod failure to its RFC 7591 §3.2.2 error code: redirect_uris issues get the dedicated code. */
|
|
26
|
+
function registrationErrorCode(issues) {
|
|
27
|
+
return issues.some((issue) => issue.path[0] === 'redirect_uris')
|
|
28
|
+
? 'invalid_redirect_uri'
|
|
29
|
+
: 'invalid_client_metadata';
|
|
30
|
+
}
|
|
31
|
+
/** Build the /oauth/register router with the injected per-IP limiter gating it. */
|
|
32
|
+
export function oauthRegisterRouter(options) {
|
|
33
|
+
const router = Router();
|
|
34
|
+
router.post('/oauth/register', options.limiter, (req, res, next) => {
|
|
35
|
+
const parsed = clientRegistrationInputSchema.safeParse(req.body);
|
|
36
|
+
if (!parsed.success) {
|
|
37
|
+
res.status(400).json({ error: registrationErrorCode(parsed.error.issues) });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
registerOAuthClient(parsed.data)
|
|
41
|
+
.then((client) => {
|
|
42
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
43
|
+
res.status(201).json(client);
|
|
44
|
+
// Fire-and-forget: audit the registration event. NEVER include
|
|
45
|
+
// client_secret — only the sanitised client_id (hard rule 6).
|
|
46
|
+
insertAuditLog({
|
|
47
|
+
actorKind: 'system',
|
|
48
|
+
action: 'dcr.register',
|
|
49
|
+
resource: client.client_id,
|
|
50
|
+
...(req.ip !== undefined ? { ip: req.ip } : {}),
|
|
51
|
+
}).catch((err) => {
|
|
52
|
+
log().warn({ err: err instanceof Error ? err.name : 'unknown' }, 'audit: dcr.register failed');
|
|
53
|
+
});
|
|
54
|
+
})
|
|
55
|
+
.catch(next);
|
|
56
|
+
});
|
|
57
|
+
return router;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=oauth-register.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-register.js","sourceRoot":"","sources":["../../src/routes/oauth-register.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,oEAAoE;AACpE,+BAA+B;AAC/B,gFAAgF;AAChF,8DAA8D;AAC9D,EAAE;AACF,mCAAmC;AACnC,4EAA4E;AAC5E,0EAA0E;AAC1E,0EAA0E;AAC1E,sDAAsD;AACtD,uEAAuE;AACvE,2EAA2E;AAC3E,4EAA4E;AAC5E,+EAA+E;AAC/E,0EAA0E;AAC1E,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,8DAA8D;AAC9D,OAAO,EA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AC;gBACb,SAAS,EAAE,QAAQ;gBACnB,MAAM,EAAE,cAAc;gBACtB,QAAQ,EAAE,MAAM,CAAC,SAAS;gBAC1B,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAChD,CAAC,CAAC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE;gBACxB,GAAG,EAAE,CAAC,IAAI,CACR,EAAE,GAAG,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,EAAE,EACpD,4BAA4B,CAC7B,CAAA;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC;aACD,KAAK,CAAC,IAAI,CAAC,CAAA;IAChB,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import type { Redis } from 'ioredis';
|
|
3
|
+
import type { RateLimiterMiddleware } from '../middleware/rate-limit.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options the boot wiring injects:
|
|
6
|
+
* - `limiter` — the per-IP rate limiter seam.
|
|
7
|
+
* - `redis` — the shared ioredis client. When present the
|
|
8
|
+
* progressive-delay failure counter lives in Redis so the budget is shared
|
|
9
|
+
* across Railway replicas; when absent it falls back to a per-process Map
|
|
10
|
+
* (single-instance / CI parity, mirroring the rate-limiter's memory fallback).
|
|
11
|
+
*/
|
|
12
|
+
export interface OAuthTokenRouterOptions {
|
|
13
|
+
limiter: RateLimiterMiddleware;
|
|
14
|
+
redis?: Redis | undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The per-process fallback counter used when no Redis client is injected
|
|
18
|
+
* (single-instance / CI). Exported for test inspection of the fallback path
|
|
19
|
+
* only — the Redis path is exercised via an injected ioredis-mock client.
|
|
20
|
+
*/
|
|
21
|
+
export declare const _exchangeFailures: Map<string, number>;
|
|
22
|
+
/** Wire the failure-tracker Redis client (or clear it for the Map fallback). */
|
|
23
|
+
export declare function setExchangeFailureRedis(redis: Redis | undefined): void;
|
|
24
|
+
/** Increment the failure counter for a client_id and refresh its reset TTL. */
|
|
25
|
+
export declare function recordExchangeFailure(clientId: string): Promise<void>;
|
|
26
|
+
/** Clear the failure counter for a client_id (a successful exchange forgives it). */
|
|
27
|
+
export declare function clearExchangeFailures(clientId: string): Promise<void>;
|
|
28
|
+
export declare function applyProgressiveDelay(clientId: string): Promise<void>;
|
|
29
|
+
/** Build the /oauth/token router behind the injected per-IP limiter. */
|
|
30
|
+
export declare function oauthTokenRouter(options: OAuthTokenRouterOptions): Router;
|