@docyrus/signin 0.14.0 → 0.14.1

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.
@@ -24,6 +24,8 @@ declare class AuthManager {
24
24
  private guidyBridge;
25
25
  private reactNativeAuth;
26
26
  private tokenRefreshTimer;
27
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
28
+ private refreshInFlight;
27
29
  private listeners;
28
30
  private navigationHandlers;
29
31
  private notificationHandlers;
@@ -139,11 +141,20 @@ declare class AuthManager {
139
141
  */
140
142
  private getValidToken;
141
143
  /**
142
- * Handle token refresh depending on mode.
144
+ * Handle token refresh depending on mode, sharing a single in-flight
145
+ * request across concurrent callers. Multiple requests crossing the expiry
146
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
147
+ * each trigger their own refresh; with refresh-token rotation only the first
148
+ * succeeds and the rest can leave us holding an expired token — exactly the
149
+ * backend "JWTExpired" the proactive path is meant to prevent.
150
+ */
151
+ private handleTokenRefresh;
152
+ /**
153
+ * Refresh depending on mode.
143
154
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
144
155
  * Iframe: send postMessage to host requesting new tokens.
145
156
  */
146
- private handleTokenRefresh;
157
+ private performTokenRefresh;
147
158
  /** Schedule proactive token refresh before expiry. */
148
159
  private scheduleTokenRefresh;
149
160
  private clearTokenRefreshTimer;
@@ -1,4 +1,4 @@
1
- export { A as AuthManager, e as AuthStateListener, D as DEFAULT_API_URL, c as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from './constants-DtyJKJgJ.js';
1
+ export { A as AuthManager, e as AuthStateListener, D as DEFAULT_API_URL, c as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from './constants-DDOgvbL5.js';
2
2
  import { OAuth2Tokens } from '@docyrus/api-client';
3
3
  import { D as DocyrusAuthConfig, R as RouteChangePayload, H as HostNavigationHandler, h as HostNotificationHandler, k as GuidyCommandHandler, v as AppToHostMessage, n as GuidyElementsMessage, q as GuidyRoutesMessage, o as GuidyPointAckMessage, G as GuidyRoute, l as GuidyCommandMessage, a as AuthMode } from './types-D4ZAVj-B.js';
4
4
  export { A as AclOperation, i as AppRouteChangeMessage, c as AuthStatus, d as DocyrusAclRule, e as DocyrusAuthContextValue, j as DocyrusNotification, f as DocyrusRole, g as DocyrusUser, m as GuidyElement, p as GuidyPointMessage, r as GuidyScanMessage, s as HostNavigationMessage, t as HostNotificationMessage, u as HostSignInMessage, P as PermissionConfig, T as TokenRefreshRequestMessage } from './types-D4ZAVj-B.js';
@@ -148,6 +148,20 @@ var StandaloneOAuth2Auth = class {
148
148
  }
149
149
  };
150
150
 
151
+ // src/core/jwt.ts
152
+ function parseJwtExp(accessToken) {
153
+ const parts = accessToken?.split(".");
154
+ if (!parts || parts.length < 2) return null;
155
+ try {
156
+ const payload = JSON.parse(
157
+ atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
158
+ );
159
+ return typeof payload["exp"] === "number" ? payload["exp"] : null;
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+
151
165
  // src/core/iframe-auth.ts
152
166
  function postToHost(data) {
153
167
  const rnwv = window.ReactNativeWebView;
@@ -380,10 +394,11 @@ var IframeAuth = class {
380
394
  }
381
395
  handleSignin(signinData) {
382
396
  if (!signinData.accessToken) return;
397
+ const exp = parseJwtExp(signinData.accessToken);
383
398
  const tokens = {
384
399
  accessToken: signinData.accessToken,
385
400
  refreshToken: signinData.refreshToken,
386
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
401
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
387
402
  scope: ""
388
403
  };
389
404
  this.onTokensReceived?.(tokens);
@@ -832,6 +847,8 @@ var AuthManager = class {
832
847
  guidyBridge = null;
833
848
  reactNativeAuth = null;
834
849
  tokenRefreshTimer = null;
850
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
851
+ refreshInFlight = null;
835
852
  listeners = /* @__PURE__ */ new Set();
836
853
  navigationHandlers = /* @__PURE__ */ new Set();
837
854
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1232,26 +1249,41 @@ var AuthManager = class {
1232
1249
  */
1233
1250
  async getValidToken() {
1234
1251
  if (!this.tokens) return null;
1235
- const expiresAt = this.resolveExpiry(this.tokens);
1236
- if (this.autoRefreshEnabled && expiresAt) {
1252
+ if (this.autoRefreshEnabled) {
1253
+ const expiresAt = this.resolveExpiry(this.tokens);
1237
1254
  const nowSec = Math.floor(Date.now() / 1e3);
1238
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1255
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1256
+ if (needsRefresh) {
1239
1257
  try {
1240
- const freshToken = await this.handleTokenRefresh();
1241
- return freshToken;
1258
+ return await this.handleTokenRefresh();
1242
1259
  } catch {
1243
- return null;
1260
+ return expiresAt === null ? this.tokens.accessToken : null;
1244
1261
  }
1245
1262
  }
1246
1263
  }
1247
1264
  return this.tokens.accessToken;
1248
1265
  }
1249
1266
  /**
1250
- * Handle token refresh depending on mode.
1267
+ * Handle token refresh depending on mode, sharing a single in-flight
1268
+ * request across concurrent callers. Multiple requests crossing the expiry
1269
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1270
+ * each trigger their own refresh; with refresh-token rotation only the first
1271
+ * succeeds and the rest can leave us holding an expired token — exactly the
1272
+ * backend "JWTExpired" the proactive path is meant to prevent.
1273
+ */
1274
+ handleTokenRefresh() {
1275
+ if (this.refreshInFlight) return this.refreshInFlight;
1276
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1277
+ this.refreshInFlight = null;
1278
+ });
1279
+ return this.refreshInFlight;
1280
+ }
1281
+ /**
1282
+ * Refresh depending on mode.
1251
1283
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1252
1284
  * Iframe: send postMessage to host requesting new tokens.
1253
1285
  */
1254
- async handleTokenRefresh() {
1286
+ async performTokenRefresh() {
1255
1287
  if (this.mode === "standalone" && this.standaloneAuth) {
1256
1288
  const newTokens = await this.standaloneAuth.refreshToken();
1257
1289
  this.tokens = newTokens;
@@ -1312,17 +1344,7 @@ var AuthManager = class {
1312
1344
  */
1313
1345
  resolveExpiry(tokens) {
1314
1346
  if (tokens.expiresAt) return tokens.expiresAt;
1315
- const parts = tokens.accessToken?.split(".");
1316
- if (parts?.length >= 2) {
1317
- try {
1318
- const payload = JSON.parse(
1319
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1320
- );
1321
- if (typeof payload["exp"] === "number") return payload["exp"];
1322
- } catch {
1323
- }
1324
- }
1325
- return null;
1347
+ return parseJwtExp(tokens.accessToken);
1326
1348
  }
1327
1349
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1328
1350
  destroy() {