@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.d.mts CHANGED
@@ -322,6 +322,37 @@ interface SessionSummary {
322
322
  createdAt: string;
323
323
  lastSeenAt: string;
324
324
  }
325
+ /**
326
+ * Result of starting a device link (new device B). The ONLY time all three
327
+ * secrets exist in the clear together — `linkToken`/`shortCode` are shown to
328
+ * the user to relay to device A, `completionSecret` is never displayed and
329
+ * must be kept on device B to call `getDeviceLinkOptions`/`completeDeviceLink`.
330
+ */
331
+ interface DeviceLinkInit {
332
+ linkToken: string;
333
+ shortCode: string;
334
+ completionSecret: string;
335
+ expiresAt: string;
336
+ }
337
+ /** Current state of a device link, as polled by the new device (B). */
338
+ interface DeviceLinkStatus {
339
+ state: 'pending' | 'approved' | 'completed' | 'rejected';
340
+ }
341
+ /**
342
+ * Tokens returned by completing a device link (#805 SP2). Same shape as
343
+ * {@link PasskeyLoginResponse} (the newly-linked device authenticates via a
344
+ * fresh passkey, not email/password) — kept as a distinct name for clarity.
345
+ */
346
+ interface DeviceLinkCompleteResponse {
347
+ user: {
348
+ id: string;
349
+ email: string | null;
350
+ roles: string[];
351
+ };
352
+ access_token: string;
353
+ refresh_token?: string;
354
+ expires_in?: number;
355
+ }
325
356
 
