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
@@ -264,6 +264,7 @@ describe('GET /oauth/authorize — SDK authorizationHandler over trekOAuthProvid
// SDK-side exchange see it.
const code = await withRequestContext(orm, () =>
containerSideOauth.createAuthCode({
passwordVersion: 0,
clientId,
userId: user.id,
redirectUri: 'https://client.example.com/cb',
+15
View File
@@ -332,6 +332,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
// Create code for client1
const code = await createAuthCode({
passwordVersion: 0,
clientId: r1.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app1.example.com/cb',
@@ -360,6 +361,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -387,6 +389,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -414,6 +417,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const { challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -441,6 +445,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -500,6 +505,7 @@ describe('POST /oauth/token — refresh_token grant', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -586,6 +592,7 @@ describe('POST /oauth/revoke', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1059,6 +1066,7 @@ describe('Sessions — /api/oauth/sessions', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1107,6 +1115,7 @@ describe('Sessions — /api/oauth/sessions', () => {
if (!client) throw new Error('the OAuth client was not created');
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: client.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1201,6 +1210,7 @@ describe('H1 — PKCE format validation', () => {
const { challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1255,6 +1265,7 @@ describe('H5 — All invalid_grant cases return identical response body', () =>
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1276,6 +1287,7 @@ describe('H5 — All invalid_grant cases return identical response body', () =>
// Redirect URI mismatch (need fresh code since code is single-use)
const code2 = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1406,6 +1418,7 @@ describe('C3 — Refresh token replay detection', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1454,6 +1467,7 @@ describe('C3 — Refresh token replay detection', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1495,6 +1509,7 @@ describe('C3 — Refresh token replay detection', () => {
const { verifier, challenge } = makePkce();
const code = await createAuthCode({
passwordVersion: 0,
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',