domma-cms 1.28.4 → 1.29.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.
Files changed (98) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/admin/js/lib/contacts-arrange.js +1 -1
  3. package/docs/DommaFeatures.md +5 -3
  4. package/package.json +4 -1
  5. package/plugins/_lib/admin/mail/body-menu.js +2 -0
  6. package/plugins/_lib/admin/mail/cms-host.js +22 -0
  7. package/plugins/_lib/admin/mail/compose-window.js +1027 -1021
  8. package/plugins/_lib/admin/mail/contacts.js +10 -8
  9. package/plugins/_lib/admin/mail/diagnostics-section.js +2 -0
  10. package/plugins/_lib/admin/mail/drag-drop.js +46 -0
  11. package/plugins/_lib/admin/mail/folder-tree.js +2 -0
  12. package/plugins/_lib/admin/mail/font-controls.js +6 -4
  13. package/plugins/_lib/admin/mail/fonts.js +1 -1
  14. package/plugins/_lib/admin/mail/host-context.js +87 -0
  15. package/plugins/_lib/admin/mail/identity.js +2 -0
  16. package/plugins/_lib/admin/mail/image-senders-section.js +2 -0
  17. package/plugins/_lib/admin/mail/keyboard.js +2 -0
  18. package/plugins/_lib/admin/mail/mail.css +1 -1
  19. package/plugins/_lib/admin/mail/mail.html +4 -0
  20. package/plugins/_lib/admin/mail/message-actions.js +2 -0
  21. package/plugins/_lib/admin/mail/oauth-panel.js +2 -0
  22. package/plugins/_lib/admin/mail/panes.js +2 -0
  23. package/plugins/_lib/admin/mail/reader-kit.js +2 -0
  24. package/plugins/_lib/admin/mail/reader-runtime.js +6534 -0
  25. package/plugins/_lib/admin/mail/reader-view.js +9 -6380
  26. package/plugins/_lib/admin/mail/resizable.js +2 -0
  27. package/plugins/_lib/admin/mail/restore-spam.js +13 -0
  28. package/plugins/_lib/admin/mail/rules.js +14 -0
  29. package/plugins/_lib/admin/mail/scheduling.js +4 -1
  30. package/plugins/_lib/admin/mail/section-kit.js +4 -1
  31. package/plugins/_lib/admin/mail/security-note.js +2 -0
  32. package/plugins/_lib/admin/mail/settings-section.js +11 -10
  33. package/plugins/_lib/admin/mail/templates.js +5 -2
  34. package/plugins/_lib/admin/mail/threads.js +2 -0
  35. package/plugins/_lib/admin/mail/vacation.js +16 -2
  36. package/plugins/_lib/admin/mail/when-closed.js +24 -2
  37. package/plugins/_lib/admin/ui/dates.js +2 -0
  38. package/plugins/_lib/admin/ui/resizable.js +7 -5
  39. package/plugins/_lib/contacts/mailContact.js +69 -0
  40. package/plugins/_lib/contacts/model.js +199 -0
  41. package/plugins/_lib/domma-mail-contacts.lock.json +10 -0
  42. package/plugins/_lib/domma-mail.lock.json +85 -0
  43. package/plugins/_lib/mail/accountFileStore.js +66 -0
  44. package/plugins/_lib/mail/accountRepository.js +617 -0
  45. package/plugins/_lib/mail/accountRoutes.js +130 -0
  46. package/plugins/_lib/mail/accounts.js +66 -940
  47. package/plugins/_lib/mail/automationLock.js +10 -0
  48. package/plugins/_lib/mail/bodyTokenProvider.js +36 -0
  49. package/plugins/_lib/mail/bodyTokens.js +5 -99
  50. package/plugins/_lib/mail/compose.js +5 -3
  51. package/plugins/_lib/mail/defaults.js +2 -0
  52. package/plugins/_lib/mail/envelope.js +3 -1
  53. package/plugins/_lib/mail/folderActions.js +83 -0
  54. package/plugins/_lib/mail/folderRuntime.js +109 -0
  55. package/plugins/_lib/mail/folders.js +4 -9
  56. package/plugins/_lib/mail/imapPool.js +11 -389
  57. package/plugins/_lib/mail/imapPoolRuntime.js +396 -0
  58. package/plugins/_lib/mail/mbox.js +2 -0
  59. package/plugins/_lib/mail/message-action.js +2 -0
  60. package/plugins/_lib/mail/messageSecurity.js +4 -111
  61. package/plugins/_lib/mail/messageSecurityProvider.js +116 -0
  62. package/plugins/_lib/mail/networkAddresses.js +77 -0
  63. package/plugins/_lib/mail/networkPolicy.js +62 -0
  64. package/plugins/_lib/mail/oauth.js +3 -451
  65. package/plugins/_lib/mail/oauthAccounts.js +178 -0
  66. package/plugins/_lib/mail/oauthRouteRuntime.js +282 -0
  67. package/plugins/_lib/mail/oauthRoutes.js +7 -289
  68. package/plugins/_lib/mail/oauthRuntime.js +456 -0
  69. package/plugins/_lib/mail/pollSchedule.js +2 -0
  70. package/plugins/_lib/mail/poller.js +4 -136
  71. package/plugins/_lib/mail/pollerRuntime.js +150 -0
  72. package/plugins/_lib/mail/priority.js +2 -0
  73. package/plugins/_lib/mail/publicMailbox.js +27 -0
  74. package/plugins/_lib/mail/readRoutes.js +14 -827
  75. package/plugins/_lib/mail/readRuntime.js +701 -0
  76. package/plugins/_lib/mail/render.js +2 -0
  77. package/plugins/_lib/mail/ruleRunner.js +27 -10
  78. package/plugins/_lib/mail/scheduler.js +11 -279
  79. package/plugins/_lib/mail/schedulerRuntime.js +259 -0
  80. package/plugins/_lib/mail/secretProvider.js +112 -0
  81. package/plugins/_lib/mail/secretbox.js +12 -225
  82. package/plugins/_lib/mail/send.js +21 -537
  83. package/plugins/_lib/mail/sieve.js +211 -0
  84. package/plugins/_lib/mail/sieveHost.js +24 -0
  85. package/plugins/_lib/mail/smtpService.js +553 -0
  86. package/plugins/_lib/mail/snoozeRuntime.js +60 -0
  87. package/plugins/_lib/mail/store.js +122 -64
  88. package/plugins/_lib/mail/subscriptions.js +11 -0
  89. package/plugins/_lib/mail/sync.js +64 -290
  90. package/plugins/_lib/mail/syncPlan.js +2 -0
  91. package/plugins/_lib/mail/syncRuntime.js +255 -0
  92. package/plugins/_lib/mail/syncSelection.js +2 -0
  93. package/plugins/_lib/mail/unreadBadge.js +2 -0
  94. package/plugins/_lib/mail/unsubscribe.js +2 -0
  95. package/plugins/_lib/mail/vacationRunner.js +3 -116
  96. package/plugins/_lib/mail/vacationRuntime.js +70 -0
  97. package/plugins/_lib/mail/write.js +44 -13
  98. package/server/routes/api/contacts.js +2 -196
