diff --git a/src/config.js b/src/config.js index 30f9221c..3fd4893a 100644 --- a/src/config.js +++ b/src/config.js @@ -27,3 +27,6 @@ iptables -A FORWARD -o wg0 -j ACCEPT; module.exports.WG_PRE_DOWN = process.env.WG_PRE_DOWN || ''; module.exports.WG_POST_DOWN = process.env.WG_POST_DOWN || ''; + +// Adding enviroment variable for the users.json file +module.exports.USERS_PATH = process.env.USERS_PATH || '/app/'; diff --git a/src/lib/Server.js b/src/lib/Server.js index f8f380bd..806769bc 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -10,10 +10,15 @@ const Util = require('./Util'); const ServerError = require('./ServerError'); const WireGuard = require('../services/WireGuard'); +// Needed to open users.json file +const fs = require('fs').promises; + +// Getting enviroment variables const { PORT, RELEASE, PASSWORD, + USERS_PATH } = require('../config'); module.exports = class Server { @@ -47,16 +52,23 @@ module.exports = class Server { }; })) .post('/api/session', Util.promisify(async req => { + // Post authentication const { + username, password, } = req.body; - - if (typeof password !== 'string') { - throw new ServerError('Missing: Password', 401); + + // Check if the credentials are correct + const tmp = await fs.readFile(path.join(USERS_PATH, 'users.json')); + const { users } = JSON.parse(tmp); + const user = users.find(findUser => findUser.username === username); + + if (typeof user !== 'object') { + throw new ServerError('Wrong Username', 401); } - if (password !== PASSWORD) { - throw new ServerError('Incorrect Password', 401); + if (user.password !== password) { + throw new ServerError('Wrong Password', 401); } req.session.authenticated = true; diff --git a/src/users.json b/src/users.json new file mode 100644 index 00000000..b772443b --- /dev/null +++ b/src/users.json @@ -0,0 +1,17 @@ +{ + "users": [ + { + "username": "alice", + "password": "password123" + }, + { + "username": "bob", + "password": "notpassword777" + }, + { + "username": "roberto", + "password": "yespassword999" + } + ] + } + \ No newline at end of file