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 5b78102..5938679 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 @@
- Option 2: Shared + Option 2: Multiple credentials
- - + +
- Option 3: File + Option 3: File with multiple credentials
@@ -75,32 +75,31 @@

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. -
  3. Shared: credentials shared with multiple nodes. (one credential)
  4. -
  5. File: the user credentials are stored in a file. (multiple credentials)
  6. +
  7. Multiple credentials: credentials shared with multiple nodes. (multiple credentials)
  8. +
  9. 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

+
+
Username
+
The username
+
Example: alice -

With Simple and Shared config types you must specify the following:

-
    -
  • Username: the username
  • -
  • - Password: the password may be in plain-text or hashed (only bcrypt is supported). -
  • -
+
Realm
+
Authorization realm for which the credentials will be valid
+
Example: node-red
-

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). -
  • -
+
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
+ +
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
+
diff --git a/nodes/http-auth.js b/nodes/http-auth.js index 08439a5..75deea9 100644 --- a/nodes/http-auth.js +++ b/nodes/http-auth.js @@ -52,9 +52,9 @@ module.exports = function (RED) { let src = 'inline'; let realm = config.realm.trim(); let realmL = realm.toLowerCase(); - let username = config.username.trim(); - let usernameL = username.toLowerCase(); - let password = config.password; + const username = config.username.trim(); + const usernameL = username.toLowerCase(); + const password = config.password; let getUser = function (_realm, _username) { if (_realm.trim().toLowerCase() === realmL && _username.trim().toLowerCase() === usernameL) { return { @@ -66,14 +66,12 @@ module.exports = function (RED) { return null; }; - const cred = RED.nodes.getNode(config.cred); - if (cred) { - src = 'cred'; - realm = cred.realm.trim(); + const multiple = RED.nodes.getNode(config.multiple); + if (multiple) { + src = 'multiple'; + realm = multiple.realm.trim(); realmL = realm.toLowerCase(); - username = cred.username.trim(); - usernameL = username.toLowerCase(); - password = cred.password; + getUser = multiple.getUser; } const file = RED.nodes.getNode(config.file); diff --git a/package-lock.json b/package-lock.json index a07a5de..0917325 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@alexandrainst/node-red-http-basic-auth", - "version": "2.3.0", + "version": "3.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@alexandrainst/node-red-http-basic-auth", - "version": "2.3.0", + "version": "3.0.0", "license": "GPL-2.0", "dependencies": { "bcryptjs": "^2.4.3" @@ -1271,10 +1271,13 @@ "devOptional": true }, "node_modules/function-bind": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", - "dev": true + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } }, "node_modules/function.prototype.name": { "version": "1.1.6", diff --git a/package.json b/package.json index a053b82..413e050 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@alexandrainst/node-red-http-basic-auth", - "version": "2.3.0", + "version": "3.0.0", "description": "Node-RED node for HTTP Basic Authorization", "keywords": [ "node-red", @@ -20,6 +20,10 @@ { "name": "Endemecio Santana", "url": "https://github.com/endemecio02" + }, + { + "name": "Paulo Albuquerque", + "url": "https://github.com/pjalbuquerque" } ], "license": "GPL-2.0", @@ -34,8 +38,8 @@ "version": ">=2.0.0", "nodes": { "http-basic-auth": "nodes/http-auth.js", - "http-basic-auth-cred": "nodes/http-auth-cred.js", - "http-basic-auth-file": "nodes/http-auth-file.js" + "http-basic-auth-file": "nodes/http-auth-file.js", + "http-basic-auth-multiple": "nodes/http-auth-multiple.js" } }, "dependencies": {