mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
fix(auth): bind a password reset link to the address it was mailed to
The reset request reads the account's address again inside the transaction that issues the link, and the link stores that address, so a link raced past an email change no longer resets the moved account. Links issued before this keep working until they expire.
This commit is contained in:
@@ -13,6 +13,7 @@ export class PasswordResetTokens {
|
||||
consumed_at?: string | null;
|
||||
created_at?: string | null;
|
||||
created_ip?: string | null;
|
||||
email?: string | null;
|
||||
}
|
||||
|
||||
export const PasswordResetTokensSchema = defineEntity({
|
||||
@@ -28,5 +29,6 @@ export const PasswordResetTokensSchema = defineEntity({
|
||||
consumed_at: p.type(DbTimestampType).nullable(),
|
||||
created_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`),
|
||||
created_ip: p.text().nullable(),
|
||||
email: p.text().nullable(),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -10,4 +10,5 @@ export interface PasswordResetTokensTable {
|
||||
consumed_at: InsertOptional<string | null>;
|
||||
created_at: InsertOptional<string | null>;
|
||||
created_ip: InsertOptional<string | null>;
|
||||
email: InsertOptional<string | null>;
|
||||
}
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { engineDdl } from '../migration-utils';
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* A password reset link remembers the address it was mailed to, so it stops
|
||||
* working once the account has moved to another address.
|
||||
*
|
||||
* `password_reset_tokens.email` holds the account's address at the moment the
|
||||
* link was issued. Every link issued before this step starts empty and keeps
|
||||
* working until it expires or is used, as it did before.
|
||||
*/
|
||||
export class Migration20200101046520_a_reset_link_belongs_to_the_address_it_was_sent_to extends Migration {
|
||||
override name = 'Migration20200101046520_a_reset_link_belongs_to_the_address_it_was_sent_to';
|
||||
|
||||
override up(): void {
|
||||
engineDdl(this, {
|
||||
sqlite: ['ALTER TABLE password_reset_tokens ADD COLUMN email TEXT'],
|
||||
postgres: ['alter table "password_reset_tokens" add column "email" text null'],
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ export interface PasswordResetTokenRow {
|
||||
consumed_at: string | null;
|
||||
created_at: string | null;
|
||||
created_ip: string | null;
|
||||
email: string | null;
|
||||
}
|
||||
|
||||
const _passwordResetTokenRowKeys: AssertRowKeys<PasswordResetTokenRow, PasswordResetTokens> = true;
|
||||
@@ -23,6 +24,7 @@ export interface NewPasswordResetTokenRow {
|
||||
token_hash: string;
|
||||
expires_at: DbTimestamp;
|
||||
created_ip: string | null;
|
||||
email: string;
|
||||
}
|
||||
|
||||
/** `AuthService.resetPassword`'s lookup (AU39). */
|
||||
@@ -31,6 +33,7 @@ export interface PasswordResetTokenLookup {
|
||||
user_id: number;
|
||||
expires_at: string;
|
||||
consumed_at: string | null;
|
||||
email: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +81,7 @@ export class PasswordResetTokensRepository extends TrekRepository<PasswordResetT
|
||||
token_hash: row.token_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_ip: row.created_ip,
|
||||
email: row.email,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -101,9 +105,18 @@ export class PasswordResetTokensRepository extends TrekRepository<PasswordResetT
|
||||
* query.
|
||||
*/
|
||||
async findByTokenHash(hash: string): Promise<PasswordResetTokenLookup | null> {
|
||||
const row = await this.findOne({ token_hash: hash }, { fields: ['id', 'user', 'expires_at', 'consumed_at'] });
|
||||
const row = await this.findOne(
|
||||
{ token_hash: hash },
|
||||
{ fields: ['id', 'user', 'expires_at', 'consumed_at', 'email'] },
|
||||
);
|
||||
return row
|
||||
? { id: row.id, user_id: row.user.id, expires_at: row.expires_at, consumed_at: row.consumed_at ?? null }
|
||||
? {
|
||||
id: row.id,
|
||||
user_id: row.user.id,
|
||||
expires_at: row.expires_at,
|
||||
consumed_at: row.consumed_at ?? null,
|
||||
email: row.email ?? null,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user