@dxv-systems/mail-graph 0.1.1 → 0.1.3

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/README.md CHANGED
@@ -168,7 +168,7 @@ export const DELETE = m365Setup.handle;
168
168
  <M365SetupPage linkToken={params.token} />
169
169
  ```
170
170
 
171
- The route set is `/api/m365/{status, mailboxes, consent/start, consent/callback, object-id, script-ran, check, script, it-link, reset}`. Every route refuses (`403 not_production`) unless `isProduction` is true, and answers `503 not_configured` without a client id. POSTs from another `Origin` are refused. An IT link is sent in the `x-m365-link` header (or `?t=` for the script download).
171
+ The route set is `/api/m365/{status, mailboxes, consent/start, consent/callback, object-id, script-ran, check, script, it-link, reset}`. Every route refuses (`403 not_production`) unless `isProduction` is true, and answers `503 not_configured` without a client id. POSTs from another `Origin` are refused (`403 forbidden_origin`, with a message that names the `appOrigin` host). The page itself, opened on any address other than `appOrigin` (such as the app's platform address), shows only a link to the same page on `appOrigin`: the status response carries that URL as `pageUrl`. An IT link is sent in the `x-m365-link` header (or `?t=` for the script download).
172
172
 
173
173
  `store` holds one `M365Connection` (see `src/setup/types.ts`); a single row with a JSON column is enough. `runAccessCheck()` runs the same check from a cron job. The helpdesk may sync only while `step === "connected"`.
174
174
 
@@ -90,6 +90,7 @@ function createM365SetupHandlers(config) {
90
90
  const checkIntervalMs = Math.max(MIN_CHECK_INTERVAL_MS, config.checkIntervalMs ?? DEFAULT_CHECK_INTERVAL_MS);
91
91
  const clientId = config.clientId ?? "";
92
92
  const itLinkUrl = (token) => `${origin}${config.itLinkPagePath.replace(/\/$/, "")}/${token}`;
93
+ const adminPageUrl = `${origin}${config.pagePath}`;
93
94
  function emptyConnection() {
94
95
  return {
95
96
  setupId: (0, signing_1.randomId)(),
@@ -135,14 +136,14 @@ function createM365SetupHandlers(config) {
135
136
  connection.step === "connected" &&
136
137
  result.setupId === connection.setupId;
137
138
  if (afterConnect)
138
- return { viewer: "it", expiresAt: null };
139
+ return { viewer: "it", token, expiresAt: null };
139
140
  if (!result.ok)
140
141
  return fail(401, "link_invalid");
141
- return { viewer: "it", expiresAt: result.expiresAt };
142
+ return { viewer: "it", token, expiresAt: result.expiresAt };
142
143
  }
143
144
  const admin = await config.getAdmin(request);
144
145
  if (!admin)
145
- return fail(401, "unauthorized");
146
+ return fail(401, "unauthorized", { pageUrl: adminPageUrl });
146
147
  return { viewer: "admin", admin };
147
148
  }
148
149
  function toStatus(connection, viewer) {
@@ -178,6 +179,7 @@ function createM365SetupHandlers(config) {
178
179
  : connection.itLinkId
179
180
  ? connection.itLinkExpiresAt
180
181
  : null,
182
+ pageUrl: viewer.viewer === "it" ? itLinkUrl(viewer.token) : adminPageUrl,
181
183
  };
182
184
  }
183
185
  async function check(connection) {
@@ -319,7 +321,7 @@ function createM365SetupHandlers(config) {
319
321
  const claims = result.claims;
320
322
  if (!connection || !claims)
321
323
  return htmlMessage(400, "Koppelen niet gelukt", messages_1.CONSENT_ERROR_MESSAGES.state_invalid);
322
- const back = claims.via === "it" && claims.l ? itLinkUrl(claims.l) : `${origin}${config.pagePath}`;
324
+ const back = claims.via === "it" && claims.l ? itLinkUrl(claims.l) : adminPageUrl;
323
325
  if (!result.ok) {
324
326
  if (result.code !== "state_stale") {
325
327
  await save({ ...connection, consentError: result.code, consentNonce: null });
@@ -555,8 +557,10 @@ function createM365SetupHandlers(config) {
555
557
  return isCallback ? htmlMessage(503, "Koppelen niet gelukt", messages_1.SETUP_ERROR_MESSAGES.not_configured) : fail(503, "not_configured");
556
558
  }
557
559
  const requestOrigin = request.headers.get("origin");
558
- if (request.method !== "GET" && requestOrigin !== null && requestOrigin !== origin)
559
- return fail(403, "forbidden_origin");
560
+ if (request.method !== "GET" && requestOrigin !== null && requestOrigin !== origin) {
561
+ const host = new URL(origin).host;
562
+ return json(403, { error: "forbidden_origin", message: (0, messages_1.forbiddenOriginMessage)(host), appOrigin: origin });
563
+ }
560
564
  return handler(request);
561
565
  }
562
566
  async function runAccessCheck() {
@@ -5,7 +5,7 @@ export declare const SETUP_ERROR_MESSAGES: Readonly<{
5
5
  readonly setup_pending: "DXV zet Microsoft 365 nog klaar voor deze app. Probeer het later opnieuw.";
6
6
  readonly unauthorized: "Je hebt geen toegang tot deze koppeling. Log in als app-beheerder.";
7
7
  readonly link_invalid: "Deze link werkt niet (meer). Vraag de app-beheerder om een nieuwe link.";
8
- readonly forbidden_origin: "Dit verzoek komt niet van de app zelf en is geweigerd.";
8
+ readonly forbidden_origin: "Dit verzoek kwam via een ander adres dan dat van de app en is geweigerd. Open deze pagina via het adres van de app.";
9
9
  readonly invalid_input: "Controleer de ingevulde gegevens.";
10
10
  readonly not_m365: "Dit domein gebruikt geen Microsoft 365. Kies mailboxen die in Microsoft 365 staan.";
11
11
  readonly mixed_tenants: "De mailboxen staan in verschillende Microsoft 365-omgevingen. Kies mailboxen uit één omgeving.";
@@ -18,5 +18,6 @@ export declare const SETUP_ERROR_MESSAGES: Readonly<{
18
18
  readonly method_not_allowed: "Deze methode is hier niet toegestaan.";
19
19
  }>;
20
20
  export type SetupErrorCode = keyof typeof SETUP_ERROR_MESSAGES;
21
+ export declare function forbiddenOriginMessage(host: string): string;
21
22
  export declare const CONSENT_ERROR_MESSAGES: Readonly<Record<ConsentErrorCode, string>>;
22
23
  export declare const EXCHANGE_SETUP_ERROR_MESSAGES: Readonly<Record<ExchangeSetupErrorCode, string>>;
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EXCHANGE_SETUP_ERROR_MESSAGES = exports.CONSENT_ERROR_MESSAGES = exports.SETUP_ERROR_MESSAGES = void 0;
4
+ exports.forbiddenOriginMessage = forbiddenOriginMessage;
4
5
  exports.SETUP_ERROR_MESSAGES = Object.freeze({
5
6
  not_production: "Microsoft 365 koppelen kan alleen in de productieomgeving van de app.",
6
7
  not_configured: "Microsoft 365 is nog niet beschikbaar voor deze app. Neem contact op met DXV.",
7
8
  setup_pending: "DXV zet Microsoft 365 nog klaar voor deze app. Probeer het later opnieuw.",
8
9
  unauthorized: "Je hebt geen toegang tot deze koppeling. Log in als app-beheerder.",
9
10
  link_invalid: "Deze link werkt niet (meer). Vraag de app-beheerder om een nieuwe link.",
10
- forbidden_origin: "Dit verzoek komt niet van de app zelf en is geweigerd.",
11
+ forbidden_origin: "Dit verzoek kwam via een ander adres dan dat van de app en is geweigerd. Open deze pagina via het adres van de app.",
11
12
  invalid_input: "Controleer de ingevulde gegevens.",
12
13
  not_m365: "Dit domein gebruikt geen Microsoft 365. Kies mailboxen die in Microsoft 365 staan.",
13
14
  mixed_tenants: "De mailboxen staan in verschillende Microsoft 365-omgevingen. Kies mailboxen uit één omgeving.",
@@ -19,6 +20,9 @@ exports.SETUP_ERROR_MESSAGES = Object.freeze({
19
20
  not_found: "Onbekende route.",
20
21
  method_not_allowed: "Deze methode is hier niet toegestaan.",
21
22
  });
23
+ function forbiddenOriginMessage(host) {
24
+ return `Dit verzoek kwam via een ander adres dan ${host} en is geweigerd. Open deze pagina via ${host} en probeer het opnieuw.`;
25
+ }
22
26
  exports.CONSENT_ERROR_MESSAGES = Object.freeze({
23
27
  state_invalid: "Deze terugkoppeling van Microsoft is ongeldig. Ga terug naar de koppelpagina en klik opnieuw op Koppelen met Microsoft.",
24
28
  state_expired: "Het koppelen duurde te lang en is verlopen. Klik opnieuw op Koppelen met Microsoft.",
@@ -131,6 +131,19 @@ function useApi(linkToken, fetchImpl) {
131
131
  return body;
132
132
  }, [linkToken, fetchImpl]);
133
133
  }
134
+ function productionPage(pageUrl) {
135
+ if (typeof pageUrl !== "string" || typeof window === "undefined")
136
+ return null;
137
+ try {
138
+ const url = new URL(pageUrl);
139
+ if (url.protocol !== "https:" && url.protocol !== "http:")
140
+ return null;
141
+ return url.origin === window.location.origin ? null : { url: url.href, host: url.host };
142
+ }
143
+ catch {
144
+ return null;
145
+ }
146
+ }
134
147
  async function signIn(api, method) {
135
148
  const { url } = await api("consent/start", { method: "POST", body: { method } });
136
149
  window.location.assign(url);
@@ -139,6 +152,7 @@ function M365SetupPage({ linkToken, helpdeskHref, settingsHref, fetch: fetchImpl
139
152
  const api = useApi(linkToken, fetchImpl);
140
153
  const [status, setStatus] = (0, react_1.useState)(null);
141
154
  const [loadError, setLoadError] = (0, react_1.useState)(null);
155
+ const [loadErrorPageUrl, setLoadErrorPageUrl] = (0, react_1.useState)(null);
142
156
  const [view, setView] = (0, react_1.useState)("auto");
143
157
  const [busy, setBusy] = (0, react_1.useState)(false);
144
158
  const [actionError, setActionError] = (0, react_1.useState)(null);
@@ -146,22 +160,27 @@ function M365SetupPage({ linkToken, helpdeskHref, settingsHref, fetch: fetchImpl
146
160
  try {
147
161
  setStatus(await api("status"));
148
162
  setLoadError(null);
163
+ setLoadErrorPageUrl(null);
149
164
  }
150
165
  catch (error) {
151
166
  setLoadError(apiErrorText(error));
167
+ const pageUrl = error instanceof ApiError ? error.body.pageUrl : null;
168
+ setLoadErrorPageUrl(typeof pageUrl === "string" ? pageUrl : null);
152
169
  }
153
170
  }, [api]);
154
171
  (0, react_1.useEffect)(() => {
155
172
  void load();
156
173
  }, [load]);
174
+ const elsewhere = productionPage(status?.pageUrl ?? loadErrorPageUrl);
157
175
  const waiting = status?.step === "awaiting_access";
158
176
  const preparing = status !== null && !status.setupReady && (status.step === "not_started" || status.step === "awaiting_consent");
177
+ const polling = elsewhere === null && (waiting || preparing);
159
178
  (0, react_1.useEffect)(() => {
160
- if (!waiting && !preparing)
179
+ if (!polling)
161
180
  return;
162
181
  const timer = setInterval(() => void load(), waiting ? POLL_INTERVAL_MS : PREPARING_POLL_INTERVAL_MS);
163
182
  return () => clearInterval(timer);
164
- }, [waiting, preparing, load]);
183
+ }, [polling, waiting, load]);
165
184
  const run = (0, react_1.useCallback)(async (action, nextView = "auto") => {
166
185
  setBusy(true);
167
186
  setActionError(null);
@@ -178,6 +197,9 @@ function M365SetupPage({ linkToken, helpdeskHref, settingsHref, fetch: fetchImpl
178
197
  setBusy(false);
179
198
  }
180
199
  }, []);
200
+ if (elsewhere) {
201
+ return ((0, jsx_runtime_1.jsxs)("div", { className: cls.root, "data-m365-setup": "", "data-wrong-origin": "", children: [(0, jsx_runtime_1.jsx)("p", { className: cls.breadcrumb, children: linkToken ? (status ? `Verzoek van ${status.companyName}` : "Microsoft 365") : "Beheer / Microsoft 365" }), (0, jsx_runtime_1.jsx)(WrongOriginNotice, { url: elsewhere.url, host: elsewhere.host, it: Boolean(linkToken) })] }));
202
+ }
181
203
  if (!status) {
182
204
  return ((0, jsx_runtime_1.jsxs)("div", { className: cls.root, "data-m365-setup": "", children: [(0, jsx_runtime_1.jsx)("p", { className: cls.breadcrumb, children: "Beheer / Microsoft 365" }), loadError ? ((0, jsx_runtime_1.jsx)("p", { role: "alert", className: cls.error, children: loadError })) : ((0, jsx_runtime_1.jsx)("p", { className: cls.muted, "aria-live": "polite", children: "Laden\u2026" }))] }));
183
205
  }
@@ -210,6 +232,9 @@ function M365SetupPage({ linkToken, helpdeskHref, settingsHref, fetch: fetchImpl
210
232
  function Header({ title, lead, badge }) {
211
233
  return ((0, jsx_runtime_1.jsxs)("div", { className: cls.header, children: [(0, jsx_runtime_1.jsxs)("div", { className: cls.actions, children: [(0, jsx_runtime_1.jsx)("h1", { className: cls.title, children: title }), badge && (0, jsx_runtime_1.jsx)("span", { className: cls.badgeOk, children: badge })] }), lead && (0, jsx_runtime_1.jsx)("p", { className: cls.lead, children: lead })] }));
212
234
  }
235
+ function WrongOriginNotice({ url, host, it }) {
236
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Header, { title: `Open deze pagina via ${host}`, lead: `Je bent nu op een ander adres van de app. Koppelen met Microsoft 365 werkt alleen via ${host}, omdat Microsoft je na het aanmelden daarheen terugstuurt.` }), (0, jsx_runtime_1.jsxs)("a", { href: url, className: cls.primaryButton, children: ["Naar ", host] }), !it && (0, jsx_runtime_1.jsx)("p", { className: cls.center, children: "Log daar zo nodig opnieuw in als app-beheerder." })] }));
237
+ }
213
238
  function MicrosoftMark() {
214
239
  return ((0, jsx_runtime_1.jsxs)("svg", { "aria-hidden": "true", width: "16", height: "16", viewBox: "0 0 16 16", fill: "currentColor", children: [(0, jsx_runtime_1.jsx)("rect", { x: "1", y: "1", width: "6.5", height: "6.5" }), (0, jsx_runtime_1.jsx)("rect", { x: "8.5", y: "1", width: "6.5", height: "6.5" }), (0, jsx_runtime_1.jsx)("rect", { x: "1", y: "8.5", width: "6.5", height: "6.5" }), (0, jsx_runtime_1.jsx)("rect", { x: "8.5", y: "8.5", width: "6.5", height: "6.5" })] }));
215
240
  }
@@ -73,4 +73,5 @@ export interface SetupStatus {
73
73
  connectedAt: string | null;
74
74
  signedInAs: string | null;
75
75
  itLinkExpiresAt: string | null;
76
+ pageUrl: string;
76
77
  }