cursedauth 0.1.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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -0
  3. package/dist/account/AccountMenu.d.ts +153 -0
  4. package/dist/account/AccountMenu.d.ts.map +1 -0
  5. package/dist/account/AccountMenu.js +388 -0
  6. package/dist/account/AccountMenu.js.map +1 -0
  7. package/dist/account/AccountNotice.d.ts +35 -0
  8. package/dist/account/AccountNotice.d.ts.map +1 -0
  9. package/dist/account/AccountNotice.js +133 -0
  10. package/dist/account/AccountNotice.js.map +1 -0
  11. package/dist/account/accountNotice.state.d.ts +20 -0
  12. package/dist/account/accountNotice.state.d.ts.map +1 -0
  13. package/dist/account/accountNotice.state.js +55 -0
  14. package/dist/account/accountNotice.state.js.map +1 -0
  15. package/dist/account/avatar.d.ts +37 -0
  16. package/dist/account/avatar.d.ts.map +1 -0
  17. package/dist/account/avatar.js +47 -0
  18. package/dist/account/avatar.js.map +1 -0
  19. package/dist/accountSlot.client.d.ts +113 -0
  20. package/dist/accountSlot.client.d.ts.map +1 -0
  21. package/dist/accountSlot.client.js +330 -0
  22. package/dist/accountSlot.client.js.map +1 -0
  23. package/dist/accountSlots.d.ts +192 -0
  24. package/dist/accountSlots.d.ts.map +1 -0
  25. package/dist/accountSlots.js +274 -0
  26. package/dist/accountSlots.js.map +1 -0
  27. package/dist/auth.d.ts +167 -0
  28. package/dist/auth.d.ts.map +1 -0
  29. package/dist/auth.js +310 -0
  30. package/dist/auth.js.map +1 -0
  31. package/dist/auth.server.d.ts +35 -0
  32. package/dist/auth.server.d.ts.map +1 -0
  33. package/dist/auth.server.js +67 -0
  34. package/dist/auth.server.js.map +1 -0
  35. package/dist/clientDescription.d.ts +186 -0
  36. package/dist/clientDescription.d.ts.map +1 -0
  37. package/dist/clientDescription.js +278 -0
  38. package/dist/clientDescription.js.map +1 -0
  39. package/dist/constantTime.d.ts +35 -0
  40. package/dist/constantTime.d.ts.map +1 -0
  41. package/dist/constantTime.js +46 -0
  42. package/dist/constantTime.js.map +1 -0
  43. package/dist/contentCensus.d.ts +59 -0
  44. package/dist/contentCensus.d.ts.map +1 -0
  45. package/dist/contentCensus.js +55 -0
  46. package/dist/contentCensus.js.map +1 -0
  47. package/dist/index.d.ts +25 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +24 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/mountGatedApp.d.ts +128 -0
  52. package/dist/mountGatedApp.d.ts.map +1 -0
  53. package/dist/mountGatedApp.js +107 -0
  54. package/dist/mountGatedApp.js.map +1 -0
  55. package/dist/runtimeMode.d.ts +28 -0
  56. package/dist/runtimeMode.d.ts.map +1 -0
  57. package/dist/runtimeMode.js +37 -0
  58. package/dist/runtimeMode.js.map +1 -0
  59. package/dist/secureRequest.d.ts +22 -0
  60. package/dist/secureRequest.d.ts.map +1 -0
  61. package/dist/secureRequest.js +27 -0
  62. package/dist/secureRequest.js.map +1 -0
  63. package/dist/sso.d.ts +373 -0
  64. package/dist/sso.d.ts.map +1 -0
  65. package/dist/sso.js +420 -0
  66. package/dist/sso.js.map +1 -0
  67. package/dist/ssoAutoSignIn.d.ts +272 -0
  68. package/dist/ssoAutoSignIn.d.ts.map +1 -0
  69. package/dist/ssoAutoSignIn.js +431 -0
  70. package/dist/ssoAutoSignIn.js.map +1 -0
  71. package/dist/ssoConsumer.server.d.ts +295 -0
  72. package/dist/ssoConsumer.server.d.ts.map +1 -0
  73. package/dist/ssoConsumer.server.js +972 -0
  74. package/dist/ssoConsumer.server.js.map +1 -0
  75. package/dist/stageIdentity.d.ts +72 -0
  76. package/dist/stageIdentity.d.ts.map +1 -0
  77. package/dist/stageIdentity.js +74 -0
  78. package/dist/stageIdentity.js.map +1 -0
  79. package/dist/ui/popover.d.ts +190 -0
  80. package/dist/ui/popover.d.ts.map +1 -0
  81. package/dist/ui/popover.js +351 -0
  82. package/dist/ui/popover.js.map +1 -0
  83. package/dist/userSessions.d.ts +205 -0
  84. package/dist/userSessions.d.ts.map +1 -0
  85. package/dist/userSessions.js +419 -0
  86. package/dist/userSessions.js.map +1 -0
  87. package/package.json +115 -0
  88. package/src/account/AccountMenu.tsx +729 -0
  89. package/src/account/AccountNotice.tsx +223 -0
  90. package/src/account/accountNotice.state.ts +66 -0
  91. package/src/account/avatar.ts +50 -0
  92. package/src/accountSlot.client.ts +362 -0
  93. package/src/accountSlots.ts +305 -0
  94. package/src/auth.server.ts +89 -0
  95. package/src/auth.ts +515 -0
  96. package/src/clientDescription.ts +366 -0
  97. package/src/constantTime.ts +45 -0
  98. package/src/contentCensus.ts +106 -0
  99. package/src/index.ts +58 -0
  100. package/src/mountGatedApp.ts +240 -0
  101. package/src/runtimeMode.ts +37 -0
  102. package/src/secureRequest.ts +25 -0
  103. package/src/sso.ts +767 -0
  104. package/src/ssoAutoSignIn.ts +544 -0
  105. package/src/ssoConsumer.server.ts +1260 -0
  106. package/src/stageIdentity.ts +76 -0
  107. package/src/ui/popover.ts +390 -0
  108. package/src/userSessions.ts +678 -0
