Skip to content

Commit c2987f4

Browse files
authored
Inserted multiple users through list.
* I inserted the list of users through the list component of the node-red itself. @pjalbuquerque * Remove http-basic-auth-cred, replaced by http-basic-auth-multiple. @Alkarex
2 parents 62fc919 + e07ae94 commit c2987f4

9 files changed

Lines changed: 214 additions & 117 deletions

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,39 @@ curl 'https://test:test@nodered.example.net/basic-auth-demo'
2222

2323
## Config
2424

25-
There are three type of configuration:
25+
There are three types of configuration:
2626

2727
1. *Simple*: each node has it’s own credentials. (one credential)
28-
2. *Shared*: credentials shared with multiple nodes. (one credential)
29-
3. *File*: the user credentials are stored in a file. (multiple credentials)
28+
2. *Multiple credentials*: credentials shared with multiple nodes. (multiple credentials)
29+
3. *File with multiple credentials*: the user credentials are stored in a file. (multiple credentials)
3030

31-
With all three config types you must specify the following:
31+
## Definitions
3232

33-
- *Realm*: what authorization realm will be used with this node.
33+
* *Username*
34+
* The username
35+
* Example: `alice`
3436

35-
With *Simple* and *Shared* config types you must specify the following:
37+
* *Realm*
38+
* Authorization realm for which the credentials will be valid
39+
* Example: `node-red`
3640

37-
- *Username*: the username
38-
- *Password*: the password may be in plain-text or hashed (only bcrypt is supported).
39-
Example of hashed password `test`:
41+
* *Password*
42+
* The password may be in plain-text or hashed (only bcrypt is supported)
43+
* Example in plain-text: `test`
44+
* Example in bcrypt: `$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6`
4045

41-
```plain
42-
$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6
43-
```
46+
* *File*
47+
* Location of the file containing the credentials relative to the presently working directory
48+
* Example: `/data/.credentials`
49+
* The format for each line is `user:realm:password`
4450

45-
With *File* config type you must specify the following:
51+
</dl>
4652

47-
- File: location of the file containing the credentials relative to the presently working directory.
48-
The format for each line is `user:realm:password`.
49-
The passwords may be in plain-text or hashed (only bcrypt is supported).
50-
Example of file:
53+
Example of file:
5154

5255
```plain
53-
user1:application1:test
54-
user2:application1:$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6
56+
user1:node-red:test
57+
user2:node-red:$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6
5558
```
5659

5760
## Hints

nodes/http-auth-cred.html

Lines changed: 0 additions & 34 deletions
This file was deleted.

nodes/http-auth-cred.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