326
357
  interface TokenStorage {
327
358
  getTokens(): Promise<StoredTokens | null>;
@@ -1256,6 +1287,77 @@ declare class AuthModule {
1256
1287
  signOutOtherSessions(): Promise<{
1257
1288
  revokedCount: number;
1258
1289
  }>;
1290
+ /**
1291
+ * Start a device-link handshake (new device B). UNAUTHENTICATED. Returns a
1292
+ * displayed `linkToken`/`shortCode` (to relay to the already-signed-in
1293
+ * device A for approval) and a never-displayed `completionSecret` (kept on
1294
+ * this device to call {@link getDeviceLinkOptions} / {@link completeDeviceLink}).
1295
+ */
1296
+ startDeviceLink(): Promise<DeviceLinkInit>;
1297
+ /**
1298
+ * Poll a device link's current state (new device B). UNAUTHENTICATED.
1299
+ */
1300
+ pollDeviceLinkStatus(linkToken: string): Promise<DeviceLinkStatus>;
1301
+ /**
1302
+ * Long-poll a device link once (new device B). UNAUTHENTICATED. The server
1303
+ * holds the request until the link leaves `pending` or ~25s pass, then returns
1304
+ * the current state — a returned `pending` just means "call again". Prefer
1305
+ * {@link waitForDeviceLinkApproval}, which loops for you.
1306
+ */
1307
+ waitDeviceLinkStatus(linkToken: string, options?: {
1308
+ signal?: AbortSignal;
1309
+ }): Promise<DeviceLinkStatus>;
1310
+ /**
1311
+ * Wait until the signed-in device decides (new device B). UNAUTHENTICATED.
1312
+ * Replaces a hand-written poll loop on {@link pollDeviceLinkStatus}: resolves
1313
+ * with the first non-`pending` state (`approved` → continue with
1314
+ * {@link getDeviceLinkOptions}; `rejected` → start over). Rejects when the link
1315
+ * expires (404 after its ~5 min TTL), on a throttle (429), or when `signal`
1316
+ * aborts. A client-side request timeout shorter than the server's ~25s hold is
1317
+ * treated as one more `pending` round, not as a failure.
1318
+ */
1319
+ waitForDeviceLinkApproval(linkToken: string, options?: {
1320
+ signal?: AbortSignal;
1321
+ }): Promise<DeviceLinkStatus>;
1322
+ /**
1323
+ * Fetch WebAuthn registration options to complete the device link (new
1324
+ * device B). UNAUTHENTICATED — requires BOTH the displayed `linkToken` AND
1325
+ * the never-displayed `completionSecret` returned by {@link startDeviceLink}.
1326
+ */
1327
+ getDeviceLinkOptions(linkToken: string, completionSecret: string): Promise<PasskeyRegistrationOptionsJSON>;
1328
+ /**
1329
+ * Verify the WebAuthn registration ceremony against the options from
1330
+ * {@link getDeviceLinkOptions}, completing the device link. UNAUTHENTICATED
1331
+ * — requires BOTH secrets, like `getDeviceLinkOptions`. Installs the
1332
+ * returned tokens into the SDK session, exactly like a passkey login, so
1333
+ * the caller is authenticated as the linked account afterwards.
1334
+ */
1335
+ completeDeviceLink(linkToken: string, completionSecret: string, response: PasskeyRegistrationResponseJSON, label?: string): Promise<DeviceLinkCompleteResponse>;
1336
+ /**
1337
+ * Review the device pending approval (signed-in device A). Authenticated.
1338
+ * `code` is the displayed `linkToken` or `shortCode` relayed from device B.
1339
+ */
1340
+ getPendingDeviceLink(code: string): Promise<{
1341
+ deviceInfo: {
1342
+ userAgent?: string;
1343
+ ip?: string;
1344
+ country?: string;
1345
+ };
1346
+ }>;
1347
+ /**
1348
+ * Approve the pending device link (signed-in device A). Authenticated.
1349
+ * Ownership/identity are derived server-side from the JWT.
1350
+ */
1351
+ approveDeviceLink(code: string): Promise<{
1352
+ approved: true;
1353
+ }>;
1354
+ /**
1355
+ * Reject the pending device link (signed-in device A). Authenticated. The new
1356
+ * device sees `rejected` immediately instead of waiting for the link to expire.
1357
+ */
1358
+ rejectDeviceLink(code: string): Promise<{
1359
+ rejected: true;
1360
+ }>;
1259
1361
  private storeTokensFromRegister;
1260
1362
  }
1261
1363
 
package/dist/index.d.ts CHANGED
@@ -322,6 +322,37 @@ interface SessionSummary {
322
322
  createdAt: string;
323
323
  lastSeenAt: string;
324
324
  }
325
+ /**
326
+ * Result of starting a device link (new device B). The ONLY time all three
327
+ * secrets exist in the clear together — `linkToken`/`shortCode` are shown to
328
+ * the user to relay to device A, `completionSecret` is never displayed and
329
+ * must be kept on device B to call `getDeviceLinkOptions`/`completeDeviceLink`.
330
+ */
331
+ interface DeviceLinkInit {
332
+ linkToken: string;
333
+ shortCode: string;
334
+ completionSecret: string;
335
+ expiresAt: string;
336
+ }
337
+ /** Current state of a device link, as polled by the new device (B). */
338
+ interface DeviceLinkStatus {
339
+ state: 'pending' | 'approved' | 'completed' | 'rejected';
340
+ }
341
+ /**
342
+ * Tokens returned by completing a device link (#805 SP2). Same shape as
343
+ * {@link PasskeyLoginResponse} (the newly-linked device authenticates via a
344
+ * fresh passkey, not email/password) — kept as a distinct name for clarity.
345
+ */
346
+ interface DeviceLinkCompleteResponse {
347
+ user: {
348
+ id: string;
349
+ email: string | null;
350
+ roles: string[];
351
+ };
352
+ access_token: string;
353
+ refresh_token?: string;
354
+ expires_in?: number;
355
+ }
325
356
 
326
357
  interface TokenStorage {
327
358
  getTokens(): Promise<StoredTokens | null>;
@@ -1256,6 +1287,77 @@ declare class AuthModule {
1256
1287
  signOutOtherSessions(): Promise<{
1257
1288
  revokedCount: number;
1258
1289
  }>;
1290
+ /**
1291
+ * Start a device-link handshake (new device B). UNAUTHENTICATED. Returns a
1292
+ * displayed `linkToken`/`shortCode` (to relay to the already-signed-in
1293
+ * device A for approval) and a never-displayed `completionSecret` (kept on
1294
+ * this device to call {@link getDeviceLinkOptions} / {@link completeDeviceLink}).
1295
+ */
1296
+ startDeviceLink(): Promise<DeviceLinkInit>;
1297
+ /**
1298
+ * Poll a device link's current state (new device B). UNAUTHENTICATED.
1299
+ */
1300
+ pollDeviceLinkStatus(linkToken: string): Promise<DeviceLinkStatus>;
1301
+ /**
1302
+ * Long-poll a device link once (new device B). UNAUTHENTICATED. The server
1303
+ * holds the request until the link leaves `pending` or ~25s pass, then returns
1304
+ * the current state — a returned `pending` just means "call again". Prefer
1305
+ * {@link waitForDeviceLinkApproval}, which loops for you.
1306
+ */
1307
+ waitDeviceLinkStatus(linkToken: string, options?: {
1308
+ signal?: AbortSignal;
1309
+ }): Promise<DeviceLinkStatus>;
1310
+ /**
1311
+ * Wait until the signed-in device decides (new device B). UNAUTHENTICATED.
1312
+ * Replaces a hand-written poll loop on {@link pollDeviceLinkStatus}: resolves
1313
+ * with the first non-`pending` state (`approved` → continue with
1314
+ * {@link getDeviceLinkOptions}; `rejected` → start over). Rejects when the link
1315
+ * expires (404 after its ~5 min TTL), on a throttle (429), or when `signal`
1316
+ * aborts. A client-side request timeout shorter than the server's ~25s hold is
1317
+ * treated as one more `pending` round, not as a failure.
1318
+ */
1319
+ waitForDeviceLinkApproval(linkToken: string, options?: {
1320
+ signal?: AbortSignal;
1321
+ }): Promise<DeviceLinkStatus>;
1322
+ /**
1323
+ * Fetch WebAuthn registration options to complete the device link (new
1324
+ * device B). UNAUTHENTICATED — requires BOTH the displayed `linkToken` AND
1325
+ * the never-displayed `completionSecret` returned by {@link startDeviceLink}.
1326
+ */
1327
+ getDeviceLinkOptions(linkToken: string, completionSecret: string): Promise<PasskeyRegistrationOptionsJSON>;
1328
+ /**
1329
+ * Verify the WebAuthn registration ceremony against the options from
1330
+ * {@link getDeviceLinkOptions}, completing the device link. UNAUTHENTICATED
1331
+ * — requires BOTH secrets, like `getDeviceLinkOptions`. Installs the
1332
+ * returned tokens into the SDK session, exactly like a passkey login, so
1333
+ * the caller is authenticated as the linked account afterwards.
1334
+ */
1335
+ completeDeviceLink(linkToken: string, completionSecret: string, response: PasskeyRegistrationResponseJSON, label?: string): Promise<DeviceLinkCompleteResponse>;
1336
+ /**
1337
+ * Review the device pending approval (signed-in device A). Authenticated.
1338
+ * `code` is the displayed `linkToken` or `shortCode` relayed from device B.
1339
+ */
1340
+ getPendingDeviceLink(code: string): Promise<{
1341
+ deviceInfo: {
1342
+ userAgent?: string;
1343
+ ip?: string;
1344
+ country?: string;
1345
+ };
1346
+ }>;
1347
+ /**
1348
+ * Approve the pending device link (signed-in device A). Authenticated.
1349
+ * Ownership/identity are derived server-side from the JWT.
1350
+ */
1351
+ approveDeviceLink(code: string): Promise<{
1352
+ approved: true;
1353
+ }>;
1354
+ /**
1355
+ * Reject the pending device link (signed-in device A). Authenticated. The new
1356
+ * device sees `rejected` immediately instead of waiting for the link to expire.
1357
+ */
1358
+ rejectDeviceLink(code: string): Promise<{
1359
+ rejected: true;
1360
+ }>;
1259
1361
  private storeTokensFromRegister;
1260
1362
  }
1261
1363
 
package/dist/index.js CHANGED
@@ -2448,6 +2448,137 @@ var AuthModule = class _AuthModule {
2448
2448
  body: {}
2449
2449
  });
2450
2450
  }
