@spacelr/sdk 0.19.4 → 0.19.5

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
@@ -467,6 +467,8 @@ interface UploadProgress {
467
467
  }
468
468
 
469
469
  type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
470
+ /** #1073: renews a session that has no refresh token (API-token sign-in). */
471
+ type ReauthCallback = () => Promise<StoredTokens>;
470
472
  /**
471
473
  * Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
472
474
  * A transient refresh failure (network/transport, a 5xx, a 429 or a 408) is NOT
@@ -481,6 +483,7 @@ declare class TokenManager {
481
483
  private refreshBufferSeconds;
482
484
  private refreshLockName;
483
485
  private refreshCallback;
486
+ private reauthCallback;
484
487
  private refreshPromise;
485
488
  private tokenRefreshedListeners;
486
489
  private authLostListeners;
@@ -488,6 +491,13 @@ declare class TokenManager {
488
491
  private pendingClear;
489
492
  constructor(storage?: TokenStorage, refreshBufferSeconds?: number, refreshLockName?: string);
490
493
  setRefreshCallback(callback: RefreshCallback): void;
494
+ /**
495
+ * #1073: a session without a refresh token (API-token sign-in) recovers by
496
+ * re-authenticating through this callback instead. `null` removes it.
497
+ */
498
+ setReauthCallback(callback: ReauthCallback | null): void;
499
+ /** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
500
+ canRenew(): Promise<boolean>;
491
501
  getAccessToken(): Promise<string | null>;
492
502
  setTokens(tokens: StoredTokens): Promise<void>;
493
503
  clearTokens(): Promise<void>;
@@ -552,6 +562,10 @@ declare class TokenManager {
552
562
  private isTokenExpired;
553
563
  private shouldRefresh;
554
564
  private tryRefresh;
565
+ /** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
566
+ private singleFlight;
567
+ /** #1073: same auth-loss classification as doRefresh, for re-authentication. */
568
+ private executeReauth;
555
569
  private executeRefresh;
556
570
  private doRefresh;
557
571
  private safeGetTokens;
@@ -1176,6 +1190,14 @@ declare class AuthModule {
1176
1190
  }>;
1177
1191
  getUserInfo(): Promise<UserInfo>;
1178
1192
  getAuthorizationUrl(params: AuthorizationUrlParams): string;
1193
+ /**
1194
+ * #1073: sign in as a machine user with a project API token (`slr_at_…`).
1195
+ * The server returns a short-lived access token and no refresh token; when
1196
+ * it expires or is rejected, the SDK exchanges the API token again. The API
1197
+ * token stays in memory only and is forgotten on logout. Never use this in a
1198
+ * browser: an API token is a long-lived secret.
1199
+ */
1200
+ loginWithApiToken(apiToken: string): Promise<void>;
1179
1201
  exchangeCode(params: ExchangeCodeParams): Promise<TokenResponse>;
1180
1202
  generatePKCE(): Promise<PKCEChallenge>;
1181
1203
  getOpenIDConfiguration(): Promise<OpenIDConfiguration>;
package/dist/index.d.ts CHANGED
@@ -467,6 +467,8 @@ interface UploadProgress {
467
467
  }
468
468
 
469
469
  type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
470
+ /** #1073: renews a session that has no refresh token (API-token sign-in). */
471
+ type ReauthCallback = () => Promise<StoredTokens>;
470
472
  /**
471
473
  * Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
472
474
  * A transient refresh failure (network/transport, a 5xx, a 429 or a 408) is NOT
@@ -481,6 +483,7 @@ declare class TokenManager {
481
483
  private refreshBufferSeconds;
482
484
  private refreshLockName;
483
485
  private refreshCallback;
486
+ private reauthCallback;
484
487
  private refreshPromise;
485
488
  private tokenRefreshedListeners;
486
489
  private authLostListeners;
@@ -488,6 +491,13 @@ declare class TokenManager {
488
491
  private pendingClear;
489
492
  constructor(storage?: TokenStorage, refreshBufferSeconds?: number, refreshLockName?: string);
490
493
  setRefreshCallback(callback: RefreshCallback): void;
494
+ /**
495
+ * #1073: a session without a refresh token (API-token sign-in) recovers by
496
+ * re-authenticating through this callback instead. `null` removes it.
497
+ */
498
+ setReauthCallback(callback: ReauthCallback | null): void;
499
+ /** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
500
+ canRenew(): Promise<boolean>;
491
501
  getAccessToken(): Promise<string | null>;
492
502
  setTokens(tokens: StoredTokens): Promise<void>;
493
503
  clearTokens(): Promise<void>;
@@ -552,6 +562,10 @@ declare class TokenManager {
552
562
  private isTokenExpired;
553
563
  private shouldRefresh;
554
564
  private tryRefresh;
565
+ /** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
566
+ private singleFlight;
567
+ /** #1073: same auth-loss classification as doRefresh, for re-authentication. */
568
+ private executeReauth;
555
569
  private executeRefresh;
