@raisindb/client 0.4.5 → 0.4.6

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
@@ -239,7 +239,7 @@ var LocalStorageTokenStorage = class {
239
239
  function isJwtCredentials(credentials) {
240
240
  return "type" in credentials && credentials.type === "jwt";
241
241
  }
242
- var AuthManager = class {
242
+ var AuthManager = class _AuthManager {
243
243
  constructor(storage) {
244
244
  this.expiresAt = null;
245
245
  this._storage = storage ?? new MemoryTokenStorage();
@@ -262,6 +262,31 @@ var AuthManager = class {
262
262
  this.expiresAt = Date.now() + expiresInMs;
263
263
  this.scheduleTokenRefresh(expiresInMs);
264
264
  }
265
+ /**
266
+ * Learn a bare token's expiry FROM THE TOKEN.
267
+ *
268
+ * `setTokens` is the only other writer of `expiresAt`, and it needs a whole
269
+ * `AuthenticateResponse`. Several paths hold nothing but a JWT string — a
270
+ * session restored from storage, `authenticate({type:'jwt'})`,
271
+ * `setIdentityTokens(token)` — and left `expiresAt` null. `isTokenExpired()`
272
+ * reads null as EXPIRED, so `isAuthenticated()` answered false for the whole
273
+ * life of every restored session, i.e. every page load after the first.
274
+ *
275
+ * It failed silently, and it had already been routed around three times
276
+ * rather than fixed: both HTTP send paths were made to stop consulting
277
+ * `isAuthenticated()` (see `request()` and `mcpStream()` in http-client),
278
+ * and Studio grew its own token parse to tell a 404 apart from a logged-out
279
+ * read. This is the missing writer those workarounds existed for.
280
+ *
281
+ * Same 60s safety margin as `setTokens`, so both writers agree on when a
282
+ * token stops counting as good.
283
+ *
284
+ * @param token - A JWT held in storage or handed to us
285
+ */
286
+ adoptToken(token) {
287
+ const exp = _AuthManager.parseToken(token)?.exp;
288
+ this.expiresAt = typeof exp === "number" ? exp * 1e3 - 6e4 : null;
289
+ }
265
290
  /**
266
291
  * Get current access token
267
292
  */
@@ -5834,6 +5859,7 @@ var RaisinHttpClient = class extends EventEmitter {
5834
5859
  async authenticate(credentials, repository) {
5835
5860
  if (isJwtCredentials(credentials)) {
5836
5861
  this.authManager.storage.setAccessToken(credentials.token);
5862
+ this.authManager.adoptToken(credentials.token);
5837
5863
  if (repository) {
5838
5864
  try {
5839
5865
  const userInfo = await this.fetchCurrentUser(repository);
@@ -6240,6 +6266,7 @@ var RaisinHttpClient = class extends EventEmitter {
6240
6266
  setIdentityTokens(accessTokenOrResult, refreshToken) {
6241
6267
  if (typeof accessTokenOrResult === "string") {
6242
6268
  this.authManager.storage.setAccessToken(accessTokenOrResult);
6269
+ this.authManager.adoptToken(accessTokenOrResult);
6243
6270
  if (refreshToken) this.authManager.storage.setRefreshToken(refreshToken);
6244
6271
  return;
6245
6272
  }
@@ -7360,6 +7387,7 @@ var _RaisinClient = class _RaisinClient extends EventEmitter2 {
7360
7387
  async authenticate(credentials) {
7361
7388
  if (isJwtCredentials(credentials)) {
7362
7389
  logger.info("[authenticate] Starting JWT authentication...");
7390
+ this.authManager.adoptToken(credentials.token);
7363
7391
  const payload = { token: credentials.token };
7364
7392
  const response = await this.sendRequestInternal(
7365
7393
  payload,
@@ -7854,7 +7882,13 @@ var _RaisinClient = class _RaisinClient extends EventEmitter2 {
7854
7882
  logger.debug(`[scheduleAutoRefresh] Scheduling refresh in ${Math.round(clamped / 1e3 / 60)} minutes`);
7855
7883
  this._refreshTimer = setTimeout(async () => {
7856
7884
  logger.info("[autoRefresh] Auto-refreshing token...");
7857
- await this.refreshToken();
7885
+ const user = await this.refreshToken();
7886
+ if (!user) {
7887
+ logger.error("[autoRefresh] Refresh failed, clearing session");
7888
+ this.authManager.clear();
7889
+ this._currentUser = null;
7890
+ this._emitAuthEvent("SESSION_EXPIRED");
7891
+ }
7858
7892
  }, clamped);
7859
7893
  }
7860
7894
  /**