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,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account SLOTS — how one browser holds several signed-in accounts at once,
|
|
3
|
+
* and how a single request says which of them it is acting as.
|
|
4
|
+
*
|
|
5
|
+
* ── The ask (2026-08-12, owner) ─────────────────────────────────────────────
|
|
6
|
+
* "I want one tab on my main account and another tab on a different account
|
|
7
|
+
* simultaneously, so I can do what I need as owner and see what other user
|
|
8
|
+
* types see." The same mechanism answers the household case the fleet already
|
|
9
|
+
* had — two people sharing a laptop who each want their own session without
|
|
10
|
+
* one of them typing a password every swap.
|
|
11
|
+
*
|
|
12
|
+
* ── Why slots, and not any of the alternatives ──────────────────────────────
|
|
13
|
+
* A cookie is per-ORIGIN, not per-tab, so "two accounts at once" cannot be two
|
|
14
|
+
* values of one cookie name. Three shapes were on the table:
|
|
15
|
+
*
|
|
16
|
+
* 1. **One cookie holding a jar of sessions, plus an index.** Rejected: every
|
|
17
|
+
* account's credential then rides in one value, so a single parse bug or a
|
|
18
|
+
* single truncated header loses or crosses ALL of them, and revoking one
|
|
19
|
+
* means rewriting the others' cookie.
|
|
20
|
+
* 2. **A bearer token in `sessionStorage`** (per-tab for free). Rejected: no
|
|
21
|
+
* cookie means top-level navigations, `<img>`/`<video>` fetches, downloads
|
|
22
|
+
* and EventSource all arrive anonymous. This fleet serves media and whole
|
|
23
|
+
* page loads behind the session; it would have authenticated the XHR layer
|
|
24
|
+
* only, which is the half that is easiest to notice working.
|
|
25
|
+
* 3. **N independent cookies, one per slot, and a selector on the request.**
|
|
26
|
+
* What this is, and what Google does. Each slot is a wholly separate
|
|
27
|
+
* opaque session id with its own row, so it is independently revocable,
|
|
28
|
+
* independently expiring, and a break in one cannot reach another.
|
|
29
|
+
*
|
|
30
|
+
* ── The selector is a HINT, never an authorization ──────────────────────────
|
|
31
|
+
* 🔴 The slot chooses WHICH OF THIS BROWSER'S OWN COOKIES to read, and nothing
|
|
32
|
+
* else. It is attacker-writable — anybody can put `?authuser=3` on a link — and
|
|
33
|
+
* that is fine precisely because the worst it achieves is making you act as an
|
|
34
|
+
* account you are *already signed into on this machine*. It can never
|
|
35
|
+
* manufacture access to an account with no cookie in that slot: an empty slot
|
|
36
|
+
* resolves to null and the request is anonymous. Nothing downstream may treat
|
|
37
|
+
* the slot number as identity; resolve it to a user through the store, always.
|
|
38
|
+
*
|
|
39
|
+
* The one real consequence is confusion — acting as B when you meant A — so
|
|
40
|
+
* every surface that renders a non-zero slot must say whose it is. That is why
|
|
41
|
+
* `AccountMenu` shows the account name unconditionally and why the URL carries
|
|
42
|
+
* the slot visibly rather than hiding it in a header alone.
|
|
43
|
+
*
|
|
44
|
+
* ── Why slot 0 is the bare cookie name ──────────────────────────────────────
|
|
45
|
+
* `roms_session` (slot 0), `roms_session__1`, `roms_session__2`. Slot 0 keeps
|
|
46
|
+
* the exact name, value and semantics every app already ships, so every live
|
|
47
|
+
* session in the fleet stays signed in across this change and every call site
|
|
48
|
+
* that never mentions a slot behaves byte-identically. There is no migration
|
|
49
|
+
* because there is nothing to migrate — multi-account is purely additive, and
|
|
50
|
+
* a browser that never opens a second account never sends a second cookie.
|
|
51
|
+
*
|
|
52
|
+
* ── Why the param is `authuser` ─────────────────────────────────────────────
|
|
53
|
+
* It is the name Google's account switcher has used for twenty years, so it is
|
|
54
|
+
* recognisable in a URL, and it is distinctive enough that no satellite's own
|
|
55
|
+
* query strings collide with it (`u` would have).
|
|
56
|
+
*
|
|
57
|
+
* This module is deliberately dependency-free so the browser half
|
|
58
|
+
* (`accountSlot.client.ts`) and the server half (`userSessions.ts`) share ONE
|
|
59
|
+
* definition of the vocabulary. Two copies of "which slot is this?" is the bug
|
|
60
|
+
* where a tab renders one account and writes as another.
|
|
61
|
+
*/
|
|
62
|
+
/**
|
|
63
|
+
* How many accounts one browser may be signed into at once, per app.
|
|
64
|
+
*
|
|
65
|
+
* Bounded because every slot's cookie rides on EVERY request to the origin,
|
|
66
|
+
* including images and polls. Five is well past the owner's stated need (an
|
|
67
|
+
* owner account plus a test account per role) and still under 500 bytes of
|
|
68
|
+
* header. Remembered accounts — the chooser list — are a separate, larger cap:
|
|
69
|
+
* being offered an account to switch to costs nothing per request.
|
|
70
|
+
*/
|
|
71
|
+
export declare const MAX_ACCOUNT_SLOTS = 5;
|
|
72
|
+
/** Highest slot number that exists, for the loops that sweep all of them. */
|
|
73
|
+
export declare const MAX_SLOT_INDEX: number;
|
|
74
|
+
/**
|
|
75
|
+
* The query parameter a URL uses to name its slot.
|
|
76
|
+
*
|
|
77
|
+
* 🔴 It has to be in the URL and not only in a header. A header can be attached
|
|
78
|
+
* to `fetch`, and nothing else: a full page load, a bookmark, a link opened in
|
|
79
|
+
* a new tab, an `<img src>` and a download all carry the URL alone. Putting the
|
|
80
|
+
* slot there is what makes "open this account in a second window" work at all,
|
|
81
|
+
* and it is what makes the second window survive a reload.
|
|
82
|
+
*/
|
|
83
|
+
export declare const ACCOUNT_SLOT_PARAM = "authuser";
|
|
84
|
+
/**
|
|
85
|
+
* The header a same-origin `fetch` uses instead, when rewriting the URL would
|
|
86
|
+
* be worse.
|
|
87
|
+
*
|
|
88
|
+
* Both are read, URL first. The header exists because an SPA's API calls are
|
|
89
|
+
* built from route constants all over an app, and threading a query param
|
|
90
|
+
* through every one of them is the change no app would apply consistently —
|
|
91
|
+
* one missed call site is a request that silently acts as the wrong account.
|
|
92
|
+
*/
|
|
93
|
+
export declare const ACCOUNT_SLOT_HEADER = "x-account-slot";
|
|
94
|
+
/**
|
|
95
|
+
* A slot number this code is willing to use, from anything at all.
|
|
96
|
+
*
|
|
97
|
+
* Everything that is not a whole number inside the range becomes 0 — the
|
|
98
|
+
* default account — rather than throwing or refusing the request. A garbled
|
|
99
|
+
* `?authuser=` must degrade to "your main account", never to an error page:
|
|
100
|
+
* the value arrives from URLs people edit, share and truncate.
|
|
101
|
+
*/
|
|
102
|
+
export declare function normalizeSlot(raw: unknown): number;
|
|
103
|
+
/**
|
|
104
|
+
* `true` only for a value that really named a slot, so callers can tell
|
|
105
|
+
* "explicitly slot 0" from "said nothing".
|
|
106
|
+
*
|
|
107
|
+
* The distinction matters exactly once, and it is load-bearing: the SSO
|
|
108
|
+
* callback has to know whether the hop was told to land in a specific slot or
|
|
109
|
+
* whether it should pick one, and `normalizeSlot` alone answers 0 to both.
|
|
110
|
+
*/
|
|
111
|
+
export declare function isExplicitSlot(raw: unknown): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* The cookie name for one slot of one app.
|
|
114
|
+
*
|
|
115
|
+
* Slot 0 is the base name unchanged — see the header for why that is the whole
|
|
116
|
+
* migration story. The `__` separator cannot collide with the base name in a
|
|
117
|
+
* cookie header: a regex anchored on `<base>=` will not match `<base>__1=`.
|
|
118
|
+
*/
|
|
119
|
+
export declare function slotCookieName(base: string, slot: number): string;
|
|
120
|
+
/** Every cookie name this app could be holding a session under, slot order. */
|
|
121
|
+
export declare function allSlotCookieNames(base: string): string[];
|
|
122
|
+
/**
|
|
123
|
+
* The slot a URL names, or null if it names none.
|
|
124
|
+
*
|
|
125
|
+
* Accepts a full URL or a path — the SPA calls it with `location.href` and the
|
|
126
|
+
* server calls it with the request URL, and neither should have to normalize
|
|
127
|
+
* first. A malformed URL is "names none", never a throw.
|
|
128
|
+
*/
|
|
129
|
+
export declare function slotFromUrl(url: string): number | null;
|
|
130
|
+
/**
|
|
131
|
+
* `url` with the slot named on it — or with it REMOVED for slot 0.
|
|
132
|
+
*
|
|
133
|
+
* Slot 0 gets a clean URL on purpose. The overwhelmingly common case is one
|
|
134
|
+
* account, and a fleet whose every address grew a `?authuser=0` would have
|
|
135
|
+
* made a multi-account feature nobody asked for visible to everybody who did
|
|
136
|
+
* not want it. It also keeps existing bookmarks, smokes and shared links
|
|
137
|
+
* character-for-character what they already are.
|
|
138
|
+
*/
|
|
139
|
+
export declare function withSlot(url: string, slot: number): string;
|
|
140
|
+
/**
|
|
141
|
+
* The account id the CLIENT believes it is acting as — the one it drew on
|
|
142
|
+
* screen. Attached by the patched `fetch` in `accountSlot.client.ts`.
|
|
143
|
+
*
|
|
144
|
+
* 🔴 Never an authorization. It cannot widen access, name a different account
|
|
145
|
+
* or select a cookie; the only outcome it can produce that the request would
|
|
146
|
+
* not otherwise have had is {@link ACCOUNT_DRIFT_STATUS}.
|
|
147
|
+
*/
|
|
148
|
+
export declare const ACCOUNT_USER_HEADER = "x-account-user";
|
|
149
|
+
/**
|
|
150
|
+
* 409 Conflict — the credential is good, the identity behind it moved.
|
|
151
|
+
*
|
|
152
|
+
* Deliberately not 401 (which the SPAs answer by bouncing to a sign-in hop, and
|
|
153
|
+
* there is nothing to sign in to — the browser holds a perfectly good session)
|
|
154
|
+
* and not 403 (which reads as "you may not", and is the answer that produced
|
|
155
|
+
* the silent failure this exists to stop).
|
|
156
|
+
*/
|
|
157
|
+
export declare const ACCOUNT_DRIFT_STATUS = 409;
|
|
158
|
+
/** The machine-readable `error` a drift refusal carries, so a client can tell it
|
|
159
|
+
* apart from every other 409 an app already answers (a full slot jar, a name
|
|
160
|
+
* collision, an edit conflict). */
|
|
161
|
+
export declare const ACCOUNT_DRIFT_CODE = "account_changed";
|
|
162
|
+
/** Who a drift refusal says you actually are now. Addresses and names only —
|
|
163
|
+
* the same facts `/api/auth/session` already hands this browser about a slot
|
|
164
|
+
* it is holding, so this publishes nothing new. */
|
|
165
|
+
export interface AccountDriftActual {
|
|
166
|
+
id: string;
|
|
167
|
+
email: string;
|
|
168
|
+
name: string;
|
|
169
|
+
}
|
|
170
|
+
/** The canonical drift body. One shape, so every consumer and the accounts app
|
|
171
|
+
* itself cannot answer this in two dialects. */
|
|
172
|
+
export interface AccountDriftBody {
|
|
173
|
+
error: typeof ACCOUNT_DRIFT_CODE;
|
|
174
|
+
/** Who the CLIENT said it was rendering for, echoed back so a banner can name
|
|
175
|
+
* both sides without having to have kept its own copy. */
|
|
176
|
+
expected: string;
|
|
177
|
+
actual: AccountDriftActual;
|
|
178
|
+
}
|
|
179
|
+
export declare function accountDriftBody(expected: string, actual: AccountDriftActual): AccountDriftBody;
|
|
180
|
+
/** `true` for a body that is a drift refusal. Total — anything at all may be
|
|
181
|
+
* passed in, because this is called on a parsed response body. */
|
|
182
|
+
export declare function isAccountDriftBody(body: unknown): body is AccountDriftBody;
|
|
183
|
+
/**
|
|
184
|
+
* The one rule, so the client's guess and the server's verdict cannot disagree.
|
|
185
|
+
*
|
|
186
|
+
* Drift is only ever declared when BOTH sides are known and they differ. An
|
|
187
|
+
* absent expectation (an old build, a subresource, a non-`fetch` caller, an app
|
|
188
|
+
* that never mounted the client module) is NOT drift — the guard has to be
|
|
189
|
+
* additive or it would start refusing requests the fleet has always allowed.
|
|
190
|
+
*/
|
|
191
|
+
export declare function isAccountDrift(expected: string | null, actual: string | null): boolean;
|
|
192
|
+
//# sourceMappingURL=accountSlots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accountSlots.d.ts","sourceRoot":"","sources":["../src/accountSlots.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC,6EAA6E;AAC7E,eAAO,MAAM,cAAc,QAAwB,CAAC;AAEpD;;;;;;;;GAQG;AACH,eAAO,M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gB,kBAAkB,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,gBAAgB,CAU1E;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAItF"}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account SLOTS — how one browser holds several signed-in accounts at once,
|
|
3
|
+
* and how a single request says which of them it is acting as.
|
|
4
|
+
*
|
|
5
|
+
* ── The ask (2026-08-12, owner) ─────────────────────────────────────────────
|
|
6
|
+
* "I want one tab on my main account and another tab on a different account
|
|
7
|
+
* simultaneously, so I can do what I need as owner and see what other user
|
|
8
|
+
* types see." The same mechanism answers the household case the fleet already
|
|
9
|
+
* had — two people sharing a laptop who each want their own session without
|
|
10
|
+
* one of them typing a password every swap.
|
|
11
|
+
*
|
|
12
|
+
* ── Why slots, and not any of the alternatives ──────────────────────────────
|
|
13
|
+
* A cookie is per-ORIGIN, not per-tab, so "two accounts at once" cannot be two
|
|
14
|
+
* values of one cookie name. Three shapes were on the table:
|
|
15
|
+
*
|
|
16
|
+
* 1. **One cookie holding a jar of sessions, plus an index.** Rejected: every
|
|
17
|
+
* account's credential then rides in one value, so a single parse bug or a
|
|
18
|
+
* single truncated header loses or crosses ALL of them, and revoking one
|
|
19
|
+
* means rewriting the others' cookie.
|
|
20
|
+
* 2. **A bearer token in `sessionStorage`** (per-tab for free). Rejected: no
|
|
21
|
+
* cookie means top-level navigations, `<img>`/`<video>` fetches, downloads
|
|
22
|
+
* and EventSource all arrive anonymous. This fleet serves media and whole
|
|
23
|
+
* page loads behind the session; it would have authenticated the XHR layer
|
|
24
|
+
* only, which is the half that is easiest to notice working.
|
|
25
|
+
* 3. **N independent cookies, one per slot, and a selector on the request.**
|
|
26
|
+
* What this is, and what Google does. Each slot is a wholly separate
|
|
27
|
+
* opaque session id with its own row, so it is independently revocable,
|
|
28
|
+
* independently expiring, and a break in one cannot reach another.
|
|
29
|
+
*
|
|
30
|
+
* ── The selector is a HINT, never an authorization ──────────────────────────
|
|
31
|
+
* 🔴 The slot chooses WHICH OF THIS BROWSER'S OWN COOKIES to read, and nothing
|
|
32
|
+
* else. It is attacker-writable — anybody can put `?authuser=3` on a link — and
|
|
33
|
+
* that is fine precisely because the worst it achieves is making you act as an
|
|
34
|
+
* account you are *already signed into on this machine*. It can never
|
|
35
|
+
* manufacture access to an account with no cookie in that slot: an empty slot
|
|
36
|
+
* resolves to null and the request is anonymous. Nothing downstream may treat
|
|
37
|
+
* the slot number as identity; resolve it to a user through the store, always.
|
|
38
|
+
*
|
|
39
|
+
* The one real consequence is confusion — acting as B when you meant A — so
|
|
40
|
+
* every surface that renders a non-zero slot must say whose it is. That is why
|
|
41
|
+
* `AccountMenu` shows the account name unconditionally and why the URL carries
|
|
42
|
+
* the slot visibly rather than hiding it in a header alone.
|
|
43
|
+
*
|
|
44
|
+
* ── Why slot 0 is the bare cookie name ──────────────────────────────────────
|
|
45
|
+
* `roms_session` (slot 0), `roms_session__1`, `roms_session__2`. Slot 0 keeps
|
|
46
|
+
* the exact name, value and semantics every app already ships, so every live
|
|
47
|
+
* session in the fleet stays signed in across this change and every call site
|
|
48
|
+
* that never mentions a slot behaves byte-identically. There is no migration
|
|
49
|
+
* because there is nothing to migrate — multi-account is purely additive, and
|
|
50
|
+
* a browser that never opens a second account never sends a second cookie.
|
|
51
|
+
*
|
|
52
|
+
* ── Why the param is `authuser` ─────────────────────────────────────────────
|
|
53
|
+
* It is the name Google's account switcher has used for twenty years, so it is
|
|
54
|
+
* recognisable in a URL, and it is distinctive enough that no satellite's own
|
|
55
|
+
* query strings collide with it (`u` would have).
|
|
56
|
+
*
|
|
57
|
+
* This module is deliberately dependency-free so the browser half
|
|
58
|
+
* (`accountSlot.client.ts`) and the server half (`userSessions.ts`) share ONE
|
|
59
|
+
* definition of the vocabulary. Two copies of "which slot is this?" is the bug
|
|
60
|
+
* where a tab renders one account and writes as another.
|
|
61
|
+
*/
|
|
62
|
+
/**
|
|
63
|
+
* How many accounts one browser may be signed into at once, per app.
|
|
64
|
+
*
|
|
65
|
+
* Bounded because every slot's cookie rides on EVERY request to the origin,
|
|
66
|
+
* including images and polls. Five is well past the owner's stated need (an
|
|
67
|
+
* owner account plus a test account per role) and still under 500 bytes of
|
|
68
|
+
* header. Remembered accounts — the chooser list — are a separate, larger cap:
|
|
69
|
+
* being offered an account to switch to costs nothing per request.
|
|
70
|
+
*/
|
|
71
|
+
export const MAX_ACCOUNT_SLOTS = 5;
|
|
72
|
+
/** Highest slot number that exists, for the loops that sweep all of them. */
|
|
73
|
+
export const MAX_SLOT_INDEX = MAX_ACCOUNT_SLOTS - 1;
|
|
74
|
+
/**
|
|
75
|
+
* The query parameter a URL uses to name its slot.
|
|
76
|
+
*
|
|
77
|
+
* 🔴 It has to be in the URL and not only in a header. A header can be attached
|
|
78
|
+
* to `fetch`, and nothing else: a full page load, a bookmark, a link opened in
|
|
79
|
+
* a new tab, an `<img src>` and a download all carry the URL alone. Putting the
|
|
80
|
+
* slot there is what makes "open this account in a second window" work at all,
|
|
81
|
+
* and it is what makes the second window survive a reload.
|
|
82
|
+
*/
|
|
83
|
+
export const ACCOUNT_SLOT_PARAM = "authuser";
|
|
84
|
+
/**
|
|
85
|
+
* The header a same-origin `fetch` uses instead, when rewriting the URL would
|
|
86
|
+
* be worse.
|
|
87
|
+
*
|
|
88
|
+
* Both are read, URL first. The header exists because an SPA's API calls are
|
|
89
|
+
* built from route constants all over an app, and threading a query param
|
|
90
|
+
* through every one of them is the change no app would apply consistently —
|
|
91
|
+
* one missed call site is a request that silently acts as the wrong account.
|
|
92
|
+
*/
|
|
93
|
+
export const ACCOUNT_SLOT_HEADER = "x-account-slot";
|
|
94
|
+
/**
|
|
95
|
+
* A slot number this code is willing to use, from anything at all.
|
|
96
|
+
*
|
|
97
|
+
* Everything that is not a whole number inside the range becomes 0 — the
|
|
98
|
+
* default account — rather than throwing or refusing the request. A garbled
|
|
99
|
+
* `?authuser=` must degrade to "your main account", never to an error page:
|
|
100
|
+
* the value arrives from URLs people edit, share and truncate.
|
|
101
|
+
*/
|
|
102
|
+
export function normalizeSlot(raw) {
|
|
103
|
+
const n = typeof raw === "number" ? raw : Number.parseInt(String(raw ?? ""), 10);
|
|
104
|
+
if (!Number.isInteger(n) || n < 0 || n > MAX_SLOT_INDEX)
|
|
105
|
+
return 0;
|
|
106
|
+
return n;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* `true` only for a value that really named a slot, so callers can tell
|
|
110
|
+
* "explicitly slot 0" from "said nothing".
|
|
111
|
+
*
|
|
112
|
+
* The distinction matters exactly once, and it is load-bearing: the SSO
|
|
113
|
+
* callback has to know whether the hop was told to land in a specific slot or
|
|
114
|
+
* whether it should pick one, and `normalizeSlot` alone answers 0 to both.
|
|
115
|
+
*/
|
|
116
|
+
export function isExplicitSlot(raw) {
|
|
117
|
+
if (raw === null || raw === undefined || raw === "")
|
|
118
|
+
return false;
|
|
119
|
+
const n = typeof raw === "number" ? raw : Number.parseInt(String(raw), 10);
|
|
120
|
+
return Number.isInteger(n) && n >= 0 && n <= MAX_SLOT_INDEX;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The cookie name for one slot of one app.
|
|
124
|
+
*
|
|
125
|
+
* Slot 0 is the base name unchanged — see the header for why that is the whole
|
|
126
|
+
* migration story. The `__` separator cannot collide with the base name in a
|
|
127
|
+
* cookie header: a regex anchored on `<base>=` will not match `<base>__1=`.
|
|
128
|
+
*/
|
|
129
|
+
export function slotCookieName(base, slot) {
|
|
130
|
+
const n = normalizeSlot(slot);
|
|
131
|
+
return n === 0 ? base : `${base}__${n}`;
|
|
132
|
+
}
|
|
133
|
+
/** Every cookie name this app could be holding a session under, slot order. */
|
|
134
|
+
export function allSlotCookieNames(base) {
|
|
135
|
+
return Array.from({ length: MAX_ACCOUNT_SLOTS }, (_, slot) => slotCookieName(base, slot));
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The slot a URL names, or null if it names none.
|
|
139
|
+
*
|
|
140
|
+
* Accepts a full URL or a path — the SPA calls it with `location.href` and the
|
|
141
|
+
* server calls it with the request URL, and neither should have to normalize
|
|
142
|
+
* first. A malformed URL is "names none", never a throw.
|
|
143
|
+
*/
|
|
144
|
+
export function slotFromUrl(url) {
|
|
145
|
+
// 🔴 The hash is cut FIRST. The fleet's SPAs are hash-routed, so
|
|
146
|
+
// `/index.html#/login?authuser=3` is extremely reachable — and everything
|
|
147
|
+
// after the `#` is a client route the browser never sends to a server.
|
|
148
|
+
// Reading it here would make the server's answer depend on a string it was
|
|
149
|
+
// not given, and make the client's answer depend on whether the app happens
|
|
150
|
+
// to put the param before or after the fragment.
|
|
151
|
+
const beforeHash = url.split("#", 1)[0] ?? "";
|
|
152
|
+
const query = beforeHash.indexOf("?");
|
|
153
|
+
if (query === -1)
|
|
154
|
+
return null;
|
|
155
|
+
const raw = new URLSearchParams(beforeHash.slice(query + 1)).get(ACCOUNT_SLOT_PARAM);
|
|
156
|
+
return isExplicitSlot(raw) ? normalizeSlot(raw) : null;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* `url` with the slot named on it — or with it REMOVED for slot 0.
|
|
160
|
+
*
|
|
161
|
+
* Slot 0 gets a clean URL on purpose. The overwhelmingly common case is one
|
|
162
|
+
* account, and a fleet whose every address grew a `?authuser=0` would have
|
|
163
|
+
* made a multi-account feature nobody asked for visible to everybody who did
|
|
164
|
+
* not want it. It also keeps existing bookmarks, smokes and shared links
|
|
165
|
+
* character-for-character what they already are.
|
|
166
|
+
*/
|
|
167
|
+
export function withSlot(url, slot) {
|
|
168
|
+
const n = normalizeSlot(slot);
|
|
169
|
+
// An empty string is not a URL and must not become one. `new URL("", base)`
|
|
170
|
+
// happily answers the base itself, so without this guard a caller with
|
|
171
|
+
// nothing to rewrite gets handed `/?authuser=1` — a navigation to the index.
|
|
172
|
+
if (url === "")
|
|
173
|
+
return url;
|
|
174
|
+
// Resolved against a base so a bare path works; the base is dropped again
|
|
175
|
+
// below by rebuilding from the parts we were actually given.
|
|
176
|
+
const base = "http://slot.invalid";
|
|
177
|
+
let parsed;
|
|
178
|
+
try {
|
|
179
|
+
parsed = new URL(url, base);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return url;
|
|
183
|
+
}
|
|
184
|
+
if (n === 0)
|
|
185
|
+
parsed.searchParams.delete(ACCOUNT_SLOT_PARAM);
|
|
186
|
+
else
|
|
187
|
+
parsed.searchParams.set(ACCOUNT_SLOT_PARAM, String(n));
|
|
188
|
+
const absolute = /^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("//");
|
|
189
|
+
return absolute ? parsed.toString() : `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
190
|
+
}
|
|
191
|
+
/* ═══════════════ Identity drift: the page renders as A, the request acts as B ═══
|
|
192
|
+
*
|
|
193
|
+
* ── The failure (owner, 2026-08-15) ─────────────────────────────────────────
|
|
194
|
+
* Signed in as `curtwphillips@gmail.com` on the auth admin page, he tried to
|
|
195
|
+
* grant another account access to family and was refused: *"The app let me stay
|
|
196
|
+
* for curtwphillips@gmail.com but the request treated me as a different user. I
|
|
197
|
+
* was allowed to keep using the UI for auth manage users area."*
|
|
198
|
+
*
|
|
199
|
+
* That is the slot mechanism working exactly as designed and producing a lie.
|
|
200
|
+
* The cookie jar is per-ORIGIN, so a slot's occupant can be replaced by ANOTHER
|
|
201
|
+
* TAB — sign a second account in, sign one out, let one expire — while the first
|
|
202
|
+
* tab is still showing a page it rendered minutes ago for whoever used to be
|
|
203
|
+
* there. Nothing tells the open tab. It keeps drawing the old name in the header,
|
|
204
|
+
* its reads answer as the new person, and its writes are refused by a rule the
|
|
205
|
+
* screen says does not apply. The owner-gated route answers `403` with an EMPTY
|
|
206
|
+
* body by design (it must not tell a non-owner who the owner is), so the console
|
|
207
|
+
* looked authorized and said nothing at all.
|
|
208
|
+
*
|
|
209
|
+
* 🔴 The tab CANNOT detect this on its own. Session cookies are `HttpOnly`, and
|
|
210
|
+
* slot 0 — where the eviction is most likely, because it is the slot every
|
|
211
|
+
* ordinary sign-in takes — installs no client machinery at all.
|
|
212
|
+
*
|
|
213
|
+
* ── The fix: state the expectation, and let the server answer it ────────────
|
|
214
|
+
* The page knows exactly one thing the server does not: WHO IT RENDERED FOR. So
|
|
215
|
+
* it says so on every request, and the server compares. This is an `If-Match` on
|
|
216
|
+
* identity, and it gets `If-Match`'s status — **409 Conflict**: the request is
|
|
217
|
+
* well-formed and the credential is valid, but it conflicts with the current
|
|
218
|
+
* state of the browser's session. Never 401 (the person is signed in) and never
|
|
219
|
+
* 403 (they are not forbidden — they are somebody else now).
|
|
220
|
+
*
|
|
221
|
+
* The header is a HINT in exactly the sense the slot is: it can only ever make a
|
|
222
|
+
* request FAIL. A forged or stale value costs the sender their own request, and
|
|
223
|
+
* nothing downstream may read it as identity — the user is still resolved from
|
|
224
|
+
* the cookie, always, and the comparison happens after.
|
|
225
|
+
*/
|
|
226
|
+
/**
|
|
227
|
+
* The account id the CLIENT believes it is acting as — the one it drew on
|
|
228
|
+
* screen. Attached by the patched `fetch` in `accountSlot.client.ts`.
|
|
229
|
+
*
|
|
230
|
+
* 🔴 Never an authorization. It cannot widen access, name a different account
|
|
231
|
+
* or select a cookie; the only outcome it can produce that the request would
|
|
232
|
+
* not otherwise have had is {@link ACCOUNT_DRIFT_STATUS}.
|
|
233
|
+
*/
|
|
234
|
+
export const ACCOUNT_USER_HEADER = "x-account-user";
|
|
235
|
+
/**
|
|
236
|
+
* 409 Conflict — the credential is good, the identity behind it moved.
|
|
237
|
+
*
|
|
238
|
+
* Deliberately not 401 (which the SPAs answer by bouncing to a sign-in hop, and
|
|
239
|
+
* there is nothing to sign in to — the browser holds a perfectly good session)
|
|
240
|
+
* and not 403 (which reads as "you may not", and is the answer that produced
|
|
241
|
+
* the silent failure this exists to stop).
|
|
242
|
+
*/
|
|
243
|
+
export const ACCOUNT_DRIFT_STATUS = 409;
|
|
244
|
+
/** The machine-readable `error` a drift refusal carries, so a client can tell it
|
|
245
|
+
* apart from every other 409 an app already answers (a full slot jar, a name
|
|
246
|
+
* collision, an edit conflict). */
|
|
247
|
+
export const ACCOUNT_DRIFT_CODE = "account_changed";
|
|
248
|
+
export function accountDriftBody(expected, actual) {
|
|
249
|
+
return { error: ACCOUNT_DRIFT_CODE, expected, actual };
|
|
250
|
+
}
|
|
251
|
+
/** `true` for a body that is a drift refusal. Total — anything at all may be
|
|
252
|
+
* passed in, because this is called on a parsed response body. */
|
|
253
|
+
export function isAccountDriftBody(body) {
|
|
254
|
+
if (typeof body !== "object" || body === null)
|
|
255
|
+
return false;
|
|
256
|
+
const b = body;
|
|
257
|
+
return (b.error === ACCOUNT_DRIFT_CODE &&
|
|
258
|
+
typeof b.expected === "string" &&
|
|
259
|
+
typeof b.actual === "object" &&
|
|
260
|
+
b.actual !== null &&
|
|
261
|
+
typeof b.actual.id === "string");
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* The one rule, so the client's guess and the server's verdict cannot disagree.
|
|
265
|
+
*
|
|
266
|
+
* Drift is only ever declared when BOTH sides are known and they differ. An
|
|
267
|
+
* absent expectation (an old build, a subresource, a non-`fetch` caller, an app
|
|
268
|
+
* that never mounted the client module) is NOT drift — the guard has to be
|
|
269
|
+
* additive or it would start refusing requests the fleet has always allowed.
|
|
270
|
+
*/
|
|
271
|
+
export function isAccountDrift(expected, actual) {
|
|
272
|
+
return (expected !== null && expected !== "" && actual !== null && actual !== "" && expected !== actual);
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=accountSlots.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accountSlots.js","sourceRoot":"","sources":["../src/accountSlots.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;A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AAE,OAAO,KAAK,CAAC;IAC5D,MAAM,CAAC,GAAG,IAA+B,CAAC;IAC1C,OAAO,CACN,CAAC,CAAC,KAAK,KAAK,kBAAkB;QAC9B,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ;QAC9B,OAAO,CAAC,CAAC,MAAM,KAAK,QAAQ;QAC5B,CAAC,CAAC,MAAM,KAAK,IAAI;QACjB,OAAQ,CAAC,CAAC,MAAkC,CAAC,EAAE,KAAK,QAAQ,CAC5D,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,QAAuB,EAAE,MAAqB;IAC5E,OAAO,CACN,QAAQ,KAAK,IAAI,IAAI,QAAQ,KAAK,EAAE,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,EAAE,IAAI,QAAQ,KAAK,MAAM,CAC/F,CAAC;AACH,CAAC"}
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
export { clearSsoAutoState, decideSsoAutoSignIn, lastSsoAttemptAt, markSsoSignedOut, SSO_ATTEMPT_COOLDOWN_MS, SSO_SIGNOUT_PATH, SSO_START_PATH, type SsoAutoDecision, type SsoAutoSkipReason, ssoErrorFromLocation, ssoSignedOutHere, startSsoSignIn, startSsoSignOut, type SsoInitScriptTarget, suppressSsoAutoSignIn, takeSsoError, useSsoAutoSignIn, useSsoErrorNotice, } from "./ssoAutoSignIn";
|
|
2
|
+
export interface SatelliteUser {
|
|
3
|
+
id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
displayName?: string;
|
|
6
|
+
/** Registry-level admin (SSO apps only) — may manage users at the accounts
|
|
7
|
+
* satellite. Apps gate their own privileged surfaces on their own rules. */
|
|
8
|
+
isAdmin?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* 🔴 The OWNER, which is a strictly smaller set than `isAdmin` and not a
|
|
11
|
+
* synonym for it. Several apps have a surface that is one person's — a fleet
|
|
12
|
+
* limit everybody else's data lives under, a spend decision — and until
|
|
13
|
+
* 2026-08-12 the client had no way to ask, so those surfaces either showed to
|
|
14
|
+
* every admin or were hidden from everybody.
|
|
15
|
+
*
|
|
16
|
+
* It is a COURTESY, never a boundary: the server refuses the write regardless
|
|
17
|
+
* (`scopes` carries `owner` too — see `ssoConsumer.server.ts`). What this
|
|
18
|
+
* flag decides is whether a control is rendered, not whether it works.
|
|
19
|
+
*/
|
|
20
|
+
isOwner?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export type SatelliteAuthStatus = "loading" | "authenticated" | "anonymous";
|
|
23
|
+
export interface SatelliteAuth {
|
|
24
|
+
status: SatelliteAuthStatus;
|
|
25
|
+
user: SatelliteUser | null;
|
|
26
|
+
signIn(email: string, password: string): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Leave. At an SSO door this ends the session at BOTH ends — this app's
|
|
29
|
+
* cookie and the accounts satellite's — because ending only the first was
|
|
30
|
+
* never a sign-out: auth's session outlived it and the next hop returned the
|
|
31
|
+
* same person without asking for a password (2026-08-10).
|
|
32
|
+
*/
|
|
33
|
+
signOut(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Hand the machine to somebody else: the accounts satellite is asked for the
|
|
36
|
+
* credential form even though it holds a live session (`prompt=login`), and
|
|
37
|
+
* the app is re-entered as whoever signs in.
|
|
38
|
+
*
|
|
39
|
+
* Absent on adapters where the idea does not apply — a single-owner app has
|
|
40
|
+
* exactly one user, so there is nobody to switch to.
|
|
41
|
+
*/
|
|
42
|
+
switchUser?(): Promise<void>;
|
|
43
|
+
/** The accounts satellite's origin, when the server named one — where
|
|
44
|
+
* passwords, 2FA and the user list live. Absent/null on a door that is not
|
|
45
|
+
* an SSO door, which is how a UI knows not to offer any of it. */
|
|
46
|
+
accountsUrl?: string | null;
|
|
47
|
+
/** Bearer token for API calls, or null. Callable per-request — never a captured string. */
|
|
48
|
+
token(): string | null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* An authentication implementation. `useSession` is a React hook: it is called from
|
|
52
|
+
* inside `SatelliteAuthProvider`'s render, so an adapter is free to hold state, subscribe
|
|
53
|
+
* to a store, or fire effects exactly as any hook would.
|
|
54
|
+
*/
|
|
55
|
+
export interface AuthAdapter {
|
|
56
|
+
/** Stable id, for diagnostics ("which adapter is this build running?"). */
|
|
57
|
+
id: string;
|
|
58
|
+
/**
|
|
59
|
+
* What the sign-in surface should ASK for. Added 2026-07-27 with the
|
|
60
|
+
* password-only adapter: `signIn(email, password)` is the shape of a
|
|
61
|
+
* multi-account app, and a single-owner satellite has no email to check. A
|
|
62
|
+
* form that collects one anyway and then discards it is a lie about what the
|
|
63
|
+
* credential is, so the adapter says which fields are real and the UI renders
|
|
64
|
+
* exactly those. Defaults to `"email+password"`, which is what every existing
|
|
65
|
+
* caller already assumed.
|
|
66
|
+
*/
|
|
67
|
+
credential?: "email+password" | "password" | "sso";
|
|
68
|
+
useSession(): SatelliteAuth;
|
|
69
|
+
}
|
|
70
|
+
export interface SatelliteAuthProviderProps {
|
|
71
|
+
adapter: AuthAdapter;
|
|
72
|
+
children: React.ReactNode;
|
|
73
|
+
}
|
|
74
|
+
export declare function SatelliteAuthProvider({ adapter, children }: SatelliteAuthProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<SatelliteAuth | null>>;
|
|
75
|
+
export declare function useSatelliteAuth(): SatelliteAuth;
|
|
76
|
+
/**
|
|
77
|
+
* The real adapter for a satellite with ONE owner and one password
|
|
78
|
+
* (`cursedauth`'s `secureRequest` on the server side).
|
|
79
|
+
*
|
|
80
|
+
* ── Why there is no token here ──────────────────────────────────────────────
|
|
81
|
+
* `token()` returns null, always, and that is not a stub. The session is an
|
|
82
|
+
* `HttpOnly` cookie: the browser attaches it to same-origin requests by itself,
|
|
83
|
+
* and JavaScript is not able to read it — which is the point. The SPA and the
|
|
84
|
+
* API are served by the same origin, so nothing has to be threaded through.
|
|
85
|
+
* `token()` stays on the interface because the multi-account adapter (cc's
|
|
86
|
+
* bearer sessions, as `apps/orch` uses) genuinely needs it.
|
|
87
|
+
*
|
|
88
|
+
* ── Why status starts "loading" ─────────────────────────────────────────────
|
|
89
|
+
* A cookie session cannot be read synchronously, so the first render genuinely
|
|
90
|
+
* does not know. Rendering the sign-in form during that gap would flash a login
|
|
91
|
+
* screen at an owner who is already signed in, on every page load; rendering the
|
|
92
|
+
* app would flash private chrome at someone who is not. `"loading"` is the
|
|
93
|
+
* honest third answer and the app shell is expected to handle it.
|
|
94
|
+
*/
|
|
95
|
+
export interface OwnerPasswordAdapterOptions {
|
|
96
|
+
/** Base path of the three auth routes. Defaults to `/api/auth`. */
|
|
97
|
+
basePath?: string;
|
|
98
|
+
/** Injectable for tests. Defaults to the global `fetch`. */
|
|
99
|
+
fetchImpl?: typeof fetch;
|
|
100
|
+
}
|
|
101
|
+
/** The principal a password-only app is signed in AS. There is exactly one. */
|
|
102
|
+
export declare const OWNER_USER: SatelliteUser;
|
|
103
|
+
export declare function createOwnerPasswordAdapter(options?: OwnerPasswordAdapterOptions): AuthAdapter;
|
|
104
|
+
/** Token the mock hands out, so a dev API call carries a recognizably fake credential. */
|
|
105
|
+
export declare const MOCK_SESSION_TOKEN = "mock-session-token";
|
|
106
|
+
export declare const MOCK_ADAPTER_IN_PRODUCTION: string;
|
|
107
|
+
export interface MockAuthAdapterOptions {
|
|
108
|
+
user?: SatelliteUser;
|
|
109
|
+
/** Start signed out, to exercise the sign-in surface. Defaults to signed in. */
|
|
110
|
+
initialStatus?: Extract<SatelliteAuthStatus, "authenticated" | "anonymous">;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Development stand-in: authenticated immediately, no server involved.
|
|
114
|
+
*
|
|
115
|
+
* It REFUSES TO EXIST in a production build. This is a hard requirement, not a nicety —
|
|
116
|
+
* an adapter that reports `authenticated` unconditionally on a public subdomain is an
|
|
117
|
+
* open door. In a Vite production bundle `import.meta.env.DEV` is compiled to `false`, so
|
|
118
|
+
* this throw cannot be switched off from the outside.
|
|
119
|
+
*/
|
|
120
|
+
export declare function createMockAuthAdapter(options?: MockAuthAdapterOptions): AuthAdapter;
|
|
121
|
+
/** What `/api/auth/session` answers in an SSO app (`cursedauth/sso-consumer`
|
|
122
|
+
* on the server side). Exported so app UIs can read `accountsUrl` for their
|
|
123
|
+
* "Manage account" link without a second shape. */
|
|
124
|
+
export interface SsoSessionInfo {
|
|
125
|
+
authenticated: boolean;
|
|
126
|
+
user?: {
|
|
127
|
+
id: string;
|
|
128
|
+
email: string;
|
|
129
|
+
name: string;
|
|
130
|
+
isAdmin: boolean;
|
|
131
|
+
isOwner?: boolean;
|
|
132
|
+
};
|
|
133
|
+
/** The accounts satellite's origin — where passwords live. */
|
|
134
|
+
accountsUrl?: string;
|
|
135
|
+
}
|
|
136
|
+
export declare function fetchSsoSession(fetchImpl?: typeof fetch, basePath?: string): Promise<SsoSessionInfo>;
|
|
137
|
+
export interface SsoRedirectAdapterOptions extends OwnerPasswordAdapterOptions {
|
|
138
|
+
/**
|
|
139
|
+
* Sign in automatically when the session probe says anonymous and the
|
|
140
|
+
* accounts satellite is the door (2026-08-01, owner ask: "don't show me a
|
|
141
|
+
* button when I'm already authorized"). Defaults to ON.
|
|
142
|
+
*
|
|
143
|
+
* The guards that keep it from becoming a redirect loop — and the four
|
|
144
|
+
* situations that suppress it — live in `ssoAutoSignIn.ts`, shared with the
|
|
145
|
+
* satellites that do not use this adapter. Pass `false` to get the old
|
|
146
|
+
* click-to-start behavior back.
|
|
147
|
+
*/
|
|
148
|
+
autoSignIn?: boolean;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The adapter for a satellite whose users live at the accounts satellite
|
|
152
|
+
* (`apps/auth`). There is no password form here to render — `credential:
|
|
153
|
+
* "sso"` tells the sign-in surface to draw ONE button, and `signIn()` performs
|
|
154
|
+
* a full-page navigation to `/api/auth/sso/start` (a redirect chain cannot be
|
|
155
|
+
* ridden by `fetch`; the browser itself must carry the auth cookie across
|
|
156
|
+
* origins and follow the grant back). The email/password arguments exist on the
|
|
157
|
+
* interface for the other adapters and are ignored here.
|
|
158
|
+
*
|
|
159
|
+
* Since 2026-08-01 the button usually presses itself: an anonymous probe at an
|
|
160
|
+
* SSO door starts the hop immediately and `status` STAYS `"loading"` across the
|
|
161
|
+
* navigation, so the shell shows its loading view rather than flashing a
|
|
162
|
+
* sign-in screen at someone who is already signed in. `"anonymous"` now means
|
|
163
|
+
* something stronger than it did — the automatic path was tried and declined,
|
|
164
|
+
* so the surface it renders is the one that can actually help.
|
|
165
|
+
*/
|
|
166
|
+
export declare function createSsoRedirectAdapter(options?: SsoRedirectAdapterOptions): AuthAdapter;
|
|
167
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AA0DA,OAAO,EACN,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,uBAAuB,EACvB,gBAAgB,EAChB,cAAc,EACd,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,oBAAoB,EACpB,gBAAgB,EAChB,cAAc,EACd,eAAe,EACf,KAAK,mBAAmB,EACxB,qBAAqB,EACrB,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,GACjB,MAAM,iBAAiB,CAAC;AAEzB,MAAM,WAAW,aAAa;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;iFAC6E;IAC7E,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,eAAe,GAAG,WAAW,CAAC;AAE5E,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,mBAAmB,CAAC;IAC5B,IAAI,EAAE,aAAa,GAAG,IAAI,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