@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 +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +84 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +84 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -206,7 +206,7 @@ var HttpClient = class {
|
|
|
206
206
|
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
207
207
|
return false;
|
|
208
208
|
}
|
|
209
|
-
const hadRefreshToken = await this.tokenManager.
|
|
209
|
+
const hadRefreshToken = await this.tokenManager.canRenew();
|
|
210
210
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
211
211
|
if (refreshed) return true;
|
|
212
212
|
if (hadRefreshToken) return false;
|
|
@@ -442,6 +442,7 @@ var BrowserTokenStorage = class {
|
|
|
442
442
|
var TokenManager = class {
|
|
443
443
|
constructor(storage, refreshBufferSeconds = 60, refreshLockName = "spacelr_token_refresh") {
|
|
444
444
|
this.refreshCallback = null;
|
|
445
|
+
this.reauthCallback = null;
|
|
445
446
|
this.refreshPromise = null;
|
|
446
447
|
this.tokenRefreshedListeners = /* @__PURE__ */ new Set();
|
|
447
448
|
this.authLostListeners = /* @__PURE__ */ new Set();
|
|
@@ -461,6 +462,18 @@ var TokenManager = class {
|
|
|
461
462
|
setRefreshCallback(callback) {
|
|
462
463
|
this.refreshCallback = callback;
|
|
463
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* #1073: a session without a refresh token (API-token sign-in) recovers by
|
|
467
|
+
* re-authenticating through this callback instead. `null` removes it.
|
|
468
|
+
*/
|
|
469
|
+
setReauthCallback(callback) {
|
|
470
|
+
this.reauthCallback = callback;
|
|
471
|
+
}
|
|
472
|
+
/** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
|
|
473
|
+
async canRenew() {
|
|
474
|
+
if (this.reauthCallback) return true;
|
|
475
|
+
return this.hasRefreshToken();
|
|
476
|
+
}
|
|
464
477
|
async getAccessToken() {
|
|
465
478
|
const tokens = await this.storage.getTokens();
|
|
466
479
|
if (!tokens) return null;
|
|
@@ -478,10 +491,12 @@ var TokenManager = class {
|
|
|
478
491
|
if (this.pendingClear) await this.pendingClear;
|
|
479
492
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
480
493
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
494
|
+
this.reauthCallback = null;
|
|
481
495
|
await this.storage.setTokens(normalised);
|
|
482
496
|
this.authLostEmitted = false;
|
|
483
497
|
}
|
|
484
498
|
async clearTokens() {
|
|
499
|
+
this.reauthCallback = null;
|
|
485
500
|
await this.storage.clearTokens();
|
|
486
501
|
this.refreshPromise = null;
|
|
487
502
|
this.authLostEmitted = false;
|
|
@@ -563,6 +578,7 @@ var TokenManager = class {
|
|
|
563
578
|
async notifyAuthLost(reason) {
|
|
564
579
|
if (this.authLostEmitted) return;
|
|
565
580
|
this.authLostEmitted = true;
|
|
581
|
+
this.reauthCallback = null;
|
|
566
582
|
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
567
583
|
});
|
|
568
584
|
await this.pendingClear;
|
|
@@ -599,12 +615,15 @@ var TokenManager = class {
|
|
|
599
615
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
600
616
|
}
|
|
601
617
|
shouldRefresh(tokens) {
|
|
602
|
-
if (!tokens.expiresAt || !tokens.refreshToken) return false;
|
|
618
|
+
if (!tokens.expiresAt || !tokens.refreshToken && !this.reauthCallback) return false;
|
|
603
619
|
const bufferMs = this.refreshBufferSeconds * 1e3;
|
|
604
620
|
return Date.now() >= tokens.expiresAt * 1e3 - bufferMs;
|
|
605
621
|
}
|
|
606
622
|
async tryRefresh(tokens) {
|
|
607
|
-
if (!tokens.refreshToken
|
|
623
|
+
if (!tokens.refreshToken) {
|
|
624
|
+
return this.reauthCallback ? this.singleFlight(() => this.executeReauth()) : null;
|
|
625
|
+
}
|
|
626
|
+
if (!this.refreshCallback) return null;
|
|
608
627
|
if (this.refreshPromise) {
|
|
609
628
|
try {
|
|
610
629
|
return await this.refreshPromise;
|
|
@@ -621,6 +640,43 @@ var TokenManager = class {
|
|
|
621
640
|
this.refreshPromise = null;
|
|
622
641
|
}
|
|
623
642
|
}
|
|
643
|
+
/** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
|
|
644
|
+
async singleFlight(run) {
|
|
645
|
+
if (!this.refreshPromise) {
|
|
646
|
+
this.refreshPromise = run();
|
|
647
|
+
try {
|
|
648
|
+
return await this.refreshPromise;
|
|
649
|
+
} catch {
|
|
650
|
+
return null;
|
|
651
|
+
} finally {
|
|
652
|
+
this.refreshPromise = null;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
try {
|
|
656
|
+
return await this.refreshPromise;
|
|
657
|
+
} catch {
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
/** #1073: same auth-loss classification as doRefresh, for re-authentication. */
|
|
662
|
+
async executeReauth() {
|
|
663
|
+
const callback = this.reauthCallback;
|
|
664
|
+
const superseded = () => this.reauthCallback !== callback;
|
|
665
|
+
let newTokens;
|
|
666
|
+
try {
|
|
667
|
+
newTokens = await callback();
|
|
668
|
+
} catch (error) {
|
|
669
|
+
if (superseded()) throw new Error("re-authentication superseded");
|
|
670
|
+
if (!this.isTransientTransportError(error)) {
|
|
671
|
+
await this.notifyAuthLost("refresh-failed");
|
|
672
|
+
}
|
|
673
|
+
throw error;
|
|
674
|
+
}
|
|
675
|
+
if (superseded()) throw new Error("re-authentication superseded");
|
|
676
|
+
await this.storage.setTokens(newTokens);
|
|
677
|
+
this.emitTokenRefreshed(newTokens);
|
|
678
|
+
return newTokens;
|
|
679
|
+
}
|
|
624
680
|
async executeRefresh(refreshToken) {
|
|
625
681
|
return this.withCrossTabLock(() => this.doRefresh(refreshToken));
|
|
626
682
|
}
|
|
@@ -678,6 +734,7 @@ var FileVisibility = /* @__PURE__ */ ((FileVisibility2) => {
|
|
|
678
734
|
FileVisibility2["PUBLIC"] = "public";
|
|
679
735
|
return FileVisibility2;
|
|
680
736
|
})(FileVisibility || {});
|
|
737
|
+
var API_TOKEN_GRANT = "urn:spacelr:grant-type:api-token";
|
|
681
738
|
var GrantType = /* @__PURE__ */ ((GrantType2) => {
|
|
682
739
|
GrantType2["AUTHORIZATION_CODE"] = "authorization_code";
|
|
683
740
|
GrantType2["CLIENT_CREDENTIALS"] = "client_credentials";
|
|
@@ -2154,6 +2211,30 @@ var AuthModule = class _AuthModule {
|
|
|
2154
2211
|
}
|
|
2155
2212
|
return url.toString();
|
|
2156
2213
|
}
|
|
2214
|
+
/**
|
|
2215
|
+
* #1073: sign in as a machine user with a project API token (`slr_at_…`).
|
|
2216
|
+
* The server returns a short-lived access token and no refresh token; when
|
|
2217
|
+
* it expires or is rejected, the SDK exchanges the API token again. The API
|
|
2218
|
+
* token stays in memory only and is forgotten on logout. Never use this in a
|
|
2219
|
+
* browser: an API token is a long-lived secret.
|
|
2220
|
+
*/
|
|
2221
|
+
async loginWithApiToken(apiToken) {
|
|
2222
|
+
const exchange = async () => {
|
|
2223
|
+
const response = await this.http.request({
|
|
2224
|
+
method: "POST",
|
|
2225
|
+
path: this.config.tokenEndpoint ?? "/auth/token",
|
|
2226
|
+
body: { grant_type: API_TOKEN_GRANT, client_id: this.config.clientId, api_token: apiToken },
|
|
2227
|
+
skipAuthRefresh: true
|
|
2228
|
+
});
|
|
2229
|
+
return {
|
|
2230
|
+
accessToken: response.access_token,
|
|
2231
|
+
expiresAt: response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0
|
|
2232
|
+
};
|
|
2233
|
+
};
|
|
2234
|
+
const tokens = await exchange();
|
|
2235
|
+
await this.installSession(tokens);
|
|
2236
|
+
this.tokenManager.setReauthCallback(exchange);
|
|
2237
|
+
}
|
|
2157
2238
|
async exchangeCode(params) {
|
|
2158
2239
|
const body = {
|
|
2159
2240
|
grant_type: params.grantType ?? "authorization_code" /* AUTHORIZATION_CODE */,
|