Skip to content

Commit

Permalink
feat: add password reset from server console
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jul 26, 2024
1 parent 580fbdb commit 984ae9e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/backend/src/services/DefaultUserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class DefaultUserService extends BaseService {
uuidv4: require('uuid').v4,
}
async _init () {
this._register_commands(this.services.get('commands'));
}
async ['__on_ready.webserver'] () {
// check if a user named `admin` exists
Expand Down Expand Up @@ -213,6 +214,41 @@ class DefaultUserService extends BaseService {
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;

0 comments on commit 984ae9e

Please sign in to comment.