Skip to content

Commit

Permalink
#30 Implemented environment variable to disable built-in authenticati…
Browse files Browse the repository at this point in the history
…on system
glenndehaan committed Mar 4, 2024
1 parent bcc44cc commit 611e8cc
Showing 6 changed files with 73 additions and 50 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -48,6 +48,8 @@ services:
UNIFI_SITE_ID: 'default'
# The 'password' used to log in to this voucher portal
SECURITY_CODE: '0000'
# Disables the login/authentication for the portal
DISABLE_AUTH: 'false'
# Voucher Types, format: expiration in minutes (required),single-use or multi-use vouchers value - '0' is for multi-use - '1' is for single-use (optional),upload speed limit in kbps (optional),download speed limit in kbps (optional),data transfer limit in MB (optional)
# To skip a parameter just but nothing in between the comma's
# After a voucher type add a semicolon, after the semicolon you can start a new voucher type
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -11,4 +11,5 @@ services:
UNIFI_PASSWORD: 'password'
UNIFI_SITE_ID: 'default'
SECURITY_CODE: '0000'
DISABLE_AUTH: 'false'
VOUCHER_TYPES: '480,0,,,;'
28 changes: 18 additions & 10 deletions middlewares/authorization.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Global variables
*/
const authDisabled = (process.env.DISABLE_AUTH === 'true') || false;

/**
* Verifies if a user is signed in
*
@@ -6,17 +11,20 @@
* @param next
*/
module.exports = async (req, res, next) => {
// Check if user has an existing authorization cookie
if(!req.cookies.authorization) {
res.redirect(302, '/login');
return;
}
// Check if authentication is enabled
if(!authDisabled) {
// Check if user has an existing authorization cookie
if (!req.cookies.authorization) {
res.redirect(302, '/login');
return;
}

// Check if password is correct
const passwordCheck = req.cookies.authorization === (process.env.SECURITY_CODE || "0000");
if(!passwordCheck) {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Password Invalid!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/login');
return;
// Check if password is correct
const passwordCheck = req.cookies.authorization === (process.env.SECURITY_CODE || "0000");
if (!passwordCheck) {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Password Invalid!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/login');
return;
}
}

next();
76 changes: 38 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -17,14 +17,14 @@
"dependencies": {
"cookie-parser": "^1.4.6",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express": "^4.18.3",
"multer": "^1.4.5-lts.1",
"node-unifi": "^2.5.1",
"tailwindcss": "^3.4.1",
"tailwindcss-text-fill": "^0.2.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"nodemon": "^3.0.3"
"nodemon": "^3.1.0"
}
}
12 changes: 12 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ const app = express();
*/
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const voucherTypes = types(process.env.VOUCHER_TYPES || '480,0,,,;');
const authDisabled = (process.env.DISABLE_AUTH === 'true') || false;

/**
* Output logo
@@ -44,6 +45,11 @@ voucherTypes.forEach((type, key) => {
console.log(`[VoucherType][${key}] ${time(type.expiration)}, ${type.usage === '1' ? 'single-use' : 'multi-use'}${typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : `${typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : ''}${typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : ''}${typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : ''}`}`);
});

/**
* Log auth status
*/
console.log(`[AUTH] ${authDisabled ? 'Disabled!' : 'Enabled!'}`);

/**
* Log controller
*/
@@ -105,6 +111,12 @@ app.get('/', (req, res) => {
res.redirect(302, '/voucher');
});
app.get('/login', (req, res) => {
// Check if authentication is disabled
if(authDisabled) {
res.redirect(302, '/voucher');
return;
}

const hour = new Date().getHours();
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';

0 comments on commit 611e8cc

Please sign in to comment.