@oxyhq/contracts 0.11.0 → 0.13.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,27 +1,15 @@
1
1
  /**
2
- * Device-first bootstrap & token contracts (auth centralization, wave 1).
2
+ * First-party login result contract.
3
3
  *
4
- * SINGLE SOURCE OF TRUTH for the wire shape of the new device-first session
5
- * bootstrap: the top-level `#oxy_boot=…` fragment the API hands back from
6
- * `GET /auth/device/bootstrap`, the token bundle a boot code / web-session
7
- * fast-path exchanges into, the persisted-refresh rotation, the native
8
- * device-token issuance, the IdP chooser's device-resolve, and the first-party
9
- * password login result (2FA arm vs. session arm). The API validates its OUTPUT
10
- * against these schemas; every consumer (`@oxyhq/core`'s device-boot mixin, the
11
- * SDK cold boot, the IdP chooser) validates its INPUT against the same
12
- * definitions, so producer and consumers cannot drift.
4
+ * SINGLE SOURCE OF TRUTH for the first-party password login result (2FA arm vs.
5
+ * session arm). The API validates its OUTPUT against this schema; every consumer
6
+ * (`@oxyhq/core`'s auth mixin) validates its INPUT against the same definition,
7
+ * so producer and consumers cannot drift.
13
8
  *
14
- * Design anchors (from the auth-centralization plan):
15
- * - The bootstrap fragment carries NO tokens and NO deviceId — only a
16
- * `state` echo (CSRF), a `reason`, a short-lived single-use `code`, and an
17
- * opaque `deviceToken`. Tokens are obtained by exchanging the `code` at
18
- * `POST /auth/device/exchange` (origin-bound GETDEL burn).
19
- * - Refresh is ONE rotating, single-use family shared by web and native.
20
- * - `loginResult` mirrors what `POST /auth/login` returns today
21
- * (`buildSessionAuthResponse` in the API's `session.controller.ts`): either a
22
- * 2FA challenge (`{ twoFactorRequired: true, loginToken }`) or a session
23
- * payload. The session arm matches `SessionAuthResponse` EXACTLY, plus an
24
- * optional `refreshToken` the new server adds for the persisted-refresh lane.
9
+ * The device transport is `deviceId` + `deviceSecret` + `POST /session/device/token`
10
+ * (see `deviceSession.ts`). The legacy cookie/bootstrap/refresh-family lanes were
11
+ * removed in the zero-cookie cutover — nothing here carries a refresh token or a
12
+ * boot fragment.
25
13
  *
26
14
  * Nested-object response shapes are declared as explicit `interface`s with the
27
15
  * runtime schema annotated `z.ZodType<Interface>` — the same rationale as
@@ -34,127 +22,25 @@
34
22
  * `require()`).
35
23
  */
36
24
  import { z } from 'zod';
