mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
fix(auth): retire password reset links when the account's email or password changes
A reset link is a credential sent to the address the account had when it was asked for. It stayed valid for its full hour after the account moved to another address or got a new password, so a link sent to the old mailbox could still set a password on the account. Changing the email in the user's own settings, changing the password, and an admin changing a user's email or setting a password now consume every live reset link of that user, in the same transaction as the change. Saving the same address, or it in another case, keeps the link, and a link asked for afterwards works as before.
This commit is contained in:
@@ -275,7 +275,8 @@ export class AdminService {
|
||||
// An address the admin enters is one the admin vouches for, so an SSO sign-in
|
||||
// may link to it again. The edit dialog sends the email on every save, so only
|
||||
// a new address counts, not the unchanged one or the same one in another case.
|
||||
if (email && email.toLowerCase() !== user.email.toLowerCase()) patch.email_self_asserted = 0;
|
||||
const emailChanged = !!email && email.toLowerCase() !== user.email.toLowerCase();
|
||||
if (emailChanged) patch.email_self_asserted = 0;
|
||||
if (role) patch.role = role;
|
||||
if (password) {
|
||||
patch.password_hash = passwordHash!;
|
||||
@@ -302,6 +303,9 @@ export class AdminService {
|
||||
}
|
||||
}
|
||||
await this.users.applyAdminEdit(userId, patch);
|
||||
// A reset link sent to the old address, or asked for before the admin set
|
||||
// a password, must not reopen the account afterwards.
|
||||
if (emailChanged || password) await this.auth.retirePasswordResetLinks(userId);
|
||||
|
||||
if (password) {
|
||||
// The version bump only invalidates JWT cookies. These two stores carry
|
||||
|
||||
@@ -209,6 +209,7 @@ export class AuthService {
|
||||
// Reset-email delivery (canonical app URL, never request headers)
|
||||
getAppUrl() { return getAppUrl(); }
|
||||
sendPasswordResetEmail(email: string, url: string, userId: number | null) { return this.mailer.sendPasswordResetEmail(email, url, userId); }
|
||||
retirePasswordResetLinks(userId: number) { return this.passwordResetTokens.consumeAllLiveForUser(userId); }
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Toggles + tokens
|
||||
@@ -651,9 +652,8 @@ export class AuthService {
|
||||
|
||||
await this.uow.transactional(async () => {
|
||||
await this.usersRepo.setPassword(userId, hash, newPv);
|
||||
// A password change ends every session (the pv bump covers the tokens from
|
||||
// before sessions were tracked) and prunes the MCP static token and OAuth
|
||||
// bearer-token stores to match, the same set the password reset revokes.
|
||||
// Ends every session, reset link, MCP and OAuth token, the same set the password reset revokes.
|
||||
await this.retirePasswordResetLinks(userId);
|
||||
await this.mcpTokens.deleteAllForUser(userId);
|
||||
try { await this.oauthTokens.revokeAllForUser(userId); } catch { /* no oauth_tokens in very old installs */ }
|
||||
// Push devices keep receiving notifications without any session, so they
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { readEnv, getAppUrl } from '../../app-config';
|
||||
import { AppSettings } from '../../db/entities/AppSettings.entity';
|
||||
import { PasswordResetTokens } from '../../db/entities/PasswordResetTokens.entity';
|
||||
import { Users } from '../../db/entities/Users.entity';
|
||||
import type { AppSettingsRepository } from '../../db/repositories/AppSettings.repository';
|
||||
import type { PasswordResetTokensRepository } from '../../db/repositories/PasswordResetTokens.repository';
|
||||
import {
|
||||
UserIdentityTakenError,
|
||||
type UsersRepository,
|
||||
@@ -58,6 +60,7 @@ export class UserProfileService {
|
||||
private readonly uow: UnitOfWork,
|
||||
@InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository,
|
||||
@InjectRepository(Users) private readonly usersRepo: UsersRepository,
|
||||
@InjectRepository(PasswordResetTokens) private readonly passwordResetTokens: PasswordResetTokensRepository,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -282,6 +285,9 @@ export class UserProfileService {
|
||||
try {
|
||||
await this.uow.transactional(async () => {
|
||||
await this.usersRepo.patchProfile(userId, changes);
|
||||
// A reset link is a credential sent to the address the account had,
|
||||
// so it must not outlive the move to another one.
|
||||
if (emailChanged) await this.passwordResetTokens.consumeAllLiveForUser(userId);
|
||||
if (!keyLocked) await this.mirrorInstanceKeys(body, isAdmin);
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user