nodes/http-auth-multiple.html

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<script type="text/javascript">
2+
/* global RED:false, $:false */
3+
RED.nodes.registerType('http-basic-auth-multiple', {
4+
category: 'config',
5+
defaults: {
6+
name: { value: '' },
7+
realm: { value: '', required: true },
8+
auths: { value: {} },
9+
},
10+
color: '#E7E7AE',
11+
icon: 'white-globe.png',
12+
label: function () { return (this.name || 'http auth multiple'); },
13+
oneditprepare: function () {
14+
function resizeRule(rule) {
15+
const newWidth = rule.width();
16+
rule.find('.red-ui-typedInput').typedInput('width', (newWidth - 15) / 2);
17+
}
18+
const authList = $('#node-input-auths-container').css('min-height', '150px').css('min-width', '450px')
19+
.editableList({
20+
addItem: function (container, i, auth) {
21+
const row = $('<div/>').appendTo(container);
22+
23+
const propertyUser = $('<input/>',
24+
{ class: 'node-input-auth-user', type: 'text', style: 'width: 30%;', placeholder: 'user' }).appendTo(row);
25+
26+
const propertyRealm = $('<input/>',
27+
{ class: 'node-input-auth-realm', type: 'text', style: 'margin-left: 3%; width: 30%;', placeholder: 'realm' }).appendTo(row);
28+
29+
const propertyPassword = $('<input/>',
30+
{ class: 'node-input-auth-password', type: 'text', style: 'margin-left: 3%; width: 30%;', placeholder: 'password' }).appendTo(row);
31+
32+
propertyUser.val(auth.user);
33+
propertyRealm.val(auth.realm);
34+
propertyPassword.val(auth.password);
35+
36+
resizeRule(container);
37+
},
38+
resizeItem: resizeRule,
39+
removable: true,
40+
});
41+
42+
if (this.auths) {
43+
for (const key in this.auths) {
44+
if ((this.auths[key]).length) {
45+
this.auths[key].forEach(function (value, index) {
46+
authList.editableList('addItem', { realm: key, user: value.user, password: value.password });
47+
});
48+
}
49+
}
50+
}
51+
},
52+
oneditsave: function () {
53+
const auths = $('#node-input-auths-container').editableList('items');
54+
const node = this;
55+
node.auths = {};
56+
auths.each(function (i) {
57+
const auth = $(this);
58+
const user = auth.find('.node-input-auth-user').val();
59+
const realm = auth.find('.node-input-auth-realm').val();
60+
const password = auth.find('.node-input-auth-password').val();
61+
62+
if (!node.auths[realm]) {
63+
node.auths[realm] = [];
64+
}
65+
66+
if (realm && user && password) {
67+
node.auths[realm].push({
68+
user,
69+
password,
70+
});
71+
}
72+
});
73+
},
74+
paletteLabel: 'http auth',
75+
});
76+
</script>
77+
<script type="text/x-red" data-template-name="http-basic-auth-multiple">
78+
<div class="form-row">
79+
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
80+
<input type="texth" id="node-config-input-name">
81+
</div>
82+
<div class="form-row">
83+
<label for="node-config-input-realm"><i class="fa fa-globe"></i> Realm</label>
84+
<input type="text" id="node-config-input-realm">
85+
</div>
86+
<div class="form-row">
87+
<label><i class="fa fa-list"></i> Auths</span>
88+
</label>
89+
</div>
90+
<div class="form-row node-input-auths-container-row">
91+
<ol id="node-input-auths-container"></ol>
92+
</div>
93+
</script>

nodes/http-auth-multiple.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module.exports = function (RED) {
2+
'use strict';
3+
4+
function HttpAuthMultipleNode(config) {
5+
RED.nodes.createNode(this, config);
6+
7+
const realm = config.realm.trim();
8+
const realmL = realm.toLowerCase();
9+
const users = {};
10+
for (const key in config.auths) {
11+
config.auths[key].forEach(function (value, index) {
12+
const _username = value.user.trim();
13+
const _usernameL = _username.toLowerCase();
14+
const _realm = key;
15+
const _realmL = _realm.toLowerCase();
16+
const _password = value.password;
17+
18+
if (_realmL === realmL) {
19+
users[_usernameL] = {
20+
realm: _realm,
21+
username: _username,
22+
password: _password,
23+
};
24+
}
25+
});
26+
}
27+
28+
this.realm = config.realm;
29+
this.getUser = function (_realm, _username) {
30+
const _realmL = _realm.trim().toLowerCase();
31+
const _usernameL = _username.trim().toLowerCase();
32+
if (_realmL === realmL && users[_usernameL]) {
33+
return {
34+
realm: users[_usernameL].realm,
35+
username: users[_usernameL].username,
36+
password: users[_usernameL].password,
37+
};
38+
}
39+
return null;
40+
};
41+
}
42+
43+
RED.nodes.registerType('http-basic-auth-multiple', HttpAuthMultipleNode);
44+
};

