Skip to content

Commit 7123f12

Browse files
committed
fix(auth): credit the impersonating administrator on the own withdraw
An own withdraw made from an impersonation session now hands runAttributedToUser { id, impersonatedBy } read off the session, as gateAdmin and the auth before-hook do, so the revoke's writes carry the attribution-only impersonated_by. Two pins read the credited actor from inside the revoke. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxU7nr4BXM7e8qMfwq
1 parent 1c32be0 commit 7123f12

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎packages/plugins/plugin-auth/src/authorized-clients-read.real-plugin.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ import {
4343
ADMIN_AUTHORIZED_CLIENT_WITHDRAW_PATH,
4444
AUTHORIZED_CLIENT_WITHDRAW_NOT_FOUND_MESSAGE,
4545
AUTHORIZED_CLIENT_WITHDRAW_PATH,
46+
type AuthorizedClientWithdrawDeps,
47+
runOwnAuthorizedClientWithdraw,
4648
} from './authorized-clients-withdraw';
49+
import { resolveAttributedActor } from './auth-actor-attribution';
4750

4851
const SECRET = 'test-secret-at-least-32-chars-long-30918';
4952
const PASSWORD = 'S3cure!Passw0rd-authorized-clients';
@@ -791,3 +794,41 @@ describe('#31048 POST /admin/authorized-clients/withdraw — another person\'s g
791794
expect(await rig.manager.verifyMcpAccessToken(deskToken), 'Bob\'s assistant still connects').not.toBeNull();
792795
});
793796
});
797+
798+
describe('#31048 the own withdraw credits the administrator at the keyboard of an impersonation session', () => {
799+
/** The actor the revoke's writes are credited to, read inside the revoke as the adapter's writes read it. */
800+
const creditOf = async (session: unknown) => {
801+
let credited: unknown = 'revoke never ran';
802+
const deps: AuthorizedClientWithdrawDeps = {
803+
readCallerSession: async () => session as any,
804+
findConsentOf: async (userId, id) => (id === 'consent_1' ? { clientId: 'client_1', userId } : null),
805+
revokeGrant: async () => {
806+
credited = await resolveAttributedActor();
807+
},
808+
userExists: async () => true,
809+
};
810+
const answer = await runOwnAuthorizedClientWithdraw(deps, {
811+
raw: new Request(`${ORIGIN}${BASE}${AUTHORIZED_CLIENT_WITHDRAW_PATH}`, {
812+
method: 'POST',
813+
headers: { 'content-type': 'application/json' },
814+
body: JSON.stringify({ id: 'consent_1' }),
815+
}),
816+
queries: {},
817+
});
818+
expect(answer.status, JSON.stringify(answer.body)).toBe(200);
819+
return credited;
820+
};
821+
822+
it('an impersonation session: the person, with the impersonating administrator beside them', async () => {
823+
expect(
824+
await creditOf({ user: { id: 'usr_person_31048' }, session: { impersonatedBy: 'usr_admin_31048' } }),
825+
).toEqual({ userId: 'usr_person_31048', impersonatedBy: 'usr_admin_31048' });
826+
});
827+
828+
it('an ordinary session: the person alone, no impersonatedBy invented', async () => {
829+
expect(await creditOf({ user: { id: 'usr_person_31048' }, session: {} })).toEqual({ userId: 'usr_person_31048' });
830+
expect(
831+
await creditOf({ user: { id: 'usr_person_31048' }, session: { impersonatedBy: ' ' } }),
832+
).toEqual({ userId: 'usr_person_31048' });
833+
});
834+
});

‎packages/plugins/plugin-auth/src/authorized-clients-withdraw.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
* client still holds from coming back if the person later authorizes the same
2525
* assistant again. Every write goes through better-auth's own storage adapter,
2626
* so the model names and field mapping are the provider's, and is attributed
27-
* to the person who withdrew it (`runAttributedToUser`; attribution only).
27+
* to the person who withdrew it, with the impersonating administrator beside
28+
* them when the session is an impersonation (`runAttributedToUser`;
29+
* attribution only).
2830
*
2931
* ## Why not better-auth's `POST /oauth2/delete-consent`
3032
*
@@ -233,7 +235,7 @@ async function readWithdrawRequest(
233235
/** Withdraw `userId`'s consent `id`, credited to `actor`. */
234236
async function withdrawConsentOf(
235237
deps: AuthorizedClientWithdrawDeps,
236-
actor: string | PlatformAdminActor,
238+
actor: { id: string; impersonatedBy?: string },
237239
userId: string,
238240
id: string,
239241
): Promise<AuthorizedClientWithdrawResult> {
@@ -258,7 +260,13 @@ export async function runOwnAuthorizedClientWithdraw(
258260

259261
const read = await readWithdrawRequest(request, AUTHORIZED_CLIENT_WITHDRAW_BODY_KEYS);
260262
if (!('values' in read)) return read;
261-
return withdrawConsentOf(deps, subject, subject, read.values.id);
263+
// An impersonation session names the administrator at the keyboard: it is
264+
// credited beside the person as the attribution-only `impersonatedBy`, read
265+
// off this session and never off the body, as `gateAdmin` and the auth
266+
// before-hook carry it.
267+
const impersonatedBy = nonEmpty((session?.session as { impersonatedBy?: unknown } | undefined)?.impersonatedBy);
268+
const actor = impersonatedBy ? { id: subject, impersonatedBy } : { id: subject };
269+
return withdrawConsentOf(deps, actor, subject, read.values.id);
262270
}
263271

264272
/**

0 commit comments

Comments
 (0)