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
+24 -4
View File
@@ -16,6 +16,8 @@ function registerMailIpcHandlers(dependencies) {
publicAccount,
normalizeAccount,
connectMail,
authorizeOAuthAccount,
listOAuthProviders,
requestBackgroundSync,
CUSTOM_FOLDER_PREFIX,
flushMailOutbox,
@@ -49,6 +51,8 @@ registerIpcHandler('mail:list-accounts', async () => {
return { accounts: store.accounts.map(publicAccount), activeId: store.activeId };
});
registerIpcHandler('mail:list-oauth-providers', async () => listOAuthProviders());
registerIpcHandler('mail:select-account', async (_event, accountId) => {
const account = await updateAccountStore(store => {
const selected = store.accounts.find(item => item.id === accountId);
@@ -74,8 +78,23 @@ registerIpcHandler('mail:remove-account', async (_event, accountId) => {
registerIpcHandler('mail:update-account', async (_event, accountId, input) => {
const existing = await readAccount(accountId);
if (!existing) throw new Error('That mail account is no longer available.');
const account = normalizeAccount({ ...existing, ...input, password: String(input?.password || '') || existing.password });
let account = normalizeAccount({
...existing,
...input,
authMethod: input?.authMethod || existing.authMethod || 'password',
password: String(input?.password || '') || existing.password || ''
});
account.id = existing.id;
if (account.authMethod === 'oauth2') {
if (!input?.reauthorizeOAuth && existing.authMethod === 'oauth2' && existing.oauthProvider === account.oauthProvider && existing.oauthRefreshToken) {
account.oauthAccessToken = existing.oauthAccessToken;
account.oauthRefreshToken = existing.oauthRefreshToken;
account.oauthAccessTokenExpiresAt = existing.oauthAccessTokenExpiresAt;
} else {
account = await authorizeOAuthAccount(account);
account.id = existing.id;
}
}
try {
const status = await connectMail(account);
const savedAccount = await updateAccountStore(store => {
@@ -100,8 +119,9 @@ registerIpcHandler('mail:update-account', async (_event, accountId, input) => {
});
registerIpcHandler('mail:connect', async (_event, input) => {
const account = normalizeAccount(input);
let account = normalizeAccount(input);
try {
if (account.authMethod === 'oauth2') account = await authorizeOAuthAccount(account);
const status = await connectMail(account);
const saved = await saveAccount(account);
requestBackgroundSync(saved.id);
@@ -133,7 +153,7 @@ registerIpcHandler('mail:sync-folder', async (_event, requestedFolder, offset =
registerIpcHandler('mail:list-folders', async () => {
const account = await readAccount();
if (!account) throw new Error('Add a mail account before listing folders.');
const client = imapClient(account);
const client = await imapClient(account);
try {
await client.connect();
return { folders: await listCustomFolders(client) };
@@ -149,7 +169,7 @@ registerIpcHandler('mail:create-folder', async (_event, input) => {
const account = await readAccount();
if (!account) throw new Error('Add a mail account before creating folders.');
const name = validFolderName(input?.name);
const client = imapClient(account);
const client = await imapClient(account);
try {
await client.connect();
const existing = await client.list();