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

Add the change-name route #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions logic/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,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 @@ -39,6 +45,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;
}
}

// Sets system password
const setSystemPassword = async password => {
await diskLogic.writeStatusFile('password', password);
Expand Down Expand Up @@ -78,6 +112,10 @@ async function changePassword(currentPassword, newPassword, jwt) {
}
}

function getChangeNameStatus() {
return changeNameStatus;
}

function getChangePasswordStatus() {
return changePasswordStatus;
}
Expand Down Expand Up @@ -257,8 +295,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);
Copy link
Contributor

@louneskmt louneskmt Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add other validators, like no special characters?

function isAlphanumeric(string) {

or
function isAlphanumericAndSpaces(string) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah right didn't looked them alpha+space seems a good idea!

Copy link
Contributor

@AaronDewes AaronDewes Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this should also be added later in the file, otherwise a name that works for signup won't work if changed to it.

validator.isString(req.body.name);

Copy link
Member

@lukechilds lukechilds Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If validator.isString(); is all we validate at signup then that's all we should do for name changes too.

Unless there's a specific bug where special characters are causing issues, but that's outside the scope of this PR and should be resolved in a separate PR.

Copy link
Author

@bguillaumat bguillaumat Jan 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okok, noted I will update that

} 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 password.
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