@@ -0,0 +1,223 @@
1
+ /**
2
+ * The one place a satellite tells you something went wrong ABOUT YOUR ACCOUNT —
3
+ * not about your data, not about a request, about who you are (2026-08-23).
4
+ *
5
+ * ── Two failures, one surface ───────────────────────────────────────────────
6
+ * Both are the owner's, both from the same walkthrough, and both were silent:
7
+ *
8
+ * 1. **A refused account switch.** *"I am auto directed back to family site as
9
+ * the user I was already. I see no indication that it did not work except
10
+ * the URL showing a query string with an ssoError field … there should be an
11
+ * indicator such as a toast popup tells me the action failed in a user
12
+ * friendly way … but it seems weird the url is going to keep saying I do not
13
+ * have access while I click around the app clearly having access."*
14
+ * 2. **The account moved underneath an open page.** *"The app let me stay for
15
+ * curtwphillips@gmail.com but the request treated me as a different user. I
16
+ * was allowed to keep using the UI for auth manage users area."* See
17
+ * `accountSlots.ts` for the mechanism and why the server answers 409.
18
+ *
19
+ * They are one surface because they are one sentence to a person — *the account
20
+ * you think you are using is not the account you are using* — and because
21
+ * putting them anywhere else would mean putting them in eighteen apps.
22
+ *
23
+ * ── 🔴 Why this is rendered BY `AccountMenu` ────────────────────────────────
24
+ * `useSsoErrorNotice` — the hook that already existed for failure (1) — was
25
+ * adopted by exactly ONE app of nineteen. So the owner's own bug report was
26
+ * "fixed" for `family` and left standing everywhere else, which is precisely the
27
+ * per-app drift this workstream exists to end. A hook cannot fix that; something
28
+ * that is ALREADY mounted can. `AccountMenu` is in nineteen app headers, it is
29
+ * where the account lives, and it renders this as a sibling — so every app got
30
+ * both notices with no per-app edit, no new provider and no import.
31
+ *
32
+ * ── Why its own stylesheet, again ───────────────────────────────────────────
33
+ * Same reason as `AccountMenu` and `FleetDirectory` beside it: the satellites
34
+ * keep per-app Tailwind `@source` lists, so a shared widget styled with utility
35
+ * classes renders correctly in whichever apps remembered to add a line and
36
+ * silently unstyled in the rest. `DESIGN.md` §3 wants a filled, high-contrast
37
+ * capsule and §5 wants 44px targets; both are literals here for that reason.
38
+ */
39
+ import { useCallback } from "react";
40
+ import { currentExpectedAccount, setExpectedAccount } from "../accountSlot.client";
41
+ import type { AccountDriftBody } from "../accountSlots";
42
+ import { startSsoSignIn } from "../ssoAutoSignIn";
43
+ import { useHostDark, useInjectedStyles } from "../ui/popover";
44
+ import type { AccountNoticeState } from "./accountNotice.state";
45
+
46
+ const STYLE_ID = "san-style";
47
+
48
+ /*
49
+ * 🔴 The width cap is `DESIGN.md` §1 ("a strict readability width of 750px–850px
50
+ * … Center this content block"), and the two action buttons are §6's action-row
51
+ * grouping: a filled primary and a text-only secondary, side by side at the end
52
+ * of the row. `min-height:44px` on both is §5, unconditionally rather than
53
+ * behind a `pointer:coarse` query — this banner is most likely to be read on a
54
+ * phone, and a 44px button on a desktop is not a defect.
55
+ */
56
+ const STYLES = `
57
+ .san{--san-bg:#fffaf0;--san-border:#e8b339;--san-text:#41320a;--san-muted:#6d5a2a;
58
+ --san-accent:#8a5a00;--san-btn:#8a5a00;--san-btn-text:#ffffff;--san-shadow:0 10px 30px rgba(16,18,22,.18);
59
+ position:fixed;top:12px;left:50%;transform:translateX(-50%);z-index:2147483000;
60
+ width:min(52rem,calc(100vw - 24px));box-sizing:border-box;
61
+ display:flex;flex-wrap:wrap;align-items:center;gap:10px 14px;
62
+ padding:12px 14px;border:1px solid var(--san-border);border-radius:12px;
63
+ background:var(--san-bg);color:var(--san-text);box-shadow:var(--san-shadow);
64
+ font:13px/1.5 ui-sans-serif,system-ui,-apple-system,sans-serif}
65
+ .san[data-dark="true"]{--san-bg:#241d0c;--san-border:#7a5c1a;--san-text:#f4e6c4;--san-muted:#c9b489;
66
+ --san-accent:#f0c04a;--san-btn:#f0c04a;--san-btn-text:#241d0c;--san-shadow:0 10px 30px rgba(0,0,0,.55)}
67
+ .san-glyph{flex:none;display:inline-flex;align-items:center;justify-content:center;
68
+ width:28px;height:28px;border-radius:50%;font-size:15px;font-weight:700;
69
+ background:color-mix(in srgb,var(--san-accent) 20%,transparent);color:var(--san-accent)}
70
+ .san-text{flex:1 1 22rem;min-width:0}
71
+ .san-title{font-weight:650;font-size:13.5px}
72
+ /* §4: secondary metadata stays at 12px+ and readable, never a thin grey whisper. */
73
+ .san-body{font-size:12.5px;color:var(--san-muted);margin-top:2px;overflow-wrap:anywhere}
74
+ .san-who{font-weight:650;color:var(--san-text)}
75
+ .san-actions{flex:none;display:flex;align-items:center;gap:8px;margin-left:auto}
76
+ .san-btn{display:inline-flex;align-items:center;justify-content:center;min-height:44px;
77
+ padding:0 14px;border-radius:9px;border:1px solid transparent;cursor:pointer;
78
+ font:inherit;font-size:13px;font-weight:600;white-space:nowrap}
79
+ .san-btn[data-variant="primary"]{background:var(--san-btn);color:var(--san-btn-text)}
80
+ .san-btn[data-variant="primary"]:hover{filter:brightness(1.08)}
81
+ .san-btn[data-variant="ghost"]{background:transparent;color:var(--san-text);
82
+ border-color:color-mix(in srgb,var(--san-border) 70%,transparent)}
83
+ .san-btn[data-variant="ghost"]:hover{background:color-mix(in srgb,var(--san-accent) 12%,transparent)}
84
+ .san-btn:focus-visible{outline:2px solid var(--san-accent);outline-offset:2px}
85
+ /* §5 again: on a phone the two buttons stack full-width rather than being squeezed
86
+ into a row narrow enough to mis-tap. */
87
+ @media (max-width:560px){
88
+ .san{gap:8px 10px}
89
+ .san-actions{margin-left:0;width:100%;display:grid;grid-template-columns:1fr;gap:8px}
90
+ .san-btn{width:100%}
91
+ }
92
+ `;
93
+
94
+ /** The person-readable name of whoever the request actually resolved to. */
95
+ const nameOf = (actual: AccountDriftBody["actual"]): string =>
96
+ actual.name?.trim() || actual.email || "another account";
97
+
98
+ export interface AccountNoticeProps {
99
+ /** What went wrong, from `useAccountNotice`. Never null — `AccountMenu` only
100
+ * fetches this module once there IS something to say. */
101
+ notice: AccountNoticeState;
102
+ /** Put the banner away. */
103
+ onDismiss: () => void;
104
+ /** The accounts satellite origin, when this app is an SSO consumer. Decides
105
+ * whether "sign back in as…" is offered at all — a single-owner door has no
106
+ * hop to make. */
107
+ accountsUrl?: string | null;
108
+ /** This app's SSO start route, for the switch-back hop. */
109
+ startPath?: string;
110
+ /**
111
+ * Who is still signed in here, for the refused-switch message.
112
+ *
113
+ * 🔴 It names them because the owner's report was two complaints, not one:
114
+ * the switch failed silently AND *"the account is still on
115
+ * curtwphillips@gmail.com in the top right header"*, which read as a second
116
+ * bug. Saying whose session survived is what makes the header's name stop
117
+ * looking wrong.
118
+ */
119
+ signedInAs?: string;
120
+ /** Test seam. Real callers never pass this. */
121
+ onReload?: () => void;
122
+ }
123
+
124
+ /**
125
+ * The banner itself — the LAZY half.
126
+ *
127
+ * 🔴 It does no watching: `useAccountNotice` in `accountNotice.state.ts` does
128
+ * that, eagerly and cheaply, and this module is only fetched once it has
129
+ * something to report. See that file for the byte budget this split answers.
130
+ */
131
+ export function AccountNotice({
132
+ accountsUrl,
133
+ startPath,
134
+ signedInAs,
135
+ notice,
136
+ onDismiss,
137
+ onReload,
138
+ }: AccountNoticeProps) {
139
+ useInjectedStyles(STYLE_ID, STYLES);
140
+ const dark = useHostDark();
141
+
142
+ /**
143
+ * Become the account this browser actually holds.
144
+ *
145
+ * 🔴 The expectation is cleared FIRST. It names a user who is no longer here,
146
+ * and a reload issues its own requests — leaving it set would refuse the very
147
+ * page load that is meant to resolve the conflict.
148
+ */
149
+ const continueAsActual = useCallback(() => {
150
+ setExpectedAccount(null);
151
+ onDismiss();
152
+ if (onReload) onReload();
153
+ else globalThis.location?.reload();
154
+ }, [onReload, onDismiss]);
155
+
156
+ /** Ask the accounts satellite for the credential form, so the person can come
157
+ * back as whoever they thought they were. */
158
+ const signInAgain = useCallback(() => {
159
+ setExpectedAccount(null);
160
+ startSsoSignIn({
161
+ startPath: startPath ?? "/api/auth/sso/start",
162
+ switchUser: true,
163
+ });
164
+ }, [startPath]);
165
+
166
+ if (notice.kind === "sso-error") {
167
+ return (
168
+ <div className="san" data-dark={dark ? "true" : "false"} role="alert">
169
+ <span className="san-glyph" aria-hidden="true">
170
+ !
171
+ </span>
172
+ <span className="san-text">
173
+ <span className="san-title">That account switch did not go through</span>
174
+ <span className="san-body">
175
+ {notice.message}. You are still signed in
176
+ {signedInAs ? (
177
+ <>
178
+ {" as "}
179
+ <span className="san-who">{signedInAs}</span>
180
+ </>
181
+ ) : (
182
+ " as before"
183
+ )}
184
+ , and nothing has changed.
185
+ </span>
186
+ </span>
187
+ <span className="san-actions">
188
+ <button type="button" className="san-btn" data-variant="primary" onClick={onDismiss}>
189
+ Got it
190
+ </button>
191
+ </span>
192
+ </div>
193
+ );
194
+ }
195
+
196
+ const actual = notice.detail.actual;
197
+ const expected = notice.detail.expected || currentExpectedAccount() || "another account";
198
+ return (
199
+ <div className="san" data-dark={dark ? "true" : "false"} role="alert">
200
+ <span className="san-glyph" aria-hidden="true">
201
+ !
202
+ </span>
203
+ <span className="san-text">
204
+ <span className="san-title">This page is showing the wrong account</span>
205
+ <span className="san-body">
206
+ It was opened as <span className="san-who">{expected}</span>, but this browser is now
207
+ signed in here as <span className="san-who">{nameOf(actual)}</span> — so anything you
208
+ change would be saved as them. Nothing has been changed.
209
+ </span>
210
+ </span>
211
+ <span className="san-actions">
212
+ <button type="button" className="san-btn" data-variant="primary" onClick={continueAsActual}>
213
+ Continue as {nameOf(actual)}
214
+ </button>
215
+ {accountsUrl ? (
216
+ <button type="button" className="san-btn" data-variant="ghost" onClick={signInAgain}>
217
+ Sign back in
218
+ </button>
219
+ ) : null}
220
+ </span>
221
+ </div>
222
+ );
223
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * The LISTENER half of `AccountNotice` — deliberately tiny, and deliberately the
3
+ * only half that is eager.
4
+ *
5
+ * ── Why the split exists ────────────────────────────────────────────────────
6
+ * The banner renders nothing at all until an account goes wrong, which is almost
7
+ * never — and it carries a ~4 KB scoped stylesheet to be styleable in nineteen
8
+ * apps that each keep their own Tailwind `@source` list. Shipping that in every
9
+ * app's ENTRY chunk to display nothing is exactly the cost `test:gates:artifacts`
10
+ * exists to catch, and it caught it: `notes` went 581.07 KB against a 578 KB
11
+ * budget the moment `AccountMenu` imported the banner directly.
12
+ *
13
+ * 🔴 Raising the budget was the wrong fix and the repo has already ruled on it
14
+ * once (unit 26: station's entry chunk was SPLIT, 783 → 468 KB, and the ceiling
15
+ * LOWERED). `notes` is *"an inbox, a search box and lists — a triage tool, often
16
+ * on a phone"*, which is the reader least able to pay for a banner they will
17
+ * probably never see.
18
+ *
19
+ * So the subscription stays eager — it MUST, because a lazily-loaded component is
20
+ * not mounted and therefore cannot hear the event that would make it appear — and
21
+ * the banner is fetched only once there is something to say.
22
+ */
23
+ import { useEffect, useState } from "react";
24
+ import { ACCOUNT_DRIFT_EVENT } from "../accountSlot.client";
25
+ import type { AccountDriftBody } from "../accountSlots";
26
+ import { takeSsoError } from "../ssoAutoSignIn";
27
+
28
+ /** What went wrong with the account, if anything. */
29
+ export type AccountNoticeState =
30
+ | { kind: "drift"; detail: AccountDriftBody }
31
+ | { kind: "sso-error"; message: string };
32
+
33
+ /**
34
+ * Watch for the two ways an account goes wrong. Returns null almost always.
35
+ *
36
+ * A hook rather than a component so the cost of watching is a `useState` and one
37
+ * listener — no styles, no markup, nothing in the entry chunk but this file.
38
+ */
39
+ export function useAccountNotice(): [
40
+ AccountNoticeState | null,
41
+ (next: AccountNoticeState | null) => void,
42
+ ] {
43
+ const [notice, setNotice] = useState<AccountNoticeState | null>(null);
44
+
45
+ useEffect(() => {
46
+ // A refused hop, taken OFF the URL as it is read — the owner's complaint was
47
+ // that the address bar kept claiming he had no access while he used the app
48
+ // perfectly well. `takeSsoError` is one-shot by design, so whoever reads it
49
+ // owns reporting it; this is now that reader for every app in the fleet.
50
+ const message = takeSsoError();
51
+ if (message) setNotice({ kind: "sso-error", message });
52
+
53
+ const onDrift = (event: Event) => {
54
+ const detail = (event as CustomEvent<AccountDriftBody>).detail;
55
+ // Drift outranks a stale hop error: it is live, and it is about the
56
+ // request the person just made rather than one navigation ago.
57
+ if (detail && typeof detail.actual?.id === "string") {
58
+ setNotice({ kind: "drift", detail });
59
+ }
60
+ };
61
+ globalThis.addEventListener?.(ACCOUNT_DRIFT_EVENT, onDrift);
62
+ return () => globalThis.removeEventListener?.(ACCOUNT_DRIFT_EVENT, onDrift);
63
+ }, []);
64
+
65
+ return [notice, setNotice];
66
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Where the fleet's ONE profile picture lives, as a URL.
3
+ *
4
+ * The owner asked for a picture set once and shown everywhere
5
+ * (`___Todo-Ready/life-app-issues.md`), so the image belongs to the ACCOUNT and is served
6
+ * by the accounts satellite: `https://auth…/avatar/<user id>`. Every app renders that URL;
7
+ * no app stores a copy, so there is no such thing as a stale one.
8
+ *
9
+ * 🔴 This helper exists so the path shape is written ONCE. Eighteen headers each
10
+ * concatenating `${accountsUrl}/avatar/${id}` is eighteen chances to disagree about the
11
+ * trailing slash — the same drift that put three copies of the breadcrumb markup in the
12
+ * fleet before unit 16.
13
+ */
14
+
15
+ /** The route `apps/auth` mounts, open, outside its gates. */
16
+ export const ACCOUNT_AVATAR_PATH = "/avatar";
17
+
18
+ /**
19
+ * The account's picture, or `undefined` when there is nothing to point at.
20
+ *
21
+ * `undefined` rather than a placeholder URL: the initials fallback is the CLIENT's
22
+ * (cursedbelt's `Avatar`), so a missing accounts origin has to read as "no src", not as a
23
+ * broken image. An app that is not an SSO door — the local vault daemon, the
24
+ * owner-password apps — passes no `accountsUrl` and correctly gets initials.
25
+ */
26
+ export function accountAvatarUrl(
27
+ accountsUrl: string | null | undefined,
28
+ userId: string | null | undefined,
29
+ ): string | undefined {
30
+ if (!accountsUrl || !userId) return undefined;
31
+ return `${accountsUrl.replace(/\/$/, "")}${ACCOUNT_AVATAR_PATH}/${encodeURIComponent(userId)}`;
32
+ }
33
+
34
+ /**
35
+ * The initials drawn in the circle when there is no picture.
36
+ *
37
+ * Deliberately the same rule as cursedbelt's `Avatar` — two initials from a two-word name,
38
+ * otherwise the first two characters of the name or the local part of the address. It is
39
+ * duplicated here for one reason and it is a real one: `AccountMenu` renders into apps with
40
+ * per-app Tailwind `@source` lists and therefore ships its own scoped stylesheet rather
41
+ * than utility classes (see that file's header), so it cannot mount a Tailwind component.
42
+ * The RULE is what has to match, and this test-covered function is what makes that provable
43
+ * rather than hopeful.
44
+ */
45
+ export function accountInitials(name?: string, email?: string): string {
46
+ const base = (name?.trim() || email?.split("@")[0] || "?").trim();
47
+ const parts = base.split(/\s+/).filter(Boolean);
48
+ if (parts.length >= 2) return `${parts[0]?.[0] ?? ""}${parts[1]?.[0] ?? ""}`.toUpperCase();
49
+ return base.slice(0, 2).toUpperCase();
50
+ }