Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force change password (WIP) #461

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
@@ -1,4 +1,4 @@
import * as bcrypt from 'bcrypt';

Check failure on line 1 in src/user/service.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument of type '{ user_id: number; password: string; force_reset: boolean; }' is not assignable to parameter of type 'DbRecordArr<UserPasswordsRecord> | readonly DbRecordArr<UserPasswordsRecord>[]'.

Check failure on line 1 in src/user/service.ts

View workflow job for this annotation

GitHub Actions / Test Postgres migrations

Argument of type '{ user_id: number; password: string; force_reset: boolean; }' is not assignable to parameter of type 'DbRecordArr<UserPasswordsRecord> | readonly DbRecordArr<UserPasswordsRecord>[]'.

Check failure on line 1 in src/user/service.ts

View workflow job for this annotation

GitHub Actions / Test MySQL migrations

Argument of type '{ user_id: number; password: string; force_reset: boolean; }' is not assignable to parameter of type 'DbRecordArr<UserPasswordsRecord> | readonly DbRecordArr<UserPasswordsRecord>[]'.

Check failure on line 1 in src/user/service.ts

View workflow job for this annotation

GitHub Actions / Test (16.x)

Argument of type '{ user_id: number; password: string; force_reset: boolean; }' is not assignable to parameter of type 'DbRecordArr<UserPasswordsRecord> | readonly DbRecordArr<UserPasswordsRecord>[]'.

Check failure on line 1 in src/user/service.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

Argument of type '{ user_id: number; password: string; force_reset: boolean; }' is not assignable to parameter of type 'DbRecordArr<UserPasswordsRecord> | readonly DbRecordArr<UserPasswordsRecord>[]'.

Check failure on line 1 in src/user/service.ts

View workflow job for this annotation

GitHub Actions / Test Sqlite migrations

Argument of type '{ user_id: number; password: string; force_reset: boolean; }' is not assignable to parameter of type 'DbRecordArr<UserPasswordsRecord> | readonly DbRecordArr<UserPasswordsRecord>[]'.
import * as otplib from 'otplib';
import db from '../database';
import { User } from '../principal/types';
Expand All @@ -7,21 +7,22 @@
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
Loading