Skip to content

Commit 984ae9e

Browse files
committed
feat: add password reset from server console
1 parent 580fbdb commit 984ae9e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/backend/src/services/DefaultUserService.js

+36
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class DefaultUserService extends BaseService {
7171
uuidv4: require('uuid').v4,
7272
}
7373
async _init () {
74+
this._register_commands(this.services.get('commands'));
7475
}
7576
async ['__on_ready.webserver'] () {
7677
// check if a user named `admin` exists
@@ -213,6 +214,41 @@ class DefaultUserService extends BaseService {
213214
return tmp_password;
214215
});
215216
}
217+
async force_tmp_password_ (user) {
218+
const db = this.services.get('database')
219+
.get(DB_WRITE, 'terminal-password-reset');
220+
const actor = await Actor.create(UserActorType, { user });
221+
return await Context.get().sub({ actor }).arun(async () => {
222+
const svc_driver = this.services.get('driver');
223+
const tmp_password = require('crypto').randomBytes(4).toString('hex');
224+
const bcrypt = require('bcrypt');
225+
const password_hashed = await bcrypt.hash(tmp_password, 8);
226+
await svc_driver.call(
227+
'puter-kvstore', 'set', {
228+
key: 'tmp_password',
229+
value: tmp_password });
230+
await db.write(
231+
`UPDATE user SET password = ? WHERE id = ?`,
232+
[
233+
password_hashed,
234+
user.id,
235+
],
236+
);
237+
return tmp_password;
238+
});
239+
}
240+
_register_commands (commands) {
241+
commands.registerCommands('default-user', [
242+
{
243+
id: 'reset-password',
244+
handler: async (args, ctx) => {
245+
const [ username ] = args;
246+
const user = await get_user({ username });
247+
await this.force_tmp_password_(user);
248+
}
249+
}
250+
]);
251+
}
216252
}
217253

218254
module.exports = DefaultUserService;

0 commit comments

Comments
 (0)