556
570
  private doRefresh;
557
571
  private safeGetTokens;
@@ -1176,6 +1190,14 @@ declare class AuthModule {
1176
1190
  }>;
1177
1191
  getUserInfo(): Promise<UserInfo>;
1178
1192
  getAuthorizationUrl(params: AuthorizationUrlParams): string;
1193
+ /**
1194
+ * #1073: sign in as a machine user with a project API token (`slr_at_…`).
1195
+ * The server returns a short-lived access token and no refresh token; when
1196
+ * it expires or is rejected, the SDK exchanges the API token again. The API
1197
+ * token stays in memory only and is forgotten on logout. Never use this in a
1198
+ * browser: an API token is a long-lived secret.
1199
+ */
1200
+ loginWithApiToken(apiToken: string): Promise<void>;
1179
1201
  exchangeCode(params: ExchangeCodeParams): Promise<TokenResponse>;
1180
1202
  generatePKCE(): Promise<PKCEChallenge>;
1181
1203
  getOpenIDConfiguration(): Promise<OpenIDConfiguration>;
package/dist/index.js CHANGED
@@ -253,7 +253,7 @@ var HttpClient = class {
253
253
  await this.tokenManager.notifyAuthLost("unauthorized");
254
254
  return false;
255
255
  }
256
- const hadRefreshToken = await this.tokenManager.hasRefreshToken();
256
+ const hadRefreshToken = await this.tokenManager.canRenew();
257
257
  const refreshed = await this.tokenManager.forceRefresh();
258
258
  if (refreshed) return true;
259
259
  if (hadRefreshToken) return false;
