inistate-mcp 1.3.4 → 1.4.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/build/activity-guard.d.ts +7 -1
- package/build/activity-guard.js +43 -22
- package/build/api.d.ts +8 -2
- package/build/api.js +28 -6
- package/build/authorize-app-routes.js +10 -85
- package/build/authorize-pages.d.ts +24 -0
- package/build/authorize-pages.js +110 -0
- package/build/backend.d.ts +121 -4
- package/build/backend.js +66 -4
- package/build/capability.d.ts +1 -1
- package/build/capability.js +10 -0
- package/build/context.d.ts +4 -0
- package/build/experimental.d.ts +6 -0
- package/build/experimental.js +8 -0
- package/build/http.js +2 -0
- package/build/oauth-provider.js +6 -0
- package/build/prompts.js +9 -2
- package/build/provenance.d.ts +7 -0
- package/build/provenance.js +32 -0
- package/build/resources.js +17 -4
- package/build/schema.d.ts +16 -0
- package/build/schema.js +206 -10
- package/build/server.js +2 -2
- package/build/tools.js +1144 -32
- package/facts-automation-guide.md +297 -0
- package/facts-module-design-guide.md +65 -0
- package/inistate-schema.json +805 -4
- package/package.json +4 -2
- package/server.json +2 -2
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
interface ActivityDef {
|
|
19
19
|
name: string;
|
|
20
20
|
actor?: string;
|
|
21
|
+
humanWhen?: string;
|
|
21
22
|
}
|
|
22
23
|
interface FieldInfo {
|
|
23
24
|
name: string;
|
|
@@ -107,8 +108,13 @@ export declare function getModuleStates(moduleName: string, fetchSchema?: Schema
|
|
|
107
108
|
*
|
|
108
109
|
* Split out so bulk callers (submit_activities) can fetch once and reuse the
|
|
109
110
|
* map across items via validateInputShapesWith.
|
|
111
|
+
*
|
|
112
|
+
* `fresh` skips the cache (and refills it): for a key the cached map does not
|
|
113
|
+
* know, which may be a field added since the schema was cached.
|
|
110
114
|
*/
|
|
111
|
-
export declare function getModuleFieldTypes(moduleName: string, fetchSchema?: SchemaFetcher
|
|
115
|
+
export declare function getModuleFieldTypes(moduleName: string, fetchSchema?: SchemaFetcher, { fresh }?: {
|
|
116
|
+
fresh?: boolean;
|
|
117
|
+
}): Promise<Map<string, string> | null>;
|
|
112
118
|
/**
|
|
113
119
|
* Synchronous shape check against a pre-built field-type map. Use this in
|
|
114
120
|
* bulk loops to avoid rebuilding the map per item. When `types` is null
|
package/build/activity-guard.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as api from "./api.js";
|
|
2
2
|
import { requestContext } from "./context.js";
|
|
3
|
+
import { isSystemFieldName } from "./schema.js";
|
|
3
4
|
const REF_FIELD_TYPES = new Set(["User", "Users", "Module", "Modules"]);
|
|
4
5
|
const LOCATION_FIELD_TYPE = "Location";
|
|
5
6
|
// Standard activities are inherent platform operations. Most bypass the actor
|
|
@@ -48,11 +49,11 @@ const defaultSchemaFetcher = async (moduleName) => {
|
|
|
48
49
|
return null;
|
|
49
50
|
}
|
|
50
51
|
};
|
|
51
|
-
async function getExtendedSchema(moduleName, fetchSchema = defaultSchemaFetcher) {
|
|
52
|
+
async function getExtendedSchema(moduleName, fetchSchema = defaultSchemaFetcher, fresh = false) {
|
|
52
53
|
const key = cacheKey("schema", [moduleName]);
|
|
53
54
|
const now = Date.now();
|
|
54
55
|
const cached = schemaCache.get(key);
|
|
55
|
-
if (cached && now - cached.at < SCHEMA_TTL_MS)
|
|
56
|
+
if (!fresh && cached && now - cached.at < SCHEMA_TTL_MS)
|
|
56
57
|
return cached.schema;
|
|
57
58
|
const schema = await fetchSchema(moduleName);
|
|
58
59
|
if (schema)
|
|
@@ -89,11 +90,11 @@ const defaultCanvasFetcher = async (moduleName) => {
|
|
|
89
90
|
return null;
|
|
90
91
|
}
|
|
91
92
|
};
|
|
92
|
-
async function getCanvasSchema(moduleName, fetchCanvas = defaultCanvasFetcher) {
|
|
93
|
+
async function getCanvasSchema(moduleName, fetchCanvas = defaultCanvasFetcher, fresh = false) {
|
|
93
94
|
const key = cacheKey("canvas", [moduleName]);
|
|
94
95
|
const now = Date.now();
|
|
95
96
|
const cached = canvasCache.get(key);
|
|
96
|
-
if (cached && now - cached.at < SCHEMA_TTL_MS)
|
|
97
|
+
if (!fresh && cached && now - cached.at < SCHEMA_TTL_MS)
|
|
97
98
|
return cached.canvas;
|
|
98
99
|
// Cache the miss too — a caller without configure access would otherwise
|
|
99
100
|
// pay a failing round trip on every submit.
|
|
@@ -112,17 +113,17 @@ function findWithActor(list, activity) {
|
|
|
112
113
|
return null;
|
|
113
114
|
}
|
|
114
115
|
/** The activity's actor as the extended schema reports it; null when it names none. */
|
|
115
|
-
async function getReportedActivityDef(moduleName, activity, fetchSchema) {
|
|
116
|
-
const schema = await getExtendedSchema(moduleName, fetchSchema);
|
|
116
|
+
async function getReportedActivityDef(moduleName, activity, fetchSchema, fresh = false) {
|
|
117
|
+
const schema = await getExtendedSchema(moduleName, fetchSchema, fresh);
|
|
117
118
|
return findWithActor(schema?.activityDetails, activity) ?? findWithActor(schema?.activities, activity);
|
|
118
119
|
}
|
|
119
|
-
async function getActivityDef(moduleName, activity, fetchSchema = defaultSchemaFetcher, fetchCanvas = defaultCanvasFetcher) {
|
|
120
|
-
const reported = await getReportedActivityDef(moduleName, activity, fetchSchema);
|
|
120
|
+
async function getActivityDef(moduleName, activity, fetchSchema = defaultSchemaFetcher, fetchCanvas = defaultCanvasFetcher, fresh = false) {
|
|
121
|
+
const reported = await getReportedActivityDef(moduleName, activity, fetchSchema, fresh);
|
|
121
122
|
if (reported)
|
|
122
123
|
return reported;
|
|
123
124
|
// A server that lists names only — the canvas carries the actor (designers
|
|
124
125
|
// can read it; anyone else gets null, and the API decides).
|
|
125
|
-
const canvas = await getCanvasSchema(moduleName, fetchCanvas);
|
|
126
|
+
const canvas = await getCanvasSchema(moduleName, fetchCanvas, fresh);
|
|
126
127
|
return findWithActor(canvas?.activities, activity);
|
|
127
128
|
}
|
|
128
129
|
const FLAG_TTL_MS = 60 * 60 * 1000;
|
|
@@ -177,6 +178,10 @@ export function getHumanBlockedStates(moduleName, entryId) {
|
|
|
177
178
|
}
|
|
178
179
|
return rec;
|
|
179
180
|
}
|
|
181
|
+
/** A hybrid activity the guard itself asks a confirmation for (one with a humanWhen is the server's to decide). */
|
|
182
|
+
function needsConfirmation(def, confirmed) {
|
|
183
|
+
return (def.actor || "").toLowerCase() === "hybrid" && !confirmed && !def.humanWhen;
|
|
184
|
+
}
|
|
180
185
|
export async function evaluateActivity(input, fetchSchema = defaultSchemaFetcher, fetchCanvas = defaultCanvasFetcher) {
|
|
181
186
|
const { module: moduleName, activity, entryId, entryIds, state, confidence, confirmed } = input;
|
|
182
187
|
// Rule 1 — confidence inflation after a prior flag.
|
|
@@ -268,17 +273,23 @@ export async function evaluateActivity(input, fetchSchema = defaultSchemaFetcher
|
|
|
268
273
|
// Rules 3 & 4 apply to custom (workflow-defined) activities, and to standard
|
|
269
274
|
// create/edit through the actor the schema reports for them (only a server
|
|
270
275
|
// with activityDetails reports one; the canvas has no standard actors).
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
else {
|
|
278
|
-
def = await getActivityDef(moduleName, activity, fetchSchema, fetchCanvas);
|
|
279
|
-
}
|
|
276
|
+
if (standard && !ACTOR_CHECKED_STANDARDS.has(standard))
|
|
277
|
+
return { ok: true };
|
|
278
|
+
const lookup = (fresh) => standard
|
|
279
|
+
? getReportedActivityDef(moduleName, standard, fetchSchema, fresh)
|
|
280
|
+
: getActivityDef(moduleName, activity, fetchSchema, fetchCanvas, fresh);
|
|
281
|
+
let def = await lookup(false);
|
|
280
282
|
if (!def)
|
|
281
283
|
return { ok: true }; // unknown activity or no actor — let the API decide.
|
|
284
|
+
// The schema is cached for minutes, and a designer may since have given this
|
|
285
|
+
// hybrid activity a humanWhen condition (or another actor). Re-read it before
|
|
286
|
+
// asking for a confirmation, so the agent is not sent to the user for one the
|
|
287
|
+
// server would not take. Only this refusal pays the extra round trip.
|
|
288
|
+
if (needsConfirmation(def, confirmed)) {
|
|
289
|
+
def = await lookup(true);
|
|
290
|
+
if (!def)
|
|
291
|
+
return { ok: true };
|
|
292
|
+
}
|
|
282
293
|
const actor = (def.actor || "").toLowerCase();
|
|
283
294
|
if (actor === "human") {
|
|
284
295
|
// Remember which states this activity would have reached, so a follow-up
|
|
@@ -303,11 +314,14 @@ export async function evaluateActivity(input, fetchSchema = defaultSchemaFetcher
|
|
|
303
314
|
message: `Activity '${activity}' is marked actor='human'. AI agents are not permitted to execute human-only activities.`,
|
|
304
315
|
activity,
|
|
305
316
|
actor: "human",
|
|
306
|
-
agent_action: "Do not retry — confirmed: true does NOT unlock this, and neither does changeState. Tell the user the activity must be performed by a
|
|
317
|
+
agent_action: "Do not retry — confirmed: true does NOT unlock this, and neither does changeState. Tell the user the activity must be performed by a person. If the user wants it done, you may offer to send it to a person for approval: submit_activity with `approval: { summary }`; never send one unasked.",
|
|
307
318
|
},
|
|
308
319
|
};
|
|
309
320
|
}
|
|
310
|
-
|
|
321
|
+
// A hybrid activity with a humanWhen condition: the server decides first
|
|
322
|
+
// whether the condition makes it human for these values (then no
|
|
323
|
+
// confirmation could unlock it), and asks for the confirmation itself when not.
|
|
324
|
+
if (needsConfirmation(def, confirmed)) {
|
|
311
325
|
return {
|
|
312
326
|
ok: false,
|
|
313
327
|
structured: {
|
|
@@ -548,9 +562,12 @@ export async function getModuleStates(moduleName, fetchSchema = defaultSchemaFet
|
|
|
548
562
|
*
|
|
549
563
|
* Split out so bulk callers (submit_activities) can fetch once and reuse the
|
|
550
564
|
* map across items via validateInputShapesWith.
|
|
565
|
+
*
|
|
566
|
+
* `fresh` skips the cache (and refills it): for a key the cached map does not
|
|
567
|
+
* know, which may be a field added since the schema was cached.
|
|
551
568
|
*/
|
|
552
|
-
export async function getModuleFieldTypes(moduleName, fetchSchema = defaultSchemaFetcher) {
|
|
553
|
-
const schema = await getExtendedSchema(moduleName, fetchSchema);
|
|
569
|
+
export async function getModuleFieldTypes(moduleName, fetchSchema = defaultSchemaFetcher, { fresh = false } = {}) {
|
|
570
|
+
const schema = await getExtendedSchema(moduleName, fetchSchema, fresh);
|
|
554
571
|
const info = schema?.information;
|
|
555
572
|
if (!info || info.length === 0)
|
|
556
573
|
return null;
|
|
@@ -638,6 +655,10 @@ export function resolveInputKeys(types, input, labels) {
|
|
|
638
655
|
for (const key of Object.keys(input)) {
|
|
639
656
|
if (types.has(key))
|
|
640
657
|
continue;
|
|
658
|
+
// A people module's Username is on its forms but never in its field list;
|
|
659
|
+
// the platform maps it (or refuses it on a module that is not one).
|
|
660
|
+
if (isSystemFieldName(key))
|
|
661
|
+
continue;
|
|
641
662
|
const labelled = labels?.get(fieldKey(key));
|
|
642
663
|
const nearMiss = byKey.get(fieldKey(key));
|
|
643
664
|
const viaLabel = !!labelled && types.has(labelled) && (!nearMiss || nearMiss === labelled);
|
package/build/api.d.ts
CHANGED
|
@@ -7,6 +7,11 @@ export declare function getWorkspaceId(): string | null;
|
|
|
7
7
|
* the two possible fixes.
|
|
8
8
|
*/
|
|
9
9
|
export declare function annotateModuleNotFound(message: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* The next step for the agent after an API error: the server's own when it sends one (it knows why, e.g. a
|
|
12
|
+
* write that may already be saved), else the status's generic one.
|
|
13
|
+
*/
|
|
14
|
+
export declare function errorAgentAction(status: number, body: Record<string, unknown>): string;
|
|
10
15
|
/** What a tool result may weigh, as the compact JSON the tools send (tools.ts `ok`). */
|
|
11
16
|
export declare const TRUNCATE_BUDGET = 30000;
|
|
12
17
|
/**
|
|
@@ -28,8 +33,9 @@ export declare const TRUNCATE_BUDGET = 30000;
|
|
|
28
33
|
*/
|
|
29
34
|
export declare function fitListPage<T>(data: T, budget?: number): T;
|
|
30
35
|
export declare function get(path: string): Promise<unknown>;
|
|
31
|
-
export declare function post(path: string, body?: unknown): Promise<unknown>;
|
|
32
|
-
export declare function
|
|
36
|
+
export declare function post(path: string, body?: unknown, extraHeaders?: Record<string, string>): Promise<unknown>;
|
|
37
|
+
export declare function del(path: string): Promise<unknown>;
|
|
38
|
+
export declare function put(path: string, body?: unknown, extraHeaders?: Record<string, string>): Promise<unknown>;
|
|
33
39
|
/**
|
|
34
40
|
* Upload a file via multipart/form-data.
|
|
35
41
|
*/
|
package/build/api.js
CHANGED
|
@@ -87,10 +87,21 @@ export function annotateModuleNotFound(message) {
|
|
|
87
87
|
? `${message.trim().replace(/\.$/, "")} in the active workspace (id ${wsid}). Check list_modules for the exact name, or call set_workspace if the module lives in another workspace.`
|
|
88
88
|
: `${message.trim().replace(/\.$/, "")}. No active workspace is set — call set_workspace (or pass workspaceId) first.`;
|
|
89
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* The next step for the agent after an API error: the server's own when it sends one (it knows why, e.g. a
|
|
92
|
+
* write that may already be saved), else the status's generic one.
|
|
93
|
+
*/
|
|
94
|
+
export function errorAgentAction(status, body) {
|
|
95
|
+
const own = body?.agent_action;
|
|
96
|
+
return typeof own === "string" && own.trim() ? own : agentAction(status);
|
|
97
|
+
}
|
|
90
98
|
function agentAction(status) {
|
|
91
99
|
switch (status) {
|
|
92
100
|
case 400:
|
|
93
101
|
return "Check the error message, correct the input, and retry.";
|
|
102
|
+
case 409:
|
|
103
|
+
// e.g. a create the source has not confirmed: submitting it again could save it twice
|
|
104
|
+
return "This conflicts with a write that may already be saved. Do not submit it again: read the entry or the list first, then tell the user.";
|
|
94
105
|
case 401:
|
|
95
106
|
return "API key or token is invalid or expired. Check credentials and retry.";
|
|
96
107
|
case 403:
|
|
@@ -117,10 +128,13 @@ async function handleResponse(res) {
|
|
|
117
128
|
error: body.error || `HTTP ${res.status}`,
|
|
118
129
|
message: annotateModuleNotFound(String(body.message || text || res.statusText)),
|
|
119
130
|
details: body.details || null,
|
|
120
|
-
agent_action:
|
|
131
|
+
agent_action: errorAgentAction(res.status, body),
|
|
121
132
|
};
|
|
122
133
|
log("api_error", { status: res.status, url: res.url, error: error.error, message: error.message, details: error.details });
|
|
123
|
-
|
|
134
|
+
// `body` keeps the server's own envelope for the tools that relay it whole
|
|
135
|
+
// (set_module_access: changes, warnings, the server's agent_action). err()
|
|
136
|
+
// serializes `structured` only, so every other tool's error is unchanged.
|
|
137
|
+
throw Object.assign(new Error(String(error.message)), { structured: error, status: res.status, body });
|
|
124
138
|
}
|
|
125
139
|
if (!text)
|
|
126
140
|
return null;
|
|
@@ -202,18 +216,26 @@ export async function get(path) {
|
|
|
202
216
|
const res = await request(`${BASE_URL}${path}`, {}, headers);
|
|
203
217
|
return handleResponse(res);
|
|
204
218
|
}
|
|
205
|
-
|
|
219
|
+
/** The JSON headers plus a call's own (e.g. the design-write provenance headers). */
|
|
220
|
+
function withHeaders(extra) {
|
|
221
|
+
return extra ? () => ({ ...headers(), ...extra }) : headers;
|
|
222
|
+
}
|
|
223
|
+
export async function post(path, body, extraHeaders) {
|
|
206
224
|
const res = await request(`${BASE_URL}${path}`, {
|
|
207
225
|
method: "POST",
|
|
208
226
|
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
209
|
-
},
|
|
227
|
+
}, withHeaders(extraHeaders));
|
|
228
|
+
return handleResponse(res);
|
|
229
|
+
}
|
|
230
|
+
export async function del(path) {
|
|
231
|
+
const res = await request(`${BASE_URL}${path}`, { method: "DELETE" }, headers);
|
|
210
232
|
return handleResponse(res);
|
|
211
233
|
}
|
|
212
|
-
export async function put(path, body) {
|
|
234
|
+
export async function put(path, body, extraHeaders) {
|
|
213
235
|
const res = await request(`${BASE_URL}${path}`, {
|
|
214
236
|
method: "PUT",
|
|
215
237
|
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
216
|
-
},
|
|
238
|
+
}, withHeaders(extraHeaders));
|
|
217
239
|
return handleResponse(res);
|
|
218
240
|
}
|
|
219
241
|
/**
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
1
|
import express from "express";
|
|
3
|
-
import {
|
|
4
|
-
import { INISTATE_LOGO_SVG } from "./inistate-logo.js";
|
|
2
|
+
import { PAGE_HEADERS, renderConfirmPage, renderExpiredPage, renderWrongBrowserPage } from "./authorize-pages.js";
|
|
5
3
|
import { browserSecretCookieName, browserSecretCookieOptions, readCookie, } from "./oauth-provider.js";
|
|
6
4
|
/**
|
|
7
5
|
* SS06370: the Inistate App approves a flow natively (no cookie), then hands the user to the
|
|
@@ -12,72 +10,20 @@ import { browserSecretCookieName, browserSecretCookieOptions, readCookie, } from
|
|
|
12
10
|
* GET /authorize/finish the browser: confirmation page (cookie + token)
|
|
13
11
|
* POST /authorize/finish the browser: Confirm -> 302 to the client with the code
|
|
14
12
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* A second tap on Confirm used to burn the flow: the first POST had already issued the code
|
|
18
|
-
* and consumed the nonce, so the retry could only say "expired". Disable the button on submit.
|
|
19
|
-
* Inline, so the CSP carries its hash instead of allowing scripts in general.
|
|
20
|
-
*/
|
|
21
|
-
const SUBMIT_SCRIPT = "document.addEventListener('submit',function(e){var b=e.target.querySelector('button');if(b){b.disabled=true;b.textContent='Connecting\\u2026'}})";
|
|
22
|
-
const SUBMIT_SCRIPT_HASH = `'sha256-${createHash("sha256").update(SUBMIT_SCRIPT).digest("base64")}'`;
|
|
23
|
-
// No form-action here: Chrome also applies it to the redirect that follows the POST, and the
|
|
24
|
-
// whole point of Confirm is to redirect to the client's own origin, so 'self' blocked it and
|
|
25
|
-
// "nothing happened". Everything interpolated into the page is escaped, so nothing here
|
|
26
|
-
// can be steered into posting elsewhere.
|
|
27
|
-
const CSP = `default-src 'none'; style-src 'unsafe-inline'; script-src ${SUBMIT_SCRIPT_HASH}; img-src data:; frame-ancestors 'none'`;
|
|
28
|
-
// Palette from the web app (webApp src/assets/scss/colors.scss): primary #2b9ec6, green #a4ce3a.
|
|
29
|
-
// The button uses a darker shade of the primary because white on #2b9ec6 is only ~3:1.
|
|
30
|
-
const STYLE = `
|
|
31
|
-
:root{color-scheme:light dark;--bg:#f4f8fa;--card:#fff;--fg:#212121;--muted:#5f6b72;--line:#e6e6e6;--brand:#2b9ec6;--green:#a4ce3a;--btn:#1d7fa3;--btn-hover:#18698a;--btn-fg:#fff;--warn-bg:#fff6e8;--warn-line:#f3d19e}
|
|
32
|
-
@media (prefers-color-scheme:dark){:root{--bg:#121a1e;--card:#1b252b;--fg:#eef3f5;--muted:#a2b0b8;--line:#2d3b43;--btn:#2b9ec6;--btn-hover:#4db3d6;--btn-fg:#06161c;--warn-bg:#2b2516;--warn-line:#5a4a1e}}
|
|
33
|
-
*{box-sizing:border-box}
|
|
34
|
-
body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:16px;background:var(--bg);color:var(--fg);font:16px/1.5 Inter,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif}
|
|
35
|
-
main{position:relative;width:100%;max-width:26rem;background:var(--card);border:1px solid var(--line);border-radius:16px;padding:32px 24px 28px;overflow:hidden;box-shadow:0 2px 12px rgba(16,40,52,.08)}
|
|
36
|
-
main::before{content:"";position:absolute;inset:0 0 auto 0;height:5px;background:linear-gradient(to right,var(--brand),var(--green))}
|
|
37
|
-
.pair{display:flex;align-items:center;gap:12px;margin:0 0 16px}
|
|
38
|
-
.logo{display:block;width:44px;height:44px;border-radius:10px}.logo svg{display:block;width:100%;height:100%}
|
|
39
|
-
.link{color:var(--muted);font-size:1.2rem;line-height:1}
|
|
40
|
-
.where{font-size:.9rem}.where strong{color:var(--fg);font-weight:600;word-break:break-all}
|
|
41
|
-
h1{margin:0 0 6px;font-size:1.3rem;line-height:1.3}
|
|
42
|
-
p{margin:0 0 14px;color:var(--muted)}
|
|
43
|
-
.account{margin:16px 0;padding:12px 14px;border:1px solid var(--line);border-left:4px solid var(--brand);border-radius:10px;word-break:break-all}
|
|
44
|
-
.account small{display:block;color:var(--muted);font-size:.8rem;margin-bottom:2px}
|
|
45
|
-
.account strong{color:var(--fg);font-weight:600}
|
|
46
|
-
.note{padding:10px 12px;border-radius:10px;background:var(--warn-bg);border:1px solid var(--warn-line);color:var(--fg);font-size:.9rem;margin:0 0 18px}
|
|
47
|
-
button{width:100%;padding:12px 16px;border:0;border-radius:10px;background:var(--btn);color:var(--btn-fg);font:inherit;font-weight:600;cursor:pointer}
|
|
48
|
-
button:hover:not(:disabled){background:var(--btn-hover)}
|
|
49
|
-
button:disabled{opacity:.6;cursor:default}
|
|
50
|
-
button:focus-visible{outline:3px solid var(--brand);outline-offset:2px}
|
|
51
|
-
`;
|
|
52
|
-
const INISTATE_MARK = `<span class="logo">${INISTATE_LOGO_SVG}</span>`;
|
|
53
|
-
function page(res, status, title, body, header = INISTATE_MARK) {
|
|
54
|
-
res
|
|
55
|
-
.status(status)
|
|
56
|
-
.set({
|
|
57
|
-
"Content-Type": "text/html; charset=utf-8",
|
|
58
|
-
"Cache-Control": "no-store",
|
|
59
|
-
"Referrer-Policy": "no-referrer",
|
|
60
|
-
// The Confirm button must not be clickjackable from another origin.
|
|
61
|
-
"X-Frame-Options": "DENY",
|
|
62
|
-
"Content-Security-Policy": CSP,
|
|
63
|
-
})
|
|
64
|
-
.send(`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">` +
|
|
65
|
-
`<title>${escapeHtml(title)}</title><style>${STYLE}</style></head><body><main><div class="pair">${header}</div>${body}</main>` +
|
|
66
|
-
`<script>${SUBMIT_SCRIPT}</script></body></html>`);
|
|
13
|
+
function sendPage(res, status, html) {
|
|
14
|
+
res.status(status).set(PAGE_HEADERS).send(html);
|
|
67
15
|
}
|
|
68
16
|
function failure(res, error) {
|
|
69
17
|
const message = error instanceof Error ? error.message : "Authorization failed";
|
|
70
18
|
if (/does not belong to this browser|Invalid finish token/.test(message)) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
19
|
+
sendPage(res, 400, renderWrongBrowserPage());
|
|
20
|
+
}
|
|
21
|
+
else if (/expired|Invalid or expired/i.test(message)) {
|
|
22
|
+
sendPage(res, 400, renderExpiredPage());
|
|
74
23
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
`<p>It may already have been used. Go back to ChatGPT or Claude and start connecting again.</p>`);
|
|
78
|
-
return;
|
|
24
|
+
else {
|
|
25
|
+
res.status(400).type("text/plain").send(message);
|
|
79
26
|
}
|
|
80
|
-
res.status(400).type("text/plain").send(message);
|
|
81
27
|
}
|
|
82
28
|
export function registerAppAuthorizeRoutes(app, provider, issuerUrl) {
|
|
83
29
|
const secure = issuerUrl.startsWith("https:");
|
|
@@ -108,28 +54,7 @@ export function registerAppAuthorizeRoutes(app, provider, issuerUrl) {
|
|
|
108
54
|
const token = String(req.query.token ?? "");
|
|
109
55
|
const cookie = readCookie(req.headers.cookie, browserSecretCookieName(nonce));
|
|
110
56
|
const info = provider.getFinishInfo(nonce, token, cookie);
|
|
111
|
-
|
|
112
|
-
// otherwise the self-chosen client name is all there is, and the host is shown plainly.
|
|
113
|
-
const brand = clientLogoForRedirect(info.redirectUri);
|
|
114
|
-
const label = brand?.name ?? (info.clientName || "this app");
|
|
115
|
-
let host = "";
|
|
116
|
-
try {
|
|
117
|
-
host = new URL(info.redirectUri).host;
|
|
118
|
-
}
|
|
119
|
-
catch {
|
|
120
|
-
/* shown without a host */
|
|
121
|
-
}
|
|
122
|
-
const header = (brand ? `<img class="logo" src="${brand.logo}" alt="${escapeHtml(brand.name)}"><span class="link" aria-hidden="true">⇄</span>` : "") +
|
|
123
|
-
INISTATE_MARK;
|
|
124
|
-
page(res, 200, "Confirm connection", `<h1>Connect ${escapeHtml(label)} to Inistate?</h1>` +
|
|
125
|
-
`<p>It will be able to work in Inistate as this account:</p>` +
|
|
126
|
-
`<div class="account"><small>Signed in as</small><strong>${escapeHtml(info.account)}</strong></div>` +
|
|
127
|
-
(host ? `<p class="where">After you confirm, you'll be sent back to <strong>${escapeHtml(host)}</strong>.</p>` : "") +
|
|
128
|
-
`<p class="note">Only continue if you just started this from ChatGPT or Claude. If someone sent you this link, close this page.</p>` +
|
|
129
|
-
`<form method="post" action="/authorize/finish">` +
|
|
130
|
-
`<input type="hidden" name="nonce" value="${escapeHtml(nonce)}">` +
|
|
131
|
-
`<input type="hidden" name="token" value="${escapeHtml(token)}">` +
|
|
132
|
-
`<button type="submit">Confirm</button></form>`, header);
|
|
57
|
+
sendPage(res, 200, renderConfirmPage({ ...info, nonce, token }));
|
|
133
58
|
}
|
|
134
59
|
catch (error) {
|
|
135
60
|
failure(res, error);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SS06370: the pages the browser shows while finishing an App-approved authorization.
|
|
3
|
+
* Pure rendering - no Express, no provider - so wording, layout and styling live in one file
|
|
4
|
+
* and are easy to change. authorize-app-routes.ts decides WHICH page to send.
|
|
5
|
+
*/
|
|
6
|
+
export declare const escapeHtml: (s: string) => string;
|
|
7
|
+
/** Headers for every page: no caching, no referrer, not frameable (Confirm must not be clickjackable). */
|
|
8
|
+
export declare const PAGE_HEADERS: Record<string, string>;
|
|
9
|
+
export interface ConfirmPageInput {
|
|
10
|
+
/** The self-chosen (dynamic client registration) name; untrusted display text. */
|
|
11
|
+
clientName: string;
|
|
12
|
+
/** Backend-verified account the connection will act as. */
|
|
13
|
+
account: string;
|
|
14
|
+
/** Where the authorization code will be returned. */
|
|
15
|
+
redirectUri: string;
|
|
16
|
+
nonce: string;
|
|
17
|
+
token: string;
|
|
18
|
+
}
|
|
19
|
+
/** The page the user lands on after approving in the App: who connects, as whom, and Confirm. */
|
|
20
|
+
export declare function renderConfirmPage(input: ConfirmPageInput): string;
|
|
21
|
+
/** The link was opened in a browser that did not start the flow (no cookie) or with the wrong token. */
|
|
22
|
+
export declare function renderWrongBrowserPage(): string;
|
|
23
|
+
/** The flow is gone: already confirmed, timed out, or never existed. */
|
|
24
|
+
export declare function renderExpiredPage(): string;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { clientLogoForRedirect } from "./client-logos.js";
|
|
3
|
+
import { INISTATE_LOGO_SVG } from "./inistate-logo.js";
|
|
4
|
+
/**
|
|
5
|
+
* SS06370: the pages the browser shows while finishing an App-approved authorization.
|
|
6
|
+
* Pure rendering - no Express, no provider - so wording, layout and styling live in one file
|
|
7
|
+
* and are easy to change. authorize-app-routes.ts decides WHICH page to send.
|
|
8
|
+
*/
|
|
9
|
+
/* ------------------------------------------------------------------ */
|
|
10
|
+
/* Escaping */
|
|
11
|
+
/* ------------------------------------------------------------------ */
|
|
12
|
+
export const escapeHtml = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
13
|
+
/** Escaped text that may wrap after an "@", so an email breaks at a sensible place on a narrow screen. */
|
|
14
|
+
const breakable = (s) => escapeHtml(s).replace(/@/g, "@<wbr>");
|
|
15
|
+
/* ------------------------------------------------------------------ */
|
|
16
|
+
/* Security headers */
|
|
17
|
+
/* ------------------------------------------------------------------ */
|
|
18
|
+
/**
|
|
19
|
+
* A second tap on Confirm used to burn the flow: the first POST had already issued the code
|
|
20
|
+
* and consumed the nonce, so the retry could only say "expired". Disable the button on submit.
|
|
21
|
+
* Inline, so the CSP carries its hash instead of allowing scripts in general.
|
|
22
|
+
*/
|
|
23
|
+
const SUBMIT_SCRIPT = "document.addEventListener('submit',function(e){var b=e.target.querySelector('button');if(b){b.disabled=true;b.textContent='Connecting\\u2026'}})";
|
|
24
|
+
const SUBMIT_SCRIPT_HASH = `'sha256-${createHash("sha256").update(SUBMIT_SCRIPT).digest("base64")}'`;
|
|
25
|
+
// No form-action here: Chrome also applies it to the redirect that follows the POST, and the
|
|
26
|
+
// whole point of Confirm is to redirect to the client's own origin, so 'self' blocked it and
|
|
27
|
+
// "nothing happened". Everything interpolated into a page is escaped, so nothing here can be
|
|
28
|
+
// steered into posting elsewhere.
|
|
29
|
+
const CSP = `default-src 'none'; style-src 'unsafe-inline'; script-src ${SUBMIT_SCRIPT_HASH}; img-src data:; frame-ancestors 'none'`;
|
|
30
|
+
/** Headers for every page: no caching, no referrer, not frameable (Confirm must not be clickjackable). */
|
|
31
|
+
export const PAGE_HEADERS = {
|
|
32
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
33
|
+
"Cache-Control": "no-store",
|
|
34
|
+
"Referrer-Policy": "no-referrer",
|
|
35
|
+
"X-Frame-Options": "DENY",
|
|
36
|
+
"Content-Security-Policy": CSP,
|
|
37
|
+
};
|
|
38
|
+
/* ------------------------------------------------------------------ */
|
|
39
|
+
/* Look */
|
|
40
|
+
/* ------------------------------------------------------------------ */
|
|
41
|
+
// Palette from the web app (webApp src/assets/scss/colors.scss): primary #2b9ec6, green #a4ce3a.
|
|
42
|
+
// The button uses a darker shade of the primary because white on #2b9ec6 is only ~3:1.
|
|
43
|
+
const STYLE = `
|
|
44
|
+
:root{color-scheme:light dark;--bg:#f4f8fa;--card:#fff;--fg:#212121;--muted:#5f6b72;--line:#e6e6e6;--brand:#2b9ec6;--green:#a4ce3a;--btn:#1d7fa3;--btn-hover:#18698a;--btn-fg:#fff;--warn-bg:#fff6e8;--warn-line:#f3d19e}
|
|
45
|
+
@media (prefers-color-scheme:dark){:root{--bg:#121a1e;--card:#1b252b;--fg:#eef3f5;--muted:#a2b0b8;--line:#2d3b43;--btn:#2b9ec6;--btn-hover:#4db3d6;--btn-fg:#06161c;--warn-bg:#2b2516;--warn-line:#5a4a1e}}
|
|
46
|
+
*{box-sizing:border-box}
|
|
47
|
+
html{-webkit-text-size-adjust:100%;text-size-adjust:100%}
|
|
48
|
+
body{margin:0;min-height:100vh;min-height:100dvh;display:flex;padding:max(16px,env(safe-area-inset-top)) max(16px,env(safe-area-inset-right)) max(16px,env(safe-area-inset-bottom)) max(16px,env(safe-area-inset-left));background:var(--bg);color:var(--fg);font:16px/1.5 Inter,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif}
|
|
49
|
+
main{position:relative;margin:auto;width:100%;max-width:26rem;background:var(--card);border:1px solid var(--line);border-radius:16px;padding:32px 24px 28px;overflow:hidden;box-shadow:0 2px 12px rgba(16,40,52,.08)}
|
|
50
|
+
main::before{content:"";position:absolute;inset:0 0 auto 0;height:5px;background:linear-gradient(to right,var(--brand),var(--green))}
|
|
51
|
+
.pair{display:flex;align-items:center;gap:12px;margin:0 0 16px}
|
|
52
|
+
.logo{display:block;width:44px;height:44px;border-radius:10px}.logo svg{display:block;width:100%;height:100%}
|
|
53
|
+
.link{color:var(--muted);font-size:1.2rem;line-height:1}
|
|
54
|
+
.where{font-size:.9rem}.where strong{color:var(--fg);font-weight:600;overflow-wrap:anywhere}
|
|
55
|
+
h1{margin:0 0 6px;font-size:1.3rem;line-height:1.3;text-wrap:balance}
|
|
56
|
+
p{margin:0 0 14px;color:var(--muted)}
|
|
57
|
+
.account{margin:16px 0;padding:12px 14px;border:1px solid var(--line);border-left:4px solid var(--brand);border-radius:10px;overflow-wrap:anywhere}
|
|
58
|
+
.account small{display:block;color:var(--muted);font-size:.8rem;margin-bottom:2px}
|
|
59
|
+
.account strong{color:var(--fg);font-weight:600}
|
|
60
|
+
.note{padding:10px 12px;border-radius:10px;background:var(--warn-bg);border:1px solid var(--warn-line);color:var(--fg);font-size:.9rem;margin:0 0 18px}
|
|
61
|
+
button{width:100%;min-height:48px;padding:12px 16px;border:0;border-radius:10px;background:var(--btn);color:var(--btn-fg);font:inherit;font-weight:600;cursor:pointer}
|
|
62
|
+
button:hover:not(:disabled){background:var(--btn-hover)}
|
|
63
|
+
button:disabled{opacity:.6;cursor:default}
|
|
64
|
+
button:focus-visible{outline:3px solid var(--brand);outline-offset:2px}
|
|
65
|
+
@media (max-width:400px){main{padding:28px 20px 24px}h1{font-size:1.2rem}.pair{margin-bottom:14px}}
|
|
66
|
+
@media (max-height:480px){main{padding-top:24px}.pair{margin-bottom:10px}}
|
|
67
|
+
`;
|
|
68
|
+
const INISTATE_MARK = `<span class="logo">${INISTATE_LOGO_SVG}</span>`;
|
|
69
|
+
/** The shared shell: head, card, logos row, body. */
|
|
70
|
+
function layout(title, body, header = INISTATE_MARK) {
|
|
71
|
+
return (`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">` +
|
|
72
|
+
`<title>${escapeHtml(title)}</title><style>${STYLE}</style></head><body><main><div class="pair">${header}</div>${body}</main>` +
|
|
73
|
+
`<script>${SUBMIT_SCRIPT}</script></body></html>`);
|
|
74
|
+
}
|
|
75
|
+
/** The page the user lands on after approving in the App: who connects, as whom, and Confirm. */
|
|
76
|
+
export function renderConfirmPage(input) {
|
|
77
|
+
// A logo and a verified name only when the code is going to that service's own host;
|
|
78
|
+
// otherwise the self-chosen client name is all there is, and the host is shown plainly.
|
|
79
|
+
const brand = clientLogoForRedirect(input.redirectUri);
|
|
80
|
+
const label = brand?.name ?? (input.clientName || "this app");
|
|
81
|
+
let host = "";
|
|
82
|
+
try {
|
|
83
|
+
host = new URL(input.redirectUri).host;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
/* shown without a host */
|
|
87
|
+
}
|
|
88
|
+
const header = (brand
|
|
89
|
+
? `<img class="logo" src="${brand.logo}" alt="${escapeHtml(brand.name)}"><span class="link" aria-hidden="true">⇄</span>`
|
|
90
|
+
: "") + INISTATE_MARK;
|
|
91
|
+
return layout("Confirm connection", `<h1>Connect ${escapeHtml(label)} to Inistate?</h1>` +
|
|
92
|
+
`<p>It will be able to work in Inistate as this account:</p>` +
|
|
93
|
+
`<div class="account"><small>Signed in as</small><strong>${breakable(input.account)}</strong></div>` +
|
|
94
|
+
(host ? `<p class="where">After you confirm, you'll be sent back to <strong>${escapeHtml(host)}</strong>.</p>` : "") +
|
|
95
|
+
`<p class="note">Only continue if you just started this from ChatGPT or Claude. If someone sent you this link, close this page.</p>` +
|
|
96
|
+
`<form method="post" action="/authorize/finish">` +
|
|
97
|
+
`<input type="hidden" name="nonce" value="${escapeHtml(input.nonce)}">` +
|
|
98
|
+
`<input type="hidden" name="token" value="${escapeHtml(input.token)}">` +
|
|
99
|
+
`<button type="submit">Confirm</button></form>`, header);
|
|
100
|
+
}
|
|
101
|
+
/** The link was opened in a browser that did not start the flow (no cookie) or with the wrong token. */
|
|
102
|
+
export function renderWrongBrowserPage() {
|
|
103
|
+
return layout("Open this link in the original browser", `<h1>Open this link in the original browser</h1>` +
|
|
104
|
+
`<p>Finish connecting in the same browser where you started from ChatGPT or Claude, then try again.</p>`);
|
|
105
|
+
}
|
|
106
|
+
/** The flow is gone: already confirmed, timed out, or never existed. */
|
|
107
|
+
export function renderExpiredPage() {
|
|
108
|
+
return layout("This link has expired", `<h1>This link has expired</h1>` +
|
|
109
|
+
`<p>It may already have been used. Go back to ChatGPT or Claude and start connecting again.</p>`);
|
|
110
|
+
}
|