feat(Crypto Node): Add Generate operation to generate random values (#2541)

*  Add generate action to crypto node

*  small fixes, nodelinter issues fixes

*  Improvements

*  Fix order

Co-authored-by: michael-radency <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
pemontto 2022-03-27 09:48:31 +01:00 committed by GitHub
parent ab08c0df15
commit b5ecccb840
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,18 @@
import { set } from 'lodash'; import {
import { IExecuteFunctions } from 'n8n-core'; set,
} from 'lodash';
import {
IExecuteFunctions,
} from 'n8n-core';
import { import {
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions, INodePropertyOptions,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
JsonObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
@ -14,8 +21,11 @@ import {
createHmac, createHmac,
createSign, createSign,
getHashes, getHashes,
randomBytes,
} from 'crypto'; } from 'crypto';
import { v4 as uuid } from 'uuid';
export class Crypto implements INodeType { export class Crypto implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Crypto', displayName: 'Crypto',
@ -37,19 +47,24 @@ export class Crypto implements INodeType {
name: 'action', name: 'action',
type: 'options', type: 'options',
options: [ options: [
{
name: 'Generate',
description: 'Generate random string',
value: 'generate',
},
{ {
name: 'Hash', name: 'Hash',
description: 'Hash a text in a specified format.', description: 'Hash a text in a specified format',
value: 'hash', value: 'hash',
}, },
{ {
name: 'Hmac', name: 'Hmac',
description: 'Hmac a text in a specified format.', description: 'Hmac a text in a specified format',
value: 'hmac', value: 'hmac',
}, },
{ {
name: 'Sign', name: 'Sign',
description: 'Sign a string using a private key.', description: 'Sign a string using a private key',
value: 'sign', value: 'sign',
}, },
], ],
@ -100,7 +115,7 @@ export class Crypto implements INodeType {
}, },
type: 'string', type: 'string',
default: '', default: '',
description: 'The value that should be hashed.', description: 'The value that should be hashed',
required: true, required: true,
}, },
{ {
@ -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', displayName: 'Encoding',
@ -187,7 +202,7 @@ export class Crypto implements INodeType {
}, },
type: 'string', type: 'string',
default: '', default: '',
description: 'The value of which the hmac should be created.', description: 'The value of which the hmac should be created',
required: true, required: true,
}, },
{ {
@ -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', displayName: 'Secret',
@ -255,7 +270,7 @@ export class Crypto implements INodeType {
}, },
type: 'string', type: 'string',
default: '', default: '',
description: 'The value that should be signed.', description: 'The value that should be signed',
required: true, required: true,
}, },
{ {
@ -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', displayName: 'Algorithm',
@ -328,10 +343,77 @@ export class Crypto implements INodeType {
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
description: 'Private key to use when signing the string.', description: 'Private key to use when signing the string',
default: '', default: '',
required: true, 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',
],
},
},
},
], ],
}; };
@ -369,9 +451,22 @@ export class Crypto implements INodeType {
item = items[i]; item = items[i];
const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string; 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; 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') { if (action === 'hash') {
const type = this.getNodeParameter('type', i) as string; const type = this.getNodeParameter('type', i) as string;
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding; const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
@ -416,7 +511,7 @@ export class Crypto implements INodeType {
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnData.push({json:{ error: error.message }}); returnData.push({ json: { error: (error as JsonObject).message } });
continue; continue;
} }
throw error; throw error;