Added time rule flags

This commit is contained in:
Alex Rennie-Lis 2024-06-06 23:41:46 +01:00
parent 8265e89d69
commit a3e0cc381b
2 changed files with 40 additions and 32 deletions

View File

@ -30,7 +30,8 @@ catch (error) {
client_secret: "password", client_secret: "password",
default_vlan_enabled: false, default_vlan_enabled: false,
mac_auth_only: false, mac_auth_only: false,
session_duration: 60 session_duration: 60,
time_rules_enabled: false
} }
} }
if (process.env['SINATRA_PORT_RADIUS_AUTH']) config.ports.radius_authentication = process.env['SINATRA_PORT_RADIUS_AUTH']; if (process.env['SINATRA_PORT_RADIUS_AUTH']) config.ports.radius_authentication = process.env['SINATRA_PORT_RADIUS_AUTH'];
@ -42,12 +43,14 @@ if (process.env['SINATRA_DEFAULT_VLAN_ID']) config.default_vlan_id = process.env
if (process.env['SINATRA_CLIENT_SECRET']) config.client_secret = process.env['SINATRA_CLIENT_SECRET']; if (process.env['SINATRA_CLIENT_SECRET']) config.client_secret = process.env['SINATRA_CLIENT_SECRET'];
if (process.env['SINATRA_MAC_AUTH_ONLY']) config.mac_auth_only = process.env['SINATRA_MAC_AUTH_ONLY']; if (process.env['SINATRA_MAC_AUTH_ONLY']) config.mac_auth_only = process.env['SINATRA_MAC_AUTH_ONLY'];
if (process.env['SINATRA_SESSION_DURATION']) config.session_duration = process.env['SINATRA_SESSION_DURATION']; if (process.env['SINATRA_SESSION_DURATION']) config.session_duration = process.env['SINATRA_SESSION_DURATION'];
if (process.env['SINATRA_TIME_RULES']) config.time_rules_enabled = process.env['SINATRA_TIME_RULES'];
// Set defaults // Set defaults
if (!config.ports.radius_authentication) config.ports.radius_authentication = 1812; if (!config.ports.radius_authentication) config.ports.radius_authentication = 1812;
if (!config.ports.radius_accounting) config.ports.radius_accounting = 1813; if (!config.ports.radius_accounting) config.ports.radius_accounting = 1813;
if (!config.ports.api) config.ports.api = 8088; if (!config.ports.api) config.ports.api = 8088;
if (!config.session_duration) config.session_duration = 1800; if (!config.session_duration) config.session_duration = 60;
if (!config.time_rules_enabled) config.time_rules_enabled = false;
// Display active configuration // Display active configuration
log.write ('Using configuration: ' + JSON.stringify (config)); log.write ('Using configuration: ' + JSON.stringify (config));

View File

@ -41,6 +41,7 @@ const resolveRange = (str) => {
module.exports = { module.exports = {
checkAuth: (rules = []) => { checkAuth: (rules = []) => {
var authorised = false; var authorised = false;
if (config.time_rules_enabled) {
if (rules.length > 0) { if (rules.length > 0) {
var now = new Date (); var now = new Date ();
var minuteOfDay = (now.getHours () * 60) + now.getMinutes (); // 0 - 1439 var minuteOfDay = (now.getHours () * 60) + now.getMinutes (); // 0 - 1439
@ -68,13 +69,17 @@ module.exports = {
actions.push (rule.action.toLowerCase ()); actions.push (rule.action.toLowerCase ());
} }
}); });
if (actions.indexOf ("allow")) { if (actions.indexOf ("allow") !== -1) {
authorised = true; authorised = true;
} }
if (actions.indexOf ("deny")) { if (actions.indexOf ("deny") !== -1) {
authorised = false; authorised = false;
} }
} }
}
else {
authorised = true;
}
return authorised; return authorised;
} }
} }