n8n/packages/nodes-base/nodes/Crypto.node.ts

417 lines
8 KiB
TypeScript
Raw Normal View History

2020-02-07 22:55:58 -08:00
import { set } from 'lodash';
2020-01-28 14:39:43 -08:00
import { IExecuteFunctions } from 'n8n-core';
import {
ILoadOptionsFunctions,
2020-01-28 14:39:43 -08:00
INodeExecutionData,
INodePropertyOptions,
2020-01-28 14:39:43 -08:00
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
createHash,
createHmac,
createSign,
getHashes,
HexBase64Latin1Encoding,
2020-01-28 14:39:43 -08:00
} from 'crypto';
export class Crypto implements INodeType {
description: INodeTypeDescription = {
displayName: 'Crypto',
name: 'crypto',
icon: 'fa:key',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["action"]}}',
2020-01-28 14:39:43 -08:00
description: 'Provide cryptographic utilities',
defaults: {
name: 'Crypto',
color: '#408000',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Action',
name: 'action',
type: 'options',
options: [
{
name: 'Hash',
description: 'Hash a text in a specified format.',
2020-10-22 06:46:03 -07:00
value: 'hash',
2020-01-28 14:39:43 -08:00
},
{
name: 'Hmac',
description: 'Hmac a text in a specified format.',
2020-10-22 06:46:03 -07:00
value: 'hmac',
2020-01-28 14:39:43 -08:00
},
{
name: 'Sign',
description: 'Sign a string using a private key.',
2020-10-22 06:46:03 -07:00
value: 'sign',
2020-01-28 14:39:43 -08:00
},
],
default: 'hash',
},
{
displayName: 'Type',
name: 'type',
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'hash',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'options',
options: [
{
name: 'MD5',
value: 'MD5',
},
{
name: 'SHA256',
value: 'SHA256',
},
{
name: 'SHA384',
value: 'SHA384',
},
2020-01-28 14:39:43 -08:00
{
name: 'SHA512',
value: 'SHA512',
},
],
default: 'MD5',
description: 'The hash type to use',
required: true,
},
{
displayName: 'Value',
name: 'value',
2020-01-28 14:39:43 -08:00
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'hash',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'string',
default: '',
description: 'The value that should be hashed.',
2020-01-28 14:39:43 -08:00
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
2020-10-22 06:46:03 -07:00
'hash',
],
},
},
description: 'Name of the property to which to write the hash.',
},
2020-01-28 14:39:43 -08:00
{
displayName: 'Encoding',
name: 'encoding',
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'hash',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'options',
options: [
{
name: 'BASE64',
value: 'base64',
},
{
name: 'HEX',
value: 'hex',
},
2020-01-28 14:39:43 -08:00
],
default: 'hex',
required: true,
},
{
displayName: 'Type',
name: 'type',
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'hmac',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'options',
options: [
{
name: 'MD5',
value: 'MD5',
},
{
name: 'SHA256',
value: 'SHA256',
},
{
name: 'SHA384',
value: 'SHA384',
},
2020-01-28 14:39:43 -08:00
{
name: 'SHA512',
value: 'SHA512',
},
],
default: 'MD5',
description: 'The hash type to use',
required: true,
},
{
displayName: 'Value',
name: 'value',
2020-01-28 14:39:43 -08:00
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'hmac',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'string',
default: '',
description: 'The value of which the hmac should be created.',
2020-01-28 14:39:43 -08:00
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
2020-10-22 06:46:03 -07:00
'hmac',
],
},
},
description: 'Name of the property to which to write the hmac.',
},
2020-01-28 14:39:43 -08:00
{
displayName: 'Secret',
name: 'secret',
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'hmac',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'string',
default: '',
required: true,
},
{
displayName: 'Encoding',
name: 'encoding',
displayOptions: {
show: {
action:[
'hmac',
],
},
},
type: 'options',
options: [
{
name: 'BASE64',
value: 'base64',
},
{
name: 'HEX',
value: 'hex',
},
2020-01-28 14:39:43 -08:00
],
default: 'hex',
required: true,
},
{
displayName: 'Value',
name: 'value',
2020-01-28 14:39:43 -08:00
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'sign',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'string',
default: '',
description: 'The value that should be signed.',
2020-01-28 14:39:43 -08:00
required: true,
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
action: [
2020-10-22 06:46:03 -07:00
'sign',
],
},
},
description: 'Name of the property to which to write the signed value.',
},
2020-01-28 14:39:43 -08:00
{
displayName: 'Algorithm',
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',
},
2020-01-28 14:39:43 -08:00
],
default: 'hex',
required: true,
},
{
displayName: 'Private Key',
name: 'privateKey',
displayOptions: {
show: {
action:[
2020-10-22 06:46:03 -07:00
'sign',
2020-01-28 14:39:43 -08:00
],
},
},
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
description: 'Private key to use when signing the string.',
default: '',
required: true,
},
],
};
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;
},
2020-10-22 06:46:03 -07:00
},
2020-01-28 14:39:43 -08:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
2020-02-07 22:55:58 -08:00
const returnData: INodeExecutionData[] = [];
2020-01-28 14:39:43 -08:00
const length = items.length as unknown as number;
const action = this.getNodeParameter('action', 0) as string;
2020-02-07 22:55:58 -08:00
let item: INodeExecutionData;
2020-01-28 14:39:43 -08:00
for (let i = 0; i < length; i++) {
2020-02-07 22:55:58 -08:00
item = items[i];
const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string;
const value = this.getNodeParameter('value', i) as string;
2020-02-07 22:55:58 -08:00
let newValue;
2020-01-28 14:39:43 -08:00
if (action === 'hash') {
const type = this.getNodeParameter('type', i) as string;
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
2020-02-07 22:55:58 -08:00
newValue = createHash(type).update(value).digest(encoding);
2020-01-28 14:39:43 -08:00
}
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 HexBase64Latin1Encoding;
2020-02-07 22:55:58 -08:00
newValue = createHmac(type, secret).update(value).digest(encoding);
2020-01-28 14:39:43 -08:00
}
if (action === 'sign') {
const algorithm = this.getNodeParameter('algorithm', i) as string;
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
const privateKey = this.getNodeParameter('privateKey', i) as string;
const sign = createSign(algorithm);
sign.write(value as string);
2020-01-28 14:39:43 -08:00
sign.end();
2020-02-07 22:55:58 -08:00
newValue = sign.sign(privateKey, encoding);
2020-01-28 14:39:43 -08:00
}
2020-02-07 22:55:58 -08:00
let newItem: INodeExecutionData;
if (dataPropertyName.includes('.')) {
// Uses dot notation so copy all data
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
};
2020-01-28 14:39:43 -08:00
} else {
2020-02-07 22:55:58 -08:00
// Does not use dot notation so shallow copy is enough
newItem = {
json: { ...item.json },
};
}
if (item.binary !== undefined) {
newItem.binary = item.binary;
2020-01-28 14:39:43 -08:00
}
2020-02-07 22:55:58 -08:00
set(newItem, `json.${dataPropertyName}`, newValue);
returnData.push(newItem);
2020-01-28 14:39:43 -08:00
}
2020-02-07 22:55:58 -08:00
return this.prepareOutputData(returnData);
2020-01-28 14:39:43 -08:00
}
}