@docyrus/signin 0.11.0 → 0.13.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.
package/dist/index.js CHANGED
@@ -154,10 +154,13 @@ var StandaloneOAuth2Auth = class {
154
154
  // src/core/iframe-auth.ts
155
155
  function postToHost(data) {
156
156
  const rnwv = window.ReactNativeWebView;
157
+ const tap = window.__DOCYRUS_DEVTOOLS_MESSAGE_TAP__;
157
158
  if (rnwv && typeof rnwv.postMessage === "function") {
159
+ if (typeof tap === "function") tap("out", data, { transport: "webview" });
158
160
  rnwv.postMessage(JSON.stringify(data));
159
161
  return;
160
162
  }
163
+ if (typeof tap === "function") tap("out", data, { transport: "iframe", targetOrigin: "*" });
161
164
  window.parent.postMessage(data, "*");
162
165
  }
163
166
  var IframeAuth = class {
@@ -167,6 +170,7 @@ var IframeAuth = class {
167
170
  onTokensReceived = null;
168
171
  navigationHandlers = /* @__PURE__ */ new Set();
169
172
  notificationHandlers = /* @__PURE__ */ new Set();
173
+ guidyHandlers = /* @__PURE__ */ new Set();
170
174
  routeSyncEnabled = false;
171
175
  originalPushState = null;
172
176
  originalReplaceState = null;
@@ -199,6 +203,7 @@ var IframeAuth = class {
199
203
  this.rejectPendingRefresh("IframeAuth stopped");
200
204
  this.navigationHandlers.clear();
201
205
  this.notificationHandlers.clear();
206
+ this.guidyHandlers.clear();
202
207
  this.teardownRouteSync();
203
208
  }
204
209
  /**
@@ -290,6 +295,25 @@ var IframeAuth = class {
290
295
  this.notificationHandlers.delete(handler);
291
296
  };
292
297
  }
298
+ /**
299
+ * Subscribe to host `guidy:*` command messages (point/scan).
300
+ * Returns an unsubscribe function. Used by the Guidy bridge runtime.
301
+ */
302
+ onGuidyCommand(handler) {
303
+ this.guidyHandlers.add(handler);
304
+ return () => {
305
+ this.guidyHandlers.delete(handler);
306
+ };
307
+ }
308
+ /**
309
+ * Post an app → host message over the same validated channel used for
310
+ * route-change and token-refresh. Used by the Guidy bridge to push its
311
+ * element inventory and point acknowledgements.
312
+ */
313
+ sendToHost(message) {
314
+ if (this.stopped) return;
315
+ postToHost(message);
316
+ }
293
317
  /**
294
318
  * Request fresh tokens from the host.
295
319
  * Posts a "token-refresh-request" to the host, then waits for
@@ -342,6 +366,19 @@ var IframeAuth = class {
342
366
  }
343
367
  if (data.type === "notification") {
344
368
  this.handleNotification(data);
369
+ return;
370
+ }
371
+ if (data.type === "guidy:point" || data.type === "guidy:scan") {
372
+ this.handleGuidyCommand(data);
373
+ }
374
+ }
375
+ handleGuidyCommand(message) {
376
+ for (const handler of this.guidyHandlers) {
377
+ try {
378
+ handler(message);
379
+ } catch (err) {
380
+ console.warn("[docyrus-signin] guidy command handler threw:", err);
381
+ }
345
382
  }
346
383
  }
347
384
  handleSignin(signinData) {
@@ -391,6 +428,237 @@ var IframeAuth = class {
391
428
  }
392
429
  };
393
430
 
431
+ // src/core/guidy-bridge.ts
432
+ var GUIDY_INTERNAL_PREFIX = "guidy-";
433
+ var HIGHLIGHT_CLASS = "guidy-bridge-highlight";
434
+ var HIGHLIGHT_STYLE_ID = "guidy-bridge-style";
435
+ var ANNOTATION_ID = "guidy-bridge-annotation";
436
+ var HIGHLIGHT_DURATION_MS = 5200;
437
+ var SCAN_DEBOUNCE_MS = 250;
438
+ var CLICK_DELAY_MS = 450;
439
+ function elementLabel(node) {
440
+ const aria = node.getAttribute("aria-label")?.trim();
441
+ if (aria) return aria;
442
+ const text = node.textContent?.replace(/\s+/g, " ").trim();
443
+ if (text) return text;
444
+ return node.getAttribute("title")?.trim() ?? "";
445
+ }
446
+ function isVisible(node) {
447
+ const rect = node.getBoundingClientRect();
448
+ if (rect.width === 0 && rect.height === 0) return false;
449
+ const style = window.getComputedStyle(node);
450
+ return style.visibility !== "hidden" && style.display !== "none";
451
+ }
452
+ var GuidyBridge = class {
453
+ send;
454
+ started = false;
455
+ observer = null;
456
+ scanTimer = null;
457
+ routeListener = null;
458
+ highlightTimer = null;
459
+ clickTimer = null;
460
+ annotationTimer = null;
461
+ constructor(send) {
462
+ this.send = send;
463
+ }
464
+ /**
465
+ * Begin reporting the element inventory and watching for changes.
466
+ * Idempotent. No-op outside a browser document.
467
+ */
468
+ start() {
469
+ if (this.started || typeof document === "undefined") return;
470
+ this.started = true;
471
+ this.ensureStyle();
472
+ this.postElements();
473
+ this.observer = new MutationObserver(() => this.scheduleScan());
474
+ this.observer.observe(document.body, {
475
+ childList: true,
476
+ subtree: true,
477
+ attributes: true,
478
+ attributeFilter: [
479
+ "id",
480
+ "hidden",
481
+ "aria-label",
482
+ "title",
483
+ "style"
484
+ ]
485
+ });
486
+ this.routeListener = () => this.scheduleScan();
487
+ window.addEventListener("popstate", this.routeListener);
488
+ window.addEventListener("hashchange", this.routeListener);
489
+ }
490
+ /** Stop watching and tear down any injected overlay. Idempotent. */
491
+ stop() {
492
+ if (!this.started) return;
493
+ this.started = false;
494
+ this.observer?.disconnect();
495
+ this.observer = null;
496
+ if (this.routeListener) {
497
+ window.removeEventListener("popstate", this.routeListener);
498
+ window.removeEventListener("hashchange", this.routeListener);
499
+ this.routeListener = null;
500
+ }
501
+ this.clearTimer("scanTimer");
502
+ this.clearTimer("highlightTimer");
503
+ this.clearTimer("clickTimer");
504
+ this.clearTimer("annotationTimer");
505
+ this.clearHighlight();
506
+ this.clearAnnotation();
507
+ }
508
+ /** Dispatch a host `guidy:*` command. Wired to `IframeAuth.onGuidyCommand`. */
509
+ handleCommand(message) {
510
+ if (message.type === "guidy:scan") {
511
+ this.postElements();
512
+ return;
513
+ }
514
+ if (message.type === "guidy:point") {
515
+ this.handlePoint(message);
516
+ }
517
+ }
518
+ /** Collect every visible anchor/button that exposes an id and a label. */
519
+ collectElements() {
520
+ if (typeof document === "undefined") return [];
521
+ const nodes = document.querySelectorAll("a[id], button[id]");
522
+ const seen = /* @__PURE__ */ new Set();
523
+ const result = [];
524
+ nodes.forEach((node) => {
525
+ const { id } = node;
526
+ if (!id || id.startsWith(GUIDY_INTERNAL_PREFIX) || seen.has(id)) return;
527
+ if (!isVisible(node)) return;
528
+ const label = elementLabel(node);
529
+ if (!label) return;
530
+ seen.add(id);
531
+ result.push({
532
+ id,
533
+ label,
534
+ tag: node.tagName.toLowerCase() === "a" ? "a" : "button"
535
+ });
536
+ });
537
+ return result;
538
+ }
539
+ postElements() {
540
+ if (!this.started) return;
541
+ const path = typeof window === "undefined" ? "" : window.location.pathname;
542
+ this.send({
543
+ type: "guidy:elements",
544
+ path,
545
+ elements: this.collectElements()
546
+ });
547
+ }
548
+ scheduleScan() {
549
+ if (this.scanTimer) return;
550
+ this.scanTimer = setTimeout(() => {
551
+ this.scanTimer = null;
552
+ this.postElements();
553
+ }, SCAN_DEBOUNCE_MS);
554
+ }
555
+ handlePoint(message) {
556
+ const element = typeof document === "undefined" ? null : document.getElementById(message.id);
557
+ if (!element) {
558
+ this.send({ type: "guidy:point-ack", id: message.id, ok: false });
559
+ return;
560
+ }
561
+ element.scrollIntoView({ block: "center", behavior: "smooth" });
562
+ this.highlight(element);
563
+ if (message.message) {
564
+ this.annotate(element, message.message);
565
+ }
566
+ if (message.click) {
567
+ this.clearTimer("clickTimer");
568
+ this.clickTimer = setTimeout(() => {
569
+ this.clickTimer = null;
570
+ element.click();
571
+ }, CLICK_DELAY_MS);
572
+ }
573
+ this.send({ type: "guidy:point-ack", id: message.id, ok: true });
574
+ }
575
+ ensureStyle() {
576
+ if (typeof document === "undefined") return;
577
+ if (document.getElementById(HIGHLIGHT_STYLE_ID)) return;
578
+ const style = document.createElement("style");
579
+ style.id = HIGHLIGHT_STYLE_ID;
580
+ style.textContent = `
581
+ .${HIGHLIGHT_CLASS} {
582
+ outline: 2px solid #3b82f6 !important;
583
+ outline-offset: 2px;
584
+ border-radius: 8px;
585
+ animation: guidy-bridge-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
586
+ }
587
+ @keyframes guidy-bridge-ping {
588
+ 0% {
589
+ box-shadow: 0 0 0 0 color-mix(in srgb, #3b82f6 50%, transparent);
590
+ }
591
+ 75%, 100% {
592
+ box-shadow: 0 0 0 10px color-mix(in srgb, #3b82f6 0%, transparent);
593
+ }
594
+ }
595
+ #${ANNOTATION_ID} {
596
+ position: fixed;
597
+ z-index: 2147483647;
598
+ max-width: 240px;
599
+ padding: 8px 12px;
600
+ border-radius: 10px;
601
+ background: #111827;
602
+ color: #f9fafb;
603
+ font: 13px/1.4 system-ui, -apple-system, sans-serif;
604
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
605
+ pointer-events: none;
606
+ }
607
+ `;
608
+ document.head.appendChild(style);
609
+ }
610
+ highlight(element) {
611
+ this.clearTimer("highlightTimer");
612
+ this.clearHighlight();
613
+ element.classList.add(HIGHLIGHT_CLASS);
614
+ this.highlightTimer = setTimeout(() => {
615
+ this.highlightTimer = null;
616
+ this.clearHighlight();
617
+ }, HIGHLIGHT_DURATION_MS);
618
+ }
619
+ clearHighlight() {
620
+ if (typeof document === "undefined") return;
621
+ document.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => node.classList.remove(HIGHLIGHT_CLASS));
622
+ }
623
+ annotate(element, message) {
624
+ if (typeof document === "undefined") return;
625
+ this.clearTimer("annotationTimer");
626
+ this.clearAnnotation();
627
+ const bubble = document.createElement("div");
628
+ bubble.id = ANNOTATION_ID;
629
+ bubble.textContent = message;
630
+ document.body.appendChild(bubble);
631
+ const rect = element.getBoundingClientRect();
632
+ const bubbleRect = bubble.getBoundingClientRect();
633
+ const gap = 10;
634
+ let top = rect.bottom + gap;
635
+ if (top + bubbleRect.height > window.innerHeight) {
636
+ top = Math.max(gap, rect.top - bubbleRect.height - gap);
637
+ }
638
+ const left = Math.min(
639
+ Math.max(gap, rect.left),
640
+ window.innerWidth - bubbleRect.width - gap
641
+ );
642
+ bubble.style.top = `${Math.round(top)}px`;
643
+ bubble.style.left = `${Math.round(left)}px`;
644
+ this.annotationTimer = setTimeout(() => {
645
+ this.annotationTimer = null;
646
+ this.clearAnnotation();
647
+ }, HIGHLIGHT_DURATION_MS);
648
+ }
649
+ clearAnnotation() {
650
+ if (typeof document === "undefined") return;
651
+ document.getElementById(ANNOTATION_ID)?.remove();
652
+ }
653
+ clearTimer(key) {
654
+ const timer = this[key];
655
+ if (timer) {
656
+ clearTimeout(timer);
657
+ this[key] = null;
658
+ }
659
+ }
660
+ };
661
+
394
662
  // src/core/sso-auth.ts
395
663
  var SsoAuth = class {
396
664
  config;
@@ -557,6 +825,7 @@ var AuthManager = class {
557
825
  user = null;
558
826
  standaloneAuth = null;
559
827
  iframeAuth = null;
828
+ guidyBridge = null;
560
829
  reactNativeAuth = null;
561
830
  tokenRefreshTimer = null;
562
831
  listeners = /* @__PURE__ */ new Set();
@@ -721,6 +990,20 @@ var AuthManager = class {
721
990
  if (this.config.syncRouteToHost) {
722
991
  this.iframeAuth.enableRouteSync();
723
992
  }
993
+ if (this.config.enableGuidyBridge) {
994
+ this.startGuidyBridge();
995
+ }
996
+ }
997
+ /**
998
+ * Start the Guidy bridge runtime: report the app's clickable inventory to
999
+ * the host and execute host scan/point/click commands. Iframe mode only.
1000
+ */
1001
+ startGuidyBridge() {
1002
+ if (!this.iframeAuth || this.guidyBridge) return;
1003
+ const bridge = new GuidyBridge((message) => this.iframeAuth?.sendToHost(message));
1004
+ this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1005
+ bridge.start();
1006
+ this.guidyBridge = bridge;
724
1007
  }
725
1008
  /**
726
1009
  * Iframe/WebView mode: start auto-syncing the embedded app's route
@@ -855,6 +1138,10 @@ var AuthManager = class {
855
1138
  await this.reactNativeAuth.logout();
856
1139
  }
857
1140
  if (this.mode === "iframe" && this.iframeAuth) {
1141
+ if (this.guidyBridge) {
1142
+ this.guidyBridge.stop();
1143
+ this.guidyBridge = null;
1144
+ }
858
1145
  this.iframeAuth.stop();
859
1146
  }
860
1147
  if (this.config.ssr) {
@@ -1025,6 +1312,10 @@ var AuthManager = class {
1025
1312
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1026
1313
  destroy() {
1027
1314
  this.clearTokenRefreshTimer();
1315
+ if (this.guidyBridge) {
1316
+ this.guidyBridge.stop();
1317
+ this.guidyBridge = null;
1318
+ }
1028
1319
  if (this.iframeAuth) this.iframeAuth.stop();
1029
1320
  this.listeners.clear();
1030
1321
  this.navigationHandlers.clear();
@@ -1380,6 +1671,6 @@ function SignInButton({
1380
1671
  );
1381
1672
  }
1382
1673
 
1383
- export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1674
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, GuidyBridge, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1384
1675
  //# sourceMappingURL=index.js.map
1385
1676
  //# sourceMappingURL=index.js.map