Skip to content

Commit

Permalink
Merge branch 'main' into two-outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex committed Oct 12, 2023
2 parents 67d4ce9 + c2987f4 commit 428b09b
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 112 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,39 @@ curl 'https://test:[email protected]/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:
</dl>

- 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
Expand Down
34 changes: 0 additions & 34 deletions nodes/http-auth-cred.html

This file was deleted.

13 changes: 0 additions & 13 deletions nodes/http-auth-cred.js

This file was deleted.

93 changes: 93 additions & 0 deletions nodes/http-auth-multiple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<script type="text/javascript">
/* global RED:false, $:false */
RED.nodes.registerType('http-basic-auth-multiple', {
category: 'config',
defaults: {
name: { value: '' },
realm: { value: '', required: true },
auths: { value: {} },
},
color: '#E7E7AE',
icon: 'white-globe.png',
label: function () { return (this.name || 'http auth multiple'); },
oneditprepare: function () {
function resizeRule(rule) {
const newWidth = rule.width();
rule.find('.red-ui-typedInput').typedInput('width', (newWidth - 15) / 2);
}
const authList = $('#node-input-auths-container').css('min-height', '150px').css('min-width', '450px')
.editableList({
addItem: function (container, i, auth) {
const row = $('<div/>').appendTo(container);

const propertyUser = $('<input/>',
{ class: 'node-input-auth-user', type: 'text', style: 'width: 30%;', placeholder: 'user' }).appendTo(row);

const propertyRealm = $('<input/>',
{ class: 'node-input-auth-realm', type: 'text', style: 'margin-left: 3%; width: 30%;', placeholder: 'realm' }).appendTo(row);

const propertyPassword = $('<input/>',
{ class: 'node-input-auth-password', type: 'text', style: 'margin-left: 3%; width: 30%;', placeholder: 'password' }).appendTo(row);

propertyUser.val(auth.user);
propertyRealm.val(auth.realm);
propertyPassword.val(auth.password);

resizeRule(container);
},
resizeItem: resizeRule,
removable: true,
});

if (this.auths) {
for (const key in this.auths) {
if ((this.auths[key]).length) {
this.auths[key].forEach(function (value, index) {
authList.editableList('addItem', { realm: key, user: value.user, password: value.password });
});
}
}
}
},
oneditsave: function () {
const auths = $('#node-input-auths-container').editableList('items');
const node = this;
node.auths = {};
auths.each(function (i) {
const auth = $(this);
const user = auth.find('.node-input-auth-user').val();
const realm = auth.find('.node-input-auth-realm').val();
const password = auth.find('.node-input-auth-password').val();

if (!node.auths[realm]) {
node.auths[realm] = [];
}

if (realm && user && password) {
node.auths[realm].push({
user,
password,
});
}
});
},
paletteLabel: 'http auth',
});
</script>
<script type="text/x-red" data-template-name="http-basic-auth-multiple">
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="texth" id="node-config-input-name">
</div>
<div class="form-row">
<label for="node-config-input-realm"><i class="fa fa-globe"></i> Realm</label>
<input type="text" id="node-config-input-realm">
</div>
<div class="form-row">
<label><i class="fa fa-list"></i> Auths</span>
</label>
</div>
<div class="form-row node-input-auths-container-row">
<ol id="node-input-auths-container"></ol>
</div>
</script>
44 changes: 44 additions & 0 deletions nodes/http-auth-multiple.js
Original file line number Diff line number Diff line change
@@ -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);
};
53 changes: 26 additions & 27 deletions nodes/http-auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' },
Expand Down Expand Up @@ -46,15 +46,15 @@
</fieldset>

<fieldset>
<legend>Option 2: Shared</legend>
<legend>Option 2: Multiple credentials</legend>
<div class="form-row">
<label for="node-input-cred"><i class="fa fa-list"></i> Credentials</label>
<input type="text" id="node-input-cred" />
<label for="node-input-multiple"><i class="fa fa-list"></i> Credentials</label>
<input type="text" id="node-input-multiple" />
</div>
</fieldset>

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

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

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

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

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

<dt>File</dt>
<dd>Location of the file containing the credentials relative to the presently working directory</dd>
<dd>Example: <kbd>/data/.credentials</kbd></dd>
<dd>The format for each line is <kbd>user:realm:password</kbd></dd>
</dl>
</script>
18 changes: 8 additions & 10 deletions nodes/http-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 428b09b

Please sign in to comment.