Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add generate action to crypto node #2541

Merged
merged 6 commits into from
Mar 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 110 additions & 15 deletions packages/nodes-base/nodes/Crypto/Crypto.node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { set } from 'lodash';
import { IExecuteFunctions } from 'n8n-core';
import {
set,
} from 'lodash';

import {
IExecuteFunctions,
} from 'n8n-core';

import {
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
} from 'n8n-workflow';

import {
Expand All @@ -14,8 +21,11 @@ import {
createHmac,
createSign,
getHashes,
randomBytes,
} from 'crypto';

import { v4 as uuid } from 'uuid';

export class Crypto implements INodeType {
description: INodeTypeDescription = {
displayName: 'Crypto',
Expand All @@ -37,19 +47,24 @@ export class Crypto implements INodeType {
name: 'action',
type: 'options',
options: [
{
name: 'Generate',
description: 'Generate random string',
value: 'generate',
},
{
name: 'Hash',
description: 'Hash a text in a specified format.',
description: 'Hash a text in a specified format',
value: 'hash',
},
{
name: 'Hmac',
description: 'Hmac a text in a specified format.',
description: 'Hmac a text in a specified format',
value: 'hmac',
},
{
name: 'Sign',
description: 'Sign a string using a private key.',
description: 'Sign a string using a private key',
value: 'sign',
},
],
Expand Down Expand Up @@ -100,7 +115,7 @@ export class Crypto implements INodeType {
},
type: 'string',
default: '',
description: 'The value that should be hashed.',
description: 'The value that should be hashed',
required: true,
},
{
Expand All @@ -116,7 +131,7 @@ export class Crypto implements INodeType {
],
},
},
description: 'Name of the property to which to write the hash.',
description: 'Name of the property to which to write the hash',
},
{
displayName: 'Encoding',
Expand Down Expand Up @@ -187,7 +202,7 @@ export class Crypto implements INodeType {
},
type: 'string',
default: '',
description: 'The value of which the hmac should be created.',
description: 'The value of which the hmac should be created',
required: true,
},
{
Expand All @@ -203,7 +218,7 @@ export class Crypto implements INodeType {
],
},
},
description: 'Name of the property to which to write the hmac.',
description: 'Name of the property to which to write the hmac',
},
{
displayName: 'Secret',
Expand Down Expand Up @@ -255,7 +270,7 @@ export class Crypto implements INodeType {
},
type: 'string',
default: '',
description: 'The value that should be signed.',
description: 'The value that should be signed',
required: true,
},
{
Expand All @@ -271,7 +286,7 @@ export class Crypto implements INodeType {
],
},
},
description: 'Name of the property to which to write the signed value.',
description: 'Name of the property to which to write the signed value',
},
{
displayName: 'Algorithm',
Expand Down Expand Up @@ -328,10 +343,77 @@ export class Crypto implements INodeType {
typeOptions: {
alwaysOpenEditWindow: true,
},
description: 'Private key to use when signing the string.',
description: 'Private key to use when signing the string',
default: '',
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
'generate',
],
},
},
description: 'Name of the property to which to write the random string',
},
{
displayName: 'Type',
name: 'encodingType',
displayOptions: {
show: {
action: [
'generate',
],
},
},
type: 'options',
options: [
{
name: 'ASCII',
value: 'ascii',
},
{
name: 'BASE64',
value: 'base64',
},
{
name: 'HEX',
value: 'hex',
},
{
name: 'UUID',
value: 'uuid',
},
],
default: 'uuid',
description: 'Encoding that will be used to generate string',
required: true,
},
{
displayName: 'Length',
name: 'stringLength',
type: 'number',
default: 32,
description: 'Length of the generated string',
displayOptions: {
show: {
action: [
'generate',
],
encodingType: [
'ascii',
'base64',
'hex',
],
},
},
},
],
};

Expand Down Expand Up @@ -369,9 +451,22 @@ export class Crypto implements INodeType {

item = items[i];
const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string;
const value = this.getNodeParameter('value', i) as string;
const value = this.getNodeParameter('value', i, '') as string;
let newValue;

if (action === 'generate') {
const encodingType = this.getNodeParameter('encodingType', i) as string;
if (encodingType === 'uuid') {
newValue = uuid();
} else {
const stringLength = this.getNodeParameter('stringLength', i) as number;
if (encodingType === 'base64') {
newValue = randomBytes(stringLength).toString(encodingType as BufferEncoding).replace(/\W/g, '').slice(0, stringLength);
} else {
newValue = randomBytes(stringLength).toString(encodingType as BufferEncoding).slice(0, stringLength);
}
}
}
if (action === 'hash') {
const type = this.getNodeParameter('type', i) as string;
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
Expand Down Expand Up @@ -413,10 +508,10 @@ export class Crypto implements INodeType {
set(newItem, `json.${dataPropertyName}`, newValue);

returnData.push(newItem);

} catch (error) {
if (this.continueOnFail()) {
returnData.push({json:{ error: error.message }});
returnData.push({ json: { error: (error as JsonObject).message } });
continue;
}
throw error;
Expand Down