nodes/http-auth.html

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
defaults: {
88
name: { value: '' },
99
file: { value: '', type: 'http-basic-auth-file', required: false },
10-
cred: { value: '', type: 'http-basic-auth-cred', required: false },
10+
multiple: { value: '', type: 'http-basic-auth-multiple', required: false },
1111
realm: { value: '' },
1212
username: { value: '' },
1313
password: { value: '' },
@@ -46,15 +46,15 @@
4646
</fieldset>
4747

4848
<fieldset>
49-
<legend>Option 2: Shared</legend>
49+
<legend>Option 2: Multiple credentials</legend>
5050
<div class="form-row">
51-
<label for="node-input-cred"><i class="fa fa-list"></i> Credentials</label>
52-
<input type="text" id="node-input-cred" />
51+
<label for="node-input-multiple"><i class="fa fa-list"></i> Credentials</label>
52+
<input type="text" id="node-input-multiple" />
5353
</div>
5454
</fieldset>
5555

5656
<fieldset>
57-
<legend>Option 3: File</legend>
57+
<legend>Option 3: File with multiple credentials</legend>
5858
<div class="form-row">
5959
<label for="node-input-file"><i class="fa fa-list"></i> File</label>
6060
<input type="text" id="node-input-file" />
@@ -75,32 +75,31 @@
7575
<p>
7676

7777
<h2>Config</h2>
78-
<p>There are three type of configuration:</p>
78+
<p>There are three types of configuration:</p>
7979
<ol>
8080
<li><i>Simple</i>: each node has it’s own credentials. (one credential)</li>
81-
<li><i>Shared</i>: credentials shared with multiple nodes. (one credential)</li>
82-
<li><i>File</i>: the user credentials are stored in a file. (multiple credentials)</li>
81+
<li><i>Multiple credentials</i>: credentials shared with multiple nodes. (multiple credentials)</li>
82+
<li><i>File with multiple credentials</i>: the user credentials are stored in a file. (multiple credentials)</li>
8383
</ol>
8484

85-
<p>With all three config types you must specify the following:</p>
86-
<ul>
87-
<li><i>Realm</i>: what authorization realm will be used with this node</li>
88-
</ul>
85+
<h2>Definitions</h2>
86+
<dl>
87+
<dt>Username</dt>
88+
<dd>The username</dd>
89+
<dd>Example: <kbd>alice</kbd>
8990

90-
<p>With <i>Simple</i> and <i>Shared</i> config types you must specify the following:</p>
91-
<ul>
92-
<li><i>Username</i>: the username</li>
93-
<li>
94-
<i>Password</i>: the password may be in plain-text or hashed (only bcrypt is supported).
95-
</li>
96-
</ul>
91+
<dt>Realm</dt>
92+
<dd>Authorization realm for which the credentials will be valid</dd>
93+
<dd>Example: <kbd>node-red</kbd></dd>
9794

98-
<p>With <i>File</i> config type you must specify the following:</p>
99-
<ul>
100-
<li>
101-
<i>File</i>: location of the file containing the credentials relative to the presently working directory.<br />
102-
The format for each line is <code>user:realm:password</code>.<br />
103-
The passwords may be in plain-text or hashed (only bcrypt is supported).
104-
</li>
105-
</ul>
95+
<dt>Password</dt>
96+
<dd>The password may be in plain-text or hashed (only bcrypt is supported)</dd>
97+
<dd>Example in plain-text: <kbd>test</kbd></dd>
98+
<dd>Example in bcrypt: <kbd>$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6</kbd></dd>
99+
100+
<dt>File</dt>
101+
<dd>Location of the file containing the credentials relative to the presently working directory</dd>
102+
<dd>Example: <kbd>/data/.credentials</kbd></dd>
103+
<dd>The format for each line is <kbd>user:realm:password</kbd></dd>
104+
</dl>
106105
</script>

nodes/http-auth.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ module.exports = function (RED) {
5050
let src = 'inline';
5151
let realm = config.realm.trim();
5252
let realmL = realm.toLowerCase();
53-
let username = config.username.trim();
54-
let usernameL = username.toLowerCase();
55-
let password = config.password;
53+
const username = config.username.trim();
54+
const usernameL = username.toLowerCase();
55+
const password = config.password;
5656
let getUser = function (_realm, _username) {
5757
if (_realm.trim().toLowerCase() === realmL && _username.trim().toLowerCase() === usernameL) {
5858
return {
@@ -64,14 +64,12 @@ module.exports = function (RED) {
6464
return null;
6565
};
6666

67-
const cred = RED.nodes.getNode(config.cred);
68-
if (cred) {
69-
src = 'cred';
70-
realm = cred.realm.trim();
67+
const multiple = RED.nodes.getNode(config.multiple);
68+
if (multiple) {
69+
src = 'multiple';
70+
realm = multiple.realm.trim();
7171
realmL = realm.toLowerCase();
72-
username = cred.username.trim();
73-
usernameL = username.toLowerCase();
74-
password = cred.password;
72+
getUser = multiple.getUser;
7573
}
7674

7775
const file = RED.nodes.getNode(config.file);

0 commit comments

Comments
 (0)