n8n-nodes-crypto-js
Version:
Use crypto.js in n8n
378 lines • 16.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 = require('node:crypto');
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 EncryptOutputType;
(function (EncryptOutputType) {
EncryptOutputType["Base64"] = "base64";
EncryptOutputType["Hex"] = "hex";
})(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: 'Sign',
description: 'Sign a string using a private key',
value: 'sign',
action: 'Sign a string using a private key',
},
{
name: 'Encrypted Symmetric Key',
description: 'Create an encrypted symmetric key using a private key',
value: 'encryptedSymmetricKey',
action: 'Create an encrypted symmetric key 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: 'Base64 Decode',
description: 'Base64 decode a string',
value: 'base64Decode',
action: 'Base64 decode a string',
},
{
name: 'Verify',
description: 'Verify data using a key and a signature',
value: 'verify',
action: 'Verify data using a key and a signature',
},
],
default: 'decrypt',
},
{
displayName: 'Key Type',
name: 'keyType',
type: 'options',
options: [
{
name: 'Private',
value: 'private',
action: 'Private',
},
{
name: 'Certificate',
value: 'certificate',
action: 'Certificate',
},
],
displayOptions: {
show: {
action: ['verify'],
},
},
default: 'private',
},
{
displayName: 'Certificate',
name: 'certificate',
type: 'string',
default: '',
description: 'The certificate that should be used to verify the input value',
required: true,
displayOptions: {
show: {
keyType: ['certificate'],
},
},
},
{
displayName: 'Signature',
name: 'signature',
type: 'string',
default: '',
description: 'The signature that should be used to verify the input value',
required: true,
displayOptions: {
show: {
action: ['verify'],
},
},
},
{
displayName: 'Custom Passphrase',
name: 'customPassphrase',
type: 'string',
default: '',
description: 'The passphrase that should be used to encrypt the input value',
hint: 'If not provided, the passphrase from the credentials will be used',
displayOptions: {
show: {
action: ['encrypt', 'decrypt'],
},
},
},
{
displayName: 'Input Value',
name: 'inputValue',
type: 'string',
default: '',
description: 'The value that should be encrypted',
required: true,
displayOptions: {
hide: {
action: ['encryptedSymmetricKey'],
},
},
},
{
displayName: 'Encrypt Output Type',
name: 'encryptOutputType',
type: 'options',
options: [
{
name: 'Base64',
value: 'base64',
action: 'Base64',
},
{
name: 'Hex',
value: 'hex',
action: 'Hex',
},
],
displayOptions: {
show: {
action: ['sign', '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'],
},
},
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 customPassphrase = this.getNodeParameter('customPassphrase', 0, '');
const { passphrase: defaultPassphrase, privateKey } = await this.getCredentials('cryptoJsCredentialsApi');
const passphrase = customPassphrase || defaultPassphrase;
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 stringToEncrypt = typeof inputValue === 'object' ? JSON.stringify(inputValue) : inputValue;
const encryptedData = crypto_js_1.AES.encrypt(stringToEncrypt, passphrase);
newValue = encryptedData.toString();
}
if (action === 'encryptPrivate') {
const encryptOutputType = this.getNodeParameter('encryptOutputType', i);
const key = new node_rsa_1.default(privateKey);
const stringToEncrypt = typeof inputValue === 'object' ? JSON.stringify(inputValue) : inputValue;
const encryptedSymmetricKey = key.encryptPrivate(stringToEncrypt, encryptOutputType);
newValue = encryptedSymmetricKey;
}
if (action === 'sign') {
if (!privateKey) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Private key is required');
}
const encryptOutputType = this.getNodeParameter('encryptOutputType', i);
const key = new node_rsa_1.default(privateKey);
const stringToEncrypt = typeof inputValue === 'object' ? JSON.stringify(inputValue) : inputValue;
const signedData = key.sign(stringToEncrypt, encryptOutputType);
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();
}
if (action === 'encryptedSymmetricKey') {
if (!privateKey) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Private key is required');
}
if (!passphrase) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Passphrase is required');
}
const key = new node_rsa_1.default(privateKey);
const encryptedSymmetricKey = key.encryptPrivate(passphrase, 'base64');
newValue = encryptedSymmetricKey;
}
if (action === 'verify') {
if (!privateKey) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Public key is required');
}
const keyType = this.getNodeParameter('keyType', i);
let key = new node_rsa_1.default(privateKey);
if (keyType === 'certificate') {
const certificate = this.getNodeParameter('certificate', i);
const x509Cert = new crypto.X509Certificate(certificate);
let keyData = x509Cert.publicKey.export({
format: "pem",
type: "pkcs1"
});
key = new node_rsa_1.default(keyData);
}
const signature = this.getNodeParameter('signature', i);
const verified = key.verify(Buffer.from(inputValue), signature, "utf8", "base64");
newValue = verified;
}
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