webcake-storefront-mcp 1.40.0 → 1.42.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/dist/api-catalog.generated.json +777 -0
- package/dist/api.js +1 -1
- package/dist/auth/login.js +71 -7
- package/dist/auth/oauth-server.js +86 -10
- package/dist/builder/attributes.js +1 -1
- package/dist/builder/catalog.js +1 -1
- package/dist/builder/element-keys.generated.json +136 -4105
- package/dist/builder/element-schema.js +297 -0
- package/dist/builder/page.js +38 -26
- package/dist/changelog.json +14 -14
- package/dist/db.js +29 -3
- package/dist/http.js +59 -33
- package/dist/persistence/prefs.js +83 -0
- package/dist/server.js +1 -1
- package/dist/tools/api-gateway.js +178 -0
- package/dist/tools/builder-extras.js +3 -1
- package/dist/tools/builder.js +12 -3
- package/dist/tools/registry.js +7 -3
- package/package.json +3 -1
package/dist/api.js
CHANGED
|
@@ -51,7 +51,7 @@ export class WebcakeCmsApi {
|
|
|
51
51
|
* Connect page and returns the new credentials (request is retried once). Remote: marks it dead. */
|
|
52
52
|
onAuthExpired;
|
|
53
53
|
async renewAuth() {
|
|
54
|
-
const fresh = this.onAuthExpired ? await this.onAuthExpired() : null;
|
|
54
|
+
const fresh = this.onAuthExpired ? await this.onAuthExpired(this.token) : null;
|
|
55
55
|
if (!fresh?.token)
|
|
56
56
|
return false;
|
|
57
57
|
this.switchToken(fresh.token);
|
package/dist/auth/login.js
CHANGED
|
@@ -5,7 +5,10 @@ import { createServer } from "node:http";
|
|
|
5
5
|
import { randomBytes } from "node:crypto";
|
|
6
6
|
import { spawn, execFile } from "node:child_process";
|
|
7
7
|
import { DEFAULT_ENV, resolveSettings } from "../config.js";
|
|
8
|
-
import {
|
|
8
|
+
import { closeSync, openSync, statSync, unlinkSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { CONFIG_DIR, getConfig, reloadConfig, setConfig } from "../db.js";
|
|
11
|
+
import { jwtExpired } from "../api.js";
|
|
9
12
|
function parseArgs(argv) {
|
|
10
13
|
const opts = { open: true };
|
|
11
14
|
for (let i = 0; i < argv.length; i++) {
|
|
@@ -196,16 +199,77 @@ export async function runLogin(argv) {
|
|
|
196
199
|
}, 180_000).unref();
|
|
197
200
|
});
|
|
198
201
|
}
|
|
199
|
-
// One browser Connect at a time
|
|
200
|
-
//
|
|
202
|
+
// One browser Connect at a time — across every stdio process on this machine (several IDE
|
|
203
|
+
// windows share ~/.webcake-storefront-mcp): the first takes a lock file and opens the
|
|
204
|
+
// browser, the others wait for the token it saves. After a failed/abandoned Connect, wait
|
|
205
|
+
// before reopening so a looping agent doesn't spam tabs.
|
|
206
|
+
const LOCK = join(CONFIG_DIR, "renew.lock");
|
|
207
|
+
const LOGIN_WAIT_MS = 185_000; // runLogin times out at 180s
|
|
201
208
|
let renewing = null;
|
|
202
209
|
let lastFail = 0;
|
|
210
|
+
/** A token in the config file newer than `stale` and not expired (another process renewed it). */
|
|
211
|
+
function savedFresh(stale) {
|
|
212
|
+
reloadConfig();
|
|
213
|
+
const token = getConfig("token") || "";
|
|
214
|
+
return token && token !== stale && !jwtExpired(token) ? { token, sessionId: getConfig("session_id") || "" } : null;
|
|
215
|
+
}
|
|
216
|
+
function takeLock() {
|
|
217
|
+
try {
|
|
218
|
+
closeSync(openSync(LOCK, "wx"));
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
try {
|
|
223
|
+
if (Date.now() - statSync(LOCK).mtimeMs > LOGIN_WAIT_MS) {
|
|
224
|
+
unlinkSync(LOCK); // left behind by a crashed process
|
|
225
|
+
return takeLock();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
catch { /* raced with its owner releasing it */ }
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
async function renew(stale) {
|
|
233
|
+
const done = savedFresh(stale);
|
|
234
|
+
if (done)
|
|
235
|
+
return done;
|
|
236
|
+
if (!takeLock()) {
|
|
237
|
+
// Someone else has the browser open — wait for the token they save.
|
|
238
|
+
for (const until = Date.now() + LOGIN_WAIT_MS; Date.now() < until;) {
|
|
239
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
240
|
+
const t = savedFresh(stale);
|
|
241
|
+
if (t)
|
|
242
|
+
return t;
|
|
243
|
+
try {
|
|
244
|
+
statSync(LOCK);
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
return savedFresh(stale);
|
|
248
|
+
} // released without a token
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
console.error("[webcake-storefront] Token hết hạn — mở trình duyệt để Kết nối lại…");
|
|
254
|
+
await runLogin([]);
|
|
255
|
+
return { token: getConfig("token") || "", sessionId: getConfig("session_id") || "" };
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
try {
|
|
259
|
+
unlinkSync(LOCK);
|
|
260
|
+
}
|
|
261
|
+
catch { /* already gone */ }
|
|
262
|
+
}
|
|
263
|
+
}
|
|
203
264
|
/** Token expired mid-session (stdio): reopen the Connect page and hand back the new credentials. */
|
|
204
|
-
export function renewInBrowser() {
|
|
265
|
+
export function renewInBrowser(staleToken = "") {
|
|
205
266
|
if (!renewing && Date.now() - lastFail > 120_000) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
267
|
+
renewing = renew(staleToken)
|
|
268
|
+
.then((r) => {
|
|
269
|
+
if (!r)
|
|
270
|
+
lastFail = Date.now();
|
|
271
|
+
return r;
|
|
272
|
+
})
|
|
209
273
|
.catch((e) => {
|
|
210
274
|
lastFail = Date.now();
|
|
211
275
|
console.error("[webcake-storefront] gia hạn token thất bại:", e?.message || e);
|
|
@@ -98,6 +98,18 @@ class MemoryStore {
|
|
|
98
98
|
return r && r.expiresAt >= now() ? r : undefined;
|
|
99
99
|
}
|
|
100
100
|
async revoke(t) { this.access.delete(t); this.refresh.delete(t); }
|
|
101
|
+
async revokeJwt(jwt) {
|
|
102
|
+
const dropped = [];
|
|
103
|
+
for (const [k, v] of this.access)
|
|
104
|
+
if (v.cred.jwt === jwt) {
|
|
105
|
+
this.access.delete(k);
|
|
106
|
+
dropped.push(k);
|
|
107
|
+
}
|
|
108
|
+
for (const [k, v] of this.refresh)
|
|
109
|
+
if (v.cred.jwt === jwt)
|
|
110
|
+
this.refresh.delete(k);
|
|
111
|
+
return dropped;
|
|
112
|
+
}
|
|
101
113
|
}
|
|
102
114
|
// ---- Postgres store (when DATABASE_URL is set) -----------------------------
|
|
103
115
|
class PgStore {
|
|
@@ -200,6 +212,12 @@ class PgStore {
|
|
|
200
212
|
await this.pool.query(`DELETE FROM oauth_access_tokens WHERE token=$1`, [t]);
|
|
201
213
|
await this.pool.query(`DELETE FROM oauth_refresh_tokens WHERE token=$1`, [t]);
|
|
202
214
|
}
|
|
215
|
+
// ponytail: no index on jwt — a seq scan, but this only runs when a JWT dies.
|
|
216
|
+
async revokeJwt(jwt) {
|
|
217
|
+
const { rows } = await this.pool.query(`DELETE FROM oauth_access_tokens WHERE jwt=$1 RETURNING token`, [jwt]);
|
|
218
|
+
await this.pool.query(`DELETE FROM oauth_refresh_tokens WHERE jwt=$1`, [jwt]);
|
|
219
|
+
return rows.map((r) => r.token);
|
|
220
|
+
}
|
|
203
221
|
}
|
|
204
222
|
// ---- Backend selection (memoized) ------------------------------------------
|
|
205
223
|
const memoryStore = new MemoryStore();
|
|
@@ -337,14 +355,72 @@ export async function completeAuthorize(internalState, jwt, wsid) {
|
|
|
337
355
|
// The OAuth tokens outlive the WebCake JWT they wrap. Once that JWT is expired (or the
|
|
338
356
|
// backend rejected it), refreshing would just re-issue the dead JWT; refusing it makes the
|
|
339
357
|
// client restart the full flow, i.e. open the Connect page again.
|
|
340
|
-
//
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
358
|
+
// Shared across instances: markJwtDead revokes every OAuth token wrapping the JWT in the
|
|
359
|
+
// store (Postgres = source of truth) and flags it in Redis for raw ?token= callers.
|
|
360
|
+
// Only a sha256 of the JWT is kept, never the token itself.
|
|
361
|
+
const DEAD_PREFIX = "wc:sf:deadjwt:";
|
|
362
|
+
const DEAD_MAX_TTL = 30 * 24 * 60 * 60 * 1000;
|
|
363
|
+
const deadLocal = new Map(); // hash → expiresAt (ms)
|
|
364
|
+
const jwtHash = (jwt) => createHash("sha256").update(jwt).digest("hex");
|
|
365
|
+
function deadTtl(jwt) {
|
|
366
|
+
try {
|
|
367
|
+
const exp = JSON.parse(Buffer.from(jwt.split(".")[1], "base64url").toString()).exp;
|
|
368
|
+
if (typeof exp === "number")
|
|
369
|
+
return Math.min(Math.max(exp * 1000 - now(), 60_000), DEAD_MAX_TTL);
|
|
370
|
+
}
|
|
371
|
+
catch { /* opaque token */ }
|
|
372
|
+
return DEAD_MAX_TTL;
|
|
373
|
+
}
|
|
374
|
+
function rememberDeadLocally(hash, ttl) {
|
|
375
|
+
if (deadLocal.size > 10_000)
|
|
376
|
+
for (const [k, exp] of deadLocal)
|
|
377
|
+
if (exp < now())
|
|
378
|
+
deadLocal.delete(k);
|
|
379
|
+
if (deadLocal.size > 10_000)
|
|
380
|
+
deadLocal.delete(deadLocal.keys().next().value); // still full: drop oldest
|
|
381
|
+
deadLocal.set(hash, now() + ttl);
|
|
382
|
+
}
|
|
383
|
+
/** Expired, or already marked dead here — no I/O. */
|
|
384
|
+
function isDeadLocally(jwt) {
|
|
385
|
+
if (jwtExpired(jwt))
|
|
386
|
+
return true;
|
|
387
|
+
const exp = deadLocal.get(jwtHash(jwt));
|
|
388
|
+
return exp !== undefined && exp >= now();
|
|
345
389
|
}
|
|
346
|
-
|
|
347
|
-
|
|
390
|
+
/** The backend rejected this JWT: make every instance treat it as dead. Never throws. */
|
|
391
|
+
export async function markJwtDead(jwt) {
|
|
392
|
+
if (!jwt)
|
|
393
|
+
return;
|
|
394
|
+
const hash = jwtHash(jwt);
|
|
395
|
+
const ttl = deadTtl(jwt);
|
|
396
|
+
rememberDeadLocally(hash, ttl);
|
|
397
|
+
const redis = getRedis();
|
|
398
|
+
try {
|
|
399
|
+
if (redis)
|
|
400
|
+
await redis.set(DEAD_PREFIX + hash, "1", "PX", ttl);
|
|
401
|
+
const dropped = await (await getStore()).revokeJwt(jwt);
|
|
402
|
+
if (redis)
|
|
403
|
+
for (const t of dropped)
|
|
404
|
+
redisCacheDel(redis, t);
|
|
405
|
+
}
|
|
406
|
+
catch (e) {
|
|
407
|
+
console.error("[oauth] markJwtDead:", e?.message ?? e);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/** True when the JWT is expired or any instance marked it dead. */
|
|
411
|
+
export async function isDeadJwt(jwt) {
|
|
412
|
+
if (!jwt)
|
|
413
|
+
return false;
|
|
414
|
+
if (isDeadLocally(jwt))
|
|
415
|
+
return true;
|
|
416
|
+
const redis = getRedis();
|
|
417
|
+
if (!redis)
|
|
418
|
+
return false;
|
|
419
|
+
const hash = jwtHash(jwt);
|
|
420
|
+
const hit = await redis.get(DEAD_PREFIX + hash).catch(() => null);
|
|
421
|
+
if (hit)
|
|
422
|
+
rememberDeadLocally(hash, deadTtl(jwt));
|
|
423
|
+
return !!hit;
|
|
348
424
|
}
|
|
349
425
|
async function issueTokens(store, cred, client_id, scope) {
|
|
350
426
|
const access = token(32);
|
|
@@ -380,7 +456,7 @@ export async function exchangeToken(p) {
|
|
|
380
456
|
if (!r) {
|
|
381
457
|
return { ok: false, status: 400, error: "invalid_grant", error_description: "Unknown or expired refresh token." };
|
|
382
458
|
}
|
|
383
|
-
if (isDeadJwt(r.cred.jwt)) {
|
|
459
|
+
if (await isDeadJwt(r.cred.jwt)) {
|
|
384
460
|
return { ok: false, status: 400, error: "invalid_grant", error_description: "WebCake session expired — sign in again (Connect)." };
|
|
385
461
|
}
|
|
386
462
|
return { ok: true, body: await issueTokens(store, r.cred, r.client_id, r.scope) };
|
|
@@ -403,11 +479,11 @@ export async function resolveAccessToken(accessToken) {
|
|
|
403
479
|
if (redis) {
|
|
404
480
|
const cached = await redisCacheGet(redis, accessToken);
|
|
405
481
|
if (cached)
|
|
406
|
-
return
|
|
482
|
+
return isDeadLocally(cached.jwt) ? undefined : cached;
|
|
407
483
|
}
|
|
408
484
|
// 2. Fall through to the store (Postgres or memory).
|
|
409
485
|
const a = await (await getStore()).getAccess(accessToken);
|
|
410
|
-
if (!a ||
|
|
486
|
+
if (!a || isDeadLocally(a.cred.jwt))
|
|
411
487
|
return undefined;
|
|
412
488
|
// Backfill the Redis cache for next time.
|
|
413
489
|
if (redis)
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// - real published pages mined from the backend DB
|
|
8
8
|
// Only real keys are listed here (no invented attributes).
|
|
9
9
|
import { readFileSync } from "node:fs";
|
|
10
|
-
let OBSERVED = {};
|
|
10
|
+
export let OBSERVED = {};
|
|
11
11
|
try {
|
|
12
12
|
OBSERVED = JSON.parse(readFileSync(new URL("./element-keys.generated.json", import.meta.url), "utf8"));
|
|
13
13
|
}
|
package/dist/builder/catalog.js
CHANGED
|
@@ -143,7 +143,7 @@ const SUMMARY = {
|
|
|
143
143
|
collapse: "Accordion / collapsible panel.",
|
|
144
144
|
breadcrumb: "Breadcrumb navigation trail.",
|
|
145
145
|
};
|
|
146
|
-
function describeType(type) {
|
|
146
|
+
export function describeType(type) {
|
|
147
147
|
return {
|
|
148
148
|
type,
|
|
149
149
|
category: CATEGORY_BY_TYPE[type] || "other",
|