2451
+ // ─── Device-link (#805 SP2) ───
2452
+ // WhatsApp-style "link a new device": the unauthenticated new device (B)
2453
+ // starts/polls/completes the link; the signed-in device (A) reviews and
2454
+ // approves it. Identity is always derived server-side from tenant headers
2455
+ // / the JWT — the SDK never sends userId/projectId/clientId in these bodies.
2456
+ /**
2457
+ * Start a device-link handshake (new device B). UNAUTHENTICATED. Returns a
2458
+ * displayed `linkToken`/`shortCode` (to relay to the already-signed-in
2459
+ * device A for approval) and a never-displayed `completionSecret` (kept on
2460
+ * this device to call {@link getDeviceLinkOptions} / {@link completeDeviceLink}).
2461
+ */
2462
+ async startDeviceLink() {
2463
+ return this.http.request({
2464
+ method: "POST",
2465
+ path: "/auth/device-link/init",
2466
+ body: {}
2467
+ });
2468
+ }
2469
+ /**
2470
+ * Poll a device link's current state (new device B). UNAUTHENTICATED.
2471
+ */
2472
+ async pollDeviceLinkStatus(linkToken) {
2473
+ return this.http.request({
2474
+ method: "GET",
2475
+ path: "/auth/device-link/status",
2476
+ query: { token: linkToken }
2477
+ });
2478
+ }
2479
+ /**
2480
+ * Long-poll a device link once (new device B). UNAUTHENTICATED. The server
2481
+ * holds the request until the link leaves `pending` or ~25s pass, then returns
2482
+ * the current state — a returned `pending` just means "call again". Prefer
2483
+ * {@link waitForDeviceLinkApproval}, which loops for you.
2484
+ */
2485
+ async waitDeviceLinkStatus(linkToken, options = {}) {
2486
+ return this.http.request({
2487
+ method: "GET",
2488
+ path: "/auth/device-link/wait",
2489
+ query: { token: linkToken },
2490
+ signal: options.signal
2491
+ });
2492
+ }
2493
+ /**
2494
+ * Wait until the signed-in device decides (new device B). UNAUTHENTICATED.
2495
+ * Replaces a hand-written poll loop on {@link pollDeviceLinkStatus}: resolves
2496
+ * with the first non-`pending` state (`approved` → continue with
2497
+ * {@link getDeviceLinkOptions}; `rejected` → start over). Rejects when the link
2498
+ * expires (404 after its ~5 min TTL), on a throttle (429), or when `signal`
2499
+ * aborts. A client-side request timeout shorter than the server's ~25s hold is
2500
+ * treated as one more `pending` round, not as a failure.
2501
+ */
2502
+ async waitForDeviceLinkApproval(linkToken, options = {}) {
2503
+ const minRoundMs = 1e3;
2504
+ for (; ; ) {
2505
+ const startedAt = Date.now();
2506
+ try {
2507
+ const status = await this.waitDeviceLinkStatus(linkToken, options);
2508
+ if (status.state !== "pending") return status;
2509
+ } catch (error) {
2510
+ if (!(error instanceof SpacelrTimeoutError)) throw error;
2511
+ }
2512
+ const remainingMs = minRoundMs - (Date.now() - startedAt);
2513
+ if (remainingMs > 0) {
2514
+ await new Promise((resolve) => setTimeout(resolve, remainingMs));
2515
+ }
2516
+ }
2517
+ }
2518
+ /**
2519
+ * Fetch WebAuthn registration options to complete the device link (new
2520
+ * device B). UNAUTHENTICATED — requires BOTH the displayed `linkToken` AND
2521
+ * the never-displayed `completionSecret` returned by {@link startDeviceLink}.
2522
+ */
2523
+ async getDeviceLinkOptions(linkToken, completionSecret) {
2524
+ return this.http.request({
2525
+ method: "POST",
2526
+ path: "/auth/device-link/options",
2527
+ body: { linkToken, completionSecret }
2528
+ });
2529
+ }
2530
+ /**
2531
+ * Verify the WebAuthn registration ceremony against the options from
2532
+ * {@link getDeviceLinkOptions}, completing the device link. UNAUTHENTICATED
2533
+ * — requires BOTH secrets, like `getDeviceLinkOptions`. Installs the
2534
+ * returned tokens into the SDK session, exactly like a passkey login, so
2535
+ * the caller is authenticated as the linked account afterwards.
2536
+ */
2537
+ async completeDeviceLink(linkToken, completionSecret, response, label) {
2538
+ const result = await this.http.request({
2539
+ method: "POST",
2540
+ path: "/auth/device-link/complete",
2541
+ body: { linkToken, completionSecret, response, label }
2542
+ });
2543
+ await this.storeTokensFromLogin(result);
2544
+ return result;
2545
+ }
2546
+ /**
2547
+ * Review the device pending approval (signed-in device A). Authenticated.
2548
+ * `code` is the displayed `linkToken` or `shortCode` relayed from device B.
2549
+ */
2550
+ async getPendingDeviceLink(code) {
2551
+ return this.http.request({
2552
+ method: "GET",
2553
+ path: "/auth/device-link/pending",
2554
+ query: { code },
2555
+ authenticated: true
2556
+ });
2557
+ }
2558
+ /**
2559
+ * Approve the pending device link (signed-in device A). Authenticated.
2560
+ * Ownership/identity are derived server-side from the JWT.
2561
+ */
2562
+ async approveDeviceLink(code) {
2563
+ return this.http.request({
2564
+ method: "POST",
2565
+ path: "/auth/device-link/approve",
2566
+ authenticated: true,
2567
+ body: { code }
2568
+ });
2569
+ }
2570
+ /**
2571
+ * Reject the pending device link (signed-in device A). Authenticated. The new
2572
+ * device sees `rejected` immediately instead of waiting for the link to expire.
2573
+ */
2574
+ async rejectDeviceLink(code) {
2575
+ return this.http.request({
2576
+ method: "POST",
2577
+ path: "/auth/device-link/reject",
2578
+ authenticated: true,
2579
+ body: { code }
2580
+ });
2581
+ }
2451
2582
  async storeTokensFromRegister(response) {
2452
2583
  if (!response.access_token) return;
2453
2584
  const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;