n8n/packages/nodes-base/nodes/Crypto/Crypto.node.ts
Iván Ovejero 70ae90fa3c
refactor: Apply more eslint-plugin-n8n-nodes-base autofixable rules (#3432)
*  Update `lintfix` script

* 👕 Remove unneeded lint exceptions

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-description-miscased-url` (#3441)

* 👕 Apply `rule node-param-placeholder-miscased-id` (#3443)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-name-wrong-for-upsert` (#3446)

* 👕 Apply `node-param-min-value-wrong-for-limit` (#3442)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-wrong-for-dynamic-options` (#3454)

* 🔨 fix

*  Fix `Assigned To` fields

Co-authored-by: Michael Kret <michael.k@radency.com>

* 👕 Apply `rule node-param-default-wrong-for-number` (#3453)

* 👕 Apply `node-param-default-wrong-for-string` (#3452)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-miscased` (#3449)

* 🔨 fix

* 🔨 exceptions

*  review fixes

* 👕 Apply `node-param-description-lowercase-first-char` (#3451)

*  fix

*  review fixes

*  fix

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-description-wrong-for-dynamic-options` (#3456)

* Rule working as intended

* Add rule

* 🔥 Remove repetitions

* 👕 Add exceptions

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Small fix for `node-param-description-wrong-for-dynamic-options`

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3460)

* 👕 Apply `node-param-description-line-break-html-tag` (#3462)

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-options-type-unsorted-items` (#3459)

*  fix

* 🔨 exceptions

* Add exception for Salesmate and Zoom

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

*  Restore `lintfix` command

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 19:23:49 +02:00

536 lines
10 KiB
TypeScript

import {
set,
} from 'lodash';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
} from 'n8n-workflow';
import {
BinaryToTextEncoding,
createHash,
createHmac,
createSign,
getHashes,
randomBytes,
} from 'crypto';
import { v4 as uuid } from 'uuid';
export class Crypto implements INodeType {
description: INodeTypeDescription = {
displayName: 'Crypto',
name: 'crypto',
icon: 'fa:key',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["action"]}}',
description: 'Provide cryptographic utilities',
defaults: {
name: 'Crypto',
color: '#408000',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Action',
name: 'action',
type: 'options',
options: [
{
name: 'Generate',
description: 'Generate random string',
value: 'generate',
},
{
name: 'Hash',
description: 'Hash a text in a specified format',
value: 'hash',
},
{
name: 'Hmac',
description: 'Hmac a text in a specified format',
value: 'hmac',
},
{
name: 'Sign',
description: 'Sign a string using a private key',
value: 'sign',
},
],
default: 'hash',
},
{
displayName: 'Type',
name: 'type',
displayOptions: {
show: {
action: [
'hash',
],
},
},
type: 'options',
options: [
{
name: 'MD5',
value: 'MD5',
},
{
name: 'SHA256',
value: 'SHA256',
},
{
name: 'SHA384',
value: 'SHA384',
},
{
name: 'SHA512',
value: 'SHA512',
},
],
default: 'MD5',
description: 'The hash type to use',
required: true,
},
{
displayName: 'Value',
name: 'value',
displayOptions: {
show: {
action: [
'hash',
],
},
},
type: 'string',
default: '',
description: 'The value that should be hashed',
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
'hash',
],
},
},
description: 'Name of the property to which to write the hash',
},
{
displayName: 'Encoding',
name: 'encoding',
displayOptions: {
show: {
action: [
'hash',
],
},
},
type: 'options',
options: [
{
name: 'BASE64',
value: 'base64',
},
{
name: 'HEX',
value: 'hex',
},
],
default: 'hex',
required: true,
},
{
displayName: 'Type',
name: 'type',
displayOptions: {
show: {
action: [
'hmac',
],
},
},
type: 'options',
options: [
{
name: 'MD5',
value: 'MD5',
},
{
name: 'SHA256',
value: 'SHA256',
},
{
name: 'SHA384',
value: 'SHA384',
},
{
name: 'SHA512',
value: 'SHA512',
},
],
default: 'MD5',
description: 'The hash type to use',
required: true,
},
{
displayName: 'Value',
name: 'value',
displayOptions: {
show: {
action: [
'hmac',
],
},
},
type: 'string',
default: '',
description: 'The value of which the hmac should be created',
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
'hmac',
],
},
},
description: 'Name of the property to which to write the hmac',
},
{
displayName: 'Secret',
name: 'secret',
displayOptions: {
show: {
action: [
'hmac',
],
},
},
type: 'string',
default: '',
required: true,
},
{
displayName: 'Encoding',
name: 'encoding',
displayOptions: {
show: {
action: [
'hmac',
],
},
},
type: 'options',
options: [
{
name: 'BASE64',
value: 'base64',
},
{
name: 'HEX',
value: 'hex',
},
],
default: 'hex',
required: true,
},
{
displayName: 'Value',
name: 'value',
displayOptions: {
show: {
action: [
'sign',
],
},
},
type: 'string',
default: '',
description: 'The value that should be signed',
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
'sign',
],
},
},
description: 'Name of the property to which to write the signed value',
},
{
displayName: 'Algorithm Name or ID',
name: 'algorithm',
displayOptions: {
show: {
action: [
'sign',
],
},
},
type: 'options',
typeOptions: {
loadOptionsMethod: 'getHashes',
},
default: '',
required: true,
},
{
displayName: 'Encoding',
name: 'encoding',
displayOptions: {
show: {
action: [
'sign',
],
},
},
type: 'options',
options: [
{
name: 'BASE64',
value: 'base64',
},
{
name: 'HEX',
value: 'hex',
},
],
default: 'hex',
required: true,
},
{
displayName: 'Private Key',
name: 'privateKey',
displayOptions: {
show: {
action: [
'sign',
],
},
},
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
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',
],
},
},
},
],
};
methods = {
loadOptions: {
// Get all the hashes to display them to user so that he can
// select them easily
async getHashes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const hashes = getHashes();
for (const hash of hashes) {
const hashName = hash;
const hashId = hash;
returnData.push({
name: hashName,
value: hashId,
});
}
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const length = items.length;
const action = this.getNodeParameter('action', 0) as string;
let item: INodeExecutionData;
for (let i = 0; i < length; i++) {
try {
item = items[i];
const dataPropertyName = this.getNodeParameter('dataPropertyName', 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;
newValue = createHash(type).update(value).digest(encoding);
}
if (action === 'hmac') {
const type = this.getNodeParameter('type', i) as string;
const secret = this.getNodeParameter('secret', i) as string;
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
newValue = createHmac(type, secret).update(value).digest(encoding);
}
if (action === 'sign') {
const algorithm = this.getNodeParameter('algorithm', i) as string;
const encoding = this.getNodeParameter('encoding', i) as BinaryToTextEncoding;
const privateKey = this.getNodeParameter('privateKey', i) as string;
const sign = createSign(algorithm);
sign.write(value as string);
sign.end();
newValue = sign.sign(privateKey, encoding);
}
let newItem: INodeExecutionData;
if (dataPropertyName.includes('.')) {
// Uses dot notation so copy all data
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
pairedItem: {
item: i,
},
};
} else {
// Does not use dot notation so shallow copy is enough
newItem = {
json: { ...item.json },
pairedItem: {
item: i,
},
};
}
if (item.binary !== undefined) {
newItem.binary = item.binary;
}
set(newItem, `json.${dataPropertyName}`, newValue);
returnData.push(newItem);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: (error as JsonObject).message,
},
pairedItem: {
item: i,
},
});
continue;
}
throw error;
}
}
return this.prepareOutputData(returnData);
}
}