@spacelr/sdk 0.16.0 → 0.18.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/dist/index.mjs CHANGED
@@ -2401,6 +2401,137 @@ var AuthModule = class _AuthModule {
2401
2401
  body: {}
2402
2402
  });
2403
2403
  }
2404
+ // ─── Device-link (#805 SP2) ───
2405
+ // WhatsApp-style "link a new device": the unauthenticated new device (B)
2406
+ // starts/polls/completes the link; the signed-in device (A) reviews and
2407
+ // approves it. Identity is always derived server-side from tenant headers
2408
+ // / the JWT — the SDK never sends userId/projectId/clientId in these bodies.
2409
+ /**
2410
+ * Start a device-link handshake (new device B). UNAUTHENTICATED. Returns a
2411
+ * displayed `linkToken`/`shortCode` (to relay to the already-signed-in
2412
+ * device A for approval) and a never-displayed `completionSecret` (kept on
2413
+ * this device to call {@link getDeviceLinkOptions} / {@link completeDeviceLink}).
2414
+ */
2415
+ async startDeviceLink() {
2416
+ return this.http.request({
2417
+ method: "POST",
2418
+ path: "/auth/device-link/init",
2419
+ body: {}
2420
+ });
2421
+ }
2422
+ /**
2423
+ * Poll a device link's current state (new device B). UNAUTHENTICATED.
2424
+ */
2425
+ async pollDeviceLinkStatus(linkToken) {
2426
+ return this.http.request({
2427
+ method: "GET",
2428
+ path: "/auth/device-link/status",
2429
+ query: { token: linkToken }
2430
+ });
2431
+ }
2432
+ /**
2433
+ * Long-poll a device link once (new device B). UNAUTHENTICATED. The server
2434
+ * holds the request until the link leaves `pending` or ~25s pass, then returns
2435
+ * the current state — a returned `pending` just means "call again". Prefer
2436
+ * {@link waitForDeviceLinkApproval}, which loops for you.
2437
+ */
2438
+ async waitDeviceLinkStatus(linkToken, options = {}) {
2439
+ return this.http.request({
2440
+ method: "GET",
2441
+ path: "/auth/device-link/wait",
2442
+ query: { token: linkToken },
2443
+ signal: options.signal
2444
+ });
2445
+ }
2446
+ /**
2447
+ * Wait until the signed-in device decides (new device B). UNAUTHENTICATED.
2448
+ * Replaces a hand-written poll loop on {@link pollDeviceLinkStatus}: resolves
2449
+ * with the first non-`pending` state (`approved` → continue with
2450
+ * {@link getDeviceLinkOptions}; `rejected` → start over). Rejects when the link
2451
+ * expires (404 after its ~5 min TTL), on a throttle (429), or when `signal`
2452
+ * aborts. A client-side request timeout shorter than the server's ~25s hold is
2453
+ * treated as one more `pending` round, not as a failure.
2454
+ */
2455
+ async waitForDeviceLinkApproval(linkToken, options = {}) {
2456
+ const minRoundMs = 1e3;
2457
+ for (; ; ) {
2458
+ const startedAt = Date.now();
2459
+ try {
2460
+ const status = await this.waitDeviceLinkStatus(linkToken, options);
2461
+ if (status.state !== "pending") return status;
2462
+ } catch (error) {
2463
+ if (!(error instanceof SpacelrTimeoutError)) throw error;
2464
+ }
2465
+ const remainingMs = minRoundMs - (Date.now() - startedAt);
2466
+ if (remainingMs > 0) {
2467
+ await new Promise((resolve) => setTimeout(resolve, remainingMs));
2468
+ }
2469
+ }
2470
+ }
2471
+ /**
2472
+ * Fetch WebAuthn registration options to complete the device link (new
2473
+ * device B). UNAUTHENTICATED — requires BOTH the displayed `linkToken` AND
2474
+ * the never-displayed `completionSecret` returned by {@link startDeviceLink}.
2475
+ */
2476
+ async getDeviceLinkOptions(linkToken, completionSecret) {
2477
+ return this.http.request({
2478
+ method: "POST",
2479
+ path: "/auth/device-link/options",
2480
+ body: { linkToken, completionSecret }
2481
+ });
2482
+ }
2483
+ /**
2484
+ * Verify the WebAuthn registration ceremony against the options from
2485
+ * {@link getDeviceLinkOptions}, completing the device link. UNAUTHENTICATED
2486
+ * — requires BOTH secrets, like `getDeviceLinkOptions`. Installs the
2487
+ * returned tokens into the SDK session, exactly like a passkey login, so
2488
+ * the caller is authenticated as the linked account afterwards.
2489
+ */
2490
+ async completeDeviceLink(linkToken, completionSecret, response, label) {
2491
+ const result = await this.http.request({
2492
+ method: "POST",
2493
+ path: "/auth/device-link/complete",
2494
+ body: { linkToken, completionSecret, response, label }
2495
+ });
2496
+ await this.storeTokensFromLogin(result);
2497
+ return result;
2498
+ }
2499
+ /**
2500
+ * Review the device pending approval (signed-in device A). Authenticated.
2501
+ * `code` is the displayed `linkToken` or `shortCode` relayed from device B.
2502
+ */
2503
+ async getPendingDeviceLink(code) {
2504
+ return this.http.request({
2505
+ method: "GET",
2506
+ path: "/auth/device-link/pending",
2507
+ query: { code },
2508
+ authenticated: true
2509
+ });
2510
+ }
2511
+ /**
2512
+ * Approve the pending device link (signed-in device A). Authenticated.
2513
+ * Ownership/identity are derived server-side from the JWT.
2514
+ */
2515
+ async approveDeviceLink(code) {
2516
+ return this.http.request({
2517
+ method: "POST",
2518
+ path: "/auth/device-link/approve",
2519
+ authenticated: true,
2520
+ body: { code }
2521
+ });
2522
+ }
2523
+ /**
2524
+ * Reject the pending device link (signed-in device A). Authenticated. The new
2525
+ * device sees `rejected` immediately instead of waiting for the link to expire.
2526
+ */
2527
+ async rejectDeviceLink(code) {
2528
+ return this.http.request({
2529
+ method: "POST",
2530
+ path: "/auth/device-link/reject",
2531
+ authenticated: true,
2532
+ body: { code }
2533
+ });
2534
+ }
2404
2535
  async storeTokensFromRegister(response) {
2405
2536
  if (!response.access_token) return;
2406
2537
  const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;