@docyrus/signin 0.18.2 → 0.19.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.
@@ -31,4 +31,5 @@ declare class ElectronTokenStorage implements OAuth2TokenStorage {
31
31
  private storeDelete;
32
32
  }
33
33
 
34
- export { type ElectronStoreAPI, ElectronTokenStorage };
34
+ export { ElectronTokenStorage };
35
+ export type { ElectronStoreAPI };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from './use-docyrus-client-C9uiHma9.js';
2
- import { G as GuidyRoute, H as HostNavigationHandler, v as HostNotificationHandler, p as MsGraphClient, w as HostNavigationRequestOptions, h as DocyrusCalendarEventPayload, m as DocyrusSharePayload, i as DocyrusEmailPayload, e as AuthStatus } from './types-BcLDyMdw.js';
3
- export { A as AclOperation, a as AclTargetType, b as AdaptiveCard, x as AppNavigationRequestMessage, y as AppRouteChangeMessage, c as AuthMode, C as CalendarSendAckMessage, z as CalendarSendMessage, B as ChatSendAckMessage, E as ChatSendMessage, F as DocySendAckMessage, I as DocySendMessage, f as DocyrusAclRule, D as DocyrusAuthConfig, g as DocyrusAuthContextValue, J as DocyrusNotification, j as DocyrusPermission, k as DocyrusRole, l as DocyrusShareAdaptiveCardPayload, n as DocyrusShareTextPayload, o as DocyrusUser, K as EmailSendAckMessage, L as EmailSendMessage, N as GuidyCommandHandler, Q as GuidyCommandMessage, S as GuidyElement, T as GuidyElementsMessage, U as GuidyPointAckMessage, V as GuidyPointMessage, W as GuidyRoutesMessage, X as GuidyScanMessage, Y as HostNavigationMessage, Z as HostNotificationMessage, _ as HostSignInMessage, $ as IframeAuth, M as MsGraphAccount, q as MsGraphRequest, a0 as MsGraphRequestMessage, r as MsGraphRequestOptions, s as MsGraphResponse, a1 as MsGraphResponseMessage, t as MsGraphStatus, a2 as MsGraphStatusRequestMessage, a3 as MsGraphStatusResponseMessage, P as PermissionConfig, u as PermissionScope, R as RouteChangePayload, a4 as TokenRefreshRequestMessage } from './types-BcLDyMdw.js';
1
+ export { D as DocyrusAuthContext, a as DocyrusAuthProvider, b as DocyrusAuthProviderProps, u as useDocyrusAuth, c as useDocyrusClient } from './use-docyrus-client-D-oTYabD.js';
2
+ import { G as GuidyRoute, H as HostNavigationHandler, v as HostNotificationHandler, p as MsGraphClient, w as HostNavigationRequestOptions, h as DocyrusCalendarEventPayload, m as DocyrusSharePayload, i as DocyrusEmailPayload, e as AuthStatus } from './types-C3TY5WcX.js';
3
+ export { A as AclOperation, a as AclTargetType, b as AdaptiveCard, x as AppNavigationRequestMessage, y as AppRouteChangeMessage, c as AuthMode, C as CalendarSendAckMessage, z as CalendarSendMessage, B as ChatSendAckMessage, E as ChatSendMessage, F as DocySendAckMessage, I as DocySendMessage, f as DocyrusAclRule, D as DocyrusAuthConfig, g as DocyrusAuthContextValue, J as DocyrusNotification, j as DocyrusPermission, k as DocyrusRole, l as DocyrusShareAdaptiveCardPayload, n as DocyrusShareTextPayload, o as DocyrusUser, K as EmailSendAckMessage, L as EmailSendMessage, N as GuidyCommandHandler, Q as GuidyCommandMessage, S as GuidyElement, T as GuidyElementsMessage, U as GuidyPointAckMessage, V as GuidyPointMessage, W as GuidyRoutesMessage, X as GuidyScanMessage, Y as HostNavigationMessage, Z as HostNotificationMessage, _ as HostSignInMessage, $ as IframeAuth, M as MsGraphAccount, q as MsGraphRequest, a0 as MsGraphRequestMessage, r as MsGraphRequestOptions, s as MsGraphResponse, a1 as MsGraphResponseMessage, t as MsGraphStatus, a2 as MsGraphStatusRequestMessage, a3 as MsGraphStatusResponseMessage, P as PermissionConfig, u as PermissionScope, R as RouteChangePayload, a4 as TokenRefreshRequestMessage } from './types-C3TY5WcX.js';
4
4
  import * as react from 'react';
5
5
  import { CSSProperties, ReactNode } from 'react';
6
- export { A as AuthManager, D as DEFAULT_API_URL, d as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, e as DEFAULT_SSR_COOKIE_KEY, g as getAllRoles, b as getAllowedOperations, h as hasPermission, c as hasRole } from './constants-CvMiR-RM.js';
6
+ export { A as AuthManager, D as DEFAULT_API_URL, d as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES, e as DEFAULT_SSR_COOKIE_KEY, g as getAllRoles, b as getAllowedOperations, h as hasPermission, c as hasRole } from './constants-DXgxjFEl.js';
7
7
  export { GuidyBridge, StandaloneOAuth2Auth, detectAuthMode } from './core-index.js';
8
8
  import '@docyrus/api-client';
9
9
 
@@ -122,4 +122,5 @@ declare function clearTokenCookie(cookieKey?: string): void;
122
122
  */
123
123
  declare function getTokenFromCookie(cookieKey?: string): string | null;
124
124
 
125
- export { AuthStatus, DocyrusCalendarEventPayload, DocyrusEmailPayload, DocyrusSharePayload, GuidyRoute, HostNavigationHandler, HostNavigationRequestOptions, HostNotificationHandler, MsGraphClient, SignInButton, type SignInButtonProps, clearTokenCookie, getTokenFromCookie, syncTokenToCookie, useDocyrusGuidyRoutes, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync, useDocyrusMsGraph, useDocyrusRequestHostNavigation, useDocyrusSendToCalendar, useDocyrusSendToChat, useDocyrusSendToDocy, useDocyrusSendToEmail };
125
+ export { AuthStatus, DocyrusCalendarEventPayload, DocyrusEmailPayload, DocyrusSharePayload, GuidyRoute, HostNavigationHandler, HostNavigationRequestOptions, HostNotificationHandler, MsGraphClient, SignInButton, clearTokenCookie, getTokenFromCookie, syncTokenToCookie, useDocyrusGuidyRoutes, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync, useDocyrusMsGraph, useDocyrusRequestHostNavigation, useDocyrusSendToCalendar, useDocyrusSendToChat, useDocyrusSendToDocy, useDocyrusSendToEmail };
126
+ export type { SignInButtonProps };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createContext, useState, useRef, useEffect, useMemo, use } from 'react';
1
+ import { createContext, useRef, useState, useEffect, useMemo, use } from 'react';
2
2
  import { BrowserOAuth2TokenStorage, OAuth2Client, RestApiClient, MemoryOAuth2TokenStorage } from '@docyrus/api-client';
3
3
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
4
4
 
@@ -6,6 +6,8 @@ import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
6
6
 
7
7
  // src/constants.ts
8
8
  var DEFAULT_API_URL = "https://alpha-api.docyrus.com";
9
+ var INVALID_HOST_API_URL = "invalid-host-api-url";
10
+ var UNTRUSTED_BACKEND_MESSAGE = "This app can\u2019t run because its host gave it an untrusted backend address.";
9
11
  var DEFAULT_OAUTH_SCOPES = [
10
12
  "offline_access",
11
13
  "Read.All",
@@ -1105,6 +1107,39 @@ function getTokenFromCookie(cookieKey) {
1105
1107
  return match ? decodeURIComponent(match[1]) : null;
1106
1108
  }
1107
1109
 
1110
+ // src/core/host-api-url.ts
1111
+ var LOOPBACK_HOSTS = [
1112
+ "localhost",
1113
+ "127.0.0.1",
1114
+ "[::1]"
1115
+ ];
1116
+ var TRUSTED_DOMAINS = ["docyrus.com", "attornaid.com"];
1117
+ function readHostApiUrl() {
1118
+ if (typeof window === "undefined" || !window.location) return void 0;
1119
+ const params = new URLSearchParams(window.location.search);
1120
+ const value = params.get("apiUrl");
1121
+ if (params.get("embedded") !== "true" || value === null) return void 0;
1122
+ try {
1123
+ const url = new URL(value);
1124
+ const {
1125
+ protocol,
1126
+ hostname,
1127
+ username,
1128
+ password,
1129
+ search,
1130
+ hash
1131
+ } = url;
1132
+ const loopback = LOOPBACK_HOSTS.includes(hostname);
1133
+ const trustedDomain = TRUSTED_DOMAINS.some((domain) => hostname === domain || hostname.endsWith(`.${domain}`));
1134
+ if (username || password || search || hash) return null;
1135
+ if (protocol !== "https:" && !(loopback && protocol === "http:")) return null;
1136
+ if (!loopback && !trustedDomain) return null;
1137
+ return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
1138
+ } catch {
1139
+ return null;
1140
+ }
1141
+ }
1142
+
1108
1143
  // src/core/auth-manager.ts
1109
1144
  var AuthManager = class {
1110
1145
  mode;
@@ -1132,7 +1167,12 @@ var AuthManager = class {
1132
1167
  notificationHandlers = /* @__PURE__ */ new Set();
1133
1168
  config;
1134
1169
  constructor(config = {}) {
1135
- this.config = config;
1170
+ const hostApiUrl = readHostApiUrl();
1171
+ this.config = hostApiUrl ? { ...config, apiUrl: hostApiUrl } : config;
1172
+ if (hostApiUrl === null) {
1173
+ this.status = "error";
1174
+ this.error = new Error(INVALID_HOST_API_URL);
1175
+ }
1136
1176
  this.mode = config.forceMode ?? detectAuthMode(config.allowedHostOrigins);
1137
1177
  }
1138
1178
  /** Whether the provider may refresh tokens on its own. Default: true. */
@@ -1180,6 +1220,11 @@ var AuthManager = class {
1180
1220
  }
1181
1221
  /** Initialize the auth manager. Must be called once on mount. */
1182
1222
  async initialize() {
1223
+ if (this.status === "error") {
1224
+ console.error("[docyrus-signin]", UNTRUSTED_BACKEND_MESSAGE);
1225
+ this.notify();
1226
+ return;
1227
+ }
1183
1228
  try {
1184
1229
  if (this.mode === "iframe" && this.getInitialTokens()) {
1185
1230
  throw new Error(
@@ -1462,6 +1507,7 @@ var AuthManager = class {
1462
1507
  * browser redirect flow, such as a prior device authorization flow.
1463
1508
  */
1464
1509
  async signInWithTokens(tokens) {
1510
+ if (this.status === "error") throw this.error;
1465
1511
  if (this.mode === "iframe") {
1466
1512
  throw new Error(
1467
1513
  "signInWithTokens() is not supported in iframe mode. Use the host postMessage signin flow or force standalone mode."
@@ -1496,6 +1542,7 @@ var AuthManager = class {
1496
1542
  * Iframe: clear local state (host manages the actual session).
1497
1543
  */
1498
1544
  async signOut() {
1545
+ if (this.status === "error") return;
1499
1546
  this.clearTokenRefreshTimer();
1500
1547
  if (this.mode === "standalone" && this.standaloneAuth) {
1501
1548
  await this.standaloneAuth.logout();
@@ -1883,33 +1930,34 @@ function hasPermission(user, operation, scope, config) {
1883
1930
  var DocyrusAuthContext = createContext(null);
1884
1931
  function DocyrusAuthProvider({
1885
1932
  children,
1933
+ renderAuthError,
1886
1934
  ...config
1887
1935
  }) {
1888
- const [status, setStatus] = useState("loading");
1889
- const [tokens, setTokens] = useState(null);
1890
- const [client, setClient] = useState(null);
1891
- const [error, setError] = useState(null);
1892
- const [user, setUser] = useState(null);
1893
- const configRef = useRef(config);
1894
1936
  const managerRef = useRef(null);
1895
1937
  if (!managerRef.current) {
1896
- managerRef.current = new AuthManager(configRef.current);
1938
+ managerRef.current = new AuthManager(config);
1897
1939
  }
1898
- const mode = managerRef.current.getMode();
1940
+ const manager = managerRef.current;
1941
+ const mode = manager.getMode();
1942
+ const [status, setStatus] = useState(() => manager.getStatus());
1943
+ const [tokens, setTokens] = useState(null);
1944
+ const [client, setClient] = useState(null);
1945
+ const [error, setError] = useState(() => manager.getError());
1946
+ const [user, setUser] = useState(null);
1899
1947
  useEffect(() => {
1900
- const manager = managerRef.current;
1901
- if (!manager) return;
1902
- const unsubscribe = manager.subscribe((state) => {
1948
+ const manager2 = managerRef.current;
1949
+ if (!manager2) return;
1950
+ const unsubscribe = manager2.subscribe((state) => {
1903
1951
  setStatus(state.status);
1904
1952
  setTokens(state.tokens);
1905
1953
  setError(state.error);
1906
- setClient(manager.getClient());
1954
+ setClient(manager2.getClient());
1907
1955
  setUser(state.user);
1908
1956
  });
1909
- manager.initialize();
1957
+ manager2.initialize();
1910
1958
  return () => {
1911
1959
  unsubscribe();
1912
- manager.destroy();
1960
+ manager2.destroy();
1913
1961
  };
1914
1962
  }, []);
1915
1963
  const signIn = useMemo(() => {
@@ -2049,7 +2097,11 @@ function DocyrusAuthProvider({
2049
2097
  msGraph,
2050
2098
  error
2051
2099
  ]);
2052
- return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });
2100
+ let content = children;
2101
+ if (status === "error" && error) {
2102
+ content = renderAuthError ? renderAuthError(error) : /* @__PURE__ */ jsx("p", { role: "alert", children: UNTRUSTED_BACKEND_MESSAGE });
2103
+ }
2104
+ return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children: content });
2053
2105
  }
2054
2106
  function useDocyrusAuth() {
2055
2107
  const context = use(DocyrusAuthContext);