n8n-node-crypto-js
Version:
Use crypto.js in n8n
297 lines • 12.5 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CryptoJs = void 0;
const crypto_js_1 = require("crypto-js");
const set_1 = __importDefault(require("lodash/set"));
const n8n_workflow_1 = require("n8n-workflow");
const node_rsa_1 = __importDefault(require("node-rsa"));
var InputValueType;
(function (InputValueType) {
InputValueType["String"] = "string";
InputValueType["Json"] = "json";
InputValueType["Binary"] = "binary";
})(InputValueType || (InputValueType = {}));
var EncryptOutputType;
(function (EncryptOutputType) {
EncryptOutputType["Base64"] = "base64";
})(EncryptOutputType || (EncryptOutputType = {}));
var DecryptOutputType;
(function (DecryptOutputType) {
DecryptOutputType["String"] = "string";
DecryptOutputType["Json"] = "json";
})(DecryptOutputType || (DecryptOutputType = {}));
class CryptoJs {
constructor() {
this.description = {
displayName: 'Crypto Js',
name: 'cryptoJs',
icon: 'fa:key',
iconColor: 'green',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["action"]}}',
description: 'JavaScript library of crypto standards.',
defaults: {
name: 'CryptoJs',
color: '#408000',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'cryptoJsCredentialsApi',
required: true,
},
],
properties: [
{
displayName: 'Action',
name: 'action',
type: 'options',
options: [
{
name: 'Encrypt',
description: 'Encrypt a string using a passphrase',
value: 'encrypt',
action: 'Encrypt a string using a passphrase',
},
{
name: 'Encrypt Private',
description: 'Encrypt a string using a private key',
value: 'encryptPrivate',
action: 'Encrypt a string using a private key',
},
{
name: 'Decrypt',
description: 'Decrypt a string using a private key',
value: 'decrypt',
action: 'Decrypt a string using a private key',
},
{
name: 'Decrypt Public',
description: 'Decrypt a string using a public key',
value: 'decryptPublic',
action: 'Decrypt a string using a public key',
},
{
name: 'Sign',
description: 'Sign a string using a private key',
value: 'sign',
action: 'Sign a string using a private key',
},
{
name: 'Base64 Decode',
description: 'Base64 decode a string',
value: 'base64Decode',
action: 'Base64 decode a string',
},
],
default: 'decrypt',
},
{
displayName: 'Input Type',
name: 'inputType',
type: 'options',
options: [
{
name: 'String',
value: 'string',
action: 'String',
},
{
name: 'Json',
value: 'json',
action: 'Json',
},
],
displayOptions: {
show: {
action: ['encrypt', 'encryptPrivate', 'sign'],
},
},
default: 'string',
},
{
displayName: 'Input Value',
name: 'inputValue',
type: 'string',
typeOptions: {
editor: 'htmlEditor',
rows: 10,
},
default: '',
description: 'The value that should be encrypted',
required: true,
},
{
displayName: 'Encrypt Output Type',
name: 'encryptOutputType',
type: 'options',
options: [
{
name: 'Base64',
value: 'base64',
action: 'Base64',
},
],
displayOptions: {
show: {
action: ['encrypt', 'encryptPrivate'],
},
},
default: 'base64',
},
{
displayName: 'Decrypt Output Type',
name: 'decryptOutputType',
type: 'options',
options: [
{
name: 'String',
value: 'string',
action: 'String',
},
{
name: 'Json',
value: 'json',
action: 'Json',
},
],
displayOptions: {
show: {
action: ['decrypt', 'decryptPublic', 'sign'],
},
},
default: 'string',
},
{
displayName: 'Output Property Name',
name: 'outputPropertyName',
type: 'string',
default: 'data',
required: true,
description: 'Name of the property to which to write the decrypted value',
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const length = items.length;
const action = this.getNodeParameter('action', 0);
const { passphrase, privateKey } = await this.getCredentials('cryptoJsCredentialsApi');
let item;
for (let i = 0; i < length; i++) {
try {
item = items[i];
const outputPropertyName = this.getNodeParameter('outputPropertyName', i);
const inputValue = this.getNodeParameter('inputValue', i, '');
let newValue;
let binaryProcessed = false;
if (action === 'encrypt') {
if (!passphrase) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Passphrase is required');
}
const encryptOutputType = this.getNodeParameter('encryptOutputType', i);
const inputType = this.getNodeParameter('inputType', i);
const stringToEncrypt = inputType === InputValueType.String ? inputValue : JSON.stringify(inputValue);
const encryptedData = crypto_js_1.AES.encrypt(stringToEncrypt, passphrase);
if (encryptOutputType === EncryptOutputType.Base64) {
newValue = encryptedData.toString();
}
}
if (action === 'encryptPrivate') {
const inputType = this.getNodeParameter('inputType', i);
const key = new node_rsa_1.default(privateKey);
const stringToEncrypt = inputType === InputValueType.String ? inputValue : JSON.stringify(inputValue);
const encryptedSymmetricKey = key.encryptPrivate(stringToEncrypt, 'base64');
newValue = encryptedSymmetricKey;
}
if (action === 'sign') {
if (!privateKey) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Private key is required');
}
const inputType = this.getNodeParameter('inputType', i);
const key = new node_rsa_1.default(privateKey);
const stringToEncrypt = inputType === InputValueType.String ? inputValue : JSON.stringify(inputValue);
const signedData = key.sign(stringToEncrypt, 'base64');
newValue = signedData;
}
if (action === 'decrypt') {
if (!passphrase) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Passphrase is required');
}
const outputType = this.getNodeParameter('decryptOutputType', i);
const decryptedData = crypto_js_1.AES.decrypt(inputValue, passphrase);
const decryptedString = decryptedData.toString(crypto_js_1.enc.Utf8);
if (outputType === DecryptOutputType.String) {
newValue = decryptedString;
}
else if (outputType === DecryptOutputType.Json) {
newValue = JSON.parse(decryptedString);
}
}
if (action === 'decryptPublic') {
if (!privateKey) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Private key is required');
}
const outputType = this.getNodeParameter('decryptOutputType', i);
const key = new node_rsa_1.default(privateKey);
const decryptedString = key.decryptPublic(inputValue, "utf8");
if (outputType === DecryptOutputType.String) {
newValue = decryptedString;
}
else if (outputType === DecryptOutputType.Json) {
newValue = JSON.parse(decryptedString);
}
}
if (action === 'base64Decode') {
newValue = Buffer.from(inputValue, 'base64').toString();
}
let newItem;
if (outputPropertyName.includes('.')) {
newItem = {
json: (0, n8n_workflow_1.deepCopy)(item.json),
pairedItem: {
item: i,
},
};
}
else {
newItem = {
json: { ...item.json },
pairedItem: {
item: i,
},
};
}
if (item.binary !== undefined && !binaryProcessed) {
newItem.binary = item.binary;
}
(0, set_1.default)(newItem, ['json', outputPropertyName], newValue);
returnData.push(newItem);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
pairedItem: {
item: i,
},
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.CryptoJs = CryptoJs;
//# sourceMappingURL=CryptoJs.node.js.map