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.
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/dist/account/AccountMenu.d.ts +153 -0
- package/dist/account/AccountMenu.d.ts.map +1 -0
- package/dist/account/AccountMenu.js +388 -0
- package/dist/account/AccountMenu.js.map +1 -0
- package/dist/account/AccountNotice.d.ts +35 -0
- package/dist/account/AccountNotice.d.ts.map +1 -0
- package/dist/account/AccountNotice.js +133 -0
- package/dist/account/AccountNotice.js.map +1 -0
- package/dist/account/accountNotice.state.d.ts +20 -0
- package/dist/account/accountNotice.state.d.ts.map +1 -0
- package/dist/account/accountNotice.state.js +55 -0
- package/dist/account/accountNotice.state.js.map +1 -0
- package/dist/account/avatar.d.ts +37 -0
- package/dist/account/avatar.d.ts.map +1 -0
- package/dist/account/avatar.js +47 -0
- package/dist/account/avatar.js.map +1 -0
- package/dist/accountSlot.client.d.ts +113 -0
- package/dist/accountSlot.client.d.ts.map +1 -0
- package/dist/accountSlot.client.js +330 -0
- package/dist/accountSlot.client.js.map +1 -0
- package/dist/accountSlots.d.ts +192 -0
- package/dist/accountSlots.d.ts.map +1 -0
- package/dist/accountSlots.js +274 -0
- package/dist/accountSlots.js.map +1 -0
- package/dist/auth.d.ts +167 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +310 -0
- package/dist/auth.js.map +1 -0
- package/dist/auth.server.d.ts +35 -0
- package/dist/auth.server.d.ts.map +1 -0
- package/dist/auth.server.js +67 -0
- package/dist/auth.server.js.map +1 -0
- package/dist/clientDescription.d.ts +186 -0
- package/dist/clientDescription.d.ts.map +1 -0
- package/dist/clientDescription.js +278 -0
- package/dist/clientDescription.js.map +1 -0
- package/dist/constantTime.d.ts +35 -0
- package/dist/constantTime.d.ts.map +1 -0
- package/dist/constantTime.js +46 -0
- package/dist/constantTime.js.map +1 -0
- package/dist/contentCensus.d.ts +59 -0
- package/dist/contentCensus.d.ts.map +1 -0
- package/dist/contentCensus.js +55 -0
- package/dist/contentCensus.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/mountGatedApp.d.ts +128 -0
- package/dist/mountGatedApp.d.ts.map +1 -0
- package/dist/mountGatedApp.js +107 -0
- package/dist/mountGatedApp.js.map +1 -0
- package/dist/runtimeMode.d.ts +28 -0
- package/dist/runtimeMode.d.ts.map +1 -0
- package/dist/runtimeMode.js +37 -0
- package/dist/runtimeMode.js.map +1 -0
- package/dist/secureRequest.d.ts +22 -0
- package/dist/secureRequest.d.ts.map +1 -0
- package/dist/secureRequest.js +27 -0
- package/dist/secureRequest.js.map +1 -0
- package/dist/sso.d.ts +373 -0
- package/dist/sso.d.ts.map +1 -0
- package/dist/sso.js +420 -0
- package/dist/sso.js.map +1 -0
- package/dist/ssoAutoSignIn.d.ts +272 -0
- package/dist/ssoAutoSignIn.d.ts.map +1 -0
- package/dist/ssoAutoSignIn.js +431 -0
- package/dist/ssoAutoSignIn.js.map +1 -0
- package/dist/ssoConsumer.server.d.ts +295 -0
- package/dist/ssoConsumer.server.d.ts.map +1 -0
- package/dist/ssoConsumer.server.js +972 -0
- package/dist/ssoConsumer.server.js.map +1 -0
- package/dist/stageIdentity.d.ts +72 -0
- package/dist/stageIdentity.d.ts.map +1 -0
- package/dist/stageIdentity.js +74 -0
- package/dist/stageIdentity.js.map +1 -0
- package/dist/ui/popover.d.ts +190 -0
- package/dist/ui/popover.d.ts.map +1 -0
- package/dist/ui/popover.js +351 -0
- package/dist/ui/popover.js.map +1 -0
- package/dist/userSessions.d.ts +205 -0
- package/dist/userSessions.d.ts.map +1 -0
- package/dist/userSessions.js +419 -0
- package/dist/userSessions.js.map +1 -0
- package/package.json +115 -0
- package/src/account/AccountMenu.tsx +729 -0
- package/src/account/AccountNotice.tsx +223 -0
- package/src/account/accountNotice.state.ts +66 -0
- package/src/account/avatar.ts +50 -0
- package/src/accountSlot.client.ts +362 -0
- package/src/accountSlots.ts +305 -0
- package/src/auth.server.ts +89 -0
- package/src/auth.ts +515 -0
- package/src/clientDescription.ts +366 -0
- package/src/constantTime.ts +45 -0
- package/src/contentCensus.ts +106 -0
- package/src/index.ts +58 -0
- package/src/mountGatedApp.ts +240 -0
- package/src/runtimeMode.ts +37 -0
- package/src/secureRequest.ts +25 -0
- package/src/sso.ts +767 -0
- package/src/ssoAutoSignIn.ts +544 -0
- package/src/ssoConsumer.server.ts +1260 -0
- package/src/stageIdentity.ts +76 -0
- package/src/ui/popover.ts +390 -0
- package/src/userSessions.ts +678 -0
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatic SSO sign-in — the private satellites stop asking for a click they
|
|
3
|
+
* already know the answer to (2026-08-01, owner ask).
|
|
4
|
+
*
|
|
5
|
+
* ── What it changes ─────────────────────────────────────────────────────────
|
|
6
|
+
* Every private satellite (roms, hobbies, notes, vault, orch, orch-companion,
|
|
7
|
+
* station) drew the same screen when signed out: one sentence and one "Sign in
|
|
8
|
+
* with your cursedalchemy account" button, whose only effect is a navigation to
|
|
9
|
+
* `/api/auth/sso/start`. For someone who already holds an auth session — the
|
|
10
|
+
* normal case, since the whole fleet shares ONE user set — that screen is a
|
|
11
|
+
* speed bump in front of a redirect that was always going to succeed. The
|
|
12
|
+
* button now presses itself; the screen is what you see when it CAN'T.
|
|
13
|
+
*
|
|
14
|
+
* ── Why the guards are the whole file ───────────────────────────────────────
|
|
15
|
+
* An unconditional "anonymous → navigate to auth" is a redirect LOOP generator,
|
|
16
|
+
* and the loops it makes are the invisible kind: the browser bounces between
|
|
17
|
+
* two origins with no error anywhere, forever. Four situations must suppress
|
|
18
|
+
* it, and each is a real failure that has already happened somewhere in this
|
|
19
|
+
* fleet:
|
|
20
|
+
*
|
|
21
|
+
* 1. **This page IS the failure landing.** The consumer's callback redirects
|
|
22
|
+
* to `/?ssoError=…` (expired grant, replay, a non-admin account at an
|
|
23
|
+
* admin-only app). Auto-starting there would send the person straight back
|
|
24
|
+
* to auth, which would hand out another grant the app refuses for the same
|
|
25
|
+
* reason, forever — and they would never read why.
|
|
26
|
+
* 2. **The session did not stick.** The round trip appears to succeed and the
|
|
27
|
+
* probe still says anonymous — a rejected cookie (SameSite, a clock, a
|
|
28
|
+
* misconfigured `publicUrl`). Nothing in the URL says so, which is exactly
|
|
29
|
+
* why the ATTEMPT is remembered: a second anonymous load inside the
|
|
30
|
+
* cooldown means the loop is already running, and we stop and show the
|
|
31
|
+
* button instead.
|
|
32
|
+
* 3. **They just signed out.** Auth's own session outlives this app's cookie,
|
|
33
|
+
* so an auto-start after "Sign out" signs you straight back in. The button
|
|
34
|
+
* is then the only way to leave. `markSsoSignedOut()` is the flag, and it
|
|
35
|
+
* lasts for the tab.
|
|
36
|
+
* 4. **This door is not an SSO door.** The local vault daemon
|
|
37
|
+
* (`createLocalOwnerAuth`) answers `/api/auth/session` with
|
|
38
|
+
* `accountsUrl: ""` on purpose, and a dev open loop answers 404. Sending
|
|
39
|
+
* either to `auth.cursedalchemy.com` — on the box that sleeps 12am–10am CT
|
|
40
|
+
* — would break the instances whose whole point is working offline. So the
|
|
41
|
+
* trigger is a NON-EMPTY `accountsUrl`: the server says where its door is,
|
|
42
|
+
* and no client has to guess.
|
|
43
|
+
*
|
|
44
|
+
* The state lives in `sessionStorage`: per tab, gone when the tab closes, and
|
|
45
|
+
* never a reason to refuse a sign-in tomorrow. Every access is wrapped —
|
|
46
|
+
* Safari's private mode throws on `sessionStorage` access, and an app that
|
|
47
|
+
* cannot store a guard should still be openable (it degrades to the button,
|
|
48
|
+
* never to a loop).
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/** Where the consumer's redirect chain begins. Kept here rather than imported
|
|
52
|
+
* from `ssoConsumer.server.ts`: that module pulls in `bun:sqlite` and hono,
|
|
53
|
+
* and this one runs in a browser. `ssoAutoSignIn.test.ts` pins them equal. */
|
|
54
|
+
import { useEffect, useRef, useState } from "react";
|
|
55
|
+
import { currentAccountSlot } from "./accountSlot.client";
|
|
56
|
+
import { ACCOUNT_SLOT_PARAM } from "./accountSlots";
|
|
57
|
+
|
|
58
|
+
export const SSO_START_PATH = "/api/auth/sso/start";
|
|
59
|
+
/**
|
|
60
|
+
* The GLOBAL sign-out door (2026-08-10). Ending this app's cookie was never a
|
|
61
|
+
* sign-out — auth's own session outlives it, so the next press of "Sign in"
|
|
62
|
+
* hands the browser straight back to the SAME person. That is not a cosmetic
|
|
63
|
+
* gap: it is why handing the laptop to somebody else did not work, because
|
|
64
|
+
* there was no way for them to become the signed-in user. This path revokes
|
|
65
|
+
* this app's session and then bounces through auth to revoke ITS session too,
|
|
66
|
+
* so "signed out" means what it says on every satellite at once.
|
|
67
|
+
*/
|
|
68
|
+
export const SSO_SIGNOUT_PATH = "/api/auth/sso/signout";
|
|
69
|
+
|
|
70
|
+
/** When the last automatic attempt was launched, ms epoch, as a string. */
|
|
71
|
+
export const SSO_ATTEMPT_KEY = "satellite.sso.autoAttempt.v1";
|
|
72
|
+
/** Present when the person signed out in this tab. Value is unused. */
|
|
73
|
+
export const SSO_SIGNED_OUT_KEY = "satellite.sso.signedOut.v1";
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* How long one failed attempt suppresses the next.
|
|
77
|
+
*
|
|
78
|
+
* It is a ceiling on the round trip, not a guess at it: the hop is two 302s
|
|
79
|
+
* and a page load, so anything still anonymous 30 seconds later did not fail
|
|
80
|
+
* slowly, it failed silently. Long enough that a slow phone on 3G is never
|
|
81
|
+
* caught, short enough that the person who fixes the cause (signs in at auth
|
|
82
|
+
* in another tab, gets the admin flag) is not left staring at a button.
|
|
83
|
+
*/
|
|
84
|
+
export const SSO_ATTEMPT_COOLDOWN_MS = 30_000;
|
|
85
|
+
|
|
86
|
+
/** Why automatic sign-in did not fire. Every value is a screen the person
|
|
87
|
+
* should see, not an error — hence a reason rather than a boolean. */
|
|
88
|
+
export type SsoAutoSkipReason =
|
|
89
|
+
| "authenticated"
|
|
90
|
+
| "not-an-sso-door"
|
|
91
|
+
| "sign-in-failed"
|
|
92
|
+
| "signed-out"
|
|
93
|
+
| "recent-attempt"
|
|
94
|
+
| "disabled";
|
|
95
|
+
|
|
96
|
+
export type SsoAutoDecision = { start: true } | { start: false; reason: SsoAutoSkipReason };
|
|
97
|
+
|
|
98
|
+
export interface SsoAutoInput {
|
|
99
|
+
/** What `/api/auth/session` said. */
|
|
100
|
+
authenticated: boolean;
|
|
101
|
+
/** The accounts origin the SERVER advertises. Empty/absent → not an SSO door. */
|
|
102
|
+
accountsUrl?: string | null;
|
|
103
|
+
/** `?ssoError=` on the current URL, if any. */
|
|
104
|
+
ssoError?: string | null;
|
|
105
|
+
/** Did this tab sign out? (`SSO_SIGNED_OUT_KEY`) */
|
|
106
|
+
signedOut: boolean;
|
|
107
|
+
/** When the last automatic attempt started, ms epoch, or null. */
|
|
108
|
+
lastAttemptAt: number | null;
|
|
109
|
+
now: number;
|
|
110
|
+
/** Off switch for a caller that wants the button back. Defaults to on. */
|
|
111
|
+
enabled?: boolean;
|
|
112
|
+
cooldownMs?: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The whole policy, as a pure function — so the guards are testable without a
|
|
117
|
+
* browser, a redirect, or the ability to observe a loop only by hanging.
|
|
118
|
+
*/
|
|
119
|
+
export function decideSsoAutoSignIn(input: SsoAutoInput): SsoAutoDecision {
|
|
120
|
+
if (input.enabled === false) return { start: false, reason: "disabled" };
|
|
121
|
+
if (input.authenticated) return { start: false, reason: "authenticated" };
|
|
122
|
+
// The server names its own door. No accountsUrl → nothing to redirect to.
|
|
123
|
+
if (!input.accountsUrl) return { start: false, reason: "not-an-sso-door" };
|
|
124
|
+
// Guard 1 — this load IS the failure. Never bounce off an explanation.
|
|
125
|
+
if (input.ssoError) return { start: false, reason: "sign-in-failed" };
|
|
126
|
+
// Guard 3 — leaving must be possible.
|
|
127
|
+
if (input.signedOut) return { start: false, reason: "signed-out" };
|
|
128
|
+
// Guard 2 — a round trip that came back anonymous is a loop in progress.
|
|
129
|
+
const cooldown = input.cooldownMs ?? SSO_ATTEMPT_COOLDOWN_MS;
|
|
130
|
+
if (input.lastAttemptAt !== null && input.now - input.lastAttemptAt < cooldown) {
|
|
131
|
+
return { start: false, reason: "recent-attempt" };
|
|
132
|
+
}
|
|
133
|
+
return { start: true };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ── The browser side of it ──────────────────────────────────────────────────
|
|
137
|
+
|
|
138
|
+
const store = (): Storage | null => {
|
|
139
|
+
try {
|
|
140
|
+
return globalThis.sessionStorage ?? null;
|
|
141
|
+
} catch {
|
|
142
|
+
// Safari private mode throws on ACCESS, not just on write.
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const read = (key: string): string | null => {
|
|
148
|
+
try {
|
|
149
|
+
return store()?.getItem(key) ?? null;
|
|
150
|
+
} catch {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const write = (key: string, value: string): void => {
|
|
156
|
+
try {
|
|
157
|
+
store()?.setItem(key, value);
|
|
158
|
+
} catch {
|
|
159
|
+
/* a tab that cannot remember degrades to the button, never to a loop */
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const drop = (key: string): void => {
|
|
164
|
+
try {
|
|
165
|
+
store()?.removeItem(key);
|
|
166
|
+
} catch {
|
|
167
|
+
/* ignore */
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/** The last automatic attempt's timestamp, or null if there wasn't one (or the
|
|
172
|
+
* stored value is not a number — a hand-edited or half-written key must read
|
|
173
|
+
* as "no attempt", which fails toward trying rather than toward stuck). */
|
|
174
|
+
export function lastSsoAttemptAt(): number | null {
|
|
175
|
+
const raw = read(SSO_ATTEMPT_KEY);
|
|
176
|
+
if (raw === null) return null;
|
|
177
|
+
const at = Number(raw);
|
|
178
|
+
return Number.isFinite(at) ? at : null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function ssoSignedOutHere(): boolean {
|
|
182
|
+
return read(SSO_SIGNED_OUT_KEY) !== null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Call from every sign-out path. Auth's session outlives this app's cookie, so
|
|
187
|
+
* without this the next render signs the person straight back in and "Sign
|
|
188
|
+
* out" is a button that does nothing visible.
|
|
189
|
+
*/
|
|
190
|
+
export function markSsoSignedOut(): void {
|
|
191
|
+
write(SSO_SIGNED_OUT_KEY, "1");
|
|
192
|
+
drop(SSO_ATTEMPT_KEY);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Clear both markers — what a confirmed session means. */
|
|
196
|
+
export function clearSsoAutoState(): void {
|
|
197
|
+
drop(SSO_ATTEMPT_KEY);
|
|
198
|
+
drop(SSO_SIGNED_OUT_KEY);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** `?ssoError=` on the current URL, defensively (a malformed search string
|
|
202
|
+
* must not take the app down on its first render). */
|
|
203
|
+
export function ssoErrorFromLocation(search?: string): string | null {
|
|
204
|
+
try {
|
|
205
|
+
return new URLSearchParams(search ?? globalThis.location?.search ?? "").get("ssoError");
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The `?ssoError=` this page load arrived with, read ONCE and remembered for as long as
|
|
213
|
+
* the page lives.
|
|
214
|
+
*
|
|
215
|
+
* ── 🔴 Why a cache, and why it is load-bearing (2026-08-25) ─────────────────
|
|
216
|
+
* `takeSsoError()` STRIPS the parameter, and two different things need to read it: the
|
|
217
|
+
* redirect guard in {@link decideSsoAutoSignIn} (guard 1 — "this page IS the failure
|
|
218
|
+
* landing, never bounce off an explanation") and whatever renders the message. Whichever
|
|
219
|
+
* runs first used to win, and the loser saw a URL with nothing on it.
|
|
220
|
+
*
|
|
221
|
+
* That is not symmetrical. If the NOTICE loses, a refusal is silent — bad. If the GUARD
|
|
222
|
+
* loses, the app immediately hops back to auth, which hands out another grant it refuses
|
|
223
|
+
* for the same reason, forever: the invisible redirect loop this whole module exists to
|
|
224
|
+
* prevent. React runs child effects before parent effects, so mounting a notice inside the
|
|
225
|
+
* signed-out branch of an app whose guard lives one component up loses the guard EVERY
|
|
226
|
+
* time — measured in a browser against collections' built artifact on 2026-08-25, where a
|
|
227
|
+
* `/?ssoError=…` load bounced straight to `auth.cursedalchemy.com`.
|
|
228
|
+
*
|
|
229
|
+
* Reading through here makes the order stop mattering. Both callers see the same answer
|
|
230
|
+
* whether or not the URL still carries it, so the hazard is not something a future reader
|
|
231
|
+
* has to remember — it is not expressible.
|
|
232
|
+
*/
|
|
233
|
+
let capturedSsoError: string | null | undefined;
|
|
234
|
+
|
|
235
|
+
export function ssoErrorForThisLoad(): string | null {
|
|
236
|
+
if (capturedSsoError === undefined) capturedSsoError = ssoErrorFromLocation();
|
|
237
|
+
return capturedSsoError;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Forget the captured value. A page load is the real scope, so ONLY a test needs this —
|
|
241
|
+
* a suite that exercises several landings inside one process. */
|
|
242
|
+
export function resetSsoErrorCapture(): void {
|
|
243
|
+
capturedSsoError = undefined;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Take `?ssoError=` off the URL, once, without touching anything else on it.
|
|
248
|
+
*
|
|
249
|
+
* 🔴 `replaceState`, never a reload or a hash change: this runs on a page that is
|
|
250
|
+
* already rendered and may be several routes deep, and either alternative would
|
|
251
|
+
* throw that away. Returns the message it removed (or null), so the ONE caller
|
|
252
|
+
* that consumed it is the one that reports it — a helper that both cleaned the
|
|
253
|
+
* URL and swallowed the message would make a failed sign-in silent, which is the
|
|
254
|
+
* exact bug this exists to fix.
|
|
255
|
+
*/
|
|
256
|
+
export function takeSsoError(): string | null {
|
|
257
|
+
// 🔴 Through the cache, not off the URL. This function is what EMPTIES the URL, so a
|
|
258
|
+
// second caller — the redirect guard above all — must still be able to learn the answer
|
|
259
|
+
// afterwards. See `ssoErrorForThisLoad`.
|
|
260
|
+
const message = ssoErrorForThisLoad();
|
|
261
|
+
if (!message) return null;
|
|
262
|
+
try {
|
|
263
|
+
const url = new URL(globalThis.location.href);
|
|
264
|
+
url.searchParams.delete("ssoError");
|
|
265
|
+
globalThis.history.replaceState(
|
|
266
|
+
globalThis.history.state,
|
|
267
|
+
"",
|
|
268
|
+
`${url.pathname}${url.search}${url.hash}`,
|
|
269
|
+
);
|
|
270
|
+
} catch {
|
|
271
|
+
/* a browser that cannot rewrite the URL still gets the message */
|
|
272
|
+
}
|
|
273
|
+
return message;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Report a refused sign-in ONCE, then take it off the URL (2026-08-15).
|
|
278
|
+
*
|
|
279
|
+
* The owner, switching accounts from `family`: *"I am auto directed back to
|
|
280
|
+
* family site as the user I was already. I see no indication that it did not
|
|
281
|
+
* work except the URL showing a query string with an ssoError field … there
|
|
282
|
+
* should be an indicator such as a toast popup tells me the action failed in a
|
|
283
|
+
* user friendly way so I quickly know what went wrong, but it seems weird the
|
|
284
|
+
* url is going to keep saying I do not have access while I click around the app
|
|
285
|
+
* clearly having access."*
|
|
286
|
+
*
|
|
287
|
+
* Both halves of that are one bug: the failure landing is the ordinary app page
|
|
288
|
+
* for whoever was ALREADY signed in, so nothing on it was ever going to mention
|
|
289
|
+
* a hop that failed for somebody else — and the parameter then rode along on
|
|
290
|
+
* every link they clicked, standing as a permanent claim that they cannot use
|
|
291
|
+
* the app they are using.
|
|
292
|
+
*
|
|
293
|
+
* 🔴 It lives here rather than in an app because every private satellite lands
|
|
294
|
+
* the same refusal, and `decideSsoAutoSignIn` ALREADY reads this parameter to
|
|
295
|
+
* decide not to bounce (which is why the app stays usable at all). One reader
|
|
296
|
+
* that acts on it and one that silently leaves it on the URL is how a message
|
|
297
|
+
* meant for a person became a permanent query string.
|
|
298
|
+
*
|
|
299
|
+
* @param onMessage what to show — a toast, an alert bar, whatever the app has.
|
|
300
|
+
*/
|
|
301
|
+
export function useSsoErrorNotice(onMessage: (message: string) => void): void {
|
|
302
|
+
// A ref so a caller that passes an inline arrow does not re-run the effect on
|
|
303
|
+
// every render and report the same failure repeatedly.
|
|
304
|
+
const handler = useRef(onMessage);
|
|
305
|
+
handler.current = onMessage;
|
|
306
|
+
useEffect(() => {
|
|
307
|
+
const message = takeSsoError();
|
|
308
|
+
if (message) handler.current(message);
|
|
309
|
+
}, []);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export interface StartSsoSignInOptions {
|
|
313
|
+
/** Defaults to `SSO_START_PATH`. */
|
|
314
|
+
startPath?: string;
|
|
315
|
+
/** Was this the machine's decision (record the attempt) or the person's
|
|
316
|
+
* (they asked, so clear the signed-out flag and let them through)? */
|
|
317
|
+
automatic?: boolean;
|
|
318
|
+
/**
|
|
319
|
+
* "Somebody ELSE wants in." Carries `switch=1` to this app's start route,
|
|
320
|
+
* which forwards `prompt=login` to auth — so the credential form is shown
|
|
321
|
+
* even though auth still holds a live session, instead of the hop silently
|
|
322
|
+
* re-issuing a grant for whoever is already signed in. Never combined with
|
|
323
|
+
* `automatic`: an automatic hop must never demand a password.
|
|
324
|
+
*/
|
|
325
|
+
switchUser?: boolean;
|
|
326
|
+
/**
|
|
327
|
+
* "Somebody else wants in TOO." Carries `add=1`, which forwards
|
|
328
|
+
* `prompt=select_account` to auth and lands the result in a FREE account
|
|
329
|
+
* slot — so whoever is already signed in here keeps working, in their own
|
|
330
|
+
* tab (2026-08-12).
|
|
331
|
+
*
|
|
332
|
+
* The difference from `switchUser` is the whole multi-account feature:
|
|
333
|
+
* switching REPLACES this tab's account, adding does not. Never combined
|
|
334
|
+
* with `automatic`, for the same reason `switchUser` is not.
|
|
335
|
+
*/
|
|
336
|
+
addAccount?: boolean;
|
|
337
|
+
/**
|
|
338
|
+
* A specific account id to sign in as, skipping the chooser. Only ever
|
|
339
|
+
* honoured by auth when this browser already holds a live session for that
|
|
340
|
+
* account — see `sessionUserAt` — so it can never be a way in, only a way
|
|
341
|
+
* to avoid retyping a password.
|
|
342
|
+
*/
|
|
343
|
+
account?: string;
|
|
344
|
+
now?: () => number;
|
|
345
|
+
navigate?: (url: string) => void;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* The deep link the browser is standing on right now, as a same-origin path, or
|
|
350
|
+
* null when there is nothing worth carrying. This is the RETURN PATH: a shared
|
|
351
|
+
* `/play/<name>` link opened signed out must come back to `/play/<name>`, not to
|
|
352
|
+
* the app index (2026-08-05). Only a real sub-path is carried — the bare index
|
|
353
|
+
* (`pathname === "/"`, which is also where the `?ssoError=` failure page lives)
|
|
354
|
+
* has no deep link to preserve, and skipping it keeps a failed attempt's URL out
|
|
355
|
+
* of the round trip. The server re-validates it (`sanitizeReturnPath`), so this
|
|
356
|
+
* side only has to CAPTURE; it never has to be the one that decides safety.
|
|
357
|
+
*/
|
|
358
|
+
function currentDeepLink(): string | null {
|
|
359
|
+
try {
|
|
360
|
+
const loc = globalThis.location;
|
|
361
|
+
if (!loc?.pathname || loc.pathname === "/") return null;
|
|
362
|
+
return `${loc.pathname}${loc.search}${loc.hash}`;
|
|
363
|
+
} catch {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Begin the redirect chain. Used by BOTH doors — the automatic one and the
|
|
370
|
+
* button — so the attempt is recorded exactly once either way, the deep-link
|
|
371
|
+
* return path is carried the same way from both, and the two can never drift
|
|
372
|
+
* into different behavior.
|
|
373
|
+
*/
|
|
374
|
+
export function startSsoSignIn(options: StartSsoSignInOptions = {}): void {
|
|
375
|
+
const now = options.now ?? Date.now;
|
|
376
|
+
if (options.automatic) {
|
|
377
|
+
write(SSO_ATTEMPT_KEY, String(now()));
|
|
378
|
+
} else {
|
|
379
|
+
// An explicit press is the person overriding every guard, including
|
|
380
|
+
// their own earlier sign-out.
|
|
381
|
+
clearSsoAutoState();
|
|
382
|
+
}
|
|
383
|
+
const base = options.startPath ?? SSO_START_PATH;
|
|
384
|
+
const deepLink = currentDeepLink();
|
|
385
|
+
const query = new URLSearchParams();
|
|
386
|
+
if (deepLink) query.set("return", deepLink);
|
|
387
|
+
// Only on an explicit press — `automatic` and `switchUser` are contradictory
|
|
388
|
+
// by construction (see the option's doc), and the guard keeps a future caller
|
|
389
|
+
// from producing a hop that silently demands a password.
|
|
390
|
+
if (options.switchUser && !options.automatic) query.set("switch", "1");
|
|
391
|
+
if (options.addAccount && !options.automatic) query.set("add", "1");
|
|
392
|
+
if (options.account) query.set("account", options.account);
|
|
393
|
+
// 🔴 The tab's account rides along, and it has to. The start route stashes
|
|
394
|
+
// the slot for the callback to land in (a registered redirect_uri cannot
|
|
395
|
+
// carry a query param, so the cookie is the only channel) — without this,
|
|
396
|
+
// re-authenticating in a second-account tab would silently mint the session
|
|
397
|
+
// into slot 0 and evict the primary account in the OTHER tab.
|
|
398
|
+
const slot = currentAccountSlot();
|
|
399
|
+
if (slot !== 0) query.set(ACCOUNT_SLOT_PARAM, String(slot));
|
|
400
|
+
const search = query.toString();
|
|
401
|
+
const url = search ? `${base}${base.includes("?") ? "&" : "?"}${search}` : base;
|
|
402
|
+
if (options.navigate) {
|
|
403
|
+
options.navigate(url);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
globalThis.location?.assign(url);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export interface StartSsoSignOutOptions {
|
|
410
|
+
/** Defaults to `SSO_SIGNOUT_PATH`. */
|
|
411
|
+
signOutPath?: string;
|
|
412
|
+
/**
|
|
413
|
+
* Leave EVERY account on this machine, not just this tab's (2026-08-12).
|
|
414
|
+
*
|
|
415
|
+
* 🔴 Off by default, and that default is the important half. With several
|
|
416
|
+
* accounts in one browser, a sign-out that ended all of them would sign the
|
|
417
|
+
* owner out of every other tab and every other app because a test account
|
|
418
|
+
* left one page — work lost in a window nobody touched. This is the
|
|
419
|
+
* shared-computer exit, asked for by name.
|
|
420
|
+
*/
|
|
421
|
+
allAccounts?: boolean;
|
|
422
|
+
navigate?: (url: string) => void;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Leave — for real, on this app AND at auth.
|
|
427
|
+
*
|
|
428
|
+
* The signed-out marker is written FIRST and for the same reason it always
|
|
429
|
+
* was: the landing page after the round trip is this app's index, signed out,
|
|
430
|
+
* and without the flag `useSsoAutoSignIn` would immediately start another hop.
|
|
431
|
+
* That hop would now correctly stop at auth's login form rather than loop, but
|
|
432
|
+
* bouncing the person to another origin the instant they signed out is not what
|
|
433
|
+
* pressing "Sign out" should look like.
|
|
434
|
+
*
|
|
435
|
+
* A full-page navigation, not a fetch: the browser itself has to carry cookies
|
|
436
|
+
* to two origins and follow the 302 chain between them.
|
|
437
|
+
*/
|
|
438
|
+
export function startSsoSignOut(options: StartSsoSignOutOptions = {}): void {
|
|
439
|
+
markSsoSignedOut();
|
|
440
|
+
const base = options.signOutPath ?? SSO_SIGNOUT_PATH;
|
|
441
|
+
const query = new URLSearchParams();
|
|
442
|
+
if (options.allAccounts) query.set("all", "1");
|
|
443
|
+
// 🔴 Name the tab's slot, or the server ends slot 0 — which is to say, it
|
|
444
|
+
// signs the OWNER out because a second-account tab pressed "Sign out".
|
|
445
|
+
const slot = currentAccountSlot();
|
|
446
|
+
if (slot !== 0) query.set(ACCOUNT_SLOT_PARAM, String(slot));
|
|
447
|
+
const search = query.toString();
|
|
448
|
+
const url = search ? `${base}${base.includes("?") ? "&" : "?"}${search}` : base;
|
|
449
|
+
if (options.navigate) {
|
|
450
|
+
options.navigate(url);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
globalThis.location?.assign(url);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// ── Reaching the sign-in card on purpose (deploy smokes) ────────────────────
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Structural type for a Playwright `Page`/`BrowserContext`. Declared rather
|
|
460
|
+
* than imported so this package keeps no dependency on a test runner — the
|
|
461
|
+
* helper below is used BY the browser smokes, not by the apps.
|
|
462
|
+
*/
|
|
463
|
+
export interface SsoInitScriptTarget {
|
|
464
|
+
addInitScript(script: (key: string) => void, arg: string): Promise<unknown>;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Seed the signed-out marker before any page script runs, so the app renders
|
|
469
|
+
* its sign-in card instead of hopping to accounts.
|
|
470
|
+
*
|
|
471
|
+
* Every satellite's browser smoke asserts that the signed-out surface renders,
|
|
472
|
+
* is styled, and carries the theme toggle — real gates that have caught a
|
|
473
|
+
* missing stylesheet and a blank root. Automatic sign-in does not delete that
|
|
474
|
+
* surface, it makes it a SECOND-visit screen (after a sign-out, a failed
|
|
475
|
+
* callback, or a hop that did not stick), so the smoke has to ask for it
|
|
476
|
+
* rather than assume it. This is the honest way to ask: it puts the browser in
|
|
477
|
+
* the exact state a real sign-out leaves it in, with nothing test-only in the
|
|
478
|
+
* app.
|
|
479
|
+
*/
|
|
480
|
+
export async function suppressSsoAutoSignIn(target: SsoInitScriptTarget): Promise<void> {
|
|
481
|
+
await target.addInitScript((key: string) => {
|
|
482
|
+
try {
|
|
483
|
+
sessionStorage.setItem(key, "1");
|
|
484
|
+
} catch {
|
|
485
|
+
/* a browser that cannot store it will not auto-start either */
|
|
486
|
+
}
|
|
487
|
+
}, SSO_SIGNED_OUT_KEY);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// ── The hook the bespoke satellites use ─────────────────────────────────────
|
|
491
|
+
// `createSsoRedirectAdapter` (roms, notes) has this built in. hobbies, vault,
|
|
492
|
+
// orch, orch-companion and station each grew their own `useSession()` before
|
|
493
|
+
// the adapter existed, so they take the same policy through one hook rather
|
|
494
|
+
// than through five copies of an effect — which is how the guards above stay a
|
|
495
|
+
// single decision rather than a convention.
|
|
496
|
+
|
|
497
|
+
export interface UseSsoAutoSignInInput {
|
|
498
|
+
/** What the session probe says. `undefined` means it has not answered yet —
|
|
499
|
+
* the hook does nothing until it has, so a loading render never redirects. */
|
|
500
|
+
authenticated: boolean | undefined;
|
|
501
|
+
/** The accounts origin the server advertises (`SsoSessionInfo.accountsUrl`). */
|
|
502
|
+
accountsUrl?: string | null;
|
|
503
|
+
/** Off switch. Defaults to on. */
|
|
504
|
+
enabled?: boolean;
|
|
505
|
+
/** Defaults to `SSO_START_PATH`. */
|
|
506
|
+
startPath?: string;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Returns `true` while the page is on its way to the accounts satellite. A
|
|
511
|
+
* caller renders its LOADING view for that — never the sign-in screen, which is
|
|
512
|
+
* the flash this whole feature exists to delete.
|
|
513
|
+
*/
|
|
514
|
+
export function useSsoAutoSignIn(input: UseSsoAutoSignInInput): boolean {
|
|
515
|
+
const { authenticated, accountsUrl, enabled, startPath } = input;
|
|
516
|
+
const [redirecting, setRedirecting] = useState(false);
|
|
517
|
+
|
|
518
|
+
useEffect(() => {
|
|
519
|
+
if (authenticated === undefined) return;
|
|
520
|
+
if (authenticated) {
|
|
521
|
+
// A confirmed session retires both guards.
|
|
522
|
+
clearSsoAutoState();
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
const decision = decideSsoAutoSignIn({
|
|
526
|
+
authenticated: false,
|
|
527
|
+
accountsUrl,
|
|
528
|
+
// 🔴 The CACHE, never the live URL. A notice rendered inside the signed-out
|
|
529
|
+
// branch is a CHILD, and React runs child effects first — so by the time this
|
|
530
|
+
// effect runs the parameter may already have been taken off the URL, and this
|
|
531
|
+
// guard would decide there was no failure and bounce into the loop.
|
|
532
|
+
ssoError: ssoErrorForThisLoad(),
|
|
533
|
+
signedOut: ssoSignedOutHere(),
|
|
534
|
+
lastAttemptAt: lastSsoAttemptAt(),
|
|
535
|
+
now: Date.now(),
|
|
536
|
+
enabled,
|
|
537
|
+
});
|
|
538
|
+
if (!decision.start) return;
|
|
539
|
+
setRedirecting(true);
|
|
540
|
+
startSsoSignIn({ startPath, automatic: true });
|
|
541
|
+
}, [authenticated, accountsUrl, enabled, startPath]);
|
|
542
|
+
|
|
543
|
+
return redirecting;
|
|
544
|
+
}
|