@oxy.so/contracts 1.5.0 → 2.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.
@@ -1,212 +0,0 @@
1
- import { z } from 'zod';
2
- import { deviceDirectorySchema } from './deviceDirectory.js';
3
- /**
4
- * The browser DeviceSession hub at `auth.oxy.so` (issue #937, Phase 5, ADR 0003).
5
- *
6
- * Two wire surfaces live in this file and they are deliberately different
7
- * shapes, because they answer to different callers:
8
- *
9
- * - The `browserHub*` schemas below the first divider are the API's
10
- * (`api.oxy.so/session/browser-hub/*`), spoken ONLY by the IdP's own
11
- * server/edge layer. They carry the raw handle and a bearer, so nothing that
12
- * speaks them may run in a browser.
13
- * - The `hub*` schemas below the second divider are the EDGE's
14
- * (`auth.oxy.so/hub/*`), spoken by the IdP SPA. They carry neither: the
15
- * handle stays in an `HttpOnly` cookie the script cannot read, and the
16
- * device-wide bearer stays at the edge.
17
- *
18
- * That split is the whole point of the phase. A single schema covering both
19
- * would let a refactor move a credential across the boundary without any type
20
- * changing shape.
21
- *
22
- * ## What is NOT reopened here
23
- *
24
- * Relying-party origins remain zero-cookie: they keep `{deviceId,
25
- * deviceSecret}` + `POST /session/device/token` and set no cookie of any kind.
26
- * `auth.oxy.so` alone holds a handle, first-party only. There is no
27
- * refresh-token family and no bootstrap hop.
28
- */
29
- /* -------------------------------------------------------------------------- */
30
- /* The cookie */
31
- /* -------------------------------------------------------------------------- */
32
- /**
33
- * The one cookie name, `__Host-` prefixed.
34
- *
35
- * The prefix is not decoration: a browser refuses to store a `__Host-` cookie
36
- * that carries a `Domain` attribute or a `Path` other than `/`, or that arrives
37
- * without `Secure`. So the name itself is what makes "bound to `auth.oxy.so`
38
- * alone, readable by no other `oxy.so` host" enforced by the client rather than
39
- * merely intended by the server — including against a compromised sibling
40
- * subdomain, which is the specific attack `Domain=.oxy.so` would have opened.
41
- */
42
- export const BROWSER_HUB_COOKIE_NAME = '__Host-oxy-device';
43
- /**
44
- * The exact attribute set the edge sets, in the order it writes them.
45
- *
46
- * Held as data rather than as a template string so a test can assert the set
47
- * rather than a rendered line, and so removing one attribute is a diff to a
48
- * named constant instead of an edit inside a string literal.
49
- *
50
- * `Max-Age` is deliberately NOT in this list — see
51
- * {@link BROWSER_HUB_HANDLE_TTL_MS}, which the edge appends. Everything here is
52
- * a SECURITY attribute and none of them is ever conditional.
53
- */
54
- export const BROWSER_HUB_COOKIE_ATTRIBUTES = ['Secure', 'HttpOnly', 'SameSite=Lax', 'Path=/'];
55
- /**
56
- * How long a hub handle lives, server-side and in the cookie alike.
57
- *
58
- * Thirty days. The cookie's `Max-Age` is derived from this same constant so the
59
- * two cannot disagree: a cookie outliving its credential produces a browser that
60
- * believes it is signed in and is refused on every call, and a credential
61
- * outliving its cookie leaves an un-addressable row alive on the server.
62
- *
63
- * A hub handle is NOT a session cookie (one with no `Max-Age`, discarded when
64
- * the browser closes). It cannot be: the thing it identifies is the browser
65
- * PROFILE's device session, and a device session that evaporates when the user
66
- * quits their browser would send them back to a QR scan every morning — the
67
- * exact failure ADR 0003 exists to remove.
68
- */
69
- export const BROWSER_HUB_HANDLE_TTL_MS = 30 * 24 * 60 * 60 * 1000;
70
- /* -------------------------------------------------------------------------- */
71
- /* api.oxy.so/session/browser-hub/* — spoken by the edge, never a browser */
72
- /* -------------------------------------------------------------------------- */
73
- /**
74
- * The raw handle, as it travels between the edge and the API.
75
- *
76
- * An opaque base64url random value and NOTHING else — no token, no user id, no
77
- * device id, no account id, no serialized state. The server stores only
78
- * `sha256(handle)`, so a dump of the column cannot address a browser.
79
- *
80
- * The minimum length is a floor against an empty or truncated value reaching a
81
- * hash comparison, not a statement about the entropy: that is fixed by the
82
- * issuer (`BROWSER_HUB_HANDLE_BYTES` in the API), and a handle is the only
83
- * credential in this flow, so it is 256 bits.
84
- */
85
- export const browserHubHandleSchema = z.string().min(32);
86
- /** Request body of every handle-presenting API endpoint. */
87
- export const browserHubHandleRequestSchema = z.object({
88
- handle: browserHubHandleSchema,
89
- });
90
- /**
91
- * Response of `POST /session/browser-hub/establish` and `.../rotate`.
92
- *
93
- * The raw handle is returned exactly ONCE, to the edge, which puts it straight
94
- * into the cookie and keeps no copy. It is never logged, never re-readable, and
95
- * never reaches the browser's script context.
96
- */
97
- export const browserHubHandleResponseSchema = z.object({
98
- handle: browserHubHandleSchema,
99
- expiresAt: z.string(),
100
- });
101
- /**
102
- * Response of `POST /session/browser-hub/resolve` — the browser's device
103
- * session, resolved from the handle alone.
104
- *
105
- * Carries a bearer because the edge needs one to run the authorize lane on the
106
- * browser's behalf. It is the ordinary short-lived access token of the device's
107
- * active context, and it stops at the edge.
108
- */
109
- export const browserHubResolveResponseSchema = z.object({
110
- accessToken: z.string().min(1),
111
- expiresAt: z.string(),
112
- directory: deviceDirectorySchema,
113
- });
114
- /**
115
- * Why a handle did not resolve. A CLOSED set, and deliberately coarse.
116
- *
117
- * - `invalid_handle` — unknown, expired, or revoked. One code for all
118
- * three: distinguishing them would tell a caller
119
- * holding a guessed value whether it ever existed.
120
- * - `no_active_session` — the handle is good and the device has nothing live
121
- * to mint for. The credential is NOT revoked; the
122
- * browser re-authenticates and keeps its cookie.
123
- */
124
- export const browserHubErrorSchema = z.enum(['invalid_handle', 'no_active_session']);
125
- /** Response of `POST /session/browser-hub/revoke`. Idempotent by construction. */
126
- export const browserHubRevokeResponseSchema = z.object({
127
- revoked: z.boolean(),
128
- });
129
- /* -------------------------------------------------------------------------- */
130
- /* auth.oxy.so/hub/* — spoken by the IdP SPA */
131
- /* -------------------------------------------------------------------------- */
132
- /**
133
- * What the SPA is allowed to know about the hub session.
134
- *
135
- * `signed_out` covers "no cookie", "cookie present but the handle no longer
136
- * resolves" and "resolved, but the device has nothing live" — from the script's
137
- * side those are one state, and collapsing them here is what stops a UI from
138
- * branching on a distinction it must not act on.
139
- *
140
- * `active` carries the DIRECTORY and no credential. A switcher renders from it;
141
- * nothing in it can be spent against the API. The bearer that produced it never
142
- * left the edge.
143
- */
144
- export const hubSessionSchema = z.discriminatedUnion('status', [
145
- z.object({ status: z.literal('signed_out') }),
146
- z.object({ status: z.literal('active'), directory: deviceDirectorySchema }),
147
- ]);
148
- /** Request body of `POST /hub/claim` — the Commons approval lane's handoff. */
149
- export const hubClaimRequestSchema = z.object({
150
- /**
151
- * The secret `sessionToken` of an approved `AuthSession`, held only by the
152
- * page that created it. The edge spends it server-side: the access token and
153
- * device secret the claim yields are consumed to establish the hub and are
154
- * then discarded, so neither is ever returned to the script.
155
- */
156
- sessionToken: z.string().min(1),
157
- });
158
- /** Request body of `POST /hub/activate` — pick the globally active context. */
159
- export const hubActivateRequestSchema = z.object({
160
- contextId: z.string().min(1),
161
- });
162
- /**
163
- * Request body of `POST /hub/authorize` — a later official origin joining.
164
- *
165
- * There is no `prompt` field, and that is not an omission. `'none'` is absent
166
- * from `buildOAuthAuthorizeUrl`'s union in `@oxy.so/core` precisely so a silent
167
- * loop cannot be rebuilt in one line, and this endpoint would be the second
168
- * place to rebuild it. A caller that needs a login or consent prompt gets one
169
- * by not passing `approve`.
170
- */
171
- export const hubAuthorizeRequestSchema = z.object({
172
- clientId: z.string().min(1),
173
- redirectUri: z.string().url(),
174
- state: z.string().optional(),
175
- codeChallenge: z.string().min(1),
176
- /** S256 only. `plain` is refused before the request leaves the edge. */
177
- codeChallengeMethod: z.literal('S256'),
178
- scope: z.string().optional(),
179
- /**
180
- * The user's explicit answer on the consent screen.
181
- *
182
- * Absent or `false` means "tell me whether consent is needed"; the edge then
183
- * returns `consent_required` and mints nothing. Only `true` may mint, and only
184
- * the consent screen's own button sends it — which is why the decision of
185
- * WHETHER consent is required is re-read from the server on both passes rather
186
- * than remembered from the first.
187
- */
188
- approve: z.boolean().optional(),
189
- });
190
- /**
191
- * Result of `POST /hub/authorize`.
192
- *
193
- * `signed_out` is the honest answer for a browser with no hub session: the SPA
194
- * runs the ordinary Commons-first authentication and tries again. It is never a
195
- * redirect the edge performs on the browser's behalf — no automatic chain
196
- * across Oxy origins, and nothing here can be hidden inside an iframe.
197
- */
198
- export const hubAuthorizeResultSchema = z.discriminatedUnion('status', [
199
- z.object({ status: z.literal('signed_out') }),
200
- z.object({
201
- status: z.literal('consent_required'),
202
- reason: z.enum(['new', 'scope_changed']),
203
- userConsentScopes: z.array(z.string()).optional(),
204
- }),
205
- z.object({
206
- status: z.literal('code'),
207
- code: z.string().min(1),
208
- state: z.string().nullable(),
209
- redirectUri: z.string(),
210
- expiresIn: z.number().int().positive(),
211
- }),
212
- ]);