feat(admin): remove the SSO link of an account whose provider changed its subject

A sign-in with another subject of the same provider is refused, so an
account whose provider re-issued the subject (a migrated or rebuilt IdP, a
changed sub format) was locked out: editing the email, as the wiki advised,
never touched the stored link. The edit dialog now offers "Remove SSO link"
for other accounts while SSO is set up (DELETE /api/admin/users/:id/sso,
audit admin.user_sso_unlink). The next SSO sign-in links again under the
rules of a first one, so it still needs a verified, not self-set email.
The log line names the remedy, and the wiki tells the two refusals apart.

Fixes #3065
This commit is contained in:
Maurice
2026-11-12 15:39:21 +01:00
parent a3a4f7cf79
commit b53130ca0d
43 changed files with 415 additions and 9 deletions
@@ -823,6 +823,14 @@ export class UsersRepository extends TrekRepository<Users> {
await this.nativeUpdate({ id }, { oidc_sub: sub, oidc_issuer: issuer });
}
/**
* `UPDATE users SET oidc_sub = NULL, oidc_issuer = NULL WHERE id = ?`: the
* admin's "Remove SSO link" (#3065). No `updated_at`, like the link it undoes.
*/
async clearOidcIdentity(id: number): Promise<void> {
await this.nativeUpdate({ id }, { oidc_sub: null, oidc_issuer: null });
}
// ---------------------------------------------------------------------
// O8 — claims-based role change
// ---------------------------------------------------------------------
+16
View File
@@ -156,6 +156,22 @@ export class AdminController {
return { success: true };
}
@Delete('users/:id/sso')
async unlinkUserSso(@CurrentUser() user: User, @Param('id') id: string, @Req() req: Request) {
const result = await this.admin.unlinkUserSso(id, user.id);
if (result.unlinked) {
await this.audit.writeAudit({
userId: user.id,
action: 'admin.user_sso_unlink',
resource: String(id),
ip: getClientIp(req),
details: { targetUser: result.email },
});
logInfo(`Admin ${user.email} removed the SSO link of user ${result.email}`);
}
return { success: true, unlinked: result.unlinked };
}
// ── Stats / permissions / audit ──
@Get('stats')
stats() {
+29
View File
@@ -398,6 +398,35 @@ export class AdminService {
return { success: true, email: target.email };
}
/**
* Forget the SSO identity stored on another account (#3065).
*
* A sign-in that arrives with a different subject of the same provider is
* refused, because that is another person at the provider as far as TREK can
* tell. When the provider itself re-issued the subject (a migrated or rebuilt
* IdP, a changed `sub` format), the account owner is locked out and editing the
* email does not help. Clearing the link hands the decision to the admin: the
* next SSO sign-in links again under the same rules as a first one, so it still
* needs a verified email and an address the account did not set itself.
*
* Never for the caller themselves, for the same reason as resetUserMfa: a
* stolen admin session must not be able to loosen the login of the account it
* came from.
*/
async unlinkUserSso(id: string, actingUserId: number): Promise<{ email: string; unlinked: boolean }> {
const targetId = Number(id);
// SECURITY: plain JS comparison, kept here like resetUserMfa's (R4).
if (targetId === actingUserId) {
throw new DomainError(400, 'You cannot remove the SSO link of your own account');
}
const target = await this.users.findById(targetId);
if (!target || target.is_guest) throw new DomainError(404, 'User not found');
if (!target.oidc_sub && !target.oidc_issuer) return { email: target.email, unlinked: false };
await this.users.clearOidcIdentity(targetId);
return { email: target.email, unlinked: true };
}
// ── Stats ──────────────────────────────────────────────────────────────────
async getStats() {
+1 -1
View File
@@ -686,7 +686,7 @@ export class OidcService implements OnModuleDestroy {
// provider, never this account under a new name.
if (user.oidc_sub && user.oidc_issuer === config.issuer) {
logWarn(
`[OIDC] Refused to link a sign-in to user ${user.id} by email: it is linked to another subject of this provider.`,
`[OIDC] Refused to link a sign-in to user ${user.id} by email: it is linked to another subject of this provider. If the provider changed the subject of the same person, an admin can remove the SSO link of this user in the admin panel.`,
);
return { error: 'email_link_blocked' };
}