@superintelligentfamily/key-js 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/doorman.d.ts +20 -0
- package/lib/doorman.js +127 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +12 -0
- package/lib/profile.d.ts +9 -0
- package/lib/profile.js +41 -0
- package/package.json +44 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Seth Shoultes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/doorman.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Profile } from "./profile.js";
|
|
2
|
+
export { PROFILE_COOKIE, PROFILE_MAX, readProfile, type Profile } from "./profile.js";
|
|
3
|
+
type Listener = (user: Profile | null) => void;
|
|
4
|
+
/**
|
|
5
|
+
* A path on this site for the return: the current one by default; anything else falls back to "/". The browser's own URL
|
|
6
|
+
* parser decides, because it drops tab, CR and LF, so "/\t//evil.test" would pass a prefix check and land off the site.
|
|
7
|
+
* The handler's safePath is the guard; this is the reference the M4 sign-in and callback copy, so it must be right too.
|
|
8
|
+
*/
|
|
9
|
+
export declare const returnPath: (p: string | undefined, loc: Location) => string;
|
|
10
|
+
export declare function createDoorman(win: Window): {
|
|
11
|
+
user: () => Profile | null;
|
|
12
|
+
signIn: (o?: {
|
|
13
|
+
returnTo?: string;
|
|
14
|
+
}) => void;
|
|
15
|
+
signOut: () => void;
|
|
16
|
+
on: (event: "change", fn: Listener) => void;
|
|
17
|
+
render: () => void;
|
|
18
|
+
check: () => void;
|
|
19
|
+
start: () => void;
|
|
20
|
+
};
|
package/lib/doorman.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// doorman.js (DESIGN §6.1, doorman#55 PR 4): the browser script for a site behind Key's members plugin. It only ever
|
|
2
|
+
// calls the site's own routes, which the plugin serves: /_doorman/sign-in, POST /_doorman/signout and /_doorman/account.
|
|
3
|
+
// It never sees a token. What it shows comes from dm_profile, a cookie the plugin's callback sets with a name and a
|
|
4
|
+
// picture, which a visitor can read or forge: it is cosmetic, and nothing that grants access reads it.
|
|
5
|
+
import { readProfile } from "./profile.js";
|
|
6
|
+
export { PROFILE_COOKIE, PROFILE_MAX, readProfile } from "./profile.js";
|
|
7
|
+
/**
|
|
8
|
+
* A path on this site for the return: the current one by default; anything else falls back to "/". The browser's own URL
|
|
9
|
+
* parser decides, because it drops tab, CR and LF, so "/\t//evil.test" would pass a prefix check and land off the site.
|
|
10
|
+
* The handler's safePath is the guard; this is the reference the M4 sign-in and callback copy, so it must be right too.
|
|
11
|
+
*/
|
|
12
|
+
export const returnPath = (p, loc) => {
|
|
13
|
+
if (p === undefined)
|
|
14
|
+
return loc.pathname + loc.search;
|
|
15
|
+
if (!p.startsWith("/"))
|
|
16
|
+
return "/";
|
|
17
|
+
try {
|
|
18
|
+
const u = new URL(p, loc.href);
|
|
19
|
+
// Dot segments can leave a path that starts "//" ("/.//evil.test" is "//evil.test"), which a browser reads as another host.
|
|
20
|
+
const out = u.pathname + u.search;
|
|
21
|
+
return u.origin === loc.origin && !out.startsWith("//") ? out : "/";
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return "/";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export function createDoorman(win) {
|
|
28
|
+
const doc = win.document;
|
|
29
|
+
const listeners = [];
|
|
30
|
+
let shown = JSON.stringify(readProfile(doc.cookie));
|
|
31
|
+
const user = () => readProfile(doc.cookie);
|
|
32
|
+
function signIn(o = {}) {
|
|
33
|
+
win.location.assign(`/_doorman/sign-in?return_to=${encodeURIComponent(returnPath(o.returnTo, win.location))}`);
|
|
34
|
+
}
|
|
35
|
+
/** A real form POST, so the plugin's 303 to Key's end_session navigates the page (DESIGN §5.2). */
|
|
36
|
+
function signOut() {
|
|
37
|
+
const f = doc.createElement("form");
|
|
38
|
+
f.method = "post";
|
|
39
|
+
f.action = "/_doorman/signout";
|
|
40
|
+
f.hidden = true;
|
|
41
|
+
doc.body.appendChild(f);
|
|
42
|
+
f.submit();
|
|
43
|
+
}
|
|
44
|
+
function userButton(el, u) {
|
|
45
|
+
const button = doc.createElement("button");
|
|
46
|
+
button.type = "button";
|
|
47
|
+
button.setAttribute("aria-haspopup", "menu");
|
|
48
|
+
button.setAttribute("aria-expanded", "false");
|
|
49
|
+
if (u.picture) {
|
|
50
|
+
const img = doc.createElement("img");
|
|
51
|
+
img.src = u.picture;
|
|
52
|
+
img.alt = "";
|
|
53
|
+
img.width = 32;
|
|
54
|
+
img.height = 32;
|
|
55
|
+
img.referrerPolicy = "no-referrer";
|
|
56
|
+
button.appendChild(img);
|
|
57
|
+
}
|
|
58
|
+
const label = doc.createElement("span");
|
|
59
|
+
label.textContent = u.name || "Account";
|
|
60
|
+
button.appendChild(label);
|
|
61
|
+
const menu = doc.createElement("div");
|
|
62
|
+
menu.setAttribute("role", "menu");
|
|
63
|
+
menu.hidden = true;
|
|
64
|
+
const account = doc.createElement("a");
|
|
65
|
+
account.href = "/_doorman/account";
|
|
66
|
+
account.setAttribute("role", "menuitem");
|
|
67
|
+
account.textContent = "Account";
|
|
68
|
+
const out = doc.createElement("button");
|
|
69
|
+
out.type = "button";
|
|
70
|
+
out.setAttribute("role", "menuitem");
|
|
71
|
+
out.textContent = "Sign out";
|
|
72
|
+
out.addEventListener("click", signOut);
|
|
73
|
+
menu.append(account, out);
|
|
74
|
+
button.addEventListener("click", () => {
|
|
75
|
+
menu.hidden = !menu.hidden;
|
|
76
|
+
button.setAttribute("aria-expanded", String(!menu.hidden));
|
|
77
|
+
});
|
|
78
|
+
el.replaceChildren(button, menu);
|
|
79
|
+
}
|
|
80
|
+
/** Shows and hides the data-doorman parts for who is signed in now; safe to run again. */
|
|
81
|
+
function render() {
|
|
82
|
+
const u = user();
|
|
83
|
+
shown = JSON.stringify(u);
|
|
84
|
+
for (const el of doc.querySelectorAll("[data-doorman=signed-in]"))
|
|
85
|
+
el.hidden = !u;
|
|
86
|
+
for (const el of doc.querySelectorAll("[data-doorman=signed-out]"))
|
|
87
|
+
el.hidden = !!u;
|
|
88
|
+
for (const el of doc.querySelectorAll("[data-doorman=user-button]")) {
|
|
89
|
+
if (u)
|
|
90
|
+
userButton(el, u);
|
|
91
|
+
else
|
|
92
|
+
el.replaceChildren();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function on(event, fn) {
|
|
96
|
+
if (event === "change")
|
|
97
|
+
listeners.push(fn);
|
|
98
|
+
}
|
|
99
|
+
/** Re-reads the cookie when the page may have changed hands (back, focus, another tab): renders and tells listeners. */
|
|
100
|
+
function check() {
|
|
101
|
+
const now = JSON.stringify(user());
|
|
102
|
+
if (now === shown)
|
|
103
|
+
return;
|
|
104
|
+
shown = now;
|
|
105
|
+
render();
|
|
106
|
+
for (const fn of listeners)
|
|
107
|
+
fn(user());
|
|
108
|
+
}
|
|
109
|
+
function start() {
|
|
110
|
+
doc.addEventListener("click", (e) => {
|
|
111
|
+
const t = e.target?.closest?.("[data-doorman=sign-in],[data-doorman=sign-out]");
|
|
112
|
+
if (!t)
|
|
113
|
+
return;
|
|
114
|
+
e.preventDefault();
|
|
115
|
+
const back = t.getAttribute("data-return-to");
|
|
116
|
+
if (t.getAttribute("data-doorman") === "sign-in")
|
|
117
|
+
signIn(back === null ? {} : { returnTo: back });
|
|
118
|
+
else
|
|
119
|
+
signOut();
|
|
120
|
+
});
|
|
121
|
+
win.addEventListener("pageshow", check);
|
|
122
|
+
win.addEventListener("focus", check);
|
|
123
|
+
doc.addEventListener("visibilitychange", check);
|
|
124
|
+
render();
|
|
125
|
+
}
|
|
126
|
+
return { user, signIn, signOut, on, render, check, start };
|
|
127
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PACKAGE_NAME = "@superintelligentfamily/key-js";
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// doorman.js's entry (DESIGN §6.1): the bundle esbuild makes for js.<dh> starts it on the page and exposes window.Doorman.
|
|
2
|
+
// The library itself is ./doorman, which the tests import directly.
|
|
3
|
+
import { createDoorman } from "./doorman.js";
|
|
4
|
+
export const PACKAGE_NAME = "@superintelligentfamily/key-js";
|
|
5
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
6
|
+
const d = createDoorman(window);
|
|
7
|
+
window.Doorman = { user: d.user, signIn: d.signIn, signOut: d.signOut, on: d.on };
|
|
8
|
+
if (document.readyState === "loading")
|
|
9
|
+
document.addEventListener("DOMContentLoaded", d.start, { once: true });
|
|
10
|
+
else
|
|
11
|
+
d.start();
|
|
12
|
+
}
|
package/lib/profile.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Profile {
|
|
2
|
+
name: string;
|
|
3
|
+
picture: string | null;
|
|
4
|
+
}
|
|
5
|
+
export declare const PROFILE_COOKIE = "dm_profile";
|
|
6
|
+
/** The cookie's cap (DESIGN §6.1): a longer value is ignored, not trusted in part. */
|
|
7
|
+
export declare const PROFILE_MAX = 1024;
|
|
8
|
+
/** The profile in a cookie header, or null: base64url JSON {"n": name, "p": picture}, an https picture only. */
|
|
9
|
+
export declare function readProfile(cookie: string): Profile | null;
|
package/lib/profile.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// dm_profile (DESIGN §6.1), with no DOM: the cosmetic name and picture a page shows for who is signed in. doorman.js,
|
|
2
|
+
// @superintelligentfamily/key-react and the handler's tests read it through here; a visitor can read or forge it, so nothing that grants
|
|
3
|
+
// access reads it.
|
|
4
|
+
export const PROFILE_COOKIE = "dm_profile";
|
|
5
|
+
/** The cookie's cap (DESIGN §6.1): a longer value is ignored, not trusted in part. */
|
|
6
|
+
export const PROFILE_MAX = 1024;
|
|
7
|
+
const NAME_MAX = 100;
|
|
8
|
+
function b64urlText(s) {
|
|
9
|
+
const bin = atob(s.replace(/-/g, "+").replace(/_/g, "/"));
|
|
10
|
+
return new TextDecoder().decode(Uint8Array.from(bin, (c) => c.charCodeAt(0)));
|
|
11
|
+
}
|
|
12
|
+
/** The profile in a cookie header, or null: base64url JSON {"n": name, "p": picture}, an https picture only. */
|
|
13
|
+
export function readProfile(cookie) {
|
|
14
|
+
const pair = cookie.split(";").map((s) => s.trim()).find((s) => s.startsWith(`${PROFILE_COOKIE}=`));
|
|
15
|
+
const raw = pair?.slice(PROFILE_COOKIE.length + 1) ?? "";
|
|
16
|
+
if (!raw || raw.length > PROFILE_MAX)
|
|
17
|
+
return null;
|
|
18
|
+
let j;
|
|
19
|
+
try {
|
|
20
|
+
j = JSON.parse(b64urlText(raw));
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (!j || typeof j !== "object")
|
|
26
|
+
return null;
|
|
27
|
+
const { n, p } = j;
|
|
28
|
+
const name = typeof n === "string" ? n.slice(0, NAME_MAX) : "";
|
|
29
|
+
let picture = null;
|
|
30
|
+
if (typeof p === "string") {
|
|
31
|
+
try {
|
|
32
|
+
const u = new URL(p);
|
|
33
|
+
if (u.protocol === "https:")
|
|
34
|
+
picture = u.href;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
picture = null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { name, picture };
|
|
41
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superintelligentfamily/key-js",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Key's browser script for a site behind Key's members plugin: who is signed in, from the site's own routes and its profile cookie. It never sees a token.",
|
|
6
|
+
"homepage": "https://superintelligentkey.com",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sethshoultes/doorman.git",
|
|
10
|
+
"directory": "packages/js"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"files": [
|
|
14
|
+
"lib",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./lib/index.d.ts",
|
|
20
|
+
"import": "./lib/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./doorman": {
|
|
23
|
+
"types": "./lib/doorman.d.ts",
|
|
24
|
+
"import": "./lib/doorman.js"
|
|
25
|
+
},
|
|
26
|
+
"./profile": {
|
|
27
|
+
"types": "./lib/profile.d.ts",
|
|
28
|
+
"import": "./lib/profile.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"esbuild": "0.28.2",
|
|
33
|
+
"happy-dom": "21.0.4"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"provenance": true
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"typecheck": "tsc --noEmit -p .",
|
|
41
|
+
"lint": "eslint .",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"check": "pnpm run typecheck && pnpm run lint && pnpm run test",
|
|
44
|
+
"build": "node scripts/build.mjs",
|
|
45
|
+
"build:npm": "tsc -p tsconfig.build.json"
|
|
46
|
+
}
|
|
6
47
|
}
|
package/README.md
DELETED