@docyrus/signin 0.14.1 → 0.16.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.
@@ -16,6 +16,8 @@ var DEFAULT_ALLOWED_HOST_PATTERN = /^https:\/\/[^/]+\.docyrus\.app$/;
16
16
  var REDIRECT_RETURN_KEY = "docyrus_auth_return_url";
17
17
  var TOKEN_EXPIRY_BUFFER_SECONDS = 60;
18
18
  var IFRAME_TOKEN_REFRESH_TIMEOUT_MS = 1e4;
19
+ var HOST_ACTION_TIMEOUT_MS = 15e3;
20
+ var MSGRAPH_REQUEST_TIMEOUT_MS = 3e4;
19
21
  var SSO_TOKEN_PARAM = "sso_token";
20
22
 
21
23
  // src/core/auth-detector.ts
@@ -189,6 +191,13 @@ var IframeAuth = class {
189
191
  stopped = false;
190
192
  refreshPromise = null;
191
193
  messageHandler = null;
194
+ /**
195
+ * In-flight app→host requests awaiting a correlated host reply, keyed by
196
+ * requestId. Powers Send to Docy / Send to Chat / the MS Graph bridge.
197
+ */
198
+ pendingRequests = /* @__PURE__ */ new Map();
199
+ /** Fallback monotonic counter for request ids when crypto.randomUUID is absent. */
200
+ requestCounter = 0;
192
201
  constructor(allowedOrigins, allowedPattern) {
193
202
  this.allowedOrigins = allowedOrigins ?? [];
194
203
  this.allowedPattern = allowedPattern ?? DEFAULT_ALLOWED_HOST_PATTERN;
@@ -212,6 +221,7 @@ var IframeAuth = class {
212
221
  this.messageHandler = null;
213
222
  }
214
223
  this.rejectPendingRefresh("IframeAuth stopped");
224
+ this.rejectAllPendingRequests("IframeAuth stopped");
215
225
  this.navigationHandlers.clear();
216
226
  this.notificationHandlers.clear();
217
227
  this.guidyHandlers.clear();
@@ -270,6 +280,19 @@ var IframeAuth = class {
270
280
  url: `${path}${search}${hash}`
271
281
  });
272
282
  }
