@oxyhq/contracts 0.14.2 → 0.16.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,10 +1,11 @@
1
1
  /**
2
2
  * First-party login result contract.
3
3
  *
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.
4
+ * SINGLE SOURCE OF TRUTH for the first-party login result (the session arm). The
5
+ * API validates its OUTPUT against this schema; every consumer (`@oxyhq/core`'s
6
+ * auth mixin) validates its INPUT against the same definition, so producer and
7
+ * consumers cannot drift. Sign-in is passkey (WebAuthn) or Commons handoff —
8
+ * password and 2FA were removed, so the only outcome is a completed session.
8
9
  *
9
10
  * The device transport is `deviceId` + `deviceSecret` + `POST /session/device/token`
10
11
  * (see `deviceSession.ts`). The legacy cookie/bootstrap/refresh-family lanes were
@@ -22,10 +23,6 @@
22
23
  * `require()`).
23
24
  */
24
25
  import { z } from 'zod';
25
- const loginTwoFactorRequiredSchema = z.object({
26
- twoFactorRequired: z.literal(true),
27
- loginToken: z.string(),
28
- });
29
26
  const securityAlertSchema = z.object({
30
27
  message: z.string(),
31
28
  anomalies: z.array(z.object({
@@ -47,7 +44,4 @@ const loginSessionResultSchema = z.object({
47
44
  avatar: z.string().optional(),
48
45
  }),
49
46
  });
50
- export const loginResultSchema = z.union([
51
- loginTwoFactorRequiredSchema,
52
- loginSessionResultSchema,
53
- ]);
47
+ export const loginResultSchema = loginSessionResultSchema;
@@ -187,7 +187,7 @@ export const domainVerificationInstructionsSchema = z.object({
187
187
  }),
188
188
  });
189
189
  export const authMethodEntrySchema = z.object({
190
- type: z.enum(['identity', 'password', 'google', 'apple', 'github', 'webauthn']),
190
+ type: z.enum(['identity', 'webauthn']),
191
191
  linkedAt: z.union([z.string(), z.date()]),
192
192
  verificationMethodId: z.string().optional(),
193
193
  credentialId: z.string().optional(),
package/dist/esm/index.js CHANGED
@@ -43,6 +43,12 @@ export {
43
43
  // Schemas
44
44
  loginResultSchema, } from './deviceBoot.js';
45
45
  export {
46
+ // Schemas
47
+ rotateKeyChallengeResponseSchema, rotateKeyCompleteRequestSchema, rotateKeyCompleteResponseSchema, } from './keyRotation.js';
48
+ export {
49
+ // Schemas — encrypted off-device identity backup (b3 Feature 1)
50
+ backupLookupIdSchema, encryptedBackupEnvelopeSchema, backupUploadRequestSchema, backupStatusResponseSchema, } from './keyRecovery.js';
51
+ export {
46
52
  // Shared primitives
47
53
  updatePlatformSchema, updateStatusSchema, updateAssetStatusSchema, sha256HexSchema, channelNameSchema, runtimeVersionSchema, rolloutPercentSchema,
48
54
  // Assets: init + complete
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Encrypted off-device identity backup contract (b3 Feature 1).
3
+ *
4
+ * SINGLE SOURCE OF TRUTH for the "encrypted identity backup" flow, where a
5
+ * client stores an encrypted copy of its self-custody identity key off-device so
6
+ * a lost/wiped device can be recovered from the recovery phrase ALONE — without
7
+ * the platform ever seeing the phrase, the derived encryption key, or the
8
+ * plaintext private key.
9
+ *
10
+ * Zero-knowledge design (mirrors the zero-cookie `DeviceSession.secretHash`
11
+ * pattern): the client derives, from the FULL 64-byte BIP-39 seed, both an
12
+ * encryption key (`backupKey`) and a locator (`lookupId`) via HKDF with
13
+ * domain-separated `info` labels. It uploads ONLY the XChaCha20-Poly1305
14
+ * ciphertext plus the raw `lookupId`; the server stores the ciphertext and
15
+ * `sha256(lookupId)` (never the raw `lookupId`). Restoration re-derives both
16
+ * from the phrase, fetches the envelope by `lookupId`, and decrypts locally.
17
+ *
18
+ * The server can neither locate a backup (it lacks the seed to compute the
19
+ * lookup id) nor decrypt one (it lacks the seed to compute the backup key) — a
20
+ * DB dump yields only opaque ciphertext keyed by an un-invertible hash.
21
+ *
22
+ * The producer (`@oxyhq/api`) validates its request/response against these
23
+ * schemas; the consumer (`@oxyhq/core` identity-backup mixin) validates its
24
+ * input against the same definitions, so the wire shape cannot drift.
25
+ *
26
+ * All shapes here are FLAT (no nested objects), so `z.infer<>` is safe under a
27
+ * consumer's node10 `moduleResolution`. Platform-agnostic — zod only, ESM-safe
28
+ * (no `require()`).
29
+ */
30
+ import { z } from 'zod';
31
+ /** 256-bit backup locator (32 bytes), lowercase/uppercase hex. */
32
+ export const backupLookupIdSchema = z
33
+ .string()
34
+ .trim()
35
+ .regex(/^[0-9a-fA-F]{64}$/, 'lookupId must be 64 hex characters');
36
+ /**
37
+ * The stored, self-describing encrypted backup as it lives at rest and travels
38
+ * on the public restore endpoint. Contains NO secret and NO locator: the
39
+ * `lookupId` is uploaded separately (see {@link backupUploadRequestSchema}) and
40
+ * only its hash is ever persisted.
41
+ *
42
+ * - `version` — envelope/KDF version, so a future scheme migration is
43
+ * distinguishable at rest.
44
+ * - `algorithm` — the AEAD used. Pinned literal so a mismatched decryptor
45
+ * fails loudly rather than silently.
46
+ * - `kdfInfo` — the HKDF `info` label used to derive the encryption key
47
+ * (domain-separation tag; documents exactly which context produced the key).
48
+ * - `nonce` — the 24-byte XChaCha20-Poly1305 nonce, hex.
49
+ * - `ciphertext` — the encrypted `{privateKey, publicKey, createdAt}` payload
50
+ * with the appended Poly1305 tag, hex.
51
+ * - `publicKeyHint` — a short, non-sensitive prefix of the backed-up identity's
52
+ * public key, so the owner can recognise WHICH identity a backup belongs to
53
+ * without exposing the full key. Bound into the AEAD associated data.
54
+ * - `createdAt` — ISO-8601 creation timestamp.
55
+ */
56
+ export const encryptedBackupEnvelopeSchema = z.object({
57
+ version: z.number().int().positive(),
58
+ algorithm: z.literal('xchacha20poly1305'),
59
+ kdfInfo: z.string().min(1),
60
+ nonce: z.string().trim().min(1),
61
+ ciphertext: z.string().trim().min(1),
62
+ publicKeyHint: z.string().trim().min(1),
63
+ createdAt: z.string().trim().min(1),
64
+ });
65
+ /**
66
+ * Request body of `POST /identity/backup` — the envelope PLUS the raw
67
+ * `lookupId`. The server sha256-hashes `lookupId` before storing it (it never
68
+ * persists the raw value), and upserts by the authenticated user id so a
69
+ * re-upload REPLACES the prior backup rather than accumulating duplicates.
70
+ */
71
+ export const backupUploadRequestSchema = encryptedBackupEnvelopeSchema.extend({
72
+ /**
73
+ * The raw 256-bit backup locator (hex), derived client-side from the seed
74
+ * with a domain-separated HKDF `info`. The server stores ONLY its sha256; a
75
+ * DB dump therefore cannot recompute a locator to enumerate backups.
76
+ */
77
+ lookupId: backupLookupIdSchema,
78
+ });
79
+ /**
80
+ * Response of `GET /identity/backup/status` (and the write/delete acks): whether
81
+ * the authenticated user has a stored backup, plus the non-sensitive hint +
82
+ * timestamp when one exists. Carries no ciphertext and no locator.
83
+ */
84
+ export const backupStatusResponseSchema = z.object({
85
+ exists: z.boolean(),
86
+ publicKeyHint: z.string().optional(),
87
+ createdAt: z.string().optional(),
88
+ });
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Key-rotation contract (b3 Feature 3 — key rotation + last-credential replacement).
3
+ *
4
+ * SINGLE SOURCE OF TRUTH for the atomic key-rotation flow:
5
+ * - `POST /auth/rotate/challenge` — mint a single-use `rotate_key` challenge.
6
+ * - `POST /auth/rotate/complete` — prove control of the CURRENT (old) key and
7
+ * atomically swap in the new one.
8
+ *
9
+ * Rotation is an atomic REPLACE of the single identity key, never a
10
+ * remove-then-add, so it never passes through a zero-auth-method state and is
11
+ * independent of the unlink guards. Because the client proves possession of the
12
+ * current key (from SecureStore OR a recovery-phrase re-derivation), the LAST
13
+ * remaining credential can be replaced — the server only cares that the
14
+ * signature validates against the current `publicKey`.
15
+ *
16
+ * The API validates its output against these schemas; `@oxyhq/core`'s identity
17
+ * mixin validates its input against the same definitions, so producer and
18
+ * consumer cannot drift.
19
+ *
20
+ * All shapes here are FLAT (no nested objects), so `z.infer<>` is safe under a
21
+ * consumer's node10 `moduleResolution` — no interface-pinning needed.
22
+ *
23
+ * Platform-agnostic — zod only, ESM-safe (no `require()`).
24
+ */
25
+ import { z } from 'zod';
26
+ /**
27
+ * Response of `POST /auth/rotate/challenge`: the single-use `rotate_key`
28
+ * challenge the client must sign with its CURRENT key, plus its expiry.
29
+ */
30
+ export const rotateKeyChallengeResponseSchema = z.object({
31
+ challenge: z.string(),
32
+ /** ISO-8601 expiry timestamp. */
33
+ expiresAt: z.string(),
34
+ });
35
+ /**
36
+ * Request body of `POST /auth/rotate/complete`.
37
+ *
38
+ * Two proofs are required:
39
+ * - `signature` — the CURRENT (old) key signs
40
+ * `JSON.stringify({ action: 'rotate_key', userId, oldPublicKey, newPublicKey,
41
+ * challenge, timestamp })` (proves control of the key being replaced).
42
+ * - `newKeyProof` — the NEW key signs
43
+ * `JSON.stringify({ action: 'rotate_key_new', userId, newPublicKey, challenge,
44
+ * timestamp })` (proof-of-possession of the key being rotated IN; prevents an
45
+ * attacker rotating their account to a re-encoding of someone else's key they
46
+ * do not control).
47
+ *
48
+ * The request carries ONLY `newPublicKey` — `oldPublicKey` and `userId` are
49
+ * derived server-side from the authenticated user document (never
50
+ * client-supplied), so a caller cannot prove control of key X while rotating
51
+ * key Y.
52
+ */
53
+ export const rotateKeyCompleteRequestSchema = z.object({
54
+ newPublicKey: z.string().trim().min(1),
55
+ challenge: z.string().trim().min(1),
56
+ signature: z.string().trim().min(1),
57
+ /** Proof-of-possession: the NEW key signs the rotate_key_new payload. */
58
+ newKeyProof: z.string().trim().min(1),
59
+ timestamp: z.number(),
60
+ /**
61
+ * When true, all OTHER active sessions for the account are revoked after a
62
+ * successful rotation (the rotating device stays signed in). Use it when the
63
+ * old key is presumed compromised.
64
+ */
65
+ signOutEverywhere: z.boolean().optional(),
66
+ });
67
+ /** Response of `POST /auth/rotate/complete`: the account's new (rotated) public key. */
68
+ export const rotateKeyCompleteResponseSchema = z.object({
69
+ success: z.boolean(),
70
+ publicKey: z.string(),
71
+ message: z.string(),
72
+ });