domma-cms 1.28.4 → 1.29.1
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/CHANGELOG.md +9 -0
- package/admin/js/lib/contacts-arrange.js +1 -1
- package/docs/DommaFeatures.md +15 -9
- package/docs/plugin-development.md +1 -1
- package/package.json +4 -1
- package/plugins/_lib/admin/mail/body-menu.js +2 -0
- package/plugins/_lib/admin/mail/cms-host.js +22 -0
- package/plugins/_lib/admin/mail/compose-window.js +1027 -1021
- package/plugins/_lib/admin/mail/contacts.js +10 -8
- package/plugins/_lib/admin/mail/diagnostics-section.js +2 -0
- package/plugins/_lib/admin/mail/drag-drop.js +46 -0
- package/plugins/_lib/admin/mail/folder-tree.js +2 -0
- package/plugins/_lib/admin/mail/font-controls.js +6 -4
- package/plugins/_lib/admin/mail/fonts.js +1 -1
- package/plugins/_lib/admin/mail/host-context.js +87 -0
- package/plugins/_lib/admin/mail/identity.js +2 -0
- package/plugins/_lib/admin/mail/image-senders-section.js +2 -0
- package/plugins/_lib/admin/mail/keyboard.js +2 -0
- package/plugins/_lib/admin/mail/mail.css +1 -1
- package/plugins/_lib/admin/mail/mail.html +4 -0
- package/plugins/_lib/admin/mail/message-actions.js +2 -0
- package/plugins/_lib/admin/mail/oauth-panel.js +2 -0
- package/plugins/_lib/admin/mail/panes.js +2 -0
- package/plugins/_lib/admin/mail/reader-kit.js +2 -0
- package/plugins/_lib/admin/mail/reader-runtime.js +6534 -0
- package/plugins/_lib/admin/mail/reader-view.js +9 -6380
- package/plugins/_lib/admin/mail/resizable.js +2 -0
- package/plugins/_lib/admin/mail/restore-spam.js +13 -0
- package/plugins/_lib/admin/mail/rules.js +14 -0
- package/plugins/_lib/admin/mail/scheduling.js +4 -1
- package/plugins/_lib/admin/mail/section-kit.js +4 -1
- package/plugins/_lib/admin/mail/security-note.js +2 -0
- package/plugins/_lib/admin/mail/settings-section.js +11 -10
- package/plugins/_lib/admin/mail/templates.js +5 -2
- package/plugins/_lib/admin/mail/threads.js +2 -0
- package/plugins/_lib/admin/mail/vacation.js +16 -2
- package/plugins/_lib/admin/mail/when-closed.js +24 -2
- package/plugins/_lib/admin/ui/dates.js +2 -0
- package/plugins/_lib/admin/ui/resizable.js +7 -5
- package/plugins/_lib/contacts/mailContact.js +69 -0
- package/plugins/_lib/contacts/model.js +199 -0
- package/plugins/_lib/domma-mail-contacts.lock.json +10 -0
- package/plugins/_lib/domma-mail.lock.json +85 -0
- package/plugins/_lib/mail/accountFileStore.js +66 -0
- package/plugins/_lib/mail/accountRepository.js +617 -0
- package/plugins/_lib/mail/accountRoutes.js +130 -0
- package/plugins/_lib/mail/accounts.js +66 -940
- package/plugins/_lib/mail/automationLock.js +10 -0
- package/plugins/_lib/mail/bodyTokenProvider.js +36 -0
- package/plugins/_lib/mail/bodyTokens.js +5 -99
- package/plugins/_lib/mail/compose.js +5 -3
- package/plugins/_lib/mail/defaults.js +2 -0
- package/plugins/_lib/mail/envelope.js +3 -1
- package/plugins/_lib/mail/folderActions.js +83 -0
- package/plugins/_lib/mail/folderRuntime.js +109 -0
- package/plugins/_lib/mail/folders.js +4 -9
- package/plugins/_lib/mail/imapPool.js +11 -389
- package/plugins/_lib/mail/imapPoolRuntime.js +396 -0
- package/plugins/_lib/mail/mbox.js +2 -0
- package/plugins/_lib/mail/message-action.js +2 -0
- package/plugins/_lib/mail/messageSecurity.js +4 -111
- package/plugins/_lib/mail/messageSecurityProvider.js +116 -0
- package/plugins/_lib/mail/networkAddresses.js +77 -0
- package/plugins/_lib/mail/networkPolicy.js +62 -0
- package/plugins/_lib/mail/oauth.js +3 -451
- package/plugins/_lib/mail/oauthAccounts.js +178 -0
- package/plugins/_lib/mail/oauthRouteRuntime.js +282 -0
- package/plugins/_lib/mail/oauthRoutes.js +7 -289
- package/plugins/_lib/mail/oauthRuntime.js +456 -0
- package/plugins/_lib/mail/pollSchedule.js +2 -0
- package/plugins/_lib/mail/poller.js +4 -136
- package/plugins/_lib/mail/pollerRuntime.js +150 -0
- package/plugins/_lib/mail/priority.js +2 -0
- package/plugins/_lib/mail/publicMailbox.js +27 -0
- package/plugins/_lib/mail/readRoutes.js +14 -827
- package/plugins/_lib/mail/readRuntime.js +701 -0
- package/plugins/_lib/mail/render.js +2 -0
- package/plugins/_lib/mail/ruleRunner.js +27 -10
- package/plugins/_lib/mail/scheduler.js +11 -279
- package/plugins/_lib/mail/schedulerRuntime.js +259 -0
- package/plugins/_lib/mail/secretProvider.js +112 -0
- package/plugins/_lib/mail/secretbox.js +12 -225
- package/plugins/_lib/mail/send.js +21 -537
- package/plugins/_lib/mail/sieve.js +211 -0
- package/plugins/_lib/mail/sieveHost.js +24 -0
- package/plugins/_lib/mail/smtpService.js +553 -0
- package/plugins/_lib/mail/snoozeRuntime.js +60 -0
- package/plugins/_lib/mail/store.js +122 -64
- package/plugins/_lib/mail/subscriptions.js +11 -0
- package/plugins/_lib/mail/sync.js +64 -290
- package/plugins/_lib/mail/syncPlan.js +2 -0
- package/plugins/_lib/mail/syncRuntime.js +255 -0
- package/plugins/_lib/mail/syncSelection.js +2 -0
- package/plugins/_lib/mail/unreadBadge.js +2 -0
- package/plugins/_lib/mail/unsubscribe.js +2 -0
- package/plugins/_lib/mail/vacationRunner.js +3 -116
- package/plugins/_lib/mail/vacationRuntime.js +70 -0
- package/plugins/_lib/mail/write.js +44 -13
- package/server/routes/api/contacts.js +2 -196
- package/server/routes/api/forms.js +8 -1
- package/server/services/viewerSession.js +1 -1
|
@@ -1,294 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The OAuth2 sign-in routes for mailboxes: Connect, the provider's callback,
|
|
3
|
-
* Disconnect and Test. Registered by Email Pro (which feature-detects this
|
|
4
|
-
* file - an engine without it simply offers no sign-in); the free reader does
|
|
5
|
-
* not register them, but reads a signed-in mailbox all the same.
|
|
6
|
-
*
|
|
7
|
-
* Each customer registers THEIR OWN app with Microsoft or Google - the Docs
|
|
8
|
-
* Pro cloud-copies design. The admin form shows this site's exact redirect
|
|
9
|
-
* URI with a guide; the client secret goes in sealed and never comes out.
|
|
10
|
-
*
|
|
11
|
-
* The callback is the one route without a session. Its `state` is a one-time
|
|
12
|
-
* ticket, ten minutes long, naming the person who pressed Connect, and the
|
|
13
|
-
* callback refuses a browser signed in to this site as somebody else.
|
|
14
|
-
*
|
|
15
|
-
* @module _lib/mail/oauthRoutes
|
|
16
|
-
*/
|
|
17
|
-
import {createHash, randomBytes, timingSafeEqual} from 'node:crypto';
|
|
18
|
-
|
|
19
1
|
import {originOf, trustedOrigin} from '../../../server/services/passwordReset.js';
|
|
20
2
|
import {resolveViewer} from '../../../server/services/viewerSession.js';
|
|
21
3
|
import * as accounts from './accounts.js';
|
|
22
|
-
import
|
|
4
|
+
import * as secrets from './secretbox.js';
|
|
23
5
|
import {verifySmtp} from './send.js';
|
|
24
|
-
import
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/** Pending sign-ins kept at once - a ceiling on what a busy (or hostile) admin can pile up. */
|
|
31
|
-
const MAX_PENDING = 200;
|
|
32
|
-
/** Pending sign-ins per person: a new one beyond this replaces their oldest. */
|
|
33
|
-
const MAX_PENDING_PER_USER = 5;
|
|
34
|
-
|
|
35
|
-
const sha256 = (v) => createHash('sha256').update(String(v)).digest();
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The browser-binding cookie's name for one sign-in. Per state, so two
|
|
39
|
-
* Connects in two tabs do not overwrite each other's cookie.
|
|
40
|
-
*
|
|
41
|
-
* @param {string} state
|
|
42
|
-
* @returns {string}
|
|
43
|
-
*/
|
|
44
|
-
export function bindCookieName(state) {
|
|
45
|
-
return `dm_mail_oauth_${sha256(state).toString('hex').slice(0, 16)}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* One cookie's value from a raw Cookie header (no cookie plugin assumed).
|
|
50
|
-
*
|
|
51
|
-
* @param {string|undefined} header
|
|
52
|
-
* @param {string} name
|
|
53
|
-
* @returns {string}
|
|
54
|
-
*/
|
|
55
|
-
export function readCookie(header, name) {
|
|
56
|
-
for (const part of String(header ?? '').split(';')) {
|
|
57
|
-
const at = part.indexOf('=');
|
|
58
|
-
if (at !== -1 && part.slice(0, at).trim() === name) return part.slice(at + 1).trim();
|
|
59
|
-
}
|
|
60
|
-
return '';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const text = (v, max = 300) => String(v ?? '').trim().slice(0, max);
|
|
64
|
-
// JSON inside a <script>: nothing in it can close the element early.
|
|
65
|
-
const js = (v) => JSON.stringify(v).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
|
|
66
|
-
const esc = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @param {object} fastify
|
|
70
|
-
* @param {{authenticate: Function, pool: object, pluginName: string, config?: object,
|
|
71
|
-
* onNeedsReconnect?: Function, onConnected?: Function, fetch?: Function}} options
|
|
72
|
-
*/
|
|
73
|
-
export function registerOAuthRoutes(fastify, {authenticate, pool, pluginName, config = {}, onNeedsReconnect, onConnected, fetch}) {
|
|
74
|
-
configureOAuth({fetch, onNeedsReconnect: onNeedsReconnect ?? null, onConnected: onConnected ?? null});
|
|
75
|
-
|
|
76
|
-
/** state -> {userId, accountId, provider, clientId, clientSecretSealed, tenant, label, username, verifier, redirectUri, expires} */
|
|
77
|
-
const pending = new Map();
|
|
78
|
-
const userId = (request) => request.user?.id ?? request.user?.sub ?? null;
|
|
79
|
-
|
|
80
|
-
/** This site's exact return address - what the customer registers with the provider. */
|
|
81
|
-
async function redirectUriFor(request) {
|
|
82
|
-
const origin = await trustedOrigin(originOf(request)).catch(() => '') || originOf(request);
|
|
83
|
-
return `${origin}/api/plugins/${pluginName}/oauth/callback`;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function sweep() {
|
|
87
|
-
const now = Date.now();
|
|
88
|
-
for (const [k, v] of pending) if (v.expires < now) pending.delete(k);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** GET /oauth/status - providers and the redirect URI, for the form's guide. */
|
|
92
|
-
fastify.get('/oauth/status', {preHandler: [authenticate]}, async (request) => {
|
|
93
|
-
const redirectUri = await redirectUriFor(request);
|
|
94
|
-
return {available: true, redirectUri, https: redirectUri.startsWith('https://'), providers: describeProviders()};
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* POST /oauth/start - where to send the person to sign in.
|
|
99
|
-
* Body: {accountId?, provider, clientId, clientSecret (blank = keep the saved one), tenant?, label?, username?}
|
|
100
|
-
*/
|
|
101
|
-
fastify.post('/oauth/start', {preHandler: [authenticate], config: {rateLimit: {max: 20, timeWindow: '1 minute'}}}, async (request, reply) => {
|
|
102
|
-
const uid = userId(request);
|
|
103
|
-
if (!uid) return reply.code(401).send({error: 'Authentication required.'});
|
|
104
|
-
const body = request.body ?? {};
|
|
105
|
-
const provider = text(body.provider, 20);
|
|
106
|
-
if (!PROVIDERS[provider]) return reply.code(400).send({error: 'Choose Microsoft or Google.'});
|
|
107
|
-
|
|
108
|
-
const accountId = body.accountId ? text(body.accountId, 80) : null;
|
|
109
|
-
const existing = accountId ? await accounts.getAccount(uid, accountId) : null;
|
|
110
|
-
if (accountId && !existing) return reply.code(404).send({error: 'That mailbox does not exist.'});
|
|
111
|
-
|
|
112
|
-
const clientId = text(body.clientId, 200);
|
|
113
|
-
if (!clientId) return reply.code(400).send({error: PROVIDERS[provider].id === 'microsoft' ? 'The Application (client) ID is needed.' : 'The Client ID is needed.'});
|
|
114
|
-
|
|
115
|
-
// Blank secret = the one already saved, but only for the same app.
|
|
116
|
-
let clientSecret = text(body.clientSecret, 2000);
|
|
117
|
-
if (!clientSecret) {
|
|
118
|
-
const o = existing?.oauth;
|
|
119
|
-
if (o?.clientSecretSealed && o.provider === provider && o.clientId === clientId) {
|
|
120
|
-
try {
|
|
121
|
-
clientSecret = open(o.clientSecretSealed);
|
|
122
|
-
} catch (err) {
|
|
123
|
-
if (err instanceof MailKeyError) return reply.code(409).send({error: 'The saved client secret can no longer be read - type it again.'});
|
|
124
|
-
throw err;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (!clientSecret) return reply.code(400).send({error: 'The client secret is needed.'});
|
|
129
|
-
|
|
130
|
-
const username = text(body.username, 254).toLowerCase();
|
|
131
|
-
if (username && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(username)) return reply.code(400).send({error: 'That is not an email address.'});
|
|
132
|
-
|
|
133
|
-
sweep();
|
|
134
|
-
// Per person first: their oldest waiting sign-in makes room. Then the
|
|
135
|
-
// site-wide ceiling, which only many people at once can reach.
|
|
136
|
-
const mine = [...pending.entries()].filter(([, v]) => v.userId === uid).sort((a, b) => a[1].expires - b[1].expires);
|
|
137
|
-
while (mine.length >= MAX_PENDING_PER_USER) pending.delete(mine.shift()[0]);
|
|
138
|
-
if (pending.size >= MAX_PENDING) return reply.code(429).send({error: 'Too many sign-ins are waiting. Try again in a few minutes.'});
|
|
139
|
-
|
|
140
|
-
const state = randomBytes(24).toString('base64url');
|
|
141
|
-
const {verifier, challenge} = pkcePair();
|
|
142
|
-
const redirectUri = await redirectUriFor(request);
|
|
143
|
-
const tenant = PROVIDERS[provider].needsTenant ? cleanTenant(body.tenant) : null;
|
|
144
|
-
// Binds the sign-in to THIS browser: the callback refuses one that does
|
|
145
|
-
// not bring the cookie back. Without it, an anonymous browser could
|
|
146
|
-
// finish a sign-in someone else started (send a colleague the
|
|
147
|
-
// provider link, and their mailbox lands under your account).
|
|
148
|
-
// HttpOnly, SameSite=Lax (sent on the provider's top-level redirect
|
|
149
|
-
// back), scoped to the callback path, gone in ten minutes.
|
|
150
|
-
const bind = randomBytes(24).toString('base64url');
|
|
151
|
-
const callbackPath = new URL(redirectUri).pathname;
|
|
152
|
-
reply.header('Set-Cookie', `${bindCookieName(state)}=${bind}; Path=${callbackPath}; Max-Age=${STATE_TTL / 1000}; HttpOnly; SameSite=Lax`
|
|
153
|
-
+ (redirectUri.startsWith('https://') ? '; Secure' : ''));
|
|
154
|
-
pending.set(state, {
|
|
155
|
-
userId: uid, accountId, provider, clientId, clientSecretSealed: seal(clientSecret), tenant,
|
|
156
|
-
label: text(body.label, 80), username, verifier, redirectUri, bindHash: sha256(bind), expires: Date.now() + STATE_TTL
|
|
157
|
-
});
|
|
158
|
-
return {
|
|
159
|
-
url: authorizeUrl({provider, clientId, tenant, redirectUri, state, codeChallenge: challenge,
|
|
160
|
-
loginHint: username || existing?.username || ''}),
|
|
161
|
-
redirectUri
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* GET /oauth/callback - the provider sends the person back here. Answers a
|
|
167
|
-
* small page that tells the mail screen (its opener) and closes itself.
|
|
168
|
-
*/
|
|
169
|
-
fastify.get('/oauth/callback', {config: {rateLimit: {max: 30, timeWindow: '1 minute'}}}, async (request, reply) => {
|
|
170
|
-
const q = request.query ?? {};
|
|
171
|
-
const key = String(q.state ?? '');
|
|
172
|
-
const ticket = pending.get(key);
|
|
173
|
-
pending.delete(key);
|
|
174
|
-
|
|
175
|
-
const page = (ok, message, origin, accountId = null) => reply
|
|
176
|
-
.type('text/html; charset=utf-8')
|
|
177
|
-
.header('Cache-Control', 'no-store')
|
|
178
|
-
.header('Referrer-Policy', 'no-referrer')
|
|
179
|
-
.header('X-Robots-Tag', 'noindex')
|
|
180
|
-
.send(`<!doctype html><meta charset="utf-8"><meta name="robots" content="noindex"><title>${ok ? 'Mailbox connected' : 'Not connected'}</title>`
|
|
181
|
-
+ `<body style="font-family:system-ui,sans-serif;display:grid;place-items:center;min-height:90vh;margin:0"><main style="max-width:30rem;text-align:center;padding:1rem">`
|
|
182
|
-
+ `<h1 style="font-size:1.3rem">${ok ? 'Mailbox connected' : 'Not connected'}</h1><p>${esc(message)}</p><p>You can close this window.</p></main>`
|
|
183
|
-
+ `<script>try{window.opener&&window.opener.postMessage(${js({type: 'domma-mail-oauth', ok, message, accountId})},${js(origin || '')});}catch(e){}${ok ? 'setTimeout(function(){window.close();},1200);' : ''}</script>`);
|
|
184
|
-
|
|
185
|
-
if (!ticket || ticket.expires < Date.now()) {
|
|
186
|
-
return page(false, 'This sign-in link has expired or was already used. Go back to Email and press Connect again.', null);
|
|
187
|
-
}
|
|
188
|
-
const origin = new URL(ticket.redirectUri).origin;
|
|
189
|
-
// Used up either way; clear it.
|
|
190
|
-
const cookieName = bindCookieName(key);
|
|
191
|
-
reply.header('Set-Cookie', `${cookieName}=; Path=${new URL(ticket.redirectUri).pathname}; Max-Age=0; HttpOnly; SameSite=Lax`);
|
|
192
|
-
if (q.error) return page(false, `The sign-in was not completed: ${text(q.error_description || q.error, 200)}`, origin);
|
|
193
|
-
|
|
194
|
-
// The browser that pressed Connect, and no other.
|
|
195
|
-
const bound = readCookie(request.headers.cookie, cookieName);
|
|
196
|
-
if (!bound || !timingSafeEqual(sha256(bound), ticket.bindHash)) {
|
|
197
|
-
return page(false, `This sign-in has to be finished in the browser that pressed Connect, with the admin open at ${origin}. Press Connect again there.`, origin);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Signed in to this site as somebody else: refuse - whoever pressed
|
|
201
|
-
// Connect must be the one who finishes it.
|
|
202
|
-
const viewer = await resolveViewer(request).catch(() => null);
|
|
203
|
-
if (viewer?.id && viewer.id !== ticket.userId) {
|
|
204
|
-
return page(false, 'This sign-in was started by someone else. Press Connect yourself.', origin);
|
|
205
|
-
}
|
|
206
|
-
if (ticket.accountId && !(await accounts.getAccount(ticket.userId, ticket.accountId))) {
|
|
207
|
-
return page(false, 'That mailbox no longer exists.', origin);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
try {
|
|
211
|
-
const clientSecret = open(ticket.clientSecretSealed);
|
|
212
|
-
const got = await exchangeCode({
|
|
213
|
-
provider: ticket.provider, clientId: ticket.clientId, clientSecret, tenant: ticket.tenant,
|
|
214
|
-
code: text(q.code, 4000), redirectUri: ticket.redirectUri, codeVerifier: ticket.verifier
|
|
215
|
-
});
|
|
216
|
-
const box = mailboxFor(ticket.provider, got.claims);
|
|
217
|
-
// Typed wins: a Microsoft 365 shared mailbox is signed in to by a
|
|
218
|
-
// person but addressed as itself (XOAUTH2 user = the shared address).
|
|
219
|
-
const username = ticket.username || box.email;
|
|
220
|
-
if (!username) {
|
|
221
|
-
return page(false, 'The provider did not say which mailbox was signed in to. Type the email address in the form and Connect again.', origin);
|
|
222
|
-
}
|
|
223
|
-
const p = PROVIDERS[ticket.provider];
|
|
224
|
-
const saved = await accounts.saveOAuthAccount(ticket.userId, ticket.accountId, {
|
|
225
|
-
label: ticket.label || username,
|
|
226
|
-
username,
|
|
227
|
-
provider: ticket.provider,
|
|
228
|
-
clientId: ticket.clientId,
|
|
229
|
-
tenant: ticket.tenant,
|
|
230
|
-
clientSecret,
|
|
231
|
-
refreshToken: got.refreshToken,
|
|
232
|
-
accessToken: got.accessToken,
|
|
233
|
-
expiresAt: got.expiresAt,
|
|
234
|
-
scope: got.scope,
|
|
235
|
-
imap: p.imap,
|
|
236
|
-
smtp: box.smtp
|
|
237
|
-
});
|
|
238
|
-
if (!saved) return page(false, 'That mailbox no longer exists.', origin);
|
|
239
|
-
// A connection opened with the old details must not be reused.
|
|
240
|
-
pool.release(`${ticket.userId}:${saved.id}`);
|
|
241
|
-
announceConnected(saved);
|
|
242
|
-
return page(true, `${username} is connected through ${p.short}.`, origin, saved.id);
|
|
243
|
-
} catch (err) {
|
|
244
|
-
fastify.log.warn(`[${pluginName}] mailbox sign-in failed: ${err.message}`);
|
|
245
|
-
return page(false, text(err.message, 300), origin);
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
/** POST /oauth/:id/disconnect - hand the sign-in back (where the provider allows) and forget it. */
|
|
250
|
-
fastify.post('/oauth/:id/disconnect', {preHandler: [authenticate]}, async (request, reply) => {
|
|
251
|
-
const uid = userId(request);
|
|
252
|
-
if (!uid) return reply.code(401).send({error: 'Authentication required.'});
|
|
253
|
-
const account = await accounts.getAccount(uid, request.params.id);
|
|
254
|
-
if (!account) return reply.code(404).send({error: 'That mailbox does not exist.'});
|
|
255
|
-
if (!accounts.isOAuth(account)) return reply.code(400).send({error: 'This mailbox uses a password, not a sign-in.'});
|
|
256
|
-
|
|
257
|
-
const result = await revokeAccount(account);
|
|
258
|
-
pool.release(`${uid}:${account.id}`);
|
|
259
|
-
// A new generation: a renewal already on the wire must not write a
|
|
260
|
-
// working token back over the disconnect (see accounts.updateOAuth).
|
|
261
|
-
const saved = await accounts.updateOAuth(uid, account.id, {
|
|
262
|
-
status: 'disconnected',
|
|
263
|
-
error: 'Disconnected. Reconnect to use this mailbox again.',
|
|
264
|
-
refreshTokenSealed: null,
|
|
265
|
-
accessTokenSealed: null,
|
|
266
|
-
expiresAt: 0
|
|
267
|
-
}, {bumpGen: true});
|
|
268
|
-
return {account: accounts.toPublic(saved), revoked: result.revoked, revokeSupported: result.supported};
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
/** POST /oauth/:id/test - sign in to both servers with the stored sign-in. */
|
|
272
|
-
fastify.post('/oauth/:id/test', {preHandler: [authenticate], config: {rateLimit: {max: 10, timeWindow: '1 minute'}}}, async (request, reply) => {
|
|
273
|
-
const uid = userId(request);
|
|
274
|
-
if (!uid) return reply.code(401).send({error: 'Authentication required.'});
|
|
275
|
-
const account = await accounts.getAccount(uid, request.params.id);
|
|
276
|
-
if (!account) return reply.code(404).send({error: 'That mailbox does not exist.'});
|
|
277
|
-
if (!accounts.isOAuth(account)) return reply.code(400).send({error: 'This mailbox uses a password, not a sign-in.'});
|
|
278
|
-
|
|
279
|
-
let imap;
|
|
280
|
-
let smtp;
|
|
281
|
-
try {
|
|
282
|
-
imap = await pool.test(accounts.toConnection(account));
|
|
283
|
-
} catch (err) {
|
|
284
|
-
imap = {ok: false, error: err.message};
|
|
285
|
-
}
|
|
286
|
-
try {
|
|
287
|
-
const out = accounts.toSmtpConnection(account);
|
|
288
|
-
smtp = out ? await verifySmtp(out.transport, config.allowInsecureTLS === true) : {ok: false, error: 'No outgoing server.'};
|
|
289
|
-
} catch (err) {
|
|
290
|
-
smtp = {ok: false, error: err.message};
|
|
291
|
-
}
|
|
292
|
-
return {ok: imap.ok && smtp.ok, imap, smtp};
|
|
293
|
-
});
|
|
6
|
+
import * as oauth from './oauth.js';
|
|
7
|
+
import {registerOAuthRuntime} from './oauthRouteRuntime.js';
|
|
8
|
+
export {bindCookieName, readCookie} from './oauthRouteRuntime.js';
|
|
9
|
+
export function registerOAuthRoutes(app, options) {
|
|
10
|
+
return registerOAuthRuntime(app, {...options, accounts, secrets, MailKeyError: secrets.MailKeyError, verifySmtp, oauth, resolveViewer,
|
|
11
|
+
redirectUriFor: async request => `${await trustedOrigin(originOf(request)).catch(() => '') || originOf(request)}/api/plugins/${options.pluginName}/oauth/callback`});
|
|
294
12
|
}
|