283
+ /**
284
+ * Ask the host shell to navigate to a path/URL. The reverse of the
285
+ * host → app `navigation` message: posts a `navigation-request` over the
286
+ * same validated channel. The host decides how to honour it.
287
+ */
288
+ requestHostNavigation(url, options) {
289
+ if (this.stopped) return;
290
+ if (typeof url !== "string" || url.length === 0) return;
291
+ const message = { type: "navigation-request", url };
292
+ if (options?.replace) message.replace = true;
293
+ if (options?.newTab) message.newTab = true;
294
+ this.sendToHost(message);
295
+ }
273
296
  teardownRouteSync() {
274
297
  if (!this.routeSyncEnabled) return;
275
298
  if (typeof window !== "undefined") {
@@ -325,6 +348,119 @@ var IframeAuth = class {
325
348
  if (this.stopped) return;
326
349
  postToHost(message);
327
350
  }
351
+ /**
352
+ * Hand a payload to the host AI assistant (Docy). Resolves when the host
353
+ * acknowledges delivery, rejects on host error or timeout.
354
+ */
355
+ sendToDocy(payload) {
356
+ const requestId = this.nextRequestId();
357
+ return this.request(
358
+ { type: "docy:send", requestId, payload },
359
+ HOST_ACTION_TIMEOUT_MS
360
+ );
361
+ }
362
+ /**
363
+ * Hand a payload to the host team-chat app. Resolves when the host
364
+ * acknowledges delivery, rejects on host error or timeout.
365
+ */
366
+ sendToChat(payload) {
367
+ const requestId = this.nextRequestId();
368
+ return this.request(
369
+ { type: "chat:send", requestId, payload },
370
+ HOST_ACTION_TIMEOUT_MS
371
+ );
372
+ }
373
+ /**
374
+ * Hand a draft email to the host. Resolves when the host acknowledges,
375
+ * rejects on host error or timeout.
376
+ */
377
+ sendToEmail(payload) {
378
+ const requestId = this.nextRequestId();
379
+ return this.request(
380
+ { type: "email:send", requestId, payload },
381
+ HOST_ACTION_TIMEOUT_MS
382
+ );
383
+ }
384
+ /**
385
+ * Hand a calendar event to the host. Resolves when the host acknowledges,
386
+ * rejects on host error or timeout.
387
+ */
388
+ sendToCalendar(payload) {
389
+ const requestId = this.nextRequestId();
390
+ return this.request(
391
+ { type: "calendar:send", requestId, payload },
392
+ HOST_ACTION_TIMEOUT_MS
393
+ );
394
+ }
395
+ /** Ask the host whether its MSAL session is signed in. */
396
+ requestMsGraphStatus() {
397
+ const requestId = this.nextRequestId();
398
+ return this.request(
399
+ { type: "msgraph:status-request", requestId },
400
+ HOST_ACTION_TIMEOUT_MS
401
+ );
402
+ }
403
+ /** Perform a Microsoft Graph request through the host's MSAL session. */
404
+ requestMsGraph(request) {
405
+ const requestId = this.nextRequestId();
406
+ return this.request(
407
+ { type: "msgraph:request", requestId, request },
408
+ MSGRAPH_REQUEST_TIMEOUT_MS
409
+ );
410
+ }
411
+ /**
412
+ * Post a correlated app→host request and await the matching host reply.
413
+ * The reply is matched by `requestId` in `handleMessage`. Rejects on timeout
414
+ * or when the bridge is stopped.
415
+ */
416
+ request(message, timeoutMs) {
417
+ if (this.stopped) {
418
+ return Promise.reject(new Error("Cannot send host request: iframe bridge stopped"));
419
+ }
420
+ const { requestId } = message;
421
+ return new Promise((resolve, reject) => {
422
+ const timeoutId = setTimeout(() => {
423
+ this.pendingRequests.delete(requestId);
424
+ reject(new Error(`Host request "${message.type}" timed out`));
425
+ }, timeoutMs);
426
+ this.pendingRequests.set(requestId, {
427
+ resolve,
428
+ reject,
429
+ timeoutId
430
+ });
431
+ this.sendToHost(message);
432
+ });
433
+ }
434
+ /** Generate a correlation id, preferring crypto.randomUUID when available. */
435
+ nextRequestId() {
436
+ const cryptoObj = typeof crypto !== "undefined" ? crypto : void 0;
437
+ if (cryptoObj && typeof cryptoObj.randomUUID === "function") {
438
+ return cryptoObj.randomUUID();
439
+ }
440
+ this.requestCounter += 1;
441
+ return `req-${Date.now().toString(36)}-${this.requestCounter}-${Math.random().toString(36).slice(2, 10)}`;
442
+ }
443
+ resolvePendingRequest(requestId, value) {
444
+ const pending = this.pendingRequests.get(requestId);
445
+ if (!pending) return;
446
+ clearTimeout(pending.timeoutId);
447
+ this.pendingRequests.delete(requestId);
448
+ pending.resolve(value);
449
+ }
450
+ rejectPendingRequest(requestId, error) {
451
+ const pending = this.pendingRequests.get(requestId);
452
+ if (!pending) return;
453
+ clearTimeout(pending.timeoutId);
454
+ this.pendingRequests.delete(requestId);
455
+ pending.reject(error);
456
+ }
457
+ rejectAllPendingRequests(reason) {
458
+ for (const pending of this.pendingRequests.values()) {
459
+ clearTimeout(pending.timeoutId);
460
+ pending.reject(new Error(reason));
461
+ }
462
+ this.pendingRequests.clear();
463
+ }
328
464
  /**
329
465
  * Request fresh tokens from the host.
330
466
  * Posts a "token-refresh-request" to the host, then waits for
@@ -381,8 +517,57 @@ var IframeAuth = class {
381
517
  }
382
518
  if (data.type === "guidy:point" || data.type === "guidy:scan") {
383
519
  this.handleGuidyCommand(data);
520
+ return;
521
+ }
522
+ if (data.type === "docy:send-ack" || data.type === "chat:send-ack" || data.type === "email:send-ack" || data.type === "calendar:send-ack") {
523
+ this.handleActionAck(data);
524
+ return;
525
+ }
526
+ if (data.type === "msgraph:status-response") {
527
+ this.handleMsGraphStatusResponse(data);
528
+ return;
529
+ }
530
+ if (data.type === "msgraph:response") {
531
+ this.handleMsGraphResponse(data);
384
532
  }
385
533
  }
534
+ handleActionAck(message) {
535
+ if (typeof message.requestId !== "string") return;
536
+ if (message.ok) {
537
+ this.resolvePendingRequest(message.requestId, void 0);
538
+ } else {
539
+ this.rejectPendingRequest(
540
+ message.requestId,
541
+ new Error(message.error || `Host rejected ${message.type}`)
542
+ );
543
+ }
544
+ }
545
+ handleMsGraphStatusResponse(message) {
546
+ if (typeof message.requestId !== "string") return;
547
+ if (message.error) {
548
+ this.rejectPendingRequest(message.requestId, new Error(message.error));
549
+ return;
550
+ }
551
+ const status = {
552
+ signedIn: !!message.signedIn,
553
+ account: message.account ?? null
554
+ };
555
+ this.resolvePendingRequest(message.requestId, status);
556
+ }
557
+ handleMsGraphResponse(message) {
558
+ if (typeof message.requestId !== "string") return;
559
+ if (message.error && typeof message.status !== "number") {
560
+ this.rejectPendingRequest(message.requestId, new Error(message.error));
561
+ return;
562
+ }
563
+ const response = {
564
+ ok: !!message.ok,
565
+ status: typeof message.status === "number" ? message.status : 0,
566
+ data: message.data,
567
+ error: message.error
568
+ };
569
+ this.resolvePendingRequest(message.requestId, response);
570
+ }
386
571
  handleGuidyCommand(message) {
387
572
  for (const handler of this.guidyHandlers) {
388
573
  try {
@@ -684,6 +869,66 @@ var GuidyBridge = class {
684
869
  }
685
870
  };
686
871
 
872
+ // src/core/ms-graph-client.ts
873
+ var MsGraphClient = class {
874
+ iframeAuth;
875
+ constructor(iframeAuth) {
876
+ this.iframeAuth = iframeAuth;
877
+ }
878
+ /** Whether the host's MSAL session is signed in, plus optional account info. */
879
+ status() {
880
+ return this.iframeAuth.requestMsGraphStatus();
881
+ }
882
+ /** Convenience: resolves to whether the host's MSAL session is signed in. */
883
+ async isSignedIn() {
884
+ const status = await this.iframeAuth.requestMsGraphStatus();
885
+ return status.signedIn;
886
+ }
887
+ /** Perform an arbitrary Microsoft Graph request via the host's MSAL session. */
888
+ request(request) {
889
+ return this.iframeAuth.requestMsGraph(request);
890
+ }
891
+ /** GET a Graph resource, e.g. `get('/me/messages?$top=5')`. */
892
+ get(path, options) {
893
+ return this.iframeAuth.requestMsGraph({ method: "GET", path, ...options });
894
+ }
895
+ /** POST a JSON body to a Graph resource. */
896
+ post(path, body, options) {
897
+ return this.iframeAuth.requestMsGraph({
898
+ method: "POST",
899
+ path,
900
+ body,
901
+ ...options
902
+ });
903
+ }
904
+ /** PATCH a JSON body to a Graph resource. */
905
+ patch(path, body, options) {
906
+ return this.iframeAuth.requestMsGraph({
907
+ method: "PATCH",
908
+ path,
909
+ body,
910
+ ...options
911
+ });
912
+ }
913
+ /** PUT a JSON body to a Graph resource. */
914
+ put(path, body, options) {
915
+ return this.iframeAuth.requestMsGraph({
916
+ method: "PUT",
917
+ path,
918
+ body,
919
+ ...options
920
+ });
921
+ }
922
+ /** DELETE a Graph resource. */
923
+ delete(path, options) {
924
+ return this.iframeAuth.requestMsGraph({ method: "DELETE", path, ...options });
925
+ }
926
+ /** Convenience: GET `/me` — the signed-in user's profile. */
927
+ getMe() {
928
+ return this.iframeAuth.requestMsGraph({ method: "GET", path: "/me" });
929
+ }
930
+ };
931
+
687
932
  // src/core/sso-auth.ts
688
933
  var SsoAuth = class {
689
934
  config;
@@ -845,6 +1090,7 @@ var AuthManager = class {
845
1090
  standaloneAuth = null;
846
1091
  iframeAuth = null;
847
1092
  guidyBridge = null;
1093
+ msGraphClient = null;
848
1094
  reactNativeAuth = null;
849
1095
  tokenRefreshTimer = null;
850
1096
  /** Shared in-flight refresh, so concurrent requests don't trigger parallel refreshes. */
@@ -987,6 +1233,7 @@ var AuthManager = class {
987
1233
  */
988
1234
  initializeIframeMode() {
989
1235
  this.iframeAuth = new IframeAuth(this.config.allowedHostOrigins);
1236
+ this.msGraphClient = new MsGraphClient(this.iframeAuth);
990
1237
  this.iframeAuth.start((tokens) => {
991
1238
  this.setAuthenticated(tokens);
992
1239
  });
@@ -1054,6 +1301,61 @@ var AuthManager = class {
1054
1301
  notifyHostRouteChange(payload) {
1055
1302
  this.iframeAuth?.notifyRouteChange(payload);
1056
1303
  }
1304
+ /**
1305
+ * Iframe/WebView mode: ask the host shell to navigate to a path/URL.
1306
+ * Posts a `navigation-request` message; the host decides how to honour it.
1307
+ * No-op outside iframe mode.
1308
+ */
1309
+ requestHostNavigation(url, options) {
1310
+ this.iframeAuth?.requestHostNavigation(url, options);
1311
+ }
1312
+ /**
1313
+ * Iframe/WebView mode: hand a payload to the host AI assistant (Docy).
1314
+ * Resolves when the host acknowledges delivery. Rejects outside iframe mode.
1315
+ */
1316
+ sendToDocy(payload) {
1317
+ if (!this.iframeAuth) {
1318
+ return Promise.reject(new Error("sendToDocy() is only available in iframe/WebView mode"));
1319
+ }
1320
+ return this.iframeAuth.sendToDocy(payload);
1321
+ }
1322
+ /**
1323
+ * Iframe/WebView mode: hand a payload to the host team-chat app.
1324
+ * Resolves when the host acknowledges delivery. Rejects outside iframe mode.
1325
+ */
1326
+ sendToChat(payload) {
1327
+ if (!this.iframeAuth) {
1328
+ return Promise.reject(new Error("sendToChat() is only available in iframe/WebView mode"));
1329
+ }
1330
+ return this.iframeAuth.sendToChat(payload);
1331
+ }
1332
+ /**
1333
+ * Iframe/WebView mode: hand a draft email to the host. Resolves when the
1334
+ * host acknowledges. Rejects outside iframe mode.
1335
+ */
1336
+ sendToEmail(payload) {
1337
+ if (!this.iframeAuth) {
1338
+ return Promise.reject(new Error("sendToEmail() is only available in iframe/WebView mode"));
1339
+ }
1340
+ return this.iframeAuth.sendToEmail(payload);
1341
+ }
1342
+ /**
1343
+ * Iframe/WebView mode: hand a calendar event to the host. Resolves when the
1344
+ * host acknowledges. Rejects outside iframe mode.
1345
+ */
1346
+ sendToCalendar(payload) {
1347
+ if (!this.iframeAuth) {
1348
+ return Promise.reject(new Error("sendToCalendar() is only available in iframe/WebView mode"));
1349
+ }
1350
+ return this.iframeAuth.sendToCalendar(payload);
1351
+ }
1352
+ /**
1353
+ * Iframe/WebView mode: get the client for the host's Microsoft Graph (MSAL)
1354
+ * session. Returns `null` outside iframe mode.
1355
+ */
1356
+ getMsGraphClient() {
1357
+ return this.msGraphClient;
1358
+ }
1057
1359
  /**
1058
1360
  * Subscribe to host `navigation` messages. Returns an unsubscribe function.
1059
1361
  * Handlers registered before iframe mode is initialized are retained and
@@ -1175,6 +1477,7 @@ var AuthManager = class {
1175
1477
  this.guidyBridge = null;
1176
1478
  }
1177
1479
  this.iframeAuth.stop();
1480
+ this.msGraphClient = null;
1178
1481
  }
1179
1482
  if (this.config.ssr) {
1180
1483
  clearTokenCookie(this.config.ssrCookieKey);
@@ -1354,6 +1657,7 @@ var AuthManager = class {
1354
1657
  this.guidyBridge = null;
1355
1658
  }
1356
1659
  if (this.iframeAuth) this.iframeAuth.stop();
1660
+ this.msGraphClient = null;
1357
1661
  this.listeners.clear();
1358
1662
  this.navigationHandlers.clear();
1359
1663
  this.notificationHandlers.clear();
@@ -1460,6 +1764,6 @@ function hasPermission(user, operation, dataSourceId, config) {
1460
1764
  );
1461
1765
  }
1462
1766
 
1463
- export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, GuidyBridge, IframeAuth, StandaloneOAuth2Auth, detectAuthMode, getAllRoles, hasPermission, hasRole };
1767
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, GuidyBridge, IframeAuth, MsGraphClient, StandaloneOAuth2Auth, detectAuthMode, getAllRoles, hasPermission, hasRole };
1464
1768
  //# sourceMappingURL=core-index.js.map
1465
1769
  //# sourceMappingURL=core-index.js.map