@docyrus/signin 0.13.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.
@@ -1,8 +1,8 @@
1
1
  import { OAuth2Tokens } from '@docyrus/api-client';
2
- import { D as DocyrusAuthConfig } from '../types-WOgdg9b0.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-WOgdg9b0.js';
4
- export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from '../use-docyrus-client-DqdJWKrF.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-BBGO2aU5.js';
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-DDOgvbL5.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;
@@ -460,10 +474,11 @@ var IframeAuth = class {
460
474
  }
461
475
  handleSignin(signinData) {
462
476
  if (!signinData.accessToken) return;
477
+ const exp = parseJwtExp(signinData.accessToken);
463
478
  const tokens = {
464
479
  accessToken: signinData.accessToken,
465
480
  refreshToken: signinData.refreshToken,
466
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
481
+ expiresAt: exp ?? Math.floor(Date.now() / 1e3) + 3600,
467
482
  scope: ""
468
483
  };
469
484
  this.onTokensReceived?.(tokens);
@@ -528,6 +543,7 @@ function isVisible(node) {
528
543
  }
529
544
  var GuidyBridge = class {
530
545
  send;
546
+ routes;
531
547
  started = false;
532
548
  observer = null;
533
549
  scanTimer = null;
@@ -535,8 +551,14 @@ var GuidyBridge = class {
535
551
  highlightTimer = null;
536
552
  clickTimer = null;
537
553
  annotationTimer = null;
538
- constructor(send) {
554
+ constructor(send, routes = []) {
539
555
  this.send = send;
556
+ this.routes = routes;
557
+ }
558
+ /** Replace the declared navigable routes and re-post them to the host. */
559
+ setRoutes(routes) {
560
+ this.routes = routes;
561
+ this.postRoutes();
540
562
  }
541
563
  /**
542
564
  * Begin reporting the element inventory and watching for changes.
@@ -547,6 +569,7 @@ var GuidyBridge = class {
547
569
  this.started = true;
548
570
  this.ensureStyle();
549
571
  this.postElements();
572
+ this.postRoutes();
550
573
  this.observer = new MutationObserver(() => this.scheduleScan());
551
574
  this.observer.observe(document.body, {
552
575
  childList: true,
@@ -586,6 +609,7 @@ var GuidyBridge = class {
586
609
  handleCommand(message) {
587
610
  if (message.type === "guidy:scan") {
588
611
  this.postElements();
612
+ this.postRoutes();
589
613
  return;
590
614
  }
591
615
  if (message.type === "guidy:point") {
@@ -622,6 +646,10 @@ var GuidyBridge = class {
622
646
  elements: this.collectElements()
623
647
  });
624
648
  }
649
+ postRoutes() {
650
+ if (!this.started) return;
651
+ this.send({ type: "guidy:routes", routes: this.routes });
652
+ }
625
653
  scheduleScan() {
626
654
  if (this.scanTimer) return;
627
655
  this.scanTimer = setTimeout(() => {
@@ -823,6 +851,8 @@ var AuthManager = class {
823
851
  guidyBridge = null;
824
852
  reactNativeAuth = null;
825
853
  tokenRefreshTimer = null;
854
+ /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
855
+ refreshInFlight = null;
826
856
  listeners = /* @__PURE__ */ new Set();
827
857
  navigationHandlers = /* @__PURE__ */ new Set();
828
858
  notificationHandlers = /* @__PURE__ */ new Set();
@@ -995,11 +1025,22 @@ var AuthManager = class {
995
1025
  */
996
1026
  startGuidyBridge() {
997
1027
  if (!this.iframeAuth || this.guidyBridge) return;
998
- const bridge = new GuidyBridge((message) => this.iframeAuth?.sendToHost(message));
1028
+ const bridge = new GuidyBridge(
1029
+ (message) => this.iframeAuth?.sendToHost(message),
1030
+ this.config.guidyRoutes ?? []
1031
+ );
999
1032
  this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1000
1033
  bridge.start();
1001
1034
  this.guidyBridge = bridge;
1002
1035
  }
1036
+ /**
1037
+ * Iframe mode: replace the routes the Guidy bridge advertises to the host.
1038
+ * No-op unless the bridge is running (`enableGuidyBridge`).
1039
+ */
1040
+ setGuidyRoutes(routes) {
1041
+ this.config.guidyRoutes = routes;
1042
+ this.guidyBridge?.setRoutes(routes);
1043
+ }
1003
1044
  /**
1004
1045
  * Iframe/WebView mode: start auto-syncing the embedded app's route
1005
1046
  * to the host. Patches history methods and listens for popstate /
@@ -1212,26 +1253,41 @@ var AuthManager = class {
1212
1253
  */
1213
1254
  async getValidToken() {
1214
1255
  if (!this.tokens) return null;
1215
- const expiresAt = this.resolveExpiry(this.tokens);
1216
- if (this.autoRefreshEnabled && expiresAt) {
1256
+ if (this.autoRefreshEnabled) {
1257
+ const expiresAt = this.resolveExpiry(this.tokens);
1217
1258
  const nowSec = Math.floor(Date.now() / 1e3);
1218
- if (nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS) {
1259
+ const needsRefresh = expiresAt === null || nowSec >= expiresAt - TOKEN_EXPIRY_BUFFER_SECONDS;
1260
+ if (needsRefresh) {
1219
1261
  try {
1220
- const freshToken = await this.handleTokenRefresh();
1221
- return freshToken;
1262
+ return await this.handleTokenRefresh();
1222
1263
  } catch {
1223
- return null;
1264
+ return expiresAt === null ? this.tokens.accessToken : null;
1224
1265
  }
1225
1266
  }
1226
1267
  }
1227
1268
  return this.tokens.accessToken;
1228
1269
  }
1229
1270
  /**
1230
- * Handle token refresh depending on mode.
1271
+ * Handle token refresh depending on mode, sharing a single in-flight
1272
+ * request across concurrent callers. Multiple requests crossing the expiry
1273
+ * buffer at once (e.g. a dashboard firing several queries) would otherwise
1274
+ * each trigger their own refresh; with refresh-token rotation only the first
1275
+ * succeeds and the rest can leave us holding an expired token — exactly the
1276
+ * backend "JWTExpired" the proactive path is meant to prevent.
1277
+ */
1278
+ handleTokenRefresh() {
1279
+ if (this.refreshInFlight) return this.refreshInFlight;
1280
+ this.refreshInFlight = this.performTokenRefresh().finally(() => {
1281
+ this.refreshInFlight = null;
1282
+ });
1283
+ return this.refreshInFlight;
1284
+ }
1285
+ /**
1286
+ * Refresh depending on mode.
1231
1287
  * Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
1232
1288
  * Iframe: send postMessage to host requesting new tokens.
1233
1289
  */
1234
- async handleTokenRefresh() {
1290
+ async performTokenRefresh() {
1235
1291
  if (this.mode === "standalone" && this.standaloneAuth) {
1236
1292
  const newTokens = await this.standaloneAuth.refreshToken();
1237
1293
  this.tokens = newTokens;
@@ -1292,17 +1348,7 @@ var AuthManager = class {
1292
1348
  */
1293
1349
  resolveExpiry(tokens) {
1294
1350
  if (tokens.expiresAt) return tokens.expiresAt;
1295
- const parts = tokens.accessToken?.split(".");
1296
- if (parts?.length >= 2) {
1297
- try {
1298
- const payload = JSON.parse(
1299
- atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"))
1300
- );
1301
- if (typeof payload["exp"] === "number") return payload["exp"];
1302
- } catch {
1303
- }
1304
- }
1305
- return null;
1351
+ return parseJwtExp(tokens.accessToken);
1306
1352
  }
1307
1353
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1308
1354
  destroy() {
@@ -1502,6 +1548,11 @@ function DocyrusAuthProvider({
1502
1548
  managerRef.current?.notifyHostRouteChange(payload);
1503
1549
  };
1504
1550
  }, []);
1551
+ const setGuidyRoutes = useMemo(() => {
1552
+ return (routes) => {
1553
+ managerRef.current?.setGuidyRoutes(routes);
1554
+ };
1555
+ }, []);
1505
1556
  const value = useMemo(() => ({
1506
1557
  status,
1507
1558
  mode,
@@ -1519,6 +1570,7 @@ function DocyrusAuthProvider({
1519
1570
  onHostNotification,
1520
1571
  enableHostRouteSync,
1521
1572
  notifyRouteChange,
1573
+ setGuidyRoutes,
1522
1574
  error
1523
1575
  }), [
1524
1576
  status,
@@ -1537,6 +1589,7 @@ function DocyrusAuthProvider({
1537
1589
  onHostNotification,
1538
1590
  enableHostRouteSync,
1539
1591
  notifyRouteChange,
1592
+ setGuidyRoutes,
1540
1593
  error
1541
1594
  ]);
1542
1595
  return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });