Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Add the change-name route
Browse files Browse the repository at this point in the history
Verify that name is alphanum+space & remove condition in catch

Remove alphanum+space verification

Update percentage
  • Loading branch information
bguillaumat committed May 2, 2021
1 parent d65fafa commit 377b9bb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
40 changes: 40 additions & 0 deletions logic/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {

Expand Down Expand Up @@ -117,6 +151,10 @@ async function changePassword(currentPassword, newPassword, jwt) {

}

function getChangeNameStatus() {
return changeNameStatus;
}

function getChangePasswordStatus() {
return changePasswordStatus;
}
Expand Down Expand Up @@ -266,8 +304,10 @@ async function refresh(user) {


module.exports = {
changeName,
changePassword,
getCachedPassword,
getChangeNameStatus,
getChangePasswordStatus,
hashCredentials,
isRegistered,
Expand Down
25 changes: 25 additions & 0 deletions routes/v1/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 377b9bb

Please sign in to comment.