fix(oauth): an authorization code mints tokens only while the password it was granted under holds

The consent page mints a two-minute authorization code from the cookie
session. A password reset or change revokes the user's OAuth tokens but
left such a pending code alone, so a code granted before the reset still
redeemed afterwards into a fresh token pair at either token endpoint.

The code now carries the password version of the session that consented,
and both token endpoints issue tokens for it only while that version is
still the account's, checked in the transaction that writes the tokens.
A refused code answers invalid_grant as any other and is audited as
password_changed.
This commit is contained in:
Maurice
2026-11-26 22:21:46 +01:00
parent c88d3abc5e
commit f9ab280b7e
13 changed files with 161 additions and 24 deletions
@@ -1,3 +1,4 @@
import { JWT_SECRET } from '../../../src/config';
import type { AuditService } from '../../../src/nest/audit/audit.service';
import { getClientIp } from '../../../src/nest/audit/client-ip';
import { RateLimitService } from '../../../src/nest/common/rate-limit.service';
@@ -8,6 +9,7 @@ import type { User } from '../../../src/types';
import { HttpException } from '@nestjs/common';
import type { Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../../../src/nest/audit/client-ip', () => ({ getClientIp: vi.fn(() => '1.2.3.4') }));
@@ -122,13 +124,44 @@ describe('OauthPublicController /token', () => {
}),
authenticateClient: vi.fn().mockReturnValue({ id: 'c' }),
verifyPKCE: vi.fn().mockReturnValue(true),
issueTokens: vi.fn().mockReturnValue({ access_token: 'at', token_type: 'Bearer' }),
issueTokensForCode: vi.fn().mockReturnValue({ access_token: 'at', token_type: 'Bearer' }),
});
await opc(svc, rl()).token(
reqWith({ grant_type: 'authorization_code', client_id: 'c', code: 'x', redirect_uri: 'u', code_verifier: 'v' }),
ok,
);
expect(ok.body).toEqual({ access_token: 'at', token_type: 'Bearer' });
// The password changed since the consent: the code grants nothing, and the refusal is audited.
const stale = makeRes();
const staleSvc = osvc({
consumeAuthCode: vi
.fn()
.mockReturnValue({
clientId: 'c',
redirectUri: 'u',
userId: 1,
scopes: ['s'],
codeChallenge: 'cc',
resource: null,
}),
authenticateClient: vi.fn().mockReturnValue({ id: 'c' }),
verifyPKCE: vi.fn().mockReturnValue(true),
issueTokensForCode: vi.fn().mockReturnValue(null),
});
const staleAudit = { writeAudit: vi.fn() };
await new OauthPublicController(staleSvc, rl(), staleAudit as unknown as AuditService).token(
reqWith({ grant_type: 'authorization_code', client_id: 'c', code: 'x', redirect_uri: 'u', code_verifier: 'v' }),
stale,
);
expect(stale.statusCode).toBe(400);
expect(stale.body).toEqual({ error: 'invalid_grant', error_description: 'Authorization grant is invalid.' });
expect(staleAudit.writeAudit).toHaveBeenCalledWith(
expect.objectContaining({
action: 'oauth.token.grant_failed',
details: { client_id: 'c', reason: 'password_changed' },
}),
);
});
it('authorization_code: maps client_id / redirect_uri / resource mismatches + pkce + client auth', async () => {
@@ -596,6 +629,29 @@ describe('OauthApiController', () => {
expect((ok as { redirect: string }).redirect).toContain('code=the_code');
});
it('authorize: the code carries the password version of the cookie session that consented', async () => {
const createAuthCode = vi.fn().mockReturnValue('the_code');
const svc = osvc({
validateAuthorizeRequest: vi.fn().mockReturnValue({ valid: true, scopes: ['s'], resource: null }),
saveConsent: vi.fn(),
createAuthCode,
});
const cookie = jwt.sign({ id: user.id, pv: 4 }, JWT_SECRET, { algorithm: 'HS256' });
await new OauthApiController(svc, rl()).authorize(
user,
{
client_id: 'c',
redirect_uri: 'https://cb',
scope: 's',
code_challenge: 'cc',
code_challenge_method: 'S256',
approved: true,
},
{ ...req, cookies: { trek_session: cookie } } as unknown as Request,
);
expect(createAuthCode).toHaveBeenCalledWith(expect.objectContaining({ passwordVersion: 4 }));
});
it('clients/sessions: 403 when MCP off, else CRUD', async () => {
expect(
await thrownAsync(() =>