@copilotz/directory 0.0.0-stage → 1.0.0-rc.34
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 +21 -0
- package/README.md +70 -2
- package/WORDS.md +197 -0
- package/actions/code.d.ts +4 -0
- package/actions/code.js +135 -0
- package/actions/host.d.ts +2 -0
- package/actions/host.js +55 -0
- package/actions/invite.d.ts +5 -0
- package/actions/invite.js +129 -0
- package/actions/org.d.ts +10 -0
- package/actions/org.js +273 -0
- package/collections/credit-code.d.ts +114 -0
- package/collections/credit-code.js +28 -0
- package/collections/invite.d.ts +81 -0
- package/collections/invite.js +20 -0
- package/collections/membership.d.ts +72 -0
- package/collections/membership.js +22 -0
- package/collections/org-host.d.ts +62 -0
- package/collections/org-host.js +30 -0
- package/collections/org.d.ts +141 -0
- package/collections/org.js +58 -0
- package/collections/staff.d.ts +42 -0
- package/collections/staff.js +12 -0
- package/contract.d.ts +8 -0
- package/contract.js +8 -0
- package/mod.d.ts +67 -0
- package/mod.js +122 -0
- package/package.json +65 -4
- package/routes/console.d.ts +26 -0
- package/routes/console.js +129 -0
- package/routes/enter.d.ts +18 -0
- package/routes/enter.js +47 -0
- package/routes/forward.d.ts +35 -0
- package/routes/forward.js +119 -0
- package/routes/orgs.d.ts +25 -0
- package/routes/orgs.js +113 -0
- package/routes/signout.d.ts +11 -0
- package/routes/signout.js +36 -0
- package/shared/enter.d.ts +36 -0
- package/shared/enter.js +82 -0
- package/shared/http.d.ts +42 -0
- package/shared/http.js +77 -0
- package/shared/member.d.ts +21 -0
- package/shared/member.js +73 -0
- package/shared/settings.d.ts +53 -0
- package/shared/settings.js +16 -0
- package/shared/state.d.ts +74 -0
- package/shared/state.js +101 -0
- package/views/web/codes.d.ts +27 -0
- package/views/web/codes.js +50 -0
- package/views/web/console-codes.d.ts +37 -0
- package/views/web/console-codes.js +166 -0
- package/views/web/console.d.ts +37 -0
- package/views/web/console.js +224 -0
- package/views/web/lists.d.ts +48 -0
- package/views/web/lists.js +42 -0
- package/views/web/org.d.ts +12 -0
- package/views/web/org.js +62 -0
- package/views/web/orgs.d.ts +148 -0
- package/views/web/orgs.js +253 -0
- package/views/web/style.d.ts +6 -0
- package/views/web/style.js +196 -0
- package/views/web/switcher.d.ts +72 -0
- package/views/web/switcher.js +354 -0
- package/views/web/words.d.ts +10 -0
- package/views/web/words.js +201 -0
- package/web.d.ts +35 -0
- package/web.js +50 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Staff's console (platform staff only, by their sign-in or the email they
|
|
3
|
+
* verified; anyone else, a 403):
|
|
4
|
+
*
|
|
5
|
+
* - `GET /console/orgs`: every org, with what each last reported;
|
|
6
|
+
* - `POST /console/orgs/<id>` `{ suspended?, name? }`;
|
|
7
|
+
* - `POST /console/orgs/<id>/move` `{ schema }`: its rows to another schema
|
|
8
|
+
* (closed first, then opened there on its next use);
|
|
9
|
+
* - `GET /console/orgs/<id>/export`: its rows, as SQL, to save;
|
|
10
|
+
* - `POST /console/orgs/<id>/delete` `{ confirm: <its name> }`: closed, its
|
|
11
|
+
* rows dropped, and it's forgotten.
|
|
12
|
+
*
|
|
13
|
+
* The rows' part is the app's (`operations` in the directory's settings);
|
|
14
|
+
* without it, a 501. Gift codes: `GET /console/codes`; `POST /console/codes`
|
|
15
|
+
* `{ amount, expires?, until?, note? }` (the code, shown this once); `POST
|
|
16
|
+
* /console/codes/revoke` `{ id }`.
|
|
17
|
+
*/
|
|
18
|
+
import { failure } from "copilotz";
|
|
19
|
+
import { personIn, profileOf } from "../shared/enter.js";
|
|
20
|
+
import { json, orgsOf } from "../shared/http.js";
|
|
21
|
+
import { settingsOf } from "../shared/settings.js";
|
|
22
|
+
import { forget } from "../shared/state.js";
|
|
23
|
+
/** The staff member the directory's session is, or a 403. */
|
|
24
|
+
async function staffOnly(ctx) {
|
|
25
|
+
if (ctx.actor.kind !== "person")
|
|
26
|
+
throw failure("forbidden", "Staff only", { public: true });
|
|
27
|
+
const person = personIn(ctx);
|
|
28
|
+
const email = (await profileOf(ctx.runtime, person))?.email;
|
|
29
|
+
const { staff } = await ctx.runtime.call("org.mine", {
|
|
30
|
+
person,
|
|
31
|
+
...(email ? { email } : {}),
|
|
32
|
+
});
|
|
33
|
+
if (!staff)
|
|
34
|
+
throw failure("forbidden", "Staff only", { public: true });
|
|
35
|
+
return person;
|
|
36
|
+
}
|
|
37
|
+
const body = async (request) => await request.json().catch(() => ({}));
|
|
38
|
+
/** `GET /console/orgs`. */
|
|
39
|
+
export const consoleOrgsRoute = async (request, ctx) => {
|
|
40
|
+
if (request.method !== "GET")
|
|
41
|
+
return json({ error: "Only GET" }, 405);
|
|
42
|
+
await staffOnly(ctx);
|
|
43
|
+
return json(await ctx.runtime.call("org.list", {}));
|
|
44
|
+
};
|
|
45
|
+
/** `/console/orgs/<id>` and `/console/orgs/<id>/<what>`: staff's operations on an org. */
|
|
46
|
+
export const operateRoute = async (request, ctx) => {
|
|
47
|
+
await staffOnly(ctx);
|
|
48
|
+
const { runtime, params } = ctx;
|
|
49
|
+
const resources = runtime.describe().resources;
|
|
50
|
+
const org = decodeURIComponent(params.org);
|
|
51
|
+
const what = params.what ? decodeURIComponent(params.what) : "";
|
|
52
|
+
const info = await runtime.query("org.info", { org });
|
|
53
|
+
if (!info)
|
|
54
|
+
return json({ error: "No such org" }, 404);
|
|
55
|
+
const ops = settingsOf({ resources }).operations ?? {};
|
|
56
|
+
if (request.method === "GET" && what === "export") {
|
|
57
|
+
if (!ops.export)
|
|
58
|
+
return json({ error: "This app doesn't export orgs" }, 501);
|
|
59
|
+
return new Response(await ops.export(info), {
|
|
60
|
+
headers: {
|
|
61
|
+
"content-type": "application/sql; charset=utf-8",
|
|
62
|
+
"content-disposition": `attachment; filename="${info.id}.sql"`,
|
|
63
|
+
"cache-control": "no-store",
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (request.method !== "POST")
|
|
68
|
+
return json({ error: "Not found" }, 404);
|
|
69
|
+
const asked = await body(request);
|
|
70
|
+
switch (what) {
|
|
71
|
+
case "": {
|
|
72
|
+
if (typeof asked.suspended === "boolean") {
|
|
73
|
+
await runtime.call("org.suspend", { org: info.id, suspended: asked.suspended });
|
|
74
|
+
}
|
|
75
|
+
if (typeof asked.name === "string" && asked.name.trim()) {
|
|
76
|
+
await runtime.call("org.rename", { org: info.id, name: asked.name.trim() });
|
|
77
|
+
}
|
|
78
|
+
forget(resources, { org: info.id });
|
|
79
|
+
return json({ org: info.id });
|
|
80
|
+
}
|
|
81
|
+
case "move": {
|
|
82
|
+
if (!ops.move)
|
|
83
|
+
return json({ error: "This app doesn't move orgs" }, 501);
|
|
84
|
+
const to = { schema: String(asked.schema ?? "").trim() };
|
|
85
|
+
if (!/^[a-z][a-z0-9_]{0,62}$/.test(to.schema)) {
|
|
86
|
+
return json({ error: "A schema name (lowercase letters, digits, _)" }, 400);
|
|
87
|
+
}
|
|
88
|
+
if (to.schema === info.placement.schema)
|
|
89
|
+
return json({ org: info.id, placement: to });
|
|
90
|
+
// Nothing writes while its rows move: it opens again where they went.
|
|
91
|
+
await orgsOf(runtime).close(info.id);
|
|
92
|
+
await ops.move(info, to);
|
|
93
|
+
await runtime.call("org.place", { org: info.id, placement: to });
|
|
94
|
+
forget(resources, { org: info.id });
|
|
95
|
+
return json({ org: info.id, placement: to });
|
|
96
|
+
}
|
|
97
|
+
case "delete": {
|
|
98
|
+
if (!ops.drop)
|
|
99
|
+
return json({ error: "This app doesn't delete orgs" }, 501);
|
|
100
|
+
// Typing its name confirms it: nothing of it is kept.
|
|
101
|
+
if (asked.confirm !== info.name) {
|
|
102
|
+
return json({ error: "Type the org's name to delete it" }, 400);
|
|
103
|
+
}
|
|
104
|
+
await orgsOf(runtime).close(info.id);
|
|
105
|
+
await ops.drop(info);
|
|
106
|
+
await runtime.call("org.delete", { org: info.id });
|
|
107
|
+
forget(resources, { org: info.id });
|
|
108
|
+
return json({ deleted: info.id });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return json({ error: "Not found" }, 404);
|
|
112
|
+
};
|
|
113
|
+
/** `GET /console/codes` and `POST /console/codes`. */
|
|
114
|
+
export const codesRoute = async (request, ctx) => {
|
|
115
|
+
const person = await staffOnly(ctx);
|
|
116
|
+
if (request.method === "GET")
|
|
117
|
+
return json(await ctx.runtime.call("code.list", {}));
|
|
118
|
+
if (request.method !== "POST")
|
|
119
|
+
return json({ error: "Only GET and POST" }, 405);
|
|
120
|
+
return await ctx.runtime.call("code.create", { ...await body(request), by: person }).then((made) => json(made), (error) => json({ error: error.message }, 400));
|
|
121
|
+
};
|
|
122
|
+
/** `POST /console/codes/revoke` `{ id }`. */
|
|
123
|
+
export const revokeCodeRoute = async (request, ctx) => {
|
|
124
|
+
if (request.method !== "POST")
|
|
125
|
+
return json({ error: "Only POST" }, 405);
|
|
126
|
+
await staffOnly(ctx);
|
|
127
|
+
const { id } = await body(request);
|
|
128
|
+
return await ctx.runtime.call("code.revoke", { id }).then(() => json({}), (error) => json({ error: error.message }, 400));
|
|
129
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `POST /enter`, an org's own (under its address: `/o/<org>/enter`), which
|
|
3
|
+
* only the directory's handoff opens (see shared/enter.ts): the person it
|
|
4
|
+
* signs goes into this org. As a sign-in does: what the directory verified
|
|
5
|
+
* about them is kept (`person.set`), they join with the handoff's role the
|
|
6
|
+
* first time (`org.join`; staff who don't belong, never), and a session
|
|
7
|
+
* starts (`session.start`), set in the org's cookie (identity's `session`,
|
|
8
|
+
* which this org's runtime names `s_<org>`). A session they already hold
|
|
9
|
+
* here, as the same person and the same standing, stays. Then `{ org }`, or
|
|
10
|
+
* a redirect to the page they asked for (`to`).
|
|
11
|
+
*/
|
|
12
|
+
import type { Route } from "copilotz";
|
|
13
|
+
/** The org's `/enter`, checking handoffs with the key of `directory` (its resources). */
|
|
14
|
+
export declare const enterRoute: (directory: Record<string, Record<string, unknown>>, org: {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
kind: string;
|
|
18
|
+
}) => Route;
|
package/routes/enter.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SESSION } from "copilotz/plugins/access/contract";
|
|
2
|
+
import { json } from "../shared/http.js";
|
|
3
|
+
import { readHandoff } from "../shared/state.js";
|
|
4
|
+
/** A path on the app (never another host's): where a handoff may send the browser. */
|
|
5
|
+
const onApp = (to) => to.startsWith("/") && !to.startsWith("//") && !to.includes("\\");
|
|
6
|
+
/** The org's `/enter`, checking handoffs with the key of `directory` (its resources). */
|
|
7
|
+
export const enterRoute = (directory, org) => async (request, { runtime, cookies }) => {
|
|
8
|
+
if (request.method !== "POST")
|
|
9
|
+
return json({ error: "Only POST" }, 405);
|
|
10
|
+
const handoff = await readHandoff(directory, new URL(request.url).searchParams);
|
|
11
|
+
if (!handoff || handoff.org !== org.id) {
|
|
12
|
+
return json({ error: "Only the directory sends people in here" }, 403);
|
|
13
|
+
}
|
|
14
|
+
if (handoff.to !== undefined && !onApp(handoff.to)) {
|
|
15
|
+
return json({ error: "Not a page of the app" }, 400);
|
|
16
|
+
}
|
|
17
|
+
const { person } = handoff;
|
|
18
|
+
const staff = handoff.staff === true;
|
|
19
|
+
await runtime.call("person.set", {
|
|
20
|
+
person,
|
|
21
|
+
...(handoff.email ? { email: handoff.email } : {}),
|
|
22
|
+
...(handoff.name ? { name: handoff.name } : {}),
|
|
23
|
+
...(handoff.picture ? { picture: handoff.picture } : {}),
|
|
24
|
+
});
|
|
25
|
+
// Staff going into an org they don't belong to aren't made one of its people.
|
|
26
|
+
if (!staff || handoff.role) {
|
|
27
|
+
await runtime.call("org.join", { person, ...(handoff.role ? { role: handoff.role } : {}) });
|
|
28
|
+
}
|
|
29
|
+
const held = cookies.get(SESSION);
|
|
30
|
+
const kept = held
|
|
31
|
+
? await runtime.query("credential.actor", { secret: held }).catch(() => null)
|
|
32
|
+
: null;
|
|
33
|
+
const session = kept?.id === person && (kept.staff === true) === staff
|
|
34
|
+
? held
|
|
35
|
+
: (await runtime.call("session.start", {
|
|
36
|
+
person,
|
|
37
|
+
...(staff ? { staff: true } : {}),
|
|
38
|
+
})).session;
|
|
39
|
+
cookies.set(SESSION, session);
|
|
40
|
+
if (handoff.to) {
|
|
41
|
+
return new Response(null, {
|
|
42
|
+
status: 302,
|
|
43
|
+
headers: { location: handoff.to, "cache-control": "no-store" },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return json({ org: { id: org.id, name: org.name, kind: org.kind } });
|
|
47
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the directory hands to an org's runtime, the request untouched (its
|
|
3
|
+
* path, its body, its socket): the org's own runtime answers it (its page,
|
|
4
|
+
* its `/connect` and socket under its address, its webhooks, its routes),
|
|
5
|
+
* by the org's own `who`.
|
|
6
|
+
*
|
|
7
|
+
* - **By its address** (`/o/<org>/…`): whatever reaches an org without a
|
|
8
|
+
* browser's session (its API with a key, its webhooks, its computers), and
|
|
9
|
+
* its page's `/connect` and socket. A 404 for no such org, a 403 for a
|
|
10
|
+
* suspended one.
|
|
11
|
+
* - **By the browser's cookie** (`/*`, matched last): the org the browser is
|
|
12
|
+
* in (`org`), with its session there (`s_<org>`). A page asked for without
|
|
13
|
+
* one (signed in at the directory, but in no org yet, or its session there
|
|
14
|
+
* gone, or the org no longer theirs) goes in first: the one they were in,
|
|
15
|
+
* else their last, else their own, made now; then the same page again
|
|
16
|
+
* (302). Anything else with no org is the lobby's (the sign-in page, the
|
|
17
|
+
* app's settings), as is everything for someone signed in nowhere.
|
|
18
|
+
* - **Hosts orgs claimed** (`*` for every host but the app's own, which the
|
|
19
|
+
* app names: `resources.hosts`): the org that claimed it, for its
|
|
20
|
+
* routes by host alone (its runtime has the app's hosts too); a host
|
|
21
|
+
* nobody claimed, a 404.
|
|
22
|
+
* - `GET /__copilotz/hosts/resolve?host=<h>`: whose a host is, `{ org,
|
|
23
|
+
* target? }` (or 404), for the app's front alone (it carries the front's
|
|
24
|
+
* secret, `resources.caller.front`), on any host: the front sends a host
|
|
25
|
+
* with a target (an app's service) straight there.
|
|
26
|
+
*/
|
|
27
|
+
import { type Route } from "copilotz";
|
|
28
|
+
/** `/o/<org>` and `/o/<org>/…`: that org, by its address. */
|
|
29
|
+
export declare const addressedRoute: Route;
|
|
30
|
+
/** `/*`: the org the browser is in, going in first when a page needs it to. */
|
|
31
|
+
export declare const currentRoute: Route;
|
|
32
|
+
/** `GET /__copilotz/hosts/resolve?host=`: whose a host is, for the front alone. */
|
|
33
|
+
export declare const resolveRoute: Route;
|
|
34
|
+
/** Every host but the app's own: the org that claimed it, its routes by host alone. */
|
|
35
|
+
export declare const claimedRoute: Route;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the directory hands to an org's runtime, the request untouched (its
|
|
3
|
+
* path, its body, its socket): the org's own runtime answers it (its page,
|
|
4
|
+
* its `/connect` and socket under its address, its webhooks, its routes),
|
|
5
|
+
* by the org's own `who`.
|
|
6
|
+
*
|
|
7
|
+
* - **By its address** (`/o/<org>/…`): whatever reaches an org without a
|
|
8
|
+
* browser's session (its API with a key, its webhooks, its computers), and
|
|
9
|
+
* its page's `/connect` and socket. A 404 for no such org, a 403 for a
|
|
10
|
+
* suspended one.
|
|
11
|
+
* - **By the browser's cookie** (`/*`, matched last): the org the browser is
|
|
12
|
+
* in (`org`), with its session there (`s_<org>`). A page asked for without
|
|
13
|
+
* one (signed in at the directory, but in no org yet, or its session there
|
|
14
|
+
* gone, or the org no longer theirs) goes in first: the one they were in,
|
|
15
|
+
* else their last, else their own, made now; then the same page again
|
|
16
|
+
* (302). Anything else with no org is the lobby's (the sign-in page, the
|
|
17
|
+
* app's settings), as is everything for someone signed in nowhere.
|
|
18
|
+
* - **Hosts orgs claimed** (`*` for every host but the app's own, which the
|
|
19
|
+
* app names: `resources.hosts`): the org that claimed it, for its
|
|
20
|
+
* routes by host alone (its runtime has the app's hosts too); a host
|
|
21
|
+
* nobody claimed, a 404.
|
|
22
|
+
* - `GET /__copilotz/hosts/resolve?host=<h>`: whose a host is, `{ org,
|
|
23
|
+
* target? }` (or 404), for the app's front alone (it carries the front's
|
|
24
|
+
* secret, `resources.caller.front`), on any host: the front sends a host
|
|
25
|
+
* with a target (an app's service) straight there.
|
|
26
|
+
*/
|
|
27
|
+
import { failure, vouched } from "copilotz";
|
|
28
|
+
import { enter, landing, personIn, profileOf, report } from "../shared/enter.js";
|
|
29
|
+
import { CURRENT, json, navigating, orgCookie, orgsOf, RESOLVE } from "../shared/http.js";
|
|
30
|
+
import { settingsOf } from "../shared/settings.js";
|
|
31
|
+
import { claimOf, infoOf } from "../shared/state.js";
|
|
32
|
+
/** The request, answered by an org's runtime, open for it: as it came. */
|
|
33
|
+
async function into(dir, org, request) {
|
|
34
|
+
const runtime = await orgsOf(dir).get(org);
|
|
35
|
+
if (!runtime)
|
|
36
|
+
return json({ error: "No such org" }, 404);
|
|
37
|
+
report(dir, org, runtime);
|
|
38
|
+
return await runtime.fetch(request);
|
|
39
|
+
}
|
|
40
|
+
/** What answers someone signed in nowhere: the lobby the app gives (its sign-in page). */
|
|
41
|
+
async function lobby(dir, request) {
|
|
42
|
+
const given = settingsOf({ resources: dir.describe().resources }).lobby;
|
|
43
|
+
return given ? await given.fetch(request) : json({ error: "Not signed in" }, 401);
|
|
44
|
+
}
|
|
45
|
+
/** `/o/<org>` and `/o/<org>/…`: that org, by its address. */
|
|
46
|
+
export const addressedRoute = async (request, { runtime, params }) => {
|
|
47
|
+
const org = decodeURIComponent(params.org);
|
|
48
|
+
const info = await infoOf(runtime, runtime.describe().resources, org);
|
|
49
|
+
if (!info)
|
|
50
|
+
return json({ error: "No such org" }, 404);
|
|
51
|
+
if (info.suspended)
|
|
52
|
+
return json({ error: "This org is suspended." }, 403);
|
|
53
|
+
return await into(runtime, info.id, request);
|
|
54
|
+
};
|
|
55
|
+
/** `/*`: the org the browser is in, going in first when a page needs it to. */
|
|
56
|
+
export const currentRoute = async (request, ctx) => {
|
|
57
|
+
const { runtime, cookies } = ctx;
|
|
58
|
+
const resources = runtime.describe().resources;
|
|
59
|
+
const page = navigating(request);
|
|
60
|
+
const org = cookies.get(CURRENT);
|
|
61
|
+
const name = org ? orgCookie(org) : undefined;
|
|
62
|
+
const held = name ? cookies.get(name) : undefined;
|
|
63
|
+
const info = org && held ? await infoOf(runtime, resources, org) : null;
|
|
64
|
+
if (info && !info.suspended) {
|
|
65
|
+
// A page goes on only with a session that's still someone there.
|
|
66
|
+
const open = page ? await orgsOf(runtime).get(info.id) : undefined;
|
|
67
|
+
const good = !page ||
|
|
68
|
+
!!await open?.query("credential.actor", { secret: held }).catch(() => null);
|
|
69
|
+
if (good)
|
|
70
|
+
return await into(runtime, info.id, request);
|
|
71
|
+
}
|
|
72
|
+
else if (info && !page) {
|
|
73
|
+
return json({ error: "This org is suspended." }, 403);
|
|
74
|
+
}
|
|
75
|
+
if (ctx.actor.kind !== "person" || !page)
|
|
76
|
+
return await lobby(runtime, request);
|
|
77
|
+
// A page, for someone signed in at the directory: in the org they were in, else one that's theirs.
|
|
78
|
+
const person = personIn(ctx);
|
|
79
|
+
const profile = await profileOf(runtime, person);
|
|
80
|
+
const called = profile?.name ?? profile?.email ?? person;
|
|
81
|
+
const url = new URL(request.url);
|
|
82
|
+
const to = url.pathname + url.search;
|
|
83
|
+
const first = org && !info?.suspended ? org : await landing(runtime, person, called, org);
|
|
84
|
+
try {
|
|
85
|
+
return await enter(request, ctx, first, { to });
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (error.code !== "forbidden")
|
|
89
|
+
throw error;
|
|
90
|
+
return await enter(request, ctx, await landing(runtime, person, called, first), { to });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** `GET /__copilotz/hosts/resolve?host=`: whose a host is, for the front alone. */
|
|
94
|
+
export const resolveRoute = async (request, { runtime }) => {
|
|
95
|
+
if (request.method !== "GET")
|
|
96
|
+
return json({ error: "Only GET" }, 405);
|
|
97
|
+
const resources = runtime.describe().resources;
|
|
98
|
+
const front = resources.caller?.front;
|
|
99
|
+
if (!vouched(request, front))
|
|
100
|
+
return json({ error: "The front's only" }, 403);
|
|
101
|
+
const host = new URL(request.url).searchParams.get("host") ?? "";
|
|
102
|
+
const claim = await claimOf(runtime, resources, host);
|
|
103
|
+
return claim ? json(claim) : json({ error: "Nobody's host" }, 404);
|
|
104
|
+
};
|
|
105
|
+
/** Every host but the app's own: the org that claimed it, its routes by host alone. */
|
|
106
|
+
export const claimedRoute = async (request, ctx) => {
|
|
107
|
+
const { runtime } = ctx;
|
|
108
|
+
const url = new URL(request.url);
|
|
109
|
+
if (url.pathname === RESOLVE)
|
|
110
|
+
return await resolveRoute(request, ctx);
|
|
111
|
+
const resources = runtime.describe().resources;
|
|
112
|
+
const claim = await claimOf(runtime, resources, url.host);
|
|
113
|
+
const info = claim ? await infoOf(runtime, resources, claim.org) : null;
|
|
114
|
+
if (!info)
|
|
115
|
+
return json({ error: "Not found" }, 404);
|
|
116
|
+
if (info.suspended)
|
|
117
|
+
throw failure("forbidden", "This org is suspended.", { public: true });
|
|
118
|
+
return await into(runtime, info.id, request);
|
|
119
|
+
};
|
package/routes/orgs.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The orgs of whoever the directory's session is (each a 401 for anyone
|
|
3
|
+
* else; what goes wrong answers its code's status, its words only when
|
|
4
|
+
* they're for anyone):
|
|
5
|
+
*
|
|
6
|
+
* - `GET /orgs`: who's signed in, the org they're in (`current`), the orgs
|
|
7
|
+
* they belong to (the last entered first), the invitations waiting for
|
|
8
|
+
* their email, whether they're platform staff, and whether the session
|
|
9
|
+
* they hold in the org they're in is staff's (`asStaff`);
|
|
10
|
+
* - `POST /orgs` `{ name }`: makes a team org, its maker its owner, and
|
|
11
|
+
* goes into it;
|
|
12
|
+
* - `POST /orgs/enter` `{ org, staff? }`: goes into another (staff into any,
|
|
13
|
+
* as staff when they ask: `staff: true`);
|
|
14
|
+
* - `POST /orgs/invites/answer` `{ invite, accept }`: joins its org (and
|
|
15
|
+
* goes into it) or declines (`{ joined: false }`).
|
|
16
|
+
*
|
|
17
|
+
* Going into an org answers `{ org }`, with its cookies (see shared/enter.ts).
|
|
18
|
+
*/
|
|
19
|
+
import { type Route } from "copilotz";
|
|
20
|
+
/** `GET /orgs` and `POST /orgs`. */
|
|
21
|
+
export declare const orgsRoute: Route;
|
|
22
|
+
/** `POST /orgs/enter` `{ org, staff? }`. */
|
|
23
|
+
export declare const enterOrgRoute: Route;
|
|
24
|
+
/** `POST /orgs/invites/answer` `{ invite, accept }`. */
|
|
25
|
+
export declare const answerInviteRoute: Route;
|
package/routes/orgs.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The orgs of whoever the directory's session is (each a 401 for anyone
|
|
3
|
+
* else; what goes wrong answers its code's status, its words only when
|
|
4
|
+
* they're for anyone):
|
|
5
|
+
*
|
|
6
|
+
* - `GET /orgs`: who's signed in, the org they're in (`current`), the orgs
|
|
7
|
+
* they belong to (the last entered first), the invitations waiting for
|
|
8
|
+
* their email, whether they're platform staff, and whether the session
|
|
9
|
+
* they hold in the org they're in is staff's (`asStaff`);
|
|
10
|
+
* - `POST /orgs` `{ name }`: makes a team org, its maker its owner, and
|
|
11
|
+
* goes into it;
|
|
12
|
+
* - `POST /orgs/enter` `{ org, staff? }`: goes into another (staff into any,
|
|
13
|
+
* as staff when they ask: `staff: true`);
|
|
14
|
+
* - `POST /orgs/invites/answer` `{ invite, accept }`: joins its org (and
|
|
15
|
+
* goes into it) or declines (`{ joined: false }`).
|
|
16
|
+
*
|
|
17
|
+
* Going into an org answers `{ org }`, with its cookies (see shared/enter.ts).
|
|
18
|
+
*/
|
|
19
|
+
import { jsonOf } from "copilotz";
|
|
20
|
+
import { enter, personIn, profileOf } from "../shared/enter.js";
|
|
21
|
+
import { CURRENT, json, orgCookie, orgsOf } from "../shared/http.js";
|
|
22
|
+
import { infoOf } from "../shared/state.js";
|
|
23
|
+
const NOT_JSON = "Send a JSON object, of 16 KiB at most";
|
|
24
|
+
/** Whether the session a request holds in an org is staff's. */
|
|
25
|
+
async function heldAsStaff(ctx, org) {
|
|
26
|
+
const name = orgCookie(org);
|
|
27
|
+
const secret = name ? ctx.cookies.get(name) : undefined;
|
|
28
|
+
if (!secret)
|
|
29
|
+
return false;
|
|
30
|
+
const info = await infoOf(ctx.runtime, ctx.runtime.describe().resources, org);
|
|
31
|
+
if (!info || info.suspended)
|
|
32
|
+
return false;
|
|
33
|
+
const runtime = await orgsOf(ctx.runtime).get(info.id);
|
|
34
|
+
const actor = await runtime?.query("credential.actor", { secret })
|
|
35
|
+
.catch(() => null);
|
|
36
|
+
return actor?.staff === true;
|
|
37
|
+
}
|
|
38
|
+
/** `GET /orgs` and `POST /orgs`. */
|
|
39
|
+
export const orgsRoute = async (request, ctx) => {
|
|
40
|
+
const { runtime } = ctx;
|
|
41
|
+
const person = personIn(ctx);
|
|
42
|
+
if (request.method === "GET") {
|
|
43
|
+
const email = (await profileOf(runtime, person))?.email ?? null;
|
|
44
|
+
const [{ orgs, staff }, { invites }] = await Promise.all([
|
|
45
|
+
runtime.call("org.mine", {
|
|
46
|
+
person,
|
|
47
|
+
...(email ? { email } : {}),
|
|
48
|
+
}),
|
|
49
|
+
email
|
|
50
|
+
? runtime.call("invite.mine", { email })
|
|
51
|
+
: Promise.resolve({ invites: [] }),
|
|
52
|
+
]);
|
|
53
|
+
const current = ctx.cookies.get(CURRENT) ?? null;
|
|
54
|
+
return json({
|
|
55
|
+
person,
|
|
56
|
+
email,
|
|
57
|
+
current,
|
|
58
|
+
orgs,
|
|
59
|
+
invites,
|
|
60
|
+
staff,
|
|
61
|
+
asStaff: staff && !!current && await heldAsStaff(ctx, current),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (request.method !== "POST")
|
|
65
|
+
return json({ error: "Only GET and POST" }, 405);
|
|
66
|
+
const asked = await jsonOf(request);
|
|
67
|
+
if (!asked)
|
|
68
|
+
return json({ error: NOT_JSON }, 400);
|
|
69
|
+
const name = typeof asked.name === "string" ? asked.name.trim() : "";
|
|
70
|
+
if (!name)
|
|
71
|
+
return json({ error: "A name" }, 400);
|
|
72
|
+
const { org } = await runtime.call("org.create", {
|
|
73
|
+
name,
|
|
74
|
+
kind: "team",
|
|
75
|
+
by: person,
|
|
76
|
+
});
|
|
77
|
+
return await enter(request, ctx, org);
|
|
78
|
+
};
|
|
79
|
+
/** `POST /orgs/enter` `{ org, staff? }`. */
|
|
80
|
+
export const enterOrgRoute = async (request, ctx) => {
|
|
81
|
+
if (request.method !== "POST")
|
|
82
|
+
return json({ error: "Only POST" }, 405);
|
|
83
|
+
personIn(ctx);
|
|
84
|
+
const asked = await jsonOf(request);
|
|
85
|
+
if (!asked)
|
|
86
|
+
return json({ error: NOT_JSON }, 400);
|
|
87
|
+
if (typeof asked.org !== "string" || !asked.org)
|
|
88
|
+
return json({ error: "Which org" }, 400);
|
|
89
|
+
return await enter(request, ctx, asked.org, { staff: asked.staff === true });
|
|
90
|
+
};
|
|
91
|
+
/** `POST /orgs/invites/answer` `{ invite, accept }`. */
|
|
92
|
+
export const answerInviteRoute = async (request, ctx) => {
|
|
93
|
+
if (request.method !== "POST")
|
|
94
|
+
return json({ error: "Only POST" }, 405);
|
|
95
|
+
const person = personIn(ctx);
|
|
96
|
+
const email = (await profileOf(ctx.runtime, person))?.email;
|
|
97
|
+
if (!email)
|
|
98
|
+
return json({ error: "Not signed in with an email" }, 401);
|
|
99
|
+
const asked = await jsonOf(request);
|
|
100
|
+
if (!asked)
|
|
101
|
+
return json({ error: NOT_JSON }, 400);
|
|
102
|
+
if (typeof asked.invite !== "string")
|
|
103
|
+
return json({ error: "Which invitation" }, 400);
|
|
104
|
+
const answered = await ctx.runtime.call("invite.answer", {
|
|
105
|
+
invite: asked.invite,
|
|
106
|
+
person,
|
|
107
|
+
email,
|
|
108
|
+
accept: asked.accept === true,
|
|
109
|
+
});
|
|
110
|
+
if (!answered.joined)
|
|
111
|
+
return json({ joined: false });
|
|
112
|
+
return await enter(request, ctx, answered.org);
|
|
113
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `POST /signout`, the directory's (in place of identity's): out of the
|
|
3
|
+
* directory and every org the browser holds a session for. The directory's
|
|
4
|
+
* session ends; each org's ends in its org when that org is open (signing
|
|
5
|
+
* out never opens one: any request may name orgs in its cookies), and
|
|
6
|
+
* whoever it was is disconnected there; the person's browsers in every org
|
|
7
|
+
* open now go too. Every one of those cookies is cleared, and which org the
|
|
8
|
+
* browser is in.
|
|
9
|
+
*/
|
|
10
|
+
import type { Route } from "copilotz";
|
|
11
|
+
export declare const signOutRoute: Route;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CURRENT, heldOrgs, json, SESSION, sessionOf } from "../shared/http.js";
|
|
2
|
+
import { settingsOf } from "../shared/settings.js";
|
|
3
|
+
export const signOutRoute = async (request, { runtime, actor, cookies }) => {
|
|
4
|
+
if (request.method !== "POST")
|
|
5
|
+
return json({ error: "Only POST" }, 405);
|
|
6
|
+
const secret = cookies.get(SESSION);
|
|
7
|
+
if (secret && actor.kind === "person") {
|
|
8
|
+
await runtime.call("session.end", { secret }, { actor }).catch(() => { });
|
|
9
|
+
runtime.disconnect(actor);
|
|
10
|
+
}
|
|
11
|
+
const orgs = settingsOf({ resources: runtime.describe().resources }).orgs;
|
|
12
|
+
await Promise.all(heldOrgs(request).map(async (org) => {
|
|
13
|
+
const held = cookies.get(sessionOf(org));
|
|
14
|
+
if (held === undefined)
|
|
15
|
+
return;
|
|
16
|
+
cookies.clear(sessionOf(org));
|
|
17
|
+
const open = orgs?.peek(org);
|
|
18
|
+
if (!open)
|
|
19
|
+
return;
|
|
20
|
+
const who = await open.query("credential.actor", { secret: held })
|
|
21
|
+
.catch(() => null);
|
|
22
|
+
if (!who)
|
|
23
|
+
return;
|
|
24
|
+
await open.call("session.end", { secret: held }, { actor: who }).catch(() => { });
|
|
25
|
+
open.disconnect(who);
|
|
26
|
+
}));
|
|
27
|
+
// Their browsers in every org open now go: one comes back only with a cookie that's someone.
|
|
28
|
+
if (actor.kind === "person") {
|
|
29
|
+
for (const [, open] of orgs?.entries() ?? []) {
|
|
30
|
+
open.disconnect({ kind: "person", id: actor.id });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
cookies.clear(SESSION);
|
|
34
|
+
cookies.clear(CURRENT);
|
|
35
|
+
return json({});
|
|
36
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Going into an org, from the directory's session: the directory lets the
|
|
3
|
+
* person in (`org.enter`: a member, or staff), signs who goes in (the
|
|
4
|
+
* handoff), and hands it to the org's own `/enter` (see member.ts), which
|
|
5
|
+
* knows them there, starts its session and sets its cookie (`s_<org>`), as
|
|
6
|
+
* a sign-in would: sessions stay the org's. The directory sets which org the
|
|
7
|
+
* browser is in (`org`) on the org's answer, and passes it on: `{ org }`, or
|
|
8
|
+
* a redirect for a page asked for (`to`).
|
|
9
|
+
*/
|
|
10
|
+
import { type RouteContext, type Runtime } from "copilotz";
|
|
11
|
+
type Profile = {
|
|
12
|
+
name?: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
picture?: string;
|
|
15
|
+
};
|
|
16
|
+
/** What a sign-in verified about someone, at the directory. */
|
|
17
|
+
export declare const profileOf: (runtime: Pick<Runtime, "query">, person: string) => Promise<Profile | null>;
|
|
18
|
+
/** Who's signed in at the directory (its session), or a 401. */
|
|
19
|
+
export declare function personIn(ctx: Pick<RouteContext, "actor">): string;
|
|
20
|
+
/**
|
|
21
|
+
* Tells the directory how an org is (its people and spaces, and that it was
|
|
22
|
+
* used now), for staff's console: at most once a minute while it's used,
|
|
23
|
+
* unless `now`. Nothing waits for it, and nothing fails for it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function report(dir: Pick<Runtime, "call" | "describe">, org: string, runtime: Pick<Runtime, "call">, now?: boolean): void;
|
|
26
|
+
/**
|
|
27
|
+
* Goes into `org` as the person the directory's session is: the org's answer
|
|
28
|
+
* (its cookie set), with which org the browser is in set too.
|
|
29
|
+
*/
|
|
30
|
+
export declare function enter(request: Request, ctx: Pick<RouteContext, "runtime" | "actor" | "cookies">, org: string, options?: {
|
|
31
|
+
staff?: boolean;
|
|
32
|
+
to?: string;
|
|
33
|
+
}): Promise<Response>;
|
|
34
|
+
/** The org someone goes back to: the one they were in last (not `not`), or their own, made now. */
|
|
35
|
+
export declare function landing(runtime: Pick<Runtime, "call">, person: string, name: string, not?: string): Promise<string>;
|
|
36
|
+
export {};
|
package/shared/enter.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Going into an org, from the directory's session: the directory lets the
|
|
3
|
+
* person in (`org.enter`: a member, or staff), signs who goes in (the
|
|
4
|
+
* handoff), and hands it to the org's own `/enter` (see member.ts), which
|
|
5
|
+
* knows them there, starts its session and sets its cookie (`s_<org>`), as
|
|
6
|
+
* a sign-in would: sessions stay the org's. The directory sets which org the
|
|
7
|
+
* browser is in (`org`) on the org's answer, and passes it on: `{ org }`, or
|
|
8
|
+
* a redirect for a page asked for (`to`).
|
|
9
|
+
*/
|
|
10
|
+
import { failure } from "copilotz";
|
|
11
|
+
import { baseOf, CURRENT, onBehalf, orgsOf } from "./http.js";
|
|
12
|
+
import { HANDOFF, reportDue, signHandoff } from "./state.js";
|
|
13
|
+
/** What a sign-in verified about someone, at the directory. */
|
|
14
|
+
export const profileOf = (runtime, person) => runtime.query("person.profile", { person }).catch(() => null);
|
|
15
|
+
/** Who's signed in at the directory (its session), or a 401. */
|
|
16
|
+
export function personIn(ctx) {
|
|
17
|
+
if (ctx.actor.kind !== "person") {
|
|
18
|
+
throw failure("unauthorized", "Not signed in", { public: true });
|
|
19
|
+
}
|
|
20
|
+
return ctx.actor.id;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Tells the directory how an org is (its people and spaces, and that it was
|
|
24
|
+
* used now), for staff's console: at most once a minute while it's used,
|
|
25
|
+
* unless `now`. Nothing waits for it, and nothing fails for it.
|
|
26
|
+
*/
|
|
27
|
+
export function report(dir, org, runtime, now = false) {
|
|
28
|
+
if (!reportDue(dir.describe().resources, org) && !now)
|
|
29
|
+
return;
|
|
30
|
+
void runtime.call("org.summary", {})
|
|
31
|
+
.then((summary) => dir.call("org.report", { org, ...summary, active: new Date().toISOString() }))
|
|
32
|
+
.catch(() => { });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Goes into `org` as the person the directory's session is: the org's answer
|
|
36
|
+
* (its cookie set), with which org the browser is in set too.
|
|
37
|
+
*/
|
|
38
|
+
export async function enter(request, ctx, org, options = {}) {
|
|
39
|
+
const { runtime } = ctx;
|
|
40
|
+
const person = personIn(ctx);
|
|
41
|
+
const profile = await profileOf(runtime, person);
|
|
42
|
+
const entered = await runtime.call("org.enter", {
|
|
43
|
+
org,
|
|
44
|
+
person,
|
|
45
|
+
...(profile?.email ? { email: profile.email } : {}),
|
|
46
|
+
...(options.staff ? { staff: true } : {}),
|
|
47
|
+
});
|
|
48
|
+
const id = entered.org.id;
|
|
49
|
+
const target = await orgsOf(runtime).get(id);
|
|
50
|
+
if (!target) {
|
|
51
|
+
throw failure("not_found", "No such org, or not one you belong to.", { public: true });
|
|
52
|
+
}
|
|
53
|
+
const query = await signHandoff(runtime.describe().resources, {
|
|
54
|
+
org: id,
|
|
55
|
+
person,
|
|
56
|
+
...(profile?.email ? { email: profile.email } : {}),
|
|
57
|
+
...(profile?.name ? { name: profile.name } : {}),
|
|
58
|
+
...(profile?.picture ? { picture: profile.picture } : {}),
|
|
59
|
+
...(entered.role ? { role: entered.role } : {}),
|
|
60
|
+
...(entered.staff ? { staff: true } : {}),
|
|
61
|
+
...(options.to ? { to: options.to } : {}),
|
|
62
|
+
});
|
|
63
|
+
const answered = await target.fetch(onBehalf(request, `${baseOf(id)}${HANDOFF}${query}`));
|
|
64
|
+
if (answered.status < 400) {
|
|
65
|
+
ctx.cookies.set(CURRENT, id);
|
|
66
|
+
report(runtime, id, target, true);
|
|
67
|
+
}
|
|
68
|
+
return answered;
|
|
69
|
+
}
|
|
70
|
+
/** The org someone goes back to: the one they were in last (not `not`), or their own, made now. */
|
|
71
|
+
export async function landing(runtime, person, name, not) {
|
|
72
|
+
const { orgs } = await runtime.call("org.mine", { person });
|
|
73
|
+
const open = orgs.filter((o) => o.id !== not && !o.suspended);
|
|
74
|
+
if (open.length)
|
|
75
|
+
return open[0].id;
|
|
76
|
+
const { org } = await runtime.call("org.create", {
|
|
77
|
+
name,
|
|
78
|
+
kind: "personal",
|
|
79
|
+
by: person,
|
|
80
|
+
});
|
|
81
|
+
return org;
|
|
82
|
+
}
|