- Save
user-authentication.js
to<node-red>/user-authentication.js
. - Add the following line to
setting.js
:
adminAuth: require("./user-authentication")
More information.
It's also recommended to enable https
connection.
user-authentication.js
to <node-red>/user-authentication.js
.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); | |
}); | |
} | |
}; |