@docyrus/signin 0.14.0 → 0.15.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.
@@ -1,8 +1,8 @@
1
1
  import { OAuth2Tokens } from '@docyrus/api-client';
2
- import { D as DocyrusAuthConfig } from '../types-D4ZAVj-B.js';
3
- export { A as AclOperation, a as AuthMode, b as AuthSessionResult, c as AuthStatus, d as DocyrusAclRule, e as DocyrusAuthContextValue, f as DocyrusRole, g as DocyrusUser, O as OpenAuthSessionFn, P as PermissionConfig } from '../types-D4ZAVj-B.js';
4
- export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from '../use-docyrus-client-o9nPwayd.js';
5
- export { A as AuthManager, D as DEFAULT_API_URL, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from '../constants-DtyJKJgJ.js';
2
+ import { D as DocyrusAuthConfig } from '../types-DzwC4Dk7.js';
3
+ export { A as AclOperation, a as AuthMode, b as AuthSessionResult, c as AuthStatus, d as DocyrusAclRule, e as DocyrusAuthContextValue, f as DocyrusRole, g as DocyrusUser, O as OpenAuthSessionFn, P as PermissionConfig } from '../types-DzwC4Dk7.js';
4
+ export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from '../use-docyrus-client-pU5z5ZHe.js';
5
+ export { A as AuthManager, D as DEFAULT_API_URL, a as DEFAULT_OAUTH_SCOPES, g as getAllRoles, h as hasPermission, b as hasRole } from '../constants-BoAqIz2O.js';
6
6
  import 'react';
7
7
 
8
8
  /**
@@ -228,6 +228,20 @@ var StandaloneOAuth2Auth = class {
228
228
  }
229
229
  };
230
230
 
231
+ // src/core/jwt.ts
232
+ function parseJwtExp(accessToken) {
233
+ const parts = accessToken?.split(".");
234
+ if (!parts || parts.length < 2) return null;
235
+ try {
236
+ const payload = JSON.parse(
237
+ atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
238
+ );
239
+ return typeof payload["exp"] === "number" ? payload["exp"] : null;
240
+ } catch {
241
+ return null;
242
+ }
243
+ }
244
+
231
245
  // src/core/iframe-auth.ts
232
246
  function postToHost(data) {
233
247
  const rnwv = window.ReactNativeWebView;
@@ -336,6 +350,19 @@ var IframeAuth = class {
336
350
  url: `${path}${search}${hash}`
337
351
  });
338
352
  }
353
+ /**
354
+ * Ask the host shell to navigate to a path/URL. The reverse of the
355
+ * host → app `navigation` message: posts a `navigation-request` over the
356
+ * same validated channel. The host decides how to honour it.
357
+ */
358
+ requestHostNavigation(url, options) {
359
+ if (this.stopped) return;
360
+ if (typeof url !== "string" || url.length === 0) return;
361
+ const message = { type: "navigation-request", url };
362
+ if (options?.replace) message.replace = true;
363
+ if (options?.newTab) message.newTab = true;
364
+ this.sendToHost(message);
365
+ }
339
366
  teardownRouteSync() {
340
367
  if (!this.routeSyncEnabled) return;
341
368
  if (typeof window !== "undefined") {
@@ -460,10 +487,11 @@ var IframeAuth = class {
460
487
  }
461
488
  handleSignin(signinData) {
462
489
  if (!signinData.accessToken) return;
490
+ const exp = parseJwtExp(signinData.accessToken);
463
491
  const tokens = {
464
492
  accessToken: signinData.accessToken,
465
493
  refreshToken: signinData.refreshToken,
466
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
494
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
467
495
  scope: ""
468
496
  };
469
497
  this.onTokensReceived?.(tokens);
@@ -836,6 +864,8 @@ var AuthManager = class {
836
864
  guidyBridge = null;
837
865
  reactNativeAuth = null;
838
866
  tokenRefreshTimer = null;
867
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
868
+ refreshInFlight = null;
839
869
  listeners = /* @__PURE__ */ new Set();
840
870
  navigationHandlers = /* @__PURE__ */ new Set();
841
871
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -1041,6 +1071,14 @@ var AuthManager = class {
1041
1071
  notifyHostRouteChange(payload) {
1042
1072
  this.iframeAuth?.notifyRouteChange(payload);
1043
1073
  }
1074
+ /**
1075
+ * Iframe/WebView mode: ask the host shell to navigate to a path/URL.
1076
+ * Posts a `navigation-request` message; the host decides how to honour it.
1077
+ * No-op outside iframe mode.
1078
+ */
1079
+ requestHostNavigation(url, options) {
1080
+ this.iframeAuth?.requestHostNavigation(url, options);
1081
+ }
1044
1082
  /**
1045
1083
  * Subscribe to host `navigation` messages. Returns an unsubscribe function.
1046
1084
  * Handlers registered before iframe mode is initialized are retained and
@@ -1236,26 +1274,41 @@ var AuthManager = class {
1236
1274
  */
1237
1275
  async getValidToken() {
1238
1276
  if (!this.tokens) return null;
1239
- const expiresAt = this.resolveExpiry(this.tokens);
1240
- if (this.autoRefreshEnabled && expiresAt) {
1277
+ if (this.autoRefreshEnabled) {
1278
+ const expiresAt = this.resolveExpiry(this.tokens);
1241
1279
  const nowSec = Math.floor(Date.now() / 1e3);
1242
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1280
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1281
+ if (needsRefresh) {
1243
1282
  try {
1244
- const freshToken = await this.handleTokenRefresh();
1245
- return freshToken;
1283
+ return await this.handleTokenRefresh();
1246
1284
  } catch {
1247
- return null;
1285
+ return expiresAt === null ? this.tokens.accessToken : null;
1248
1286
  }
1249
1287
  }
1250
1288
  }
1251
1289
  return this.tokens.accessToken;
1252
1290
  }
1253
1291
  /**
1254
- * Handle token refresh depending on mode.
1292
+ * Handle token refresh depending on mode, sharing a single in-flight
1293
+ * request across concurrent callers. Multiple requests crossing the expiry
1294
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1295
+ * each trigger their own refresh; with refresh-token rotation only the first
1296
+ * succeeds and the rest can leave us holding an expired token — exactly the
1297
+ * backend "JWTExpired" the proactive path is meant to prevent.
1298
+ */
1299
+ handleTokenRefresh() {
1300
+ if (this.refreshInFlight) return this.refreshInFlight;
1301
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1302
+ this.refreshInFlight = null;
1303
+ });
1304
+ return this.refreshInFlight;
1305
+ }
1306
+ /**
1307
+ * Refresh depending on mode.
1255
1308
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1256
1309
  * Iframe: send postMessage to host requesting new tokens.
1257
1310
  */
1258
- async handleTokenRefresh() {
1311
+ async performTokenRefresh() {
1259
1312
  if (this.mode === "standalone" && this.standaloneAuth) {
1260
1313
  const newTokens = await this.standaloneAuth.refreshToken();
1261
1314
  this.tokens = newTokens;
@@ -1316,17 +1369,7 @@ var AuthManager = class {
1316
1369
  */
1317
1370
  resolveExpiry(tokens) {
1318
1371
  if (tokens.expiresAt) return tokens.expiresAt;
1319
- const parts = tokens.accessToken?.split(".");
1320
- if (parts?.length >= 2) {
1321
- try {
1322
- const payload = JSON.parse(
1323
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1324
- );
1325
- if (typeof payload["exp"] === "number") return payload["exp"];
1326
- } catch {
1327
- }
1328
- }
1329
- return null;
1372
+ return parseJwtExp(tokens.accessToken);
1330
1373
  }
1331
1374
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1332
1375
  destroy() {
@@ -1526,6 +1569,11 @@ function DocyrusAuthProvider({
1526
1569
  managerRef.current?.notifyHostRouteChange(payload);
1527
1570
  };
1528
1571
  }, []);
1572
+ const requestHostNavigation = useMemo(() => {
1573
+ return (url, options) => {
1574
+ managerRef.current?.requestHostNavigation(url, options);
1575
+ };
1576
+ }, []);
1529
1577
  const setGuidyRoutes = useMemo(() => {
1530
1578
  return (routes) => {
1531
1579
  managerRef.current?.setGuidyRoutes(routes);
@@ -1548,6 +1596,7 @@ function DocyrusAuthProvider({
1548
1596
  onHostNotification,
1549
1597
  enableHostRouteSync,
1550
1598
  notifyRouteChange,
1599
+ requestHostNavigation,
1551
1600
  setGuidyRoutes,
1552
1601
  error
1553
1602
  }), [
@@ -1567,6 +1616,7 @@ function DocyrusAuthProvider({
1567
1616
  onHostNotification,
1568
1617
  enableHostRouteSync,
1569
1618
  notifyRouteChange,
1619
+ requestHostNavigation,
1570
1620
  setGuidyRoutes,
1571
1621
  error
1572
1622
  ]);