@@ -489,6 +489,7 @@ var BrowserTokenStorage = class {
489
489
  var TokenManager = class {
490
490
  constructor(storage, refreshBufferSeconds = 60, refreshLockName = "spacelr_token_refresh") {
491
491
  this.refreshCallback = null;
492
+ this.reauthCallback = null;
492
493
  this.refreshPromise = null;
493
494
  this.tokenRefreshedListeners = /* @__PURE__ */ new Set();
494
495
  this.authLostListeners = /* @__PURE__ */ new Set();
@@ -508,6 +509,18 @@ var TokenManager = class {
508
509
  setRefreshCallback(callback) {
509
510
  this.refreshCallback = callback;
510
511
  }
512
+ /**
513
+ * #1073: a session without a refresh token (API-token sign-in) recovers by
514
+ * re-authenticating through this callback instead. `null` removes it.
515
+ */
516
+ setReauthCallback(callback) {
517
+ this.reauthCallback = callback;
518
+ }
519
+ /** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
520
+ async canRenew() {
521
+ if (this.reauthCallback) return true;
522
+ return this.hasRefreshToken();
523
+ }
511
524
  async getAccessToken() {
512
525
  const tokens = await this.storage.getTokens();
513
526
  if (!tokens) return null;
@@ -525,10 +538,12 @@ var TokenManager = class {
525
538
  if (this.pendingClear) await this.pendingClear;
526
539
  const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
527
540
  const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
541
+ this.reauthCallback = null;
528
542
  await this.storage.setTokens(normalised);
529
543
  this.authLostEmitted = false;
530
544
  }
531
545
  async clearTokens() {
546
+ this.reauthCallback = null;
532
547
  await this.storage.clearTokens();
533
548
  this.refreshPromise = null;
534
549
  this.authLostEmitted = false;
@@ -610,6 +625,7 @@ var TokenManager = class {
610
625
  async notifyAuthLost(reason) {
611
626
  if (this.authLostEmitted) return;
612
627
  this.authLostEmitted = true;
628
+ this.reauthCallback = null;
613
629
  this.pendingClear = this.storage.clearTokens().catch(() => {
614
630
  });
615
631
  await this.pendingClear;
@@ -646,12 +662,15 @@ var TokenManager = class {
646
662
  return Date.now() >= tokens.expiresAt * 1e3;
647
663
  }
648
664
  shouldRefresh(tokens) {
649
- if (!tokens.expiresAt || !tokens.refreshToken) return false;
665
+ if (!tokens.expiresAt || !tokens.refreshToken && !this.reauthCallback) return false;
650
666
  const bufferMs = this.refreshBufferSeconds * 1e3;
651
667
  return Date.now() >= tokens.expiresAt * 1e3 - bufferMs;
652
668
  }
653
669
  async tryRefresh(tokens) {
654
- if (!tokens.refreshToken || !this.refreshCallback) return null;
670
+ if (!tokens.refreshToken) {
671
+ return this.reauthCallback ? this.singleFlight(() => this.executeReauth()) : null;
672
+ }
673
+ if (!this.refreshCallback) return null;
655
674
  if (this.refreshPromise) {
656
675
  try {
657
676
  return await this.refreshPromise;
@@ -668,6 +687,43 @@ var TokenManager = class {
668
687
  this.refreshPromise = null;
669
688
  }
670
689
  }
690
+ /** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
691
+ async singleFlight(run) {
692
+ if (!this.refreshPromise) {
693
+ this.refreshPromise = run();
694
+ try {
695
+ return await this.refreshPromise;
696
+ } catch {
697
+ return null;
698
+ } finally {
699
+ this.refreshPromise = null;
700
+ }
701
+ }
702
+ try {
703
+ return await this.refreshPromise;
704
+ } catch {
705
+ return null;
706
+ }
707
+ }
708
+ /** #1073: same auth-loss classification as doRefresh, for re-authentication. */
709
+ async executeReauth() {
710
+ const callback = this.reauthCallback;
711
+ const superseded = () => this.reauthCallback !== callback;
712
+ let newTokens;
713
+ try {
714
+ newTokens = await callback();
715
+ } catch (error) {
716
+ if (superseded()) throw new Error("re-authentication superseded");
717
+ if (!this.isTransientTransportError(error)) {
718
+ await this.notifyAuthLost("refresh-failed");
719
+ }
720
+ throw error;
721
+ }
722
+ if (superseded()) throw new Error("re-authentication superseded");
723
+ await this.storage.setTokens(newTokens);
724
+ this.emitTokenRefreshed(newTokens);
725
+ return newTokens;
726
+ }
671
727
  async executeRefresh(refreshToken) {
672
728
  return this.withCrossTabLock(() => this.doRefresh(refreshToken));
673
729
  }
@@ -725,6 +781,7 @@ var FileVisibility = /* @__PURE__ */ ((FileVisibility2) => {
725
781
  FileVisibility2["PUBLIC"] = "public";
726
782
  return FileVisibility2;
727
783
  })(FileVisibility || {});
784
+ var API_TOKEN_GRANT = "urn:spacelr:grant-type:api-token";
728
785
  var GrantType = /* @__PURE__ */ ((GrantType2) => {
729
786
  GrantType2["AUTHORIZATION_CODE"] = "authorization_code";
730
787
  GrantType2["CLIENT_CREDENTIALS"] = "client_credentials";
@@ -2201,6 +2258,30 @@ var AuthModule = class _AuthModule {
2201
2258
  }
2202
2259
  return url.toString();
2203
2260
  }
2261
+ /**
2262
+ * #1073: sign in as a machine user with a project API token (`slr_at_…`).
2263
+ * The server returns a short-lived access token and no refresh token; when
2264
+ * it expires or is rejected, the SDK exchanges the API token again. The API
2265
+ * token stays in memory only and is forgotten on logout. Never use this in a
2266
+ * browser: an API token is a long-lived secret.
2267
+ */
2268
+ async loginWithApiToken(apiToken) {
2269
+ const exchange = async () => {
2270
+ const response = await this.http.request({
2271
+ method: "POST",
2272
+ path: this.config.tokenEndpoint ?? "/auth/token",
2273
+ body: { grant_type: API_TOKEN_GRANT, client_id: this.config.clientId, api_token: apiToken },
2274
+ skipAuthRefresh: true
2275
+ });
2276
+ return {
2277
+ accessToken: response.access_token,
2278
+ expiresAt: response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0
2279
+ };
2280
+ };
2281
+ const tokens = await exchange();
2282
+ await this.installSession(tokens);
2283
+ this.tokenManager.setReauthCallback(exchange);
2284
+ }
2204
2285
  async exchangeCode(params) {
2205
2286
  const body = {
2206
2287
  grant_type: params.grantType ?? "authorization_code" /* AUTHORIZATION_CODE */,