From 377b9bb67a612ad7b7565919c646e407b96d3b1c Mon Sep 17 00:00:00 2001 From: Bastien Date: Sun, 2 May 2021 12:12:09 +0200 Subject: [PATCH] Add the change-name route Verify that name is alphanum+space & remove condition in catch Remove alphanum+space verification Update percentage --- logic/auth.js | 40 ++++++++++++++++++++++++++++++++++++++++ routes/v1/account.js | 25 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/logic/auth.js b/logic/auth.js index dd7b391..f1a9912 100644 --- a/logic/auth.js +++ b/logic/auth.js @@ -16,14 +16,20 @@ const saltRounds = 10; const SYSTEM_USER = UUID.fetchBootUUID() || 'admin'; let devicePassword = ''; +let changeNameStatus; let changePasswordStatus; +resetChangeNameStatus(); resetChangePasswordStatus(); function resetChangePasswordStatus() { changePasswordStatus = { percent: 0 }; } +function resetChangeNameStatus() { + changeNameStatus = { percent: 0 }; +} + async function sleepSeconds(seconds) { return new Promise(resolve => { setTimeout(resolve, seconds * constants.TIME.ONE_SECOND_IN_MILLIS); @@ -40,6 +46,34 @@ function getCachedPassword() { return devicePassword; } +// Change the device name +async function changeName(name) { + resetChangeNameStatus(); + + changeNameStatus.percent = 1; // eslint-disable-line no-magic-numbers + + changeNameStatus.percent = 30; // eslint-disable-line no-magic-numbers + + try { + // get user data + const user = await diskLogic.readUserFile(); + changeNameStatus.percent = 60; // eslint-disable-line no-magic-numbers + + // update user name + user.name = name; + + // update user file + await diskLogic.writeUserFile({ ...user }); + + changeNameStatus.percent = 100; + } catch (error) { + changeNameStatus.error = true; + changeNameStatus.percent = 100; + + throw error; + } +} + // Change the device and lnd password. async function changePassword(currentPassword, newPassword, jwt) { @@ -117,6 +151,10 @@ async function changePassword(currentPassword, newPassword, jwt) { } +function getChangeNameStatus() { + return changeNameStatus; +} + function getChangePasswordStatus() { return changePasswordStatus; } @@ -266,8 +304,10 @@ async function refresh(user) { module.exports = { + changeName, changePassword, getCachedPassword, + getChangeNameStatus, getChangePasswordStatus, hashCredentials, isRegistered, diff --git a/routes/v1/account.js b/routes/v1/account.js index 00f2795..c1dcaae 100644 --- a/routes/v1/account.js +++ b/routes/v1/account.js @@ -13,6 +13,31 @@ const validator = require('utils/validator.js'); const COMPLETE = 100; +router.post('/change-name', auth.jwt, safeHandler(async (req, res, next) => { + const newName = req.body.newName; + + try { + validator.isString(newName); + } catch (error) { + return next(error); + } + + const status = await authLogic.getChangeNameStatus(); + + // return a conflict if a change name process is already running + if (status.percent > 0 && status.percent !== COMPLETE) { + return res.status(constants.STATUS_CODES.CONFLICT).json(); + } + + try { + // start change name process in the background and immediately return + await authLogic.changeName(newName); + return res.status(constants.STATUS_CODES.OK).json(); + } catch (error) { + return next(error); + } +})); + // Endpoint to change your lnd password. Wallet must exist and be unlocked. router.post('/change-password', auth.convertReqBodyToBasicAuth, auth.basic, incorrectPasswordAuthHandler, safeHandler(async (req, res, next) => { // Use password from the body by default. Basic auth has issues handling special characters.