37
- import { userResponseSchema } from './userResponse.js';
38
- /* -------------------------------------------------------------------------- */
39
- /* Bootstrap fragment */
40
- /* -------------------------------------------------------------------------- */
41
- /**
42
- * Why the bootstrap hop resolved the way it did.
43
- * - `session` — the device cookie resolved an active session; a `code` is
44
- * present to exchange for tokens.
45
- * - `no_session` — the device is known but has no active session; no `code`.
46
- * - `new_device` — first contact; the cookie was just planted, no session yet.
47
- */
48
- export const deviceBootReasonSchema = z.enum(['session', 'no_session', 'new_device']);
49
- /**
50
- * The `#oxy_boot=<json>` fragment `GET /auth/device/bootstrap` appends to the
51
- * `return_to` URL. Carries the CSRF `state` echo, the resolution `reason`, the
52
- * opaque `deviceToken`, and — ONLY on the `session` arm — the single-use
53
- * exchange `code`. NEVER carries tokens or a deviceId.
54
- *
55
- * Discriminated on `reason` so the code↔reason coupling is enforced by the
56
- * schema, not by the consumer: the `session` arm REQUIRES `code`, and the
57
- * `no_session` / `new_device` arms omit it (a stray `code` on those arms is
58
- * stripped). A `session` fragment WITHOUT a code therefore fails to parse and
59
- * is treated as "no usable fragment" rather than half-processed.
60
- */
61
- const deviceBootFragmentBase = {
62
- v: z.literal(1),
63
- state: z.string().min(1).max(256),
64
- deviceToken: z.string().min(20).max(512),
65
- };
66
- export const deviceBootFragmentSchema = z.discriminatedUnion('reason', [
67
- z.object({
68
- ...deviceBootFragmentBase,
69
- reason: z.literal('session'),
70
- code: z.string().min(20).max(128),
71
- }),
72
- z.object({
73
- ...deviceBootFragmentBase,
74
- reason: z.literal('no_session'),
75
- }),
76
- z.object({
77
- ...deviceBootFragmentBase,
78
- reason: z.literal('new_device'),
79
- }),
80
- ]);
81
- /* -------------------------------------------------------------------------- */
82
- /* Boot-code exchange */
83
- /* -------------------------------------------------------------------------- */
84
- /** Request body for `POST /auth/device/exchange` — the single-use boot code. */
85
- export const deviceExchangeRequestSchema = z.object({
86
- code: z.string().min(20).max(128),
87
- });
88
- export const authTokenBundleSchema = z.object({
89
- sessionId: z.string(),
90
- accessToken: z.string(),
91
- refreshToken: z.string(),
92
- expiresAt: z.string(),
93
- user: userResponseSchema,
94
- });
95
- // Internal (unexported) arm schemas — PLAIN `z.object` literals (no
96
- // `z.ZodType<>` annotation) so `z.discriminatedUnion` can introspect the
97
- // `reason` discriminator. The node10 `.d.ts`-degradation safety comes from the
98
- // EXPORTED symbols instead: the public types are explicit interfaces
99
- // (`WebSessionSession` / `WebSessionNoSession` / `WebSessionResult`) and the
100
- // exported schema is annotated `z.ZodType<WebSessionResult>` below, so the
101
- // emitted declaration states the shape literally rather than a degradable
102
- // `z.infer<>` of the nested `session` object.
103
- const webSessionSessionSchema = z.object({
104
- reason: z.literal('session'),
105
- session: authTokenBundleSchema,
106
- deviceToken: z.string().min(1),
107
- });
108
- const webSessionNoSessionSchema = z.object({
109
- reason: z.enum(['no_session', 'new_device']),
110
- deviceToken: z.string().min(1),
111
- });
112
- // Discriminated on `reason` — a true `discriminatedUnion` (not `z.union`): it
113
- // dispatches on the discriminator instead of sequentially probing each arm,
114
- // giving precise per-arm errors.
115
- export const webSessionResultSchema = z.discriminatedUnion('reason', [
116
- webSessionSessionSchema,
117
- webSessionNoSessionSchema,
118
- ]);
119
- /* -------------------------------------------------------------------------- */
120
- /* Refresh-token rotation (web + native, one implementation) */
121
- /* -------------------------------------------------------------------------- */
122
- /** Request body for `POST /auth/refresh-token` — the current refresh token. */
123
- export const tokenRefreshRequestSchema = z.object({
124
- refreshToken: z.string().min(20),
125
- });
126
- /**
127
- * Wire shape of `POST /auth/refresh-token`: the rotated (single-use) family —
128
- * a new access token, the next refresh token, the new access-token expiry, and
129
- * the owning session id. `expiresAt` is an ISO string.
130
- */
131
- export const tokenRefreshResponseSchema = z.object({
132
- accessToken: z.string(),
133
- refreshToken: z.string(),
134
- expiresAt: z.string(),
135
- sessionId: z.string(),
136
- });
137
- /* -------------------------------------------------------------------------- */
138
- /* Native device-token issuance */
139
- /* -------------------------------------------------------------------------- */
140
- /**
141
- * Wire shape of `POST /auth/device/token` — issues (or rotates) the opaque
142
- * device token for the native channel. The deviceId is taken from the bearer
143
- * JWT claims server-side; only the token comes back.
144
- */
145
- export const deviceTokenIssueResponseSchema = z.object({
146
- deviceToken: z.string(),
147
- });
148
25
  const loginTwoFactorRequiredSchema = z.object({
149
26
  twoFactorRequired: z.literal(true),
150
27
  loginToken: z.string(),
151
28
  });
