diff --git a/README.md b/README.md index e9c05c5..af98a7f 100644 --- a/README.md +++ b/README.md @@ -22,36 +22,39 @@ curl 'https://test:test@nodered.example.net/basic-auth-demo' ## Config -There are three type of configuration: +There are three types of configuration: 1. *Simple*: each node has it’s own credentials. (one credential) -2. *Shared*: credentials shared with multiple nodes. (one credential) -3. *File*: the user credentials are stored in a file. (multiple credentials) +2. *Multiple credentials*: credentials shared with multiple nodes. (multiple credentials) +3. *File with multiple credentials*: the user credentials are stored in a file. (multiple credentials) -With all three config types you must specify the following: +## Definitions -- *Realm*: what authorization realm will be used with this node. +* *Username* + * The username + * Example: `alice` -With *Simple* and *Shared* config types you must specify the following: +* *Realm* + * Authorization realm for which the credentials will be valid + * Example: `node-red` -- *Username*: the username -- *Password*: the password may be in plain-text or hashed (only bcrypt is supported). - Example of hashed password `test`: +* *Password* + * The password may be in plain-text or hashed (only bcrypt is supported) + * Example in plain-text: `test` + * Example in bcrypt: `$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6` -```plain -$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6 -``` +* *File* + * Location of the file containing the credentials relative to the presently working directory + * Example: `/data/.credentials` + * The format for each line is `user:realm:password` -With *File* config type you must specify the following: + -- File: location of the file containing the credentials relative to the presently working directory. - The format for each line is `user:realm:password`. - The passwords may be in plain-text or hashed (only bcrypt is supported). - Example of file: +Example of file: ```plain -user1:application1:test -user2:application1:$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6 +user1:node-red:test +user2:node-red:$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6 ``` ## Hints diff --git a/nodes/http-auth-cred.html b/nodes/http-auth-cred.html deleted file mode 100644 index 328d53b..0000000 --- a/nodes/http-auth-cred.html +++ /dev/null @@ -1,34 +0,0 @@ - - - diff --git a/nodes/http-auth-cred.js b/nodes/http-auth-cred.js deleted file mode 100644 index 96269e2..0000000 --- a/nodes/http-auth-cred.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = function (RED) { - 'use strict'; - - function HttpAuthCredNode(config) { - RED.nodes.createNode(this, config); - - this.realm = config.realm; - this.username = config.username; - this.password = config.password; - } - - RED.nodes.registerType('http-basic-auth-cred', HttpAuthCredNode); -}; diff --git a/nodes/http-auth-multiple.html b/nodes/http-auth-multiple.html new file mode 100644 index 0000000..ba12823 --- /dev/null +++ b/nodes/http-auth-multiple.html @@ -0,0 +1,93 @@ + + diff --git a/nodes/http-auth-multiple.js b/nodes/http-auth-multiple.js new file mode 100644 index 0000000..2b416cc --- /dev/null +++ b/nodes/http-auth-multiple.js @@ -0,0 +1,44 @@ +module.exports = function (RED) { + 'use strict'; + + function HttpAuthMultipleNode(config) { + RED.nodes.createNode(this, config); + + const realm = config.realm.trim(); + const realmL = realm.toLowerCase(); + const users = {}; + for (const key in config.auths) { + config.auths[key].forEach(function (value, index) { + const _username = value.user.trim(); + const _usernameL = _username.toLowerCase(); + const _realm = key; + const _realmL = _realm.toLowerCase(); + const _password = value.password; + + if (_realmL === realmL) { + users[_usernameL] = { + realm: _realm, + username: _username, + password: _password, + }; + } + }); + } + + this.realm = config.realm; + this.getUser = function (_realm, _username) { + const _realmL = _realm.trim().toLowerCase(); + const _usernameL = _username.trim().toLowerCase(); + if (_realmL === realmL && users[_usernameL]) { + return { + realm: users[_usernameL].realm, + username: users[_usernameL].username, + password: users[_usernameL].password, + }; + } + return null; + }; + } + + RED.nodes.registerType('http-basic-auth-multiple', HttpAuthMultipleNode); +}; diff --git a/nodes/http-auth.html b/nodes/http-auth.html index 41bcae3..e3eae5e 100644 --- a/nodes/http-auth.html +++ b/nodes/http-auth.html @@ -7,7 +7,7 @@ defaults: { name: { value: '' }, file: { value: '', type: 'http-basic-auth-file', required: false }, - cred: { value: '', type: 'http-basic-auth-cred', required: false }, + multiple: { value: '', type: 'http-basic-auth-multiple', required: false }, realm: { value: '' }, username: { value: '' }, password: { value: '' }, @@ -46,15 +46,15 @@