Implemented desktop OAuth2 account setup for Google and Microsoft. The flow uses the system browser and PKCE; access and refresh tokens stay in the main process and encrypted account store. OAuth connections use fixed provider endpoints, and existing password-based accounts remain supported. Android remains password-based in this phase.

This commit is contained in:
2026-10-17 00:51:23 +03:00
parent 1ef934045f
commit ea825adcc1
29 changed files with 943 additions and 71 deletions
+36 -32
View File
@@ -32,6 +32,9 @@ const { createMailboxSyncService } = require('./mail/sync-service.cjs');
const { createBackgroundSyncCoordinator } = require('./mail/sync-coordinator.cjs');
const { createIdleWatcherManager } = require('./mail/idle-watcher-manager.cjs');
const { createSnoozeReminderService } = require('./mail/snooze-reminder-service.cjs');
const { createOAuth2Service, loadOAuthProviderConfig } = require('./mail/oauth2-service.cjs');
const { createOAuthTokenManager } = require('./mail/oauth-token-manager.cjs');
const { createMailConnections } = require('./mail/connection-factory.cjs');
const { isConnectivityFailure } = require('./mail/connectivity.cjs');
const { assertTrustedIpcSender, isTrustedApplicationUrl } = require('./security/ipc-sender.cjs');
const { validateIpcArguments } = require('./security/ipc-arguments.cjs');
@@ -98,6 +101,29 @@ function updateAccountStore(update) {
return operation;
}
const oauth2Service = createOAuth2Service({
providers: loadOAuthProviderConfig(),
openExternal: url => shell.openExternal(url)
});
const oauthTokenManager = createOAuthTokenManager({
oauth2Service,
async persistTokens(accountId, tokens) {
if (!accountId) return;
await updateAccountStore(store => {
const account = store.accounts.find(item => item.id === accountId);
if (account?.authMethod === 'oauth2') Object.assign(account, tokens);
});
}
});
async function authorizeOAuthAccount(account) {
return { ...account, ...await oauth2Service.authorize(account.oauthProvider, account.email) };
}
function listOAuthProviders() {
return oauth2Service.listProviders();
}
function sendMailSyncUpdate(update) {
for (const window of BrowserWindow.getAllWindows()) {
if (!window.isDestroyed() && !window.webContents.isDestroyed()) {
@@ -170,35 +196,11 @@ function activeDavConfig(store, type, id = null) {
return store[key].find(item => item.id === (id || store[activeKey])) || null;
}
function imapClient(account, options = {}) {
return new ImapFlow({
host: account.imapHost,
port: account.imapPort,
secure: false,
doSTARTTLS: true,
auth: { user: account.email, pass: account.password },
tls: { rejectUnauthorized: true },
logger: false,
connectionTimeout: 15000,
greetingTimeout: 10000,
socketTimeout: 20000,
...options
});
}
function smtpTransport(account) {
return nodemailer.createTransport({
host: account.smtpHost,
port: account.smtpPort,
secure: false,
requireTLS: true,
auth: { user: account.email, pass: account.password },
tls: { rejectUnauthorized: true, servername: account.smtpHost },
connectionTimeout: 15000,
greetingTimeout: 10000,
socketTimeout: 20000
});
}
const { imapClient, smtpTransport } = createMailConnections({
ImapFlow,
createTransport: nodemailer.createTransport,
oauthTokenManager
});
function assertSecureStorage() {
if (process.platform === 'linux' && typeof safeStorage.getSelectedStorageBackend === 'function') {
@@ -235,12 +237,12 @@ async function saveAccount(account) {
}
async function connectMail(account) {
const client = imapClient(account);
const client = await imapClient(account);
let transport;
try {
await client.connect();
const mailbox = await client.mailboxOpen('INBOX', { readOnly: true });
transport = smtpTransport(account);
transport = await smtpTransport(account);
await transport.verify();
return { mailbox: mailbox.name, messages: mailbox.exists };
} finally {
@@ -317,7 +319,7 @@ async function fetchSnoozedMailbox(account, offset, limit) {
.filter(item => Number(item.dueAt) > now)
.sort((first, second) => Number(first.dueAt) - Number(second.dueAt));
const selected = pending.slice(Math.max(0, Number(offset) || 0), Math.max(0, Number(offset) || 0) + Math.max(1, Math.min(50, Number(limit) || 20)));
const client = imapClient(account);
const client = await imapClient(account);
const messages = [];
try {
await client.connect();
@@ -462,6 +464,8 @@ registerMailIpcHandlers({
publicAccount,
normalizeAccount,
connectMail,
authorizeOAuthAccount,
listOAuthProviders,
requestBackgroundSync,
CUSTOM_FOLDER_PREFIX,
flushMailOutbox,