29
+ const securityAlertSchema = z.object({
30
+ message: z.string(),
31
+ anomalies: z.array(z.object({
32
+ type: z.string(),
33
+ reason: z.string(),
34
+ details: z.string().optional(),
35
+ })),
36
+ });
152
37
  const loginSessionResultSchema = z.object({
153
38
  sessionId: z.string(),
154
39
  deviceId: z.string(),
155
40
  expiresAt: z.string(),
156
41
  accessToken: z.string().optional(),
157
- refreshToken: z.string().optional(),
42
+ deviceSecret: z.string().optional(),
43
+ securityAlert: securityAlertSchema.optional(),
158
44
  user: z.object({
159
45
  id: z.string(),
160
46
  username: z.string().optional(),
@@ -165,24 +51,3 @@ export const loginResultSchema = z.union([
165
51
  loginTwoFactorRequiredSchema,
166
52
  loginSessionResultSchema,
167
53
  ]);
168
- /* -------------------------------------------------------------------------- */
169
- /* IdP chooser device-resolve */
170
- /* -------------------------------------------------------------------------- */
171
- /**
172
- * Request body for `POST /auth/device/resolve` (X-Oxy-Internal, called by the
173
- * IdP chooser) — the device key the chooser read from the first-party
174
- * `oxy_device` cookie.
175
- */
176
- export const deviceResolveRequestSchema = z.object({
177
- deviceKey: z.string().min(20),
178
- });
179
- const deviceResolveAccountSchema = z.object({
180
- user: userResponseSchema,
181
- sessionId: z.string(),
182
- accessToken: z.string(),
183
- expiresAt: z.string(),
184
- });
185
- export const deviceResolveResponseSchema = z.object({
186
- activeAccountId: z.string().nullable(),
187
- accounts: z.array(deviceResolveAccountSchema),
188
- });
@@ -20,3 +20,30 @@ export const deviceSessionSyncSchema = z.object({
20
20
  state: deviceSessionStateSchema,
21
21
  activeToken: activeTokenSchema.nullable(),
22
22
  });
23
+ /* -------------------------------------------------------------------------- */
24
+ /* Device-secret token mint (phase 2c — zero-cookie transport) */
25
+ /* -------------------------------------------------------------------------- */
26
+ /**
27
+ * Request body for `POST /session/device/token` — the client presents the
28
+ * `deviceId` it stored first-party plus the opaque `deviceSecret`. NO bearer:
29
+ * possession of the secret IS the proof of device ownership. The server matches
30
+ * `sha256(deviceSecret)` against the device's stored `secretHash` (constant-time)
31
+ * and mints a short access token for the device's active account.
32
+ */
33
+ export const deviceTokenMintRequestSchema = z.object({
34
+ deviceId: z.string().min(1),
35
+ deviceSecret: z.string().min(1),
36
+ });
37
+ /**
38
+ * Wire shape of a successful `POST /session/device/token`: the freshly-minted
39
+ * short access token for the active account, its expiry, the NEXT rotating
40
+ * device secret the client must persist (rotation-in-use — the presented secret
41
+ * stays valid for a short grace so multi-tab races don't lock out), and the
42
+ * projected device-session state.
43
+ */
44
+ export const deviceTokenMintResponseSchema = z.object({
45
+ accessToken: z.string(),
46
+ expiresAt: z.string(),
47
+ nextDeviceSecret: z.string(),
48
+ state: deviceSessionStateSchema,
49
+ });
package/dist/esm/index.js CHANGED
@@ -40,7 +40,7 @@ credentialRecordSchema, verifiableCredentialResponseSchema, credentialIssueResul
40
40
  export {
41
41
  // Schemas
42
42
  linkPreviewSchema, linkPreviewBatchRequestSchema, linkPreviewBatchResponseSchema, linkPreviewResponseSchema, } from './links.js';
43
- export { sessionAccountSchema, deviceSessionStateSchema, activeTokenSchema, deviceSessionSyncSchema, } from './deviceSession.js';
43
+ export { sessionAccountSchema, deviceSessionStateSchema, activeTokenSchema, deviceSessionSyncSchema, deviceTokenMintRequestSchema, deviceTokenMintResponseSchema, } from './deviceSession.js';
44
44
  export {
45
45
  // Schemas
46
- deviceBootReasonSchema, deviceBootFragmentSchema, deviceExchangeRequestSchema, authTokenBundleSchema, webSessionResultSchema, tokenRefreshRequestSchema, tokenRefreshResponseSchema, deviceTokenIssueResponseSchema, loginResultSchema, deviceResolveRequestSchema, deviceResolveResponseSchema, } from './deviceBoot.js';
46
+ loginResultSchema, } from './deviceBoot.js';
@@ -16,8 +16,7 @@
16
16
  *
17
17
  * Faithful to the producers:
18
18
  * - `packages/api/src/utils/userTransform.ts` `formatUserResponse` — the
19
- * canonical serialization used by the device-first bootstrap/exchange
20
- * endpoints (`deviceAuth.ts`), login/signup, device sessions, etc.
19
+ * canonical serialization used by login/signup, device sessions, etc.
21
20
  * Emits `id` (NOT `_id`), forwards `username` verbatim (may be absent), and
22
21
  * emits `name` as the structured `{ first, last, full, displayName }`
23
22
  * subdocument.