mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Small modifications to Crypto-Node
This commit is contained in:
parent
81c0382b3d
commit
91688299b7
|
@ -13,6 +13,7 @@ import {
|
||||||
createHmac,
|
createHmac,
|
||||||
createSign,
|
createSign,
|
||||||
getHashes,
|
getHashes,
|
||||||
|
HexBase64Latin1Encoding,
|
||||||
} from 'crypto';
|
} from 'crypto';
|
||||||
|
|
||||||
export class Crypto implements INodeType {
|
export class Crypto implements INodeType {
|
||||||
|
@ -22,6 +23,7 @@ export class Crypto implements INodeType {
|
||||||
icon: 'fa:key',
|
icon: 'fa:key',
|
||||||
group: ['transform'],
|
group: ['transform'],
|
||||||
version: 1,
|
version: 1,
|
||||||
|
subtitle: '={{$parameter["action"]}}',
|
||||||
description: 'Provide cryptographic utilities',
|
description: 'Provide cryptographic utilities',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: 'Crypto',
|
name: 'Crypto',
|
||||||
|
@ -83,8 +85,8 @@ export class Crypto implements INodeType {
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Field Name',
|
displayName: 'Value',
|
||||||
name: 'fieldName',
|
name: 'value',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
action:[
|
action:[
|
||||||
|
@ -94,8 +96,24 @@ export class Crypto implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
|
description: 'The value that should be hashed.',
|
||||||
required: true,
|
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',
|
displayName: 'Encoding',
|
||||||
name: 'encoding',
|
name: 'encoding',
|
||||||
|
@ -108,14 +126,14 @@ export class Crypto implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
|
||||||
name: 'HEX',
|
|
||||||
value: 'hex',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'BASE64',
|
name: 'BASE64',
|
||||||
value: 'base64',
|
value: 'base64',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'HEX',
|
||||||
|
value: 'hex',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
default: 'hex',
|
default: 'hex',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -150,8 +168,8 @@ export class Crypto implements INodeType {
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Field Name',
|
displayName: 'Value',
|
||||||
name: 'fieldName',
|
name: 'value',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
action:[
|
action:[
|
||||||
|
@ -161,8 +179,24 @@ export class Crypto implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
|
description: 'The value of which the hmac should be created.',
|
||||||
required: true,
|
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',
|
displayName: 'Secret',
|
||||||
name: 'secret',
|
name: 'secret',
|
||||||
|
@ -189,21 +223,21 @@ export class Crypto implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
|
||||||
name: 'HEX',
|
|
||||||
value: 'hex',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'BASE64',
|
name: 'BASE64',
|
||||||
value: 'base64',
|
value: 'base64',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'HEX',
|
||||||
|
value: 'hex',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
default: 'hex',
|
default: 'hex',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Field Name',
|
displayName: 'Value',
|
||||||
name: 'fieldName',
|
name: 'value',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
action:[
|
action:[
|
||||||
|
@ -213,8 +247,24 @@ export class Crypto implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
|
description: 'The value that should be signed.',
|
||||||
required: true,
|
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',
|
displayName: 'Algorithm',
|
||||||
name: 'algorithm',
|
name: 'algorithm',
|
||||||
|
@ -244,14 +294,14 @@ export class Crypto implements INodeType {
|
||||||
},
|
},
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
|
||||||
name: 'HEX',
|
|
||||||
value: 'hex',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'BASE64',
|
name: 'BASE64',
|
||||||
value: 'base64',
|
value: 'base64',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'HEX',
|
||||||
|
value: 'hex',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
default: 'hex',
|
default: 'hex',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -302,48 +352,37 @@ export class Crypto implements INodeType {
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
const length = items.length as unknown as number;
|
const length = items.length as unknown as number;
|
||||||
let responseData;
|
let responseData;
|
||||||
|
const action = this.getNodeParameter('action', 0) as string;
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
const action = this.getNodeParameter('action', 0) as string;
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', i) as string;
|
||||||
|
const value = this.getNodeParameter('value', i) as string;
|
||||||
|
|
||||||
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 string;
|
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
|
||||||
const fieldName = this.getNodeParameter('fieldName', i) as string;
|
|
||||||
const clone = { ...items[i].json };
|
const clone = { ...items[i].json };
|
||||||
if (clone[fieldName] === undefined) {
|
clone[dataPropertyName] = createHash(type).update(value).digest(encoding);
|
||||||
throw new Error(`The field ${fieldName} does not exist on the input data`);
|
|
||||||
}
|
|
||||||
//@ts-ignore
|
|
||||||
clone[fieldName] = createHash(type).update(clone[fieldName] as string).digest(encoding);
|
|
||||||
responseData = clone;
|
responseData = clone;
|
||||||
}
|
}
|
||||||
if (action === 'hmac') {
|
if (action === 'hmac') {
|
||||||
const type = this.getNodeParameter('type', i) as string;
|
const type = this.getNodeParameter('type', i) as string;
|
||||||
const secret = this.getNodeParameter('secret', i) as string;
|
const secret = this.getNodeParameter('secret', i) as string;
|
||||||
const encoding = this.getNodeParameter('encoding', i) as string;
|
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
|
||||||
const fieldName = this.getNodeParameter('fieldName', i) as string;
|
|
||||||
const clone = { ...items[i].json };
|
const clone = { ...items[i].json };
|
||||||
if (clone[fieldName] === undefined) {
|
clone[dataPropertyName] = createHmac(type, secret).update(value).digest(encoding);
|
||||||
throw new Error(`The field ${fieldName} does not exist on the input data`);
|
|
||||||
}
|
|
||||||
//@ts-ignore
|
|
||||||
clone[fieldName] = createHmac(type, secret).update(clone[fieldName] as string).digest(encoding);
|
|
||||||
responseData = clone;
|
responseData = clone;
|
||||||
}
|
}
|
||||||
if (action === 'sign') {
|
if (action === 'sign') {
|
||||||
const algorithm = this.getNodeParameter('algorithm', i) as string;
|
const algorithm = this.getNodeParameter('algorithm', i) as string;
|
||||||
const fieldName = this.getNodeParameter('fieldName', i) as string;
|
const encoding = this.getNodeParameter('encoding', i) as HexBase64Latin1Encoding;
|
||||||
const encoding = this.getNodeParameter('encoding', i) as string;
|
const privateKey = this.getNodeParameter('privateKey', i) as string;
|
||||||
const privateKey = this.getNodeParameter('privateKey', i) as IDataObject;
|
|
||||||
const clone = { ...items[i].json };
|
const clone = { ...items[i].json };
|
||||||
if (clone[fieldName] === undefined) {
|
const sign = createSign(algorithm);
|
||||||
throw new Error(`The field ${fieldName} does not exist on the input data`);
|
sign.write(value as string);
|
||||||
}
|
|
||||||
const sign = createSign(algorithm)
|
|
||||||
sign.write(clone[fieldName] as string);
|
|
||||||
sign.end();
|
sign.end();
|
||||||
//@ts-ignore
|
|
||||||
const signature = sign.sign(privateKey, encoding);
|
const signature = sign.sign(privateKey, encoding);
|
||||||
clone[fieldName] = signature;
|
clone[dataPropertyName] = signature;
|
||||||
responseData = clone;
|
responseData = clone;
|
||||||
}
|
}
|
||||||
if (Array.isArray(responseData)) {
|
if (Array.isArray(responseData)) {
|
||||||
|
|
Loading…
Reference in a new issue