Skip to content

Instantly share code, notes, and snippets.

@doino-gretchenliev
Last active October 12, 2022 16:01
Show Gist options
  • Save doino-gretchenliev/8e09e751a8c1eaeb7364c586deea8c4b to your computer and use it in GitHub Desktop.
Save doino-gretchenliev/8e09e751a8c1eaeb7364c586deea8c4b to your computer and use it in GitHub Desktop.
Node-RED Radius custom authentication and authorization. #node-red #nodered #radius #authentication #javascript

Node-RED Radius custom authentication and authorization

Requirements

Installation

  1. Save user-authentication.js to <node-red>/user-authentication.js.
  2. Add the following line to setting.js:
adminAuth: require("./user-authentication")

More information. It's also recommended to enable https connection.

const path = require("path");
const fs = require("fs");
const Client = require("node-radius-client");
const radiusUtils = require("node-radius-utils");
const userNameAttr = radiusUtils.dictionaries.rfc2865.attributes.USER_NAME;
const userPasswordAttr =
radiusUtils.dictionaries.rfc2865.attributes.USER_PASSWORD;
const RADIUS_SECRET = "SECRET";
const client = new Client({
host: "192.168.1.1",
hostPort: 1812
});
const usersFile = path.resolve(__dirname, "users.json");
if (!fs.existsSync(usersFile)) {
users = {};
json = JSON.stringify(users);
fs.writeFileSync(usersFile, json, "utf8");
}
module.exports = {
type: "credentials",
users: function(username) {
return new Promise(function(resolve) {
data = fs.readFileSync(usersFile, "utf8");
users = JSON.parse(data);
if (username in users) {
resolve({ username: username, permissions: users[username] });
}
resolve(null);
});
},
authenticate: function(username, password) {
return new Promise(function(resolve) {
client
.accessRequest({
secret: RADIUS_SECRET,
attributes: [[userNameAttr, username], [userPasswordAttr, password]]
})
.then(() => {
data = fs.readFileSync(usersFile, "utf8");
users = JSON.parse(data);
users[username] = "*";
json = JSON.stringify(users);
fs.writeFileSync(usersFile, json, "utf8");
resolve({ username: username, permissions: users[username] });
})
.catch(() => {
console.log(`Auth error for user: [${username}]`);
resolve(null);
});
});
},
default: function() {
return new Promise(function(resolve) {
// Resolve with the user object for the default user.
// If no default user exists, resolve with null.
resolve(null);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment