Browse Source

Postman authentication with users.json file

pull/665/head
flavio 3 years ago
parent
commit
e8b662d865
  1. 3
      src/config.js
  2. 22
      src/lib/Server.js
  3. 17
      src/users.json

3
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/';

22
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;

17
src/users.json

@ -0,0 +1,17 @@
{
"users": [
{
"username": "alice",
"password": "password123"
},
{
"username": "bob",
"password": "notpassword777"
},
{
"username": "roberto",
"password": "yespassword999"
}
]
}
Loading…
Cancel
Save