fix(oidc): verify mailbox identity before linking accounts

Preserve the provider mailbox on new accounts. For existing accounts, use the stored issuer and subject or check mailbox identity before linking. Keep the candidate lookup and identity update inside the same transaction.
This commit is contained in:
Maurice
2026-12-01 11:20:09 +01:00
parent 0d521f0930
commit 1ff8a814ae
8 changed files with 60 additions and 78 deletions
+2 -20
View File
@@ -478,9 +478,8 @@ export class UsersRepository extends TrekRepository<Users> {
* in SQL, so a JS-lowered bind here would disagree with the column on
* every non-ASCII cased character (program rule 18; Plan 3b Task 7
* review, H1: a `JOSÉ@x.com` account could register but never log back
* in). Distinct from `findByEmailLoweredBind` below, which mirrors O4's
* different legacy statement — do not collapse the two back into one
* method (D4).
* in). OIDC also uses this to find a candidate before checking the mailbox
* local part for an exact match.
*/
async findByEmailCI(email: string): Promise<UserRow | null> {
const platform = this.getEntityManager().getPlatform();
@@ -488,23 +487,6 @@ export class UsersRepository extends TrekRepository<Users> {
return user ? (toRow(user) as UserRow) : null;
}
/**
* `SELECT * FROM users WHERE LOWER(email) = ? AND COALESCE(is_guest, 0) = 0`
* — O4 (`OidcService.findOrCreateUser`, `oidc.service.ts:655`), whose only
* caller JS-lowers the email itself (`oidc.service.ts:620`,
* `userInfo.email.trim().toLowerCase()`) before calling this method,
* exactly mirroring the legacy statement's `LOWER(email) = ?` (SQL
* `LOWER()` on the column, a bind the CALLER already lowered — not a
* second SQL `LOWER()` on the value side). Do not point a caller that has
* NOT already lowered its value at this method — use `findByEmailCI`
* above instead.
*/
async findByEmailLoweredBind(alreadyLoweredEmail: string): Promise<UserRow | null> {
const platform = this.getEntityManager().getPlatform();
const user = await this.findOne({ [lower(platform, 'email')]: alreadyLoweredEmail, is_guest: 0 });
return user ? (toRow(user) as UserRow) : null;
}
// ---------------------------------------------------------------------
// AuthService.demoLogin (auth.service.ts:~376) — inventory-correction
// addition, Plan 3b Task 1 fix round item 7 (pre-authorised for Task 5).