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:
@@ -6,6 +6,7 @@ const { hasIpcSchema, validateIpcArguments } = require('../desktop/security/ipc-
|
||||
|
||||
test('validates supported IPC channels and their positional argument types', () => {
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:connect', [{ email: 'a@example.com' }]));
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:list-oauth-providers', []));
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:sync-folder', ['Inbox', 0, { hasAttachments: true }]));
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:disconnect', []));
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:send', [{ to: 'a@example.com', attachments: [] }]));
|
||||
@@ -50,6 +51,16 @@ test('validates channel-specific field shapes, limits, and enums', () => {
|
||||
imapHost: 'imap.example.test', smtpHost: 'smtp.example.test',
|
||||
rules: [{ senderContains: 'alerts', action: 'flag', value: '' }]
|
||||
}]));
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:connect', [{
|
||||
email: 'user@gmail.com', authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User'
|
||||
}]));
|
||||
assert.doesNotThrow(() => validateIpcArguments('mail:update-account', ['account-1', {
|
||||
authMethod: 'oauth2', oauthProvider: 'microsoft', reauthorizeOAuth: true
|
||||
}]));
|
||||
assert.throws(() => validateIpcArguments('mail:connect', [{ authMethod: 'oauth2', oauthProvider: 'other' }]), /oauthProvider/);
|
||||
assert.throws(() => validateIpcArguments('mail:connect', [{
|
||||
email: 'user@gmail.com', authMethod: 'oauth2', oauthProvider: 'unknown'
|
||||
}]), /oauthProvider/);
|
||||
assert.throws(() => validateIpcArguments('mail:connect', [{ rules: Array(51).fill({ action: 'flag' }) }]), /Invalid argument 1/);
|
||||
assert.throws(() => validateIpcArguments('mail:update-message', [{ uid: '4', action: 'star' }]), /uid/);
|
||||
assert.throws(() => validateIpcArguments('mail:update-message', [{ uid: 4, action: 'delete-anything' }]), /action/);
|
||||
|
||||
@@ -45,7 +45,7 @@ test('registers mail/account IPC handlers through the shared boundary', () => {
|
||||
|
||||
assert.deepEqual(registered.map(item => item.channel).sort(), [
|
||||
'mail:connect', 'mail:create-folder', 'mail:delete-draft', 'mail:disconnect',
|
||||
'mail:get-account', 'mail:list-accounts', 'mail:list-drafts', 'mail:list-folders',
|
||||
'mail:get-account', 'mail:list-accounts', 'mail:list-drafts', 'mail:list-folders', 'mail:list-oauth-providers',
|
||||
'mail:open-external', 'mail:preview-attachment', 'mail:remove-account', 'mail:save-attachment',
|
||||
'mail:save-draft', 'mail:select-account', 'mail:send', 'mail:set-snooze',
|
||||
'mail:sync-folder', 'mail:sync-inbox', 'mail:update-account', 'mail:update-message'
|
||||
|
||||
@@ -43,6 +43,34 @@ test('rejects invalid account fields and malformed mail rules', () => {
|
||||
}), /must move messages/);
|
||||
});
|
||||
|
||||
test('normalizes OAuth2 accounts to provider servers without requiring a password', () => {
|
||||
const google = normalizeAccount({
|
||||
email: 'user@gmail.com', authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User'
|
||||
});
|
||||
assert.equal(google.password, '');
|
||||
assert.equal(google.authMethod, 'oauth2');
|
||||
assert.equal(google.oauthProvider, 'google');
|
||||
assert.equal(google.imapHost, 'imap.gmail.com');
|
||||
assert.equal(google.imapPort, 993);
|
||||
assert.equal(google.imapSecure, true);
|
||||
assert.equal(google.smtpHost, 'smtp.gmail.com');
|
||||
const untrustedEndpoints = normalizeAccount({
|
||||
email: 'user@gmail.com', authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User',
|
||||
imapHost: 'attacker.example.test', smtpHost: 'attacker.example.test', imapPort: 143
|
||||
});
|
||||
assert.equal(untrustedEndpoints.imapHost, 'imap.gmail.com');
|
||||
assert.equal(untrustedEndpoints.smtpHost, 'smtp.gmail.com');
|
||||
assert.equal(untrustedEndpoints.imapPort, 993);
|
||||
|
||||
const microsoft = normalizeAccount({
|
||||
email: 'user@outlook.com', authMethod: 'oauth2', oauthProvider: 'microsoft', displayName: 'User'
|
||||
});
|
||||
assert.equal(microsoft.imapHost, 'outlook.office365.com');
|
||||
assert.equal(microsoft.smtpHost, 'smtp.office365.com');
|
||||
assert.throws(() => normalizeAccount({ email: 'user@example.test', authMethod: 'oauth2', displayName: 'User' }), /Choose Google or Microsoft/);
|
||||
assert.throws(() => normalizeAccount({ email: 'user@example.test', authMethod: 'oauth', password: 'secret', displayName: 'User' }), /supported sign-in method/);
|
||||
});
|
||||
|
||||
test('public account projection does not expose the password or private mailbox state', () => {
|
||||
const account = normalizeAccount(accountInput);
|
||||
const result = publicAccount({
|
||||
@@ -54,6 +82,8 @@ test('public account projection does not expose the password or private mailbox
|
||||
assert.equal(result.id, 'account-1');
|
||||
assert.equal(result.email, 'user@example.com');
|
||||
assert.equal('password' in result, false);
|
||||
assert.equal('oauthRefreshToken' in result, false);
|
||||
assert.equal('oauthAccessToken' in result, false);
|
||||
assert.equal('drafts' in result, false);
|
||||
assert.equal('mailOutbox' in result, false);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { normalizeAccount, publicAccount } = require('../desktop/mail-model.cjs');
|
||||
const { registerMailIpcHandlers } = require('../desktop/ipc/mail.cjs');
|
||||
|
||||
function registerOAuthMailHandlers(overrides = {}) {
|
||||
const handlers = new Map();
|
||||
registerMailIpcHandlers({
|
||||
registerIpcHandler(channel, handler) { handlers.set(channel, handler); },
|
||||
normalizeAccount,
|
||||
publicAccount,
|
||||
async connectMail() { return { mailbox: 'INBOX', messages: 0 }; },
|
||||
async authorizeOAuthAccount(account) {
|
||||
return {
|
||||
...account,
|
||||
oauthAccessToken: 'private-access-token',
|
||||
oauthRefreshToken: 'private-refresh-token',
|
||||
oauthAccessTokenExpiresAt: Date.now() + 3600_000
|
||||
};
|
||||
},
|
||||
listOAuthProviders() { return [{ id: 'google', label: 'Google', configured: true }, { id: 'microsoft', label: 'Microsoft', configured: false }]; },
|
||||
async saveAccount(account) { return { ...account, id: 'oauth-account' }; },
|
||||
requestBackgroundSync() {},
|
||||
randomUUID: () => 'generated-account-id',
|
||||
...overrides
|
||||
});
|
||||
return handlers;
|
||||
}
|
||||
|
||||
test('OAuth account setup authorizes in the main process and never returns tokens to the renderer', async () => {
|
||||
const calls = [];
|
||||
const handlers = registerOAuthMailHandlers({
|
||||
async authorizeOAuthAccount(account) {
|
||||
calls.push(['authorize', account.oauthProvider]);
|
||||
return { ...account, oauthAccessToken: 'private-access-token', oauthRefreshToken: 'private-refresh-token', oauthAccessTokenExpiresAt: Date.now() + 3600_000 };
|
||||
},
|
||||
async connectMail(account) {
|
||||
calls.push(['connect', account.authMethod, account.oauthRefreshToken]);
|
||||
return { mailbox: 'INBOX', messages: 0 };
|
||||
},
|
||||
async saveAccount(account) {
|
||||
calls.push(['save', account.oauthRefreshToken]);
|
||||
return { ...account, id: 'oauth-account' };
|
||||
}
|
||||
});
|
||||
|
||||
const result = await handlers.get('mail:connect')({}, {
|
||||
email: 'user@gmail.com', authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User'
|
||||
});
|
||||
|
||||
assert.deepEqual(calls.map(call => call[0]), ['authorize', 'connect', 'save']);
|
||||
assert.deepEqual(calls[1], ['connect', 'oauth2', 'private-refresh-token']);
|
||||
assert.equal(result.authMethod, 'oauth2');
|
||||
assert.equal(result.oauthProvider, 'google');
|
||||
assert.equal('oauthAccessToken' in result, false);
|
||||
assert.equal('oauthRefreshToken' in result, false);
|
||||
assert.equal('password' in result, false);
|
||||
});
|
||||
|
||||
test('OAuth provider discovery reports configured status without exposing client credentials', async () => {
|
||||
const handlers = registerOAuthMailHandlers();
|
||||
assert.deepEqual(await handlers.get('mail:list-oauth-providers')(), [
|
||||
{ id: 'google', label: 'Google', configured: true },
|
||||
{ id: 'microsoft', label: 'Microsoft', configured: false }
|
||||
]);
|
||||
});
|
||||
|
||||
test('editing an OAuth account preserves its token unless the user requests reauthorization', async () => {
|
||||
const existing = {
|
||||
...normalizeAccount({ email: 'user@gmail.com', displayName: 'User', authMethod: 'oauth2', oauthProvider: 'google' }),
|
||||
id: 'oauth-account', oauthAccessToken: 'old-access', oauthRefreshToken: 'old-refresh', oauthAccessTokenExpiresAt: 10
|
||||
};
|
||||
const store = { accounts: [existing] };
|
||||
let authorizations = 0;
|
||||
const handlers = new Map();
|
||||
registerMailIpcHandlers({
|
||||
registerIpcHandler(channel, handler) { handlers.set(channel, handler); },
|
||||
normalizeAccount,
|
||||
publicAccount,
|
||||
async readAccount(id) { return store.accounts.find(account => account.id === id); },
|
||||
async updateAccountStore(update) { return update(store); },
|
||||
async connectMail() { return { mailbox: 'INBOX', messages: 0 }; },
|
||||
async authorizeOAuthAccount(account) {
|
||||
authorizations += 1;
|
||||
return { ...account, oauthAccessToken: 'new-access', oauthRefreshToken: 'new-refresh', oauthAccessTokenExpiresAt: Date.now() + 3600_000 };
|
||||
},
|
||||
requestBackgroundSync() {}
|
||||
});
|
||||
const event = {};
|
||||
|
||||
await handlers.get('mail:update-account')(event, 'oauth-account', {
|
||||
authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User'
|
||||
});
|
||||
assert.equal(authorizations, 0);
|
||||
assert.equal(store.accounts[0].oauthRefreshToken, 'old-refresh');
|
||||
|
||||
const result = await handlers.get('mail:update-account')(event, 'oauth-account', {
|
||||
authMethod: 'oauth2', oauthProvider: 'google', reauthorizeOAuth: true, displayName: 'User'
|
||||
});
|
||||
assert.equal(authorizations, 1);
|
||||
assert.equal(store.accounts[0].oauthRefreshToken, 'new-refresh');
|
||||
assert.equal('oauthRefreshToken' in result, false);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
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);
|
||||
});
|
||||
Reference in New Issue
Block a user