Register theme and organization setup routes
This commit is contained in:
1 parent
ed081626b5
commit
934cc08e77
2 files changed
+23
-8
No files matched your search
@@ -39,9 +39,10 @@ const root = resolve(import.meta.dirname, 'public');
|
||||
export function routeTable() {
|
||||
const routes = [];
|
||||
return Object.freeze({
|
||||
add(method, pattern, handler) {
|
||||
add(method, pattern, handler, { publicRead = false } = {}) {
|
||||
if (!['GET', 'POST'].includes(method) || !(typeof pattern === 'string' && pattern.startsWith('/api/') || pattern instanceof RegExp && pattern.source.startsWith('^\\/api\\/')) || typeof handler !== 'function') throw new TypeError('invalid Console route');
|
||||
routes.push({ method, pattern, handler });
|
||||
if (publicRead && method !== 'GET') throw new TypeError('public route must be a read');
|
||||
routes.push({ method, pattern, handler, publicRead });
|
||||
},
|
||||
find(method, path) {
|
||||
return routes.find(route => route.method === method && (typeof route.pattern === 'string' ? route.pattern === path : (route.pattern.lastIndex = 0, route.pattern.test(path))));
|
||||
@@ -710,6 +711,12 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
const confirmations = createConfirmations();
|
||||
const registered = routeTable();
|
||||
for (const registerRoutes of routes) registerRoutes(registered);
|
||||
registered.add('GET', '/api/themes', (req, res) => themesHTTP(req, res, { log }), { publicRead: true });
|
||||
const adminRoute = (req, res, context) => orgAdminHTTP(req, res, { ...context, admin: orgAdmin ??= defaultOrgAdmin(), log });
|
||||
for (const path of ['/api/org-template', '/api/org-validate', '/api/org-admin', /^\/api\/orgs\/[^/]+\/(?:admin|setup)(?:\/(?:queue|bus|project\/[^/]+))?$/]) {
|
||||
for (const method of ['GET', 'POST']) registered.add(method, path, adminRoute);
|
||||
}
|
||||
registered.add('POST', '/api/orgs', adminRoute);
|
||||
const upstream = boardURL(board);
|
||||
// `userOptions` (FED R6, #1800) is `session => options`, the bus-backed options over that session
|
||||
// user's own transport (console-bus.mjs). Only a signed-in listener has user sessions.
|
||||
@@ -844,10 +851,11 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
return json(res, 403, { error: 'unavailable', sent: false });
|
||||
}
|
||||
if (hook) {
|
||||
if (!currentSession || !named || (granted && req.method !== 'GET')) return json(res, 403, { error: 'owner-only' });
|
||||
if (!hook.publicRead && (!currentSession || !named || (granted && req.method !== 'GET'))) return json(res, 403, { error: 'owner-only' });
|
||||
const origin = localOrigin ?? sso?.publicOrigin;
|
||||
if (req.method !== 'GET' && (one(req, 'origin') !== origin || one(req, 'sec-fetch-site') !== 'same-origin')) return json(res, 403, { error: 'cross-origin request refused' });
|
||||
return hook.handler(req, res, { path: originalPath, search, session: currentSession, orgId, origin });
|
||||
if (req.method !== 'GET' && one(req, 'origin') !== origin) return json(res, 403, { error: 'cross-origin request refused' });
|
||||
if (req.method !== 'GET' && one(req, 'sec-fetch-site') !== 'same-origin') return json(res, 403, { error: 'cross-site request refused' });
|
||||
return hook.handler(req, res, { path: originalPath, search, session: ssoSession, orgId, origin, owner: !tester && !granted && named && sso !== null, send: owner.send !== null });
|
||||
}
|
||||
const writer = tester ? { tester: true, user: 'tester', origin: TESTER_ORIGIN } : signer;
|
||||
// P4 page and verbs are owner-only, including reads and static page assets.
|
||||
@@ -886,8 +894,6 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
return projectRemoteHTTP(req, res, { path, read: requestRead, write: send ? requestWrite : null,
|
||||
session: ssoSession, live: () => sso.session(req), origin: localOrigin ?? sso.publicOrigin });
|
||||
}
|
||||
// Stream S6: organization create and setup (org-admin.mjs), owner only.
|
||||
if (await orgAdminHTTP(req, res, { path, search, admin: orgAdmin ??= defaultOrgAdmin(), session: ssoSession, owner: !tester && !granted && named && sso !== null, send: send !== null, origin: sso ? (localOrigin ?? sso.publicOrigin) : null, log })) return;
|
||||
if (tester && ['/project-remote.js', '/project-remote.css', '/files.js', '/files.css'].includes(path)) return json(res, 403, { error: 'owner-only' });
|
||||
// Row 194 (#1692, D4.1): the tester also launches and terminates.
|
||||
if (tester && !['GET', 'HEAD'].includes(req.method) && !['/api/instances', '/api/routes', '/api/name', '/api/agent/launch', '/api/agent/terminate'].includes(path) && !TESTER_CHAT_WRITE.test(path))
|
||||
@@ -998,7 +1004,6 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
if (tester && verb === 'thread.claims') return json(res, 403, { error: 'tester-refused' });
|
||||
return busRead(res, bus, verb, search);
|
||||
}
|
||||
if (path === '/api/themes') return themesHTTP(req, res, { log });
|
||||
if (path === '/api/task-fields') {
|
||||
if (req.method !== 'GET') return json(res, 405, { error: 'method not allowed' });
|
||||
return consoleRead(res, () => fieldTable(fieldsFile));
|
||||
|
||||
@@ -58,6 +58,16 @@ async function console_(t, opts = {}) {
|
||||
}
|
||||
const browserDoc = id => forBrowser(template(id)).doc;
|
||||
|
||||
test('real theme and setup handlers are registered with organization routing', async t => {
|
||||
const organizations = { list: async () => [], resolve: async id => id === 'acme' ? { busHealthy: false } : null };
|
||||
const c = await console_(t, { organizations });
|
||||
assert.equal((await c.call('GET', '/api/themes')).status, 200);
|
||||
assert.equal((await c.call('GET', '/api/org-template?id=acme')).status, 200);
|
||||
assert.equal((await c.call('POST', '/api/orgs', { id: 'acme', doc: browserDoc('acme') })).status, 201);
|
||||
assert.equal((await c.call('GET', '/api/orgs/acme/setup')).status, 200);
|
||||
assert.equal((await c.call('POST', '/api/orgs/acme/setup/bus', {})).status, 201);
|
||||
});
|
||||
|
||||
test('the template comes from templateRender without server-held paths', async t => {
|
||||
const c = await console_(t);
|
||||
const r = await c.call('GET', '/api/org-template?id=acme');
|
||||
|
||||
Reference in new issue
Block a user