Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/changepassword/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ChangePasswordController extends Controller {
return;
}

await UserService.updatePassword(user, userNewPassword);
await UserService.updatePassword(user, userNewPassword, false);

ctx.session = {
user: user,
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/20220804145233_add_reset_flag_to_users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {

await knex.schema.alterTable('user_passwords', table => {
table
.string('force_reset')
.nullable();
});

await knex.raw('UPDATE user_passwords SET force_reset = false WHERE force_reset IS NULL');

await knex.schema.alterTable('user_passwords', table => {
table
.string('force_reset')
.notNullable()
.alter();
});
}


export async function down(knex: Knex): Promise<void> {

await knex.schema.createTable('user_passwords', table => {
table
.dropColumn('force_reset');
});

}

2 changes: 1 addition & 1 deletion src/register/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class UserRegistrationController extends Controller {
);
}

await userService.createPassword(user, userPassword);
await userService.createPassword(user, userPassword, false);

if (addMfa && getSetting('registration.mfa.enabled')) {
ctx.session = {
Expand Down
2 changes: 1 addition & 1 deletion src/reset-password/controller/reset-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ResetPasswordController extends Controller {
return;
}

await UserService.updatePassword(user, resetNewPassword);
await UserService.updatePassword(user, resetNewPassword, false);

delete ctx.session.resetPasswordUser;
log(EventType.resetPasswordSuccess, ctx.ip()!, user.id);
Expand Down
2 changes: 1 addition & 1 deletion src/user/controller/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserPasswordController extends Controller {

const password = userBody.newPassword;

await userService.updatePassword(user, password);
await userService.updatePassword(user, password, false);

ctx.response.status = 204;

Expand Down
11 changes: 6 additions & 5 deletions src/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ type PasswordRow = {
password: Buffer;
};

export async function createPassword(user: User, password: string): Promise<void> {
export async function createPassword(user: User, password: string, forceReset: boolean): Promise<void> {

await db('user_passwords').insert({
user_id: user.id,
password: await bcrypt.hash(password, 12)
password: await bcrypt.hash(password, 12),
force_reset: forceReset
});

}

export async function updatePassword(user: User, password: string): Promise<void> {
export async function updatePassword(user: User, password: string, force_reset: boolean): Promise<void> {

const query = 'INSERT INTO user_passwords (password, user_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE password = ?';
const query = 'INSERT INTO user_passwords (password, user_id, force_reset) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET password = ?';
const hashedPw = await bcrypt.hash(password, 12);

await db.raw(query, [hashedPw, user.id, hashedPw]);
await db.raw(query, [hashedPw, user.id, force_reset, hashedPw]);

}

Expand Down