This commit is contained in:
yuto-yuto 2021-07-11 06:16:21 +02:00
parent 2e1cab23a4
commit 456f4332e3
6 changed files with 39 additions and 21 deletions

View File

@ -8,15 +8,15 @@ const AsciiRange = {
max: 126,
}
export async function generatePassword(size: number): Promise<string> {
export async function generatePassword(length: number): Promise<string> {
let result = "";
while (true) {
const bytes = await promisify(crypto.randomBytes)(size * 2);
const bytes = await promisify(crypto.randomBytes)(length * 2);
const byteArray = Array.from(bytes);
const filtered = byteArray.filter(isInAsciiRange);
result += String.fromCharCode(...filtered);
if (result.length >= size) {
result = result.slice(0, size);
if (result.length >= length) {
result = result.slice(0, length);
break;
}
}

View File

@ -1,15 +1,33 @@
<script type="text/javascript">
const nodeName = "password-generator";
RED.nodes.registerType(nodeName, {
category: "function",
color: "#D8BFD8",
defaults: {
name: { value: "" },
length: { value: "", required: true, validate: RED.validators.number() },
setTo: { value: "" },
},
inputs: 1,
outputs: 1,
icon: "font-awesome/fa-key",
label: function () {
return this.name || nodeName;
},
});
</script>
<script type="text/html" data-template-name="password-generator">
<div class="form-row">
<label for="node-input-key"><i class="fa fa-key"></i> Key prop</label>
<input type="text" id="node-input-key" placeholder="payload.key">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-size"><i class="fa fa-database"></i> Size</label>
<input type="text" id="node-input-size">
<label for="node-input-length"> Length</label>
<input type="text" id="node-input-length">
</div>
<div class="form-row">
<label for="node-input-to"><i class="fa fa-database"></i> to</label>
<input type="text" id="node-input-to">
<label for="node-input-setTo"><i class="fa fa-ellipsis-he"></i> to</label>
<input type="text" id="node-input-setTo">
</div>
</script>
@ -18,9 +36,9 @@
<h3>Configuration</h3>
<dl class="message-properties">
<dt>size</dt>
<dd> The output size of the string. </dd>
<dt>Length</dt>
<dd> Password length. </dd>
<dt>to</dt>
<dd> The property to set password. </dd>
<dd> The property to set generated password. </dd>
</dl>
</script>

View File

@ -15,7 +15,7 @@ export = (RED: nodered.NodeAPI): void => {
RED.nodes.createNode(this, config);
this.on("input", async (msg: any, send, done) => {
const password = await generatePassword(config.size);
const password = await generatePassword(config.length);
const valueSetPath = msg.to || config.setTo || "payload";
msg = yutolity.setValue(msg, valueSetPath, password);
send(msg);

View File

@ -2,12 +2,12 @@ import * as nodered from "node-red";
export interface PasswordGeneratorNodeDef
extends nodered.NodeDef {
size: number;
length: number;
setTo?: string;
}
export interface PasswordGeneratorNodeProperties
extends nodered.EditorNodeProperties {
size: number;
length: number;
setTo?: string;
}

View File

@ -3,12 +3,13 @@ import { PasswordGeneratorNodeProperties } from "./PasswordGeneratorNodeDef";
declare const RED: nodered.EditorRED;
const nodeName = "password-generator";
RED.nodes.registerType<PasswordGeneratorNodeProperties>(nodeName, {
category: "function",
color: "#a6bbcf",
defaults: {
name: { value: "" },
size: { value: "", validate: RED.validators.number() },
length: { value: "", required: true, validate: RED.validators.number() },
setTo: { value: "" },
},
inputs: 1,
@ -17,4 +18,4 @@ RED.nodes.registerType<PasswordGeneratorNodeProperties>(nodeName, {
label: function () {
return this.name || nodeName;
},
});
});

View File

@ -1,6 +1,5 @@
import "mocha";
import { expect } from "chai";
// import * as helper from "node-red-node-test-helper";
import helper = require("node-red-node-test-helper");
import valueChangeNode = require("../lib/PasswordGeneratorNode");
@ -20,12 +19,12 @@ describe("PasswordGeneratorNode", () => {
const nodeId = "node-id";
const outNodeId = "out-node-id";
function createFlow(args?: { size?: number, setTo?: string }) {
function createFlow(args?: { length?: number, setTo?: string }) {
return [
{
id: nodeId,
type: "password-generator",
size: args?.size || 10,
length: args?.length || 10,
setTo: args?.setTo,
name: "generator-name",
wires: [[outNodeId]]