mirror of
https://github.com/yuto-yuto/node-red-contrib-password-generator.git
synced 2024-11-21 19:06:44 +01:00
fix
This commit is contained in:
parent
2e1cab23a4
commit
456f4332e3
@ -8,15 +8,15 @@ const AsciiRange = {
|
|||||||
max: 126,
|
max: 126,
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generatePassword(size: number): Promise<string> {
|
export async function generatePassword(length: number): Promise<string> {
|
||||||
let result = "";
|
let result = "";
|
||||||
while (true) {
|
while (true) {
|
||||||
const bytes = await promisify(crypto.randomBytes)(size * 2);
|
const bytes = await promisify(crypto.randomBytes)(length * 2);
|
||||||
const byteArray = Array.from(bytes);
|
const byteArray = Array.from(bytes);
|
||||||
const filtered = byteArray.filter(isInAsciiRange);
|
const filtered = byteArray.filter(isInAsciiRange);
|
||||||
result += String.fromCharCode(...filtered);
|
result += String.fromCharCode(...filtered);
|
||||||
if (result.length >= size) {
|
if (result.length >= length) {
|
||||||
result = result.slice(0, size);
|
result = result.slice(0, length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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">
|
<script type="text/html" data-template-name="password-generator">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-key"><i class="fa fa-key"></i> Key prop</label>
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
<input type="text" id="node-input-key" placeholder="payload.key">
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-size"><i class="fa fa-database"></i> Size</label>
|
<label for="node-input-length"> Length</label>
|
||||||
<input type="text" id="node-input-size">
|
<input type="text" id="node-input-length">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-to"><i class="fa fa-database"></i> to</label>
|
<label for="node-input-setTo"><i class="fa fa-ellipsis-he"></i> to</label>
|
||||||
<input type="text" id="node-input-to">
|
<input type="text" id="node-input-setTo">
|
||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -18,9 +36,9 @@
|
|||||||
|
|
||||||
<h3>Configuration</h3>
|
<h3>Configuration</h3>
|
||||||
<dl class="message-properties">
|
<dl class="message-properties">
|
||||||
<dt>size</dt>
|
<dt>Length</dt>
|
||||||
<dd> The output size of the string. </dd>
|
<dd> Password length. </dd>
|
||||||
<dt>to</dt>
|
<dt>to</dt>
|
||||||
<dd> The property to set password. </dd>
|
<dd> The property to set generated password. </dd>
|
||||||
</dl>
|
</dl>
|
||||||
</script>
|
</script>
|
||||||
|
@ -15,7 +15,7 @@ export = (RED: nodered.NodeAPI): void => {
|
|||||||
RED.nodes.createNode(this, config);
|
RED.nodes.createNode(this, config);
|
||||||
|
|
||||||
this.on("input", async (msg: any, send, done) => {
|
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";
|
const valueSetPath = msg.to || config.setTo || "payload";
|
||||||
msg = yutolity.setValue(msg, valueSetPath, password);
|
msg = yutolity.setValue(msg, valueSetPath, password);
|
||||||
send(msg);
|
send(msg);
|
||||||
|
@ -2,12 +2,12 @@ import * as nodered from "node-red";
|
|||||||
|
|
||||||
export interface PasswordGeneratorNodeDef
|
export interface PasswordGeneratorNodeDef
|
||||||
extends nodered.NodeDef {
|
extends nodered.NodeDef {
|
||||||
size: number;
|
length: number;
|
||||||
setTo?: string;
|
setTo?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PasswordGeneratorNodeProperties
|
export interface PasswordGeneratorNodeProperties
|
||||||
extends nodered.EditorNodeProperties {
|
extends nodered.EditorNodeProperties {
|
||||||
size: number;
|
length: number;
|
||||||
setTo?: string;
|
setTo?: string;
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,13 @@ import { PasswordGeneratorNodeProperties } from "./PasswordGeneratorNodeDef";
|
|||||||
|
|
||||||
declare const RED: nodered.EditorRED;
|
declare const RED: nodered.EditorRED;
|
||||||
const nodeName = "password-generator";
|
const nodeName = "password-generator";
|
||||||
|
|
||||||
RED.nodes.registerType<PasswordGeneratorNodeProperties>(nodeName, {
|
RED.nodes.registerType<PasswordGeneratorNodeProperties>(nodeName, {
|
||||||
category: "function",
|
category: "function",
|
||||||
color: "#a6bbcf",
|
color: "#a6bbcf",
|
||||||
defaults: {
|
defaults: {
|
||||||
name: { value: "" },
|
name: { value: "" },
|
||||||
size: { value: "", validate: RED.validators.number() },
|
length: { value: "", required: true, validate: RED.validators.number() },
|
||||||
setTo: { value: "" },
|
setTo: { value: "" },
|
||||||
},
|
},
|
||||||
inputs: 1,
|
inputs: 1,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import "mocha";
|
import "mocha";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
// import * as helper from "node-red-node-test-helper";
|
|
||||||
import helper = require("node-red-node-test-helper");
|
import helper = require("node-red-node-test-helper");
|
||||||
|
|
||||||
import valueChangeNode = require("../lib/PasswordGeneratorNode");
|
import valueChangeNode = require("../lib/PasswordGeneratorNode");
|
||||||
@ -20,12 +19,12 @@ describe("PasswordGeneratorNode", () => {
|
|||||||
|
|
||||||
const nodeId = "node-id";
|
const nodeId = "node-id";
|
||||||
const outNodeId = "out-node-id";
|
const outNodeId = "out-node-id";
|
||||||
function createFlow(args?: { size?: number, setTo?: string }) {
|
function createFlow(args?: { length?: number, setTo?: string }) {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
id: nodeId,
|
id: nodeId,
|
||||||
type: "password-generator",
|
type: "password-generator",
|
||||||
size: args?.size || 10,
|
length: args?.length || 10,
|
||||||
setTo: args?.setTo,
|
setTo: args?.setTo,
|
||||||
name: "generator-name",
|
name: "generator-name",
|
||||||
wires: [[outNodeId]]
|
wires: [[outNodeId]]
|
||||||
|
Loading…
Reference in New Issue
Block a user