feat: add password reset from server console

This commit is contained in:
KernelDeimos 2024-07-23 20:15:34 -04:00 committed by Eric Dubé
parent 580fbdb113
commit 984ae9e6a2

View File

@ -71,6 +71,7 @@ class DefaultUserService extends BaseService {
uuidv4: require('uuid').v4, uuidv4: require('uuid').v4,
} }
async _init () { async _init () {
this._register_commands(this.services.get('commands'));
} }
async ['__on_ready.webserver'] () { async ['__on_ready.webserver'] () {
// check if a user named `admin` exists // check if a user named `admin` exists
@ -213,6 +214,41 @@ class DefaultUserService extends BaseService {
return tmp_password; return tmp_password;
}); });
} }
async force_tmp_password_ (user) {
const db = this.services.get('database')
.get(DB_WRITE, 'terminal-password-reset');
const actor = await Actor.create(UserActorType, { user });
return await Context.get().sub({ actor }).arun(async () => {
const svc_driver = this.services.get('driver');
const tmp_password = require('crypto').randomBytes(4).toString('hex');
const bcrypt = require('bcrypt');
const password_hashed = await bcrypt.hash(tmp_password, 8);
await svc_driver.call(
'puter-kvstore', 'set', {
key: 'tmp_password',
value: tmp_password });
await db.write(
`UPDATE user SET password = ? WHERE id = ?`,
[
password_hashed,
user.id,
],
);
return tmp_password;
});
}
_register_commands (commands) {
commands.registerCommands('default-user', [
{
id: 'reset-password',
handler: async (args, ctx) => {
const [ username ] = args;
const user = await get_user({ username });
await this.force_tmp_password_(user);
}
}
]);
}
} }
module.exports = DefaultUserService; module.exports = DefaultUserService;