@oxyhq/core 1.11.15 → 1.11.17

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/dist/esm/index.js CHANGED
@@ -22,6 +22,7 @@ export { OXY_CLOUD_URL, oxyClient } from './OxyServices.js';
22
22
  export { AuthManager, createAuthManager } from './AuthManager.js';
23
23
  export { CrossDomainAuth, createCrossDomainAuth } from './CrossDomainAuth.js';
24
24
  export { ServiceCredentialMismatchError } from './mixins/OxyServices.auth.js';
25
+ export { OxyAppDataIdentifierError } from './mixins/OxyServices.appData.js';
25
26
  // --- Crypto / Identity ---
26
27
  export { KeyManager, SignatureService, RecoveryPhraseService, IdentityAlreadyExistsError, IdentityPersistError, } from './crypto/index.js';
27
28
  // --- Models & Types ---
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Per-user App-Data Mixin
3
+ *
4
+ * Thin client around `/users/me/app-data/...` — a generic per-user JSON KV
5
+ * store on the API. Authenticated callers can read, write, list, and delete
6
+ * entries scoped to their own user account.
7
+ *
8
+ * Identifier rules (must match the API):
9
+ * - Both `namespace` and `key` must match `/^[a-z0-9_-]{1,64}$/u`.
10
+ *
11
+ * Limits (enforced by the API):
12
+ * - Serialized JSON values are capped at 64 KB.
13
+ * - Writes are rate-limited to 100 / minute / user.
14
+ *
15
+ * Intended use cases are small bits of cross-device app state — e.g. Academy
16
+ * course progress, "last viewed" markers, dismissed banner flags. Do not use
17
+ * this for large blobs or anything that needs server-side querying; it's a
18
+ * write-it-and-read-it-back store.
19
+ */
20
+ /**
21
+ * Identifier validator — mirror of the API regex. Validating client-side
22
+ * gives consumers a clean throw before the request even leaves the device.
23
+ */
24
+ const APP_DATA_IDENTIFIER_PATTERN = /^[a-z0-9_-]{1,64}$/u;
25
+ /** Thrown when a namespace or key fails the kebab/snake-case validator. */
26
+ export class OxyAppDataIdentifierError extends Error {
27
+ constructor(field, value) {
28
+ super(`Invalid app-data ${field} "${value}": must match [a-z0-9_-]{1,64} (lowercase letters, digits, dashes, underscores).`);
29
+ this.name = 'OxyAppDataIdentifierError';
30
+ }
31
+ }
32
+ function assertIdentifier(field, value) {
33
+ if (!APP_DATA_IDENTIFIER_PATTERN.test(value)) {
34
+ throw new OxyAppDataIdentifierError(field, value);
35
+ }
36
+ }
37
+ export function OxyServicesAppDataMixin(Base) {
38
+ return class extends Base {
39
+ constructor(...args) {
40
+ super(...args);
41
+ }
42
+ /**
43
+ * Read the value stored under `(namespace, key)` for the current user.
44
+ *
45
+ * @returns The previously-stored value, or `null` if nothing has been
46
+ * stored yet. Never throws on "not found" — a missing entry is
47
+ * semantically a `null` value.
48
+ */
49
+ async getAppData(namespace, key) {
50
+ assertIdentifier('namespace', namespace);
51
+ assertIdentifier('key', key);
52
+ return this.withAuthRetry(async () => {
53
+ const response = await this.makeRequest('GET', `/users/me/app-data/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`, undefined, { cache: false });
54
+ return response?.value ?? null;
55
+ }, 'getAppData');
56
+ }
57
+ /**
58
+ * Upsert the value under `(namespace, key)` for the current user.
59
+ *
60
+ * Returns the value the server confirmed it stored — typically the same
61
+ * value the caller passed in, but consumers should prefer the returned
62
+ * value (the API is the source of truth).
63
+ *
64
+ * @throws OxyAppDataIdentifierError when namespace or key is malformed.
65
+ */
66
+ async setAppData(namespace, key, value) {
67
+ assertIdentifier('namespace', namespace);
68
+ assertIdentifier('key', key);
69
+ return this.withAuthRetry(async () => {
70
+ const response = await this.makeRequest('PUT', `/users/me/app-data/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`, { value }, { cache: false });
71
+ // The server echoes the stored value back; fall back to the caller's
72
+ // input only if the server somehow omitted it (defensive — the route
73
+ // always sets it).
74
+ return (response?.value ?? value);
75
+ }, 'setAppData');
76
+ }
77
+ /**
78
+ * Delete the value stored under `(namespace, key)` for the current user.
79
+ *
80
+ * Idempotent — resolves successfully whether or not the entry existed.
81
+ */
82
+ async deleteAppData(namespace, key) {
83
+ assertIdentifier('namespace', namespace);
84
+ assertIdentifier('key', key);
85
+ await this.withAuthRetry(async () => {
86
+ await this.makeRequest('DELETE', `/users/me/app-data/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`, undefined, { cache: false });
87
+ }, 'deleteAppData');
88
+ }
89
+ /**
90
+ * List every entry stored under `namespace` for the current user.
91
+ *
92
+ * Returns an empty object when the namespace contains no entries (the
93
+ * endpoint never 404s on an empty namespace).
94
+ */
95
+ async listAppData(namespace) {
96
+ assertIdentifier('namespace', namespace);
97
+ return this.withAuthRetry(async () => {
98
+ const response = await this.makeRequest('GET', `/users/me/app-data/${encodeURIComponent(namespace)}`, undefined, { cache: false });
99
+ return response?.entries ?? {};
100
+ }, 'listAppData');
101
+ }
102
+ };
103
+ }
@@ -317,6 +317,11 @@ export function OxyServicesAuthMixin(Base) {
317
317
  }
318
318
  /**
319
319
  * Get access token by session ID
320
+ *
321
+ * SECURITY: this endpoint requires the caller to already hold a
322
+ * bearer token whose user owns the referenced session (C1 hardening
323
+ * in the API). For the device-flow / QR sign-in case where the
324
+ * client has no bearer token yet, use `claimSessionByToken` instead.
320
325
  */
321
326
  async getTokenBySession(sessionId) {
322
327
  try {
@@ -328,6 +333,40 @@ export function OxyServicesAuthMixin(Base) {
328
333
  throw this.handleError(error);
329
334
  }
330
335
  }
336
+ /**
337
+ * Exchange a device-flow sessionToken for the first access token.
338
+ *
339
+ * The originating client holds a 128-bit `sessionToken` that nobody
340
+ * else has seen — it was generated client-side, sent once on
341
+ * `POST /auth/session/create`, and is never echoed back. After
342
+ * another authenticated device approves the session via
343
+ * `POST /auth/session/authorize/{sessionToken}` (bearer-authed) and
344
+ * the auth socket / poll loop notifies this client, the client
345
+ * exchanges its `sessionToken` here for the first access token,
346
+ * refresh token, sessionId, and the authorized user.
347
+ *
348
+ * This call requires no Authorization header — the high-entropy
349
+ * `sessionToken` IS the credential (RFC 8628 §3.4). The exchange is
350
+ * single-use; replay attempts are rejected with 401.
351
+ *
352
+ * @param sessionToken - The same sessionToken the SDK passed to
353
+ * `POST /auth/session/create` at the start of the flow.
354
+ * @param options.deviceFingerprint - Optional fingerprint of the
355
+ * originating client device.
356
+ */
357
+ async claimSessionByToken(sessionToken, options = {}) {
358
+ try {
359
+ const res = await this.makeRequest('POST', '/auth/session/claim', {
360
+ sessionToken,
361
+ ...(options.deviceFingerprint ? { deviceFingerprint: options.deviceFingerprint } : {}),
362
+ }, { cache: false, retry: false });
363
+ this.setTokens(res.accessToken, res.refreshToken);
364
+ return res;
365
+ }
366
+ catch (error) {
367
+ throw this.handleError(error);
368
+ }
369
+ }
331
370
  /**
332
371
  * Get sessions by session ID
333
372
  */
@@ -25,6 +25,7 @@ import { OxyServicesFeaturesMixin } from './OxyServices.features.js';
25
25
  import { OxyServicesTopicsMixin } from './OxyServices.topics.js';
26
26
  import { OxyServicesManagedAccountsMixin } from './OxyServices.managedAccounts.js';
27
27
  import { OxyServicesContactsMixin } from './OxyServices.contacts.js';
28
+ import { OxyServicesAppDataMixin } from './OxyServices.appData.js';
28
29
  /**
29
30
  * Mixin pipeline - applied in order from first to last.
30
31
  *
@@ -64,6 +65,7 @@ const MIXIN_PIPELINE = [
64
65
  OxyServicesTopicsMixin,
65
66
  OxyServicesManagedAccountsMixin,
66
67
  OxyServicesContactsMixin,
68
+ OxyServicesAppDataMixin,
67
69
  // Utility (last, can use all above)
68
70
  OxyServicesUtilityMixin,
69
71
  ];