diff --git a/src/lib/Server.js b/src/lib/Server.js index 009ab4a3..038732a1 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -48,15 +48,15 @@ module.exports = class Server { }; })) .post('/api/session', Util.promisify(async req => { - // Post authentication + // Authentication const { username, password, } = req.body; // Check if the credentials are correct - const tmp = await fs.readFile(path.join(USERS_PATH, 'users.json')); - const { users } = JSON.parse(tmp); + const usersJson = await fs.readFile(path.join(USERS_PATH, 'users.json'), 'utf8'); + const users = JSON.parse(usersJson); const user = users.find(findUser => findUser.username === username); if (typeof user !== 'object') { @@ -68,12 +68,12 @@ module.exports = class Server { } req.session.authenticated = true; + req.session.username = user.username; req.session.save(); debug(`New Session: ${req.session.id}`); })) - // WireGuard .use((req, res, next) => { if (req.session && req.session.authenticated) { @@ -91,6 +91,32 @@ module.exports = class Server { debug(`Deleted Session: ${sessionId}`); })) + .post('/api/updatePassword', Util.promisify(async req => { + const { + newPassword, + checkPassword, + } = req.body; + const username = req.session.username; + + const usersJson = await fs.readFile(path.join(USERS_PATH, 'users.json'), 'utf8'); + const users = JSON.parse(usersJson); + const user = users.find(findUser => findUser.username === username); + + if (typeof user !== 'object') { + throw new ServerError('This session.username does not exists', 401); + } + + if (newPassword !== checkPassword) { + throw new ServerError("Passwords don't match", 401); + } + + user.password = newPassword; + await fs.writeFile(path.join(USERS_PATH, 'users.json'), JSON.stringify(users, false, 2), { + mode: 0o660, + }); + + })) + // WireGuard .get('/api/wireguard/client', Util.promisify(async req => { return WireGuard.getClients(); })) diff --git a/src/users.json b/src/users.json index b772443b..0a7f63c6 100644 --- a/src/users.json +++ b/src/users.json @@ -1,17 +1,14 @@ -{ - "users": [ - { - "username": "alice", - "password": "password123" - }, - { - "username": "bob", - "password": "notpassword777" - }, - { - "username": "roberto", - "password": "yespassword999" - } - ] +[ + { + "username": "alice", + "password": "password123" + }, + { + "username": "bob", + "password": "notpassword777" + }, + { + "username": "roberto", + "password": "yespassword999" } - \ No newline at end of file +] \ No newline at end of file diff --git a/src/www/index.html b/src/www/index.html index 5425a0de..43c5d275 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -54,6 +54,32 @@ + +
+