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
+104
View File
@@ -0,0 +1,104 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { createOAuthTokenManager } = require('../desktop/mail/oauth-token-manager.cjs');
const { createMailConnections } = require('../desktop/mail/connection-factory.cjs');
test('uses a fresh OAuth access token without refreshing it', async () => {
let refreshes = 0;
const manager = createOAuthTokenManager({
oauth2Service: { async refresh() { refreshes += 1; } },
async persistTokens() {},
now: () => 1_000
});
const account = {
id: 'account-1', authMethod: 'oauth2', oauthProvider: 'google',
oauthAccessToken: 'fresh-token', oauthRefreshToken: 'refresh-token', oauthAccessTokenExpiresAt: 120_000
};
assert.equal(await manager.getAccessToken(account), 'fresh-token');
assert.equal(refreshes, 0);
});
test('refreshes expired OAuth tokens once and persists the encrypted-store fields', async () => {
let refreshes = 0;
const persisted = [];
const manager = createOAuthTokenManager({
oauth2Service: {
async refresh(provider, refreshToken) {
refreshes += 1;
assert.equal(provider, 'microsoft');
assert.equal(refreshToken, 'old-refresh');
await new Promise(resolve => setImmediate(resolve));
return { oauthAccessToken: 'fresh-token', oauthRefreshToken: 'new-refresh', oauthAccessTokenExpiresAt: 200_000 };
}
},
async persistTokens(id, tokens) { persisted.push({ id, tokens }); },
now: () => 1_000
});
const first = { id: 'account-2', authMethod: 'oauth2', oauthProvider: 'microsoft', oauthRefreshToken: 'old-refresh', oauthAccessTokenExpiresAt: 0 };
const second = { ...first };
assert.deepEqual(await Promise.all([manager.getAccessToken(first), manager.getAccessToken(second)]), ['fresh-token', 'fresh-token']);
assert.equal(refreshes, 1);
assert.equal(persisted.length, 1);
assert.equal(persisted[0].id, 'account-2');
assert.equal(first.oauthRefreshToken, 'new-refresh');
assert.equal(second.oauthAccessToken, 'fresh-token');
});
test('rejects expired OAuth accounts without a refresh token', async () => {
const manager = createOAuthTokenManager({
oauth2Service: { async refresh() { assert.fail('must not refresh without a token'); } },
async persistTokens() {},
now: () => 100_000
});
await assert.rejects(manager.getAccessToken({ authMethod: 'oauth2', oauthProvider: 'google' }), /Reconnect this OAuth2 account/);
});
test('mail connection factory uses OAuth bearer tokens and TLS provider defaults', async () => {
const imapOptions = [];
const smtpOptions = [];
const manager = { async getAccessToken(account) { return `token:${account.email}`; } };
const connections = createMailConnections({
ImapFlow: class { constructor(options) { imapOptions.push(options); } },
createTransport(options) { smtpOptions.push(options); return { options }; },
oauthTokenManager: manager
});
const oauthAccount = {
authMethod: 'oauth2', oauthProvider: 'google', email: 'user@gmail.com',
imapHost: 'imap.gmail.com', imapPort: 993, imapSecure: true,
smtpHost: 'smtp.gmail.com', smtpPort: 587, smtpSecure: false,
oauthAccessTokenExpiresAt: 123456
};
await connections.imapClient(oauthAccount, { autoIdleDelay: 10 });
await connections.smtpTransport(oauthAccount);
assert.deepEqual(imapOptions[0].auth, { user: 'user@gmail.com', accessToken: 'token:user@gmail.com' });
assert.equal(imapOptions[0].secure, true);
assert.equal(imapOptions[0].doSTARTTLS, false);
assert.equal(imapOptions[0].autoIdleDelay, 10);
assert.deepEqual(smtpOptions[0].auth, {
type: 'OAuth2', user: 'user@gmail.com', accessToken: 'token:user@gmail.com', expires: 123456
});
assert.equal(smtpOptions[0].requireTLS, true);
});
test('mail connection factory retains password and STARTTLS behavior for existing accounts', async () => {
let imapOptions;
let smtpOptions;
const connections = createMailConnections({
ImapFlow: class { constructor(options) { imapOptions = options; } },
createTransport(options) { smtpOptions = options; return {}; },
oauthTokenManager: { async getAccessToken() { assert.fail('password accounts must not request OAuth tokens'); } }
});
const account = {
email: 'user@example.test', password: 'mail-password', imapHost: 'imap.example.test', imapPort: 143,
smtpHost: 'smtp.example.test', smtpPort: 587
};
await connections.imapClient(account);
await connections.smtpTransport(account);
assert.deepEqual(imapOptions.auth, { user: account.email, pass: account.password });
assert.equal(imapOptions.secure, false);
assert.equal(imapOptions.doSTARTTLS, true);
assert.deepEqual(smtpOptions.auth, { user: account.email, pass: account.password });
assert.equal(smtpOptions.requireTLS, true);
});