@@ -0,0 +1,456 @@
1
+ // Source: @domma-mail/core 0.18.0, packages/core/src/mail/oauthRuntime.js.
2
+ // Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
3
+ /**
4
+ * OAuth2 sign-in for mailboxes (XOAUTH2) - Microsoft 365 / Outlook.com and Gmail.
5
+ *
6
+ * A mailbox can be signed in with the provider instead of a password: no app
7
+ * password, and a sign-in the provider can withdraw. The customer registers
8
+ * THEIR OWN app with the provider (the Docs Pro cloud-copies design) - the
9
+ * site never holds a client id of ours, so nobody else's app or verification
10
+ * stands between a customer and their own mail.
11
+ *
12
+ * What is kept (sealed with secretbox, the same key as mailbox passwords):
13
+ * the client secret, the refresh token and the current access token.
14
+ *
15
+ * ## Refreshing
16
+ *
17
+ * An access token lasts about an hour. It is renewed at the point of use, not
18
+ * on a timer: IMAP and SMTP only present it when a connection is opened, and
19
+ * a pooled IMAP connection stays signed in after its token has expired.
20
+ *
21
+ * - before expiry: `toConnection()` carries `expiresAt` and a `refresh`
22
+ * function; the pool renews when under a minute is left.
23
+ * - on an auth failure: the pool (and nodemailer, through its
24
+ * `provisionCallback`) asks for a forced renewal and tries ONCE more.
25
+ *
26
+ * A refresh the provider REFUSES (invalid_grant and friends - revoked,
27
+ * expired, a Google "Testing" app's 7-day limit, a client secret that ran
28
+ * out) marks the account "needs reconnecting" and tells its owner. From then
29
+ * on `toConnection()` throws OAuthReconnectError without touching the network,
30
+ * so the poller cannot hammer a dead sign-in. A refresh that merely could not
31
+ * be done (network, 5xx) changes nothing and is tried again next time.
32
+ *
33
+ * Microsoft ROTATES refresh tokens: every refresh may answer a new one, and the
34
+ * new one is saved. Google answers none on refresh; the stored one is kept.
35
+ *
36
+ * Endpoints and scopes from the vendors' documentation (2026-10):
37
+ * - learn.microsoft.com .../v2-oauth2-auth-code-flow and
38
+ * .../how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
39
+ * - developers.google.com .../oauth2/web-server and .../gmail/imap/xoauth2-protocol
40
+ *
41
+ * @module _lib/mail/oauth
42
+ */
43
+ import {createHash, randomBytes} from 'node:crypto';
44
+
45
+ export function createOAuth({accounts, secrets, OAuthReconnectError}) {
46
+ const {open, seal} = secrets;
47
+ /** The tenant id Microsoft gives every personal (Outlook.com) account in an id_token. */
48
+ const MS_CONSUMER_TENANT = '9188040d-6c67-4c5b-b112-36a304b66dad';
49
+
50
+ /** Renew when less than this is left, so a token never expires mid-handshake. */
51
+ const REFRESH_SKEW_MS = 60_000;
52
+
53
+ const MS_MAIL_SCOPES = ['https://outlook.office.com/IMAP.AccessAsUser.All', 'https://outlook.office.com/SMTP.Send'];
54
+
55
+ /**
56
+ * The two providers. Hosts are fixed: the token only works against the
57
+ * provider's own servers, so there is nothing for the customer to type and no
58
+ * host to guard.
59
+ */
60
+ const PROVIDERS = {
61
+ microsoft: {
62
+ id: 'microsoft',
63
+ label: 'Microsoft 365 / Outlook.com',
64
+ short: 'Microsoft',
65
+ needsTenant: true,
66
+ authorizeUrl: (tenant) => `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/authorize`,
67
+ tokenUrl: (tenant) => `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token`,
68
+ // openid + email only to learn the address signed in; all the mail
69
+ // scopes are one resource (outlook.office.com), which Microsoft
70
+ // requires of a single token.
71
+ scopes: [...MS_MAIL_SCOPES, 'offline_access', 'openid', 'email'],
72
+ refreshScope: [...MS_MAIL_SCOPES, 'offline_access'].join(' '),
73
+ // Recommended for confidential clients too (the auth-code-flow page).
74
+ pkce: true,
75
+ extraAuthParams: {response_mode: 'query', prompt: 'select_account'},
76
+ imap: {host: 'outlook.office365.com', port: 993, secure: true},
77
+ smtp: {host: 'smtp.office365.com', port: 587, secure: false},
78
+ // Outlook.com's own outgoing server (support.microsoft.com, Outlook.com settings).
79
+ smtpPersonal: {host: 'smtp-mail.outlook.com', port: 587, secure: false},
80
+ // Microsoft offers no endpoint for an app to hand one refresh token back.
81
+ revokeUrl: null
82
+ },
83
+ google: {
84
+ id: 'google',
85
+ label: 'Gmail / Google Workspace',
86
+ short: 'Google',
87
+ needsTenant: false,
88
+ authorizeUrl: () => 'https://accounts.google.com/o/oauth2/v2/auth',
89
+ tokenUrl: () => 'https://oauth2.googleapis.com/token',
90
+ scopes: ['https://mail.google.com/', 'openid', 'email'],
91
+ refreshScope: null,
92
+ // PKCE as well as state + secret (Google accepts S256 code_challenge).
93
+ pkce: true,
94
+ // offline + consent, or Google sends no refresh token on a second connect.
95
+ extraAuthParams: {access_type: 'offline', prompt: 'consent', include_granted_scopes: 'true'},
96
+ imap: {host: 'imap.gmail.com', port: 993, secure: true},
97
+ smtp: {host: 'smtp.gmail.com', port: 465, secure: true},
98
+ smtpPersonal: null,
99
+ revokeUrl: 'https://oauth2.googleapis.com/revoke'
100
+ }
101
+ };
102
+
103
+ /** Token-endpoint errors that no retry will clear: the person has to sign in again. */
104
+ const PERMANENT = new Set(['invalid_grant', 'invalid_client', 'unauthorized_client', 'interaction_required',
105
+ 'consent_required', 'invalid_scope', 'access_denied']);
106
+
107
+ /** Swappable for tests - nothing real is reachable from a test run. */
108
+ let fetchImpl = (...args) => globalThis.fetch(...args);
109
+ /** Told when an account needs reconnecting, and when it is connected again. */
110
+ let hooks = {};
111
+
112
+ /**
113
+ * @param {{fetch?: Function, onNeedsReconnect?: Function, onConnected?: Function}} options
114
+ * @returns {void}
115
+ */
116
+ function configureOAuth({fetch, onNeedsReconnect, onConnected} = {}) {
117
+ if (fetch) fetchImpl = fetch;
118
+ if (onNeedsReconnect !== undefined || onConnected !== undefined) {
119
+ hooks = {...hooks, ...(onNeedsReconnect !== undefined ? {onNeedsReconnect} : {}),
120
+ ...(onConnected !== undefined ? {onConnected} : {})};
121
+ }
122
+ }
123
+
124
+ /** Tests: back to the real network and no listeners. */
125
+ function _resetOAuth() {
126
+ fetchImpl = (...args) => globalThis.fetch(...args);
127
+ hooks = {};
128
+ inFlight.clear();
129
+ }
130
+
131
+ /**
132
+ * A tenant segment for Microsoft's URLs: `common`, `organizations`,
133
+ * `consumers` or a tenant id/domain. Anything else falls back to `common`
134
+ * rather than being pasted into a URL.
135
+ *
136
+ * @param {string} value
137
+ * @returns {string}
138
+ */
139
+ function cleanTenant(value) {
140
+ const t = String(value ?? '').trim();
141
+ // Dot-separated labels only: no empty label, no `..`, nothing that could
142
+ // walk a path segment.
143
+ return t.length >= 2 && t.length <= 100 && /^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*$/.test(t) ? t : 'common';
144
+ }
145
+
146
+ /**
147
+ * PKCE pair (RFC 7636, S256).
148
+ *
149
+ * @returns {{verifier: string, challenge: string}}
150
+ */
151
+ function pkcePair() {
152
+ const verifier = randomBytes(48).toString('base64url');
153
+ return {verifier, challenge: createHash('sha256').update(verifier).digest('base64url')};
154
+ }
155
+
156
+ /**
157
+ * The provider's sign-in address. Pure.
158
+ *
159
+ * @param {{provider: string, clientId: string, tenant?: string, redirectUri: string, state: string,
160
+ * codeChallenge?: string, loginHint?: string}} o
161
+ * @returns {string}
162
+ */
163
+ function authorizeUrl({provider, clientId, tenant, redirectUri, state, codeChallenge, loginHint}) {
164
+ const p = PROVIDERS[provider];
165
+ if (!p) throw new Error('Unknown provider.');
166
+ const q = new URLSearchParams({
167
+ client_id: String(clientId ?? '').trim(),
168
+ response_type: 'code',
169
+ redirect_uri: redirectUri,
170
+ scope: p.scopes.join(' '),
171
+ state,
172
+ ...p.extraAuthParams
173
+ });
174
+ if (p.pkce && codeChallenge) {
175
+ q.set('code_challenge', codeChallenge);
176
+ q.set('code_challenge_method', 'S256');
177
+ }
178
+ if (loginHint) q.set('login_hint', loginHint);
179
+ return `${p.authorizeUrl(cleanTenant(tenant))}?${q}`;
180
+ }
181
+
182
+ /**
183
+ * The claims of an id_token. NOT verified: it came straight from the token
184
+ * endpoint over TLS in answer to our own request (OpenID Connect Core 3.1.3.7
185
+ * allows that), and it is used only to learn the address signed in.
186
+ *
187
+ * @param {string} idToken
188
+ * @returns {object}
189
+ */
190
+ function idTokenClaims(idToken) {
191
+ try {
192
+ const part = String(idToken ?? '').split('.')[1];
193
+ return part ? JSON.parse(Buffer.from(part, 'base64url').toString('utf8')) : {};
194
+ } catch {
195
+ return {};
196
+ }
197
+ }
198
+
199
+ /**
200
+ * The mailbox address and outgoing server a token response points at. Pure.
201
+ *
202
+ * @param {string} provider
203
+ * @param {object} claims - idTokenClaims()
204
+ * @returns {{email: string, smtp: {host: string, port: number, secure: boolean}}}
205
+ */
206
+ function mailboxFor(provider, claims) {
207
+ const p = PROVIDERS[provider];
208
+ const email = String(claims?.email || claims?.preferred_username || claims?.upn || '').trim().toLowerCase();
209
+ const personal = provider === 'microsoft' && claims?.tid === MS_CONSUMER_TENANT;
210
+ return {email: /@/.test(email) ? email : '', smtp: personal && p.smtpPersonal ? p.smtpPersonal : p.smtp};
211
+ }
212
+
213
+ /**
214
+ * Does this stored sign-in need renewing before use? Pure.
215
+ *
216
+ * @param {{expiresAt?: number, accessTokenSealed?: string}} oauth
217
+ * @param {number} [now]
218
+ * @returns {boolean}
219
+ */
220
+ function needsRefresh(oauth, now = Date.now()) {
221
+ if (!oauth?.accessTokenSealed) return true;
222
+ return !(Number(oauth.expiresAt) > now + REFRESH_SKEW_MS);
223
+ }
224
+
225
+ /**
226
+ * Sort a token endpoint failure into "sign in again" or "try later". Pure.
227
+ *
228
+ * @param {number} status
229
+ * @param {object|null} body
230
+ * @returns {{permanent: boolean, code: string, message: string}}
231
+ */
232
+ function classifyTokenError(status, body) {
233
+ const code = String(body?.error ?? '').trim();
234
+ const said = String(body?.error_description ?? '').replace(/[\r\n\t]+/g, ' ').split(/Trace ID:/i)[0].trim().slice(0, 200);
235
+ const permanent = PERMANENT.has(code) || (status === 400 && !code) || status === 401;
236
+ let message;
237
+ if (code === 'invalid_client') message = 'The app\'s client secret was refused - it may have expired. Make a new one, then Reconnect.';
238
+ else if (code === 'invalid_grant') message = 'The sign-in has expired or was withdrawn. Reconnect to sign in again.';
239
+ else if (permanent) message = `The provider refused to renew the sign-in${said ? `: ${said}` : ''}. Reconnect to sign in again.`;
240
+ else message = `The provider could not renew the sign-in just now${said ? ` (${said})` : ` (${status || 'no answer'})`}. It will be tried again.`;
241
+ return {permanent, code, message};
242
+ }
243
+
244
+ /**
245
+ * POST a form to a fixed provider endpoint. Redirects are refused (the hosts
246
+ * are known; a 3xx is not something to follow with a secret in the body).
247
+ *
248
+ * @returns {Promise<{status: number, body: object|null}>}
249
+ */
250
+ async function postForm(url, params) {
251
+ const res = await fetchImpl(url, {
252
+ method: 'POST',
253
+ headers: {'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json'},
254
+ body: new URLSearchParams(params).toString(),
255
+ redirect: 'manual',
256
+ signal: AbortSignal.timeout(20_000)
257
+ });
258
+ let body = null;
259
+ try { body = await res.json(); } catch { body = null; }
260
+ return {status: res.status, ok: res.ok, body};
261
+ }
262
+
263
+ /**
264
+ * Trade the code from the callback for tokens.
265
+ *
266
+ * @param {{provider: string, clientId: string, clientSecret: string, tenant?: string,
267
+ * code: string, redirectUri: string, codeVerifier?: string}} o
268
+ * @returns {Promise<{accessToken: string, refreshToken: string, expiresAt: number, scope: string, claims: object}>}
269
+ */
270
+ async function exchangeCode({provider, clientId, clientSecret, tenant, code, redirectUri, codeVerifier}) {
271
+ const p = PROVIDERS[provider];
272
+ const params = {client_id: clientId, client_secret: clientSecret, code, redirect_uri: redirectUri, grant_type: 'authorization_code'};
273
+ if (p.pkce && codeVerifier) params.code_verifier = codeVerifier;
274
+ let r;
275
+ try {
276
+ r = await postForm(p.tokenUrl(cleanTenant(tenant)), params);
277
+ } catch (err) {
278
+ throw new Error(`${p.short} could not be reached to finish the sign-in: ${err.message}`);
279
+ }
280
+ if (!r.ok || !r.body?.access_token) {
281
+ const why = classifyTokenError(r.status, r.body);
282
+ throw new Error(`${p.short} refused the sign-in${why.code ? ` (${why.code})` : ''}: ${String(r.body?.error_description || r.status).split(/Trace ID:/i)[0].trim().slice(0, 200)}`);
283
+ }
284
+ if (!r.body.refresh_token) {
285
+ throw new Error(provider === 'google'
286
+ ? 'Google sent no refresh token. Remove the app from your Google account\'s third-party access, then Connect again.'
287
+ : 'Microsoft sent no refresh token - is offline_access among the app\'s API permissions?');
288
+ }
289
+ if (provider === 'google' && r.body.scope && !String(r.body.scope).split(' ').includes('https://mail.google.com/')) {
290
+ throw new Error('Mail access was not granted - tick it on Google\'s consent screen, then Connect again.');
291
+ }
292
+ return {
293
+ accessToken: r.body.access_token,
294
+ refreshToken: r.body.refresh_token,
295
+ expiresAt: Date.now() + (Number(r.body.expires_in) || 3600) * 1000,
296
+ scope: String(r.body.scope ?? ''),
297
+ claims: idTokenClaims(r.body.id_token)
298
+ };
299
+ }
300
+
301
+ /** One refresh per account at a time - a rotated refresh token must not be spent twice. */
302
+ const inFlight = new Map();
303
+
304
+ /**
305
+ * Renew an account's access token (or hand back the stored one while it is
306
+ * still good, unless `force`). Saves the new token, and a rotated refresh
307
+ * token. Throws OAuthReconnectError when the provider refuses for good (the
308
+ * account is marked and its owner told), a plain Error when it merely failed.
309
+ *
310
+ * @param {string} userId
311
+ * @param {string} accountId
312
+ * @param {{force?: boolean}} [options]
313
+ * @returns {Promise<{accessToken: string, expiresAt: number, account: object}>}
314
+ */
315
+ function refreshAccount(userId, accountId, {force = false} = {}) {
316
+ const key = `${userId}:${accountId}`;
317
+ const running = inFlight.get(key);
318
+ if (running) return running;
319
+ const job = doRefresh(userId, accountId, force).finally(() => inFlight.delete(key));
320
+ inFlight.set(key, job);
321
+ return job;
322
+ }
323
+
324
+ async function doRefresh(userId, accountId, force) {
325
+ // Always the stored record: another caller may have rotated the token since
326
+ // this account object was read.
327
+ const account = await accounts.getAccount(userId, accountId);
328
+ if (!account || !accounts.isOAuth(account)) throw new Error('That mailbox is not signed in with a provider.');
329
+ if (accounts.needsReconnect(account)) throw new OAuthReconnectError(account.oauth?.error || undefined);
330
+ const o = account.oauth;
331
+ if (!force && !needsRefresh(o)) {
332
+ return {accessToken: open(o.accessTokenSealed), expiresAt: Number(o.expiresAt), account};
333
+ }
334
+
335
+ const p = PROVIDERS[o.provider];
336
+ // The generation this renewal works against - see updateOAuth.
337
+ const gen = Number(o.gen) || 0;
338
+ let clientSecret;
339
+ let refreshToken;
340
+ try {
341
+ clientSecret = open(o.clientSecretSealed);
342
+ refreshToken = open(o.refreshTokenSealed);
343
+ } catch (err) {
344
+ // The key moved (MAIL_SECRET added, say): nothing to refresh with.
345
+ return markBroken(account, err.message);
346
+ }
347
+
348
+ const params = {client_id: o.clientId, client_secret: clientSecret, refresh_token: refreshToken, grant_type: 'refresh_token'};
349
+ if (p.refreshScope) params.scope = p.refreshScope;
350
+ let r;
351
+ try {
352
+ r = await postForm(p.tokenUrl(cleanTenant(o.tenant)), params);
353
+ } catch (err) {
354
+ throw new Error(`${p.short} could not be reached to renew the sign-in: ${err.message}`);
355
+ }
356
+ if (!r.ok || !r.body?.access_token) {
357
+ const why = classifyTokenError(r.status, r.body);
358
+ if (why.permanent) return markBroken(account, why.message);
359
+ throw new Error(why.message);
360
+ }
361
+
362
+ const expiresAt = Date.now() + (Number(r.body.expires_in) || 3600) * 1000;
363
+ const saved = await accounts.updateOAuth(userId, accountId, {
364
+ accessTokenSealed: seal(r.body.access_token),
365
+ expiresAt,
366
+ // Rotation: Microsoft answers a new one and the old must be discarded.
367
+ ...(r.body.refresh_token && r.body.refresh_token !== refreshToken ? {refreshTokenSealed: seal(r.body.refresh_token)} : {}),
368
+ refreshedAt: new Date().toISOString(),
369
+ error: null
370
+ }, {expectGen: gen, requireOk: true});
371
+ if (!saved) {
372
+ // Reconnected or Disconnected while this renewal was on the wire: the
373
+ // stored state is newer and wins. Answer from it, never over it.
374
+ const now = await accounts.getAccount(userId, accountId);
375
+ if (!now || accounts.needsReconnect(now)) throw new OAuthReconnectError(now?.oauth?.error || undefined);
376
+ try {
377
+ return {accessToken: open(now.oauth.accessTokenSealed), expiresAt: Number(now.oauth.expiresAt), account: now};
378
+ } catch {
379
+ throw new Error('The sign-in changed while it was being renewed. Try again.');
380
+ }
381
+ }
382
+ return {accessToken: r.body.access_token, expiresAt, account: saved};
383
+ }
384
+
385
+ /** Mark an account as needing a new sign-in, tell its owner, and throw. */
386
+ async function markBroken(account, message) {
387
+ const wasOk = account.oauth?.status === 'ok';
388
+ // Only the sign-in that was refused: a fresh one made meanwhile (Reconnect)
389
+ // is a different generation and must not be marked broken by the old one.
390
+ const saved = await accounts.updateOAuth(account.userId, account.id,
391
+ {status: 'reconnect', error: message, accessTokenSealed: null},
392
+ {expectGen: Number(account.oauth?.gen) || 0});
393
+ if (!saved) throw new Error('The sign-in changed while it was being renewed. Try again.');
394
+ if (wasOk) {
395
+ try { hooks.onNeedsReconnect?.(saved ?? account, message); } catch { /* a notice never breaks mail */ }
396
+ }
397
+ throw new OAuthReconnectError(message);
398
+ }
399
+
400
+ /**
401
+ * The function `toConnection()` hangs on a connection: renew (or force a
402
+ * renewal) and answer the new auth for that protocol.
403
+ *
404
+ * @param {object} account
405
+ * @returns {(force: boolean) => Promise<{accessToken: string, expiresAt: number}>}
406
+ */
407
+ function refresherFor(account) {
408
+ return async (force = false) => {
409
+ const r = await refreshAccount(account.userId, account.id, {force});
410
+ return {accessToken: r.accessToken, expiresAt: r.expiresAt};
411
+ };
412
+ }
413
+
414
+ /**
415
+ * Hand the refresh token back to the provider, where it can be (Google).
416
+ * Best-effort: a sign-in we forget is gone from here either way.
417
+ *
418
+ * @param {object} account
419
+ * @returns {Promise<{revoked: boolean, supported: boolean}>}
420
+ */
421
+ async function revokeAccount(account) {
422
+ const p = PROVIDERS[account?.oauth?.provider];
423
+ if (!p?.revokeUrl) return {revoked: false, supported: false};
424
+ let token;
425
+ try {
426
+ token = open(account.oauth.refreshTokenSealed);
427
+ } catch {
428
+ return {revoked: false, supported: true};
429
+ }
430
+ try {
431
+ const r = await postForm(p.revokeUrl, {token});
432
+ return {revoked: r.ok, supported: true};
433
+ } catch {
434
+ return {revoked: false, supported: true};
435
+ }
436
+ }
437
+
438
+ /** Tell the listener an account is connected again (the callback). */
439
+ function announceConnected(account) {
440
+ try { hooks.onConnected?.(account); } catch { /* ditto */ }
441
+ }
442
+
443
+ /**
444
+ * What the admin is told about the providers - no secrets.
445
+ *
446
+ * @returns {object[]}
447
+ */
448
+ function describeProviders() {
449
+ return Object.values(PROVIDERS).map(p => ({
450
+ id: p.id, label: p.label, short: p.short, needsTenant: p.needsTenant,
451
+ imap: p.imap, smtp: p.smtp, scopes: p.scopes, canRevoke: Boolean(p.revokeUrl)
452
+ }));
453
+ }
454
+
455
+ return {REFRESH_SKEW_MS, PROVIDERS, configureOAuth, _resetOAuth, cleanTenant, pkcePair, authorizeUrl, idTokenClaims, mailboxFor, needsRefresh, classifyTokenError, exchangeCode, refreshAccount, refresherFor, revokeAccount, announceConnected, describeProviders};
456
+ }
@@ -1,3 +1,5 @@
1
+ // Source: @domma-mail/core 0.18.0, packages/core/src/mail/pollSchedule.js.
2
+ // Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
1
3
  /**
2
4
  * Deciding which mailboxes are due a poll.
3
5
  *
@@ -1,136 +1,4 @@
1
- /**
2
- * The background poller.
3
- *
4
- * One ticker for the whole plugin rather than a timer per mailbox: mailboxes
5
- * come and go while the process runs, and a per-mailbox timer means tracking
6
- * and cancelling each one. A single tick asks `selectDuePolls()` who is due
7
- * and works through them.
8
- *
9
- * The scheduling decision - including the backoff that stops a dead mail
10
- * server being knocked every few minutes for a week - lives in
11
- * `pollSchedule.js`, pure and tested there.
12
- *
13
- * @module _lib/mail/poller
14
- */
15
- import {selectDuePolls} from './pollSchedule.js';
16
- import {describeError} from './imapPool.js';
17
-
18
- /** How often the ticker looks for work. Finer than any poll interval. */
19
- const TICK_MS = 30_000;
20
-
21
- /**
22
- * Create a poller.
23
- *
24
- * @param {{listAccounts: Function, syncAccount: Function, defaultMinutes: number,
25
- * logger: object, tickMs?: number}} deps
26
- * @returns {{start: Function, stop: Function, state: Function, pollNow: Function}}
27
- */
28
- export function createPoller({listAccounts, syncAccount, defaultMinutes, logger, tickMs = TICK_MS}) {
29
- /** @type {Map<string, {lastAttemptAt?: number, failures?: number, running?: boolean, lastError?: string|null}>} */
30
- const state = new Map();
31
- let timer = null;
32
- let stopped = false;
33
-
34
- /**
35
- * Sync one mailbox, recording whether it worked.
36
- *
37
- * @param {object} account
38
- * @returns {Promise<void>}
39
- */
40
- async function runOne(account) {
41
- const entry = state.get(account.id) ?? {};
42
- state.set(account.id, {...entry, running: true, lastAttemptAt: Date.now()});
43
-
44
- try {
45
- const result = await syncAccount(account);
46
- state.set(account.id, {
47
- lastAttemptAt: Date.now(), failures: 0, running: false, lastError: null,
48
- lastResult: result
49
- });
50
- } catch (err) {
51
- const failures = (entry.failures ?? 0) + 1;
52
- state.set(account.id, {
53
- lastAttemptAt: Date.now(), failures, running: false, lastError: describeError(err)
54
- });
55
- // Warn, not error: a mail server being unreachable is an ordinary
56
- // condition, and the backoff already handles it.
57
- logger.warn(
58
- `[mail] poll failed for mailbox ${account.id} (${failures} in a row): ${err.message}`
59
- );
60
- }
61
- }
62
-
63
- /**
64
- * One pass: find who is due and sync them, one at a time.
65
- *
66
- * Sequential on purpose. Polling every mailbox at once means opening every
67
- * connection at once, which is exactly the burst a mail server throttles.
68
- *
69
- * @returns {Promise<void>}
70
- */
71
- async function tick() {
72
- if (stopped) return;
73
-
74
- let accounts;
75
- try {
76
- accounts = await listAccounts();
77
- } catch (err) {
78
- logger.warn(`[mail] could not list mailboxes to poll: ${err.message}`);
79
- return;
80
- }
81
-
82
- // Forget state for mailboxes that have been removed, so the map does
83
- // not grow for the life of the process.
84
- const live = new Set(accounts.map(a => a.id));
85
- for (const id of [...state.keys()]) {
86
- if (!live.has(id)) state.delete(id);
87
- }
88
-
89
- const due = selectDuePolls(accounts, state, {now: Date.now(), defaultMinutes});
90
- for (const {id} of due) {
91
- if (stopped) return;
92
- const account = accounts.find(a => a.id === id);
93
- if (account) await runOne(account);
94
- }
95
- }
96
-
97
- return {
98
- /**
99
- * @returns {void}
100
- */
101
- start() {
102
- if (timer) return;
103
- stopped = false;
104
- timer = setInterval(() => {
105
- tick().catch(err => logger.warn(`[mail] poll tick failed: ${err.message}`));
106
- }, tickMs);
107
- // Never hold the process open for a poll.
108
- timer.unref?.();
109
- },
110
-
111
- /**
112
- * @returns {void}
113
- */
114
- stop() {
115
- stopped = true;
116
- if (timer) clearInterval(timer);
117
- timer = null;
118
- },
119
-
120
- /**
121
- * Run a pass immediately, for a manual "sync now".
122
- *
123
- * @returns {Promise<void>}
124
- */
125
- pollNow: tick,
126
-
127
- /**
128
- * What the poller currently believes about each mailbox.
129
- *
130
- * @returns {object}
131
- */
132
- state() {
133
- return Object.fromEntries(state);
134
- }
135
- };
136
- }
1
+ // Source: @domma-mail/core 0.18.0, packages/core/src/mail/poller.js.
2
+ // Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
3
+ /** Compatibility entry point for the shared mail poller. */
4
+ export {createPoller} from './pollerRuntime.js';