const test = require('node:test'); const assert = require('node:assert/strict'); const { createOAuth2Service, loadOAuthProviderConfig } = require('../desktop/mail/oauth2-service.cjs'); test('loads only externally configured OAuth client identifiers', () => { assert.deepEqual(loadOAuthProviderConfig({}), {}); const providers = loadOAuthProviderConfig({ EP_MAIL_GOOGLE_OAUTH_CLIENT_ID: 'google-client.apps.example', EP_MAIL_GOOGLE_OAUTH_CLIENT_SECRET: 'google-desktop-secret', EP_MAIL_MICROSOFT_OAUTH_CLIENT_ID: 'microsoft-client', EP_MAIL_MICROSOFT_OAUTH_TENANT_ID: 'organizations' }); assert.equal(providers.google.clientId, 'google-client.apps.example'); assert.equal(providers.microsoft.tenant, 'organizations'); assert.throws(() => loadOAuthProviderConfig({ EP_MAIL_MICROSOFT_OAUTH_CLIENT_ID: 'microsoft-client', EP_MAIL_MICROSOFT_OAUTH_TENANT_ID: 'https://attacker.example' }), /tenant ID is invalid/); }); test('authorizes Google and Microsoft with PKCE, state validation, and loopback redirects', async () => { const providers = loadOAuthProviderConfig({ EP_MAIL_GOOGLE_OAUTH_CLIENT_ID: 'google-client', EP_MAIL_GOOGLE_OAUTH_CLIENT_SECRET: 'google-secret', EP_MAIL_MICROSOFT_OAUTH_CLIENT_ID: 'microsoft-client', EP_MAIL_MICROSOFT_OAUTH_TENANT_ID: 'common' }); const authorizationUrls = []; const tokenRequests = []; const service = createOAuth2Service({ providers, async openExternal(value) { const authorization = new URL(value); authorizationUrls.push(authorization); const callback = new URL(authorization.searchParams.get('redirect_uri')); callback.searchParams.set('code', 'authorization-code'); callback.searchParams.set('state', authorization.searchParams.get('state')); const result = await fetch(callback); assert.equal(result.status, 200); }, async fetchImpl(url, options) { tokenRequests.push({ url, body: new URLSearchParams(options.body) }); return { ok: true, async json() { return { access_token: 'access-token', refresh_token: 'refresh-token', expires_in: 3600 }; } }; } }); for (const provider of ['google', 'microsoft']) { const tokens = await service.authorize(provider, 'user@example.test'); assert.deepEqual(Object.keys(tokens).sort(), [ 'oauthAccessToken', 'oauthAccessTokenExpiresAt', 'oauthRefreshToken' ]); assert.equal(tokens.oauthAccessToken, 'access-token'); assert.equal(tokens.oauthRefreshToken, 'refresh-token'); } assert.equal(authorizationUrls.length, 2); for (const authorization of authorizationUrls) { assert.match(authorization.searchParams.get('code_challenge'), /^[A-Za-z0-9_-]{43}$/); assert.equal(authorization.searchParams.get('code_challenge_method'), 'S256'); assert.equal(authorization.searchParams.get('login_hint'), 'user@example.test'); assert.ok(authorization.searchParams.get('state').length >= 40); assert.match(authorization.searchParams.get('redirect_uri'), /^http:\/\/(?:127\.0\.0\.1|localhost):\d+$/); } assert.equal(authorizationUrls[0].searchParams.get('scope'), 'https://mail.google.com/'); assert.match(authorizationUrls[1].searchParams.get('scope'), /IMAP\.AccessAsUser\.All/); assert.match(authorizationUrls[1].searchParams.get('scope'), /SMTP\.Send/); assert.equal(tokenRequests.length, 2); assert.equal(tokenRequests[0].body.get('code_verifier').length, 64); assert.equal(tokenRequests[0].body.get('client_secret'), 'google-secret'); assert.equal(tokenRequests[1].body.has('client_secret'), false); assert.equal(tokenRequests[0].body.get('code'), 'authorization-code'); }); test('refreshes OAuth tokens and preserves the current refresh token if omitted by provider', async () => { let posted; const service = createOAuth2Service({ providers: loadOAuthProviderConfig({ EP_MAIL_MICROSOFT_OAUTH_CLIENT_ID: 'microsoft-client' }), async openExternal() {}, async fetchImpl(url, options) { posted = { url, body: new URLSearchParams(options.body) }; return { ok: true, async json() { return { access_token: 'new-access-token', expires_in: 1800 }; } }; } }); const tokens = await service.refresh('microsoft', 'stored-refresh-token'); assert.equal(posted.body.get('grant_type'), 'refresh_token'); assert.equal(posted.body.get('refresh_token'), 'stored-refresh-token'); assert.equal(tokens.oauthAccessToken, 'new-access-token'); assert.equal(tokens.oauthRefreshToken, 'stored-refresh-token'); assert.ok(tokens.oauthAccessTokenExpiresAt > Date.now()); await assert.rejects(service.refresh('microsoft', ''), /Reconnect this Microsoft account/); }); test('rejects a loopback callback with an invalid OAuth state before exchanging its code', async () => { let tokenRequests = 0; const service = createOAuth2Service({ providers: loadOAuthProviderConfig({ EP_MAIL_GOOGLE_OAUTH_CLIENT_ID: 'google-client' }), async openExternal(value) { const authorization = new URL(value); const callback = new URL(authorization.searchParams.get('redirect_uri')); callback.searchParams.set('code', 'attacker-code'); callback.searchParams.set('state', 'wrong-state'); const response = await fetch(callback); assert.equal(response.status, 400); }, async fetchImpl() { tokenRequests += 1; throw new Error('must not exchange an untrusted code'); } }); await assert.rejects(service.authorize('google', 'user@gmail.com'), /state validation failed/); assert.equal(tokenRequests, 0); });