UNPKG

n8n-nodes-aes

Version:

n8n community node to encrypt and decrypt data using crypto-js.

224 lines 10.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pgp = void 0; const n8n_workflow_1 = require("n8n-workflow"); const openpgp = __importStar(require("openpgp")); class Pgp { constructor() { this.description = { displayName: 'PGP', name: 'PGP', icon: 'file:pgplogo.svg', group: ['input'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Use PGP to encrypt or decrypt data', defaults: { name: 'PGP', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'pgpKey', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Encrypt', value: 'encrypt', }, { name: 'Decrypt', value: 'decrypt', }, ], default: 'encrypt', description: 'Which operation to use?', }, { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'File', value: 'file', }, { name: 'String', value: 'string', }, ], default: 'file', }, { displayName: 'Text', name: 'text', type: 'string', default: '', displayOptions: { show: { type: ['string'], }, }, }, { displayName: 'Binary Property', name: 'binaryPropertyName', type: 'string', default: 'data', displayOptions: { show: { type: ['file'], }, }, }, { displayName: 'Output Property', name: 'outputBinaryPropertyName', type: 'string', default: '', displayOptions: { show: { type: ['file'], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('pgpKey'); let responseData; for (let i = 0; i < items.length; i++) { const dataType = this.getNodeParameter('type', i); try { if (operation === 'encrypt') { const publicKey = await openpgp.readKey({ armoredKey: credentials.key }); if (dataType === 'string') { const data = this.getNodeParameter('text', i); const encrypted = await openpgp.encrypt({ message: await openpgp.createMessage({ text: data }), encryptionKeys: publicKey, }); responseData = [{ encrypted }]; } if (dataType === 'file') { const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); const outputBinaryPropertyName = this.getNodeParameter('outputBinaryPropertyName', i, 'encrypted'); const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName); let pgpData; if (binaryData.id) { pgpData = this.helpers.getBinaryStream(binaryData.id); } else { pgpData = Buffer.from(binaryData.data, n8n_workflow_1.BINARY_ENCODING); } const encrypted = await openpgp.encrypt({ message: await openpgp.createMessage({ binary: pgpData }), encryptionKeys: publicKey, format: 'binary', }); const buffer = Buffer.from(encrypted); items[i].binary[outputBinaryPropertyName] = await this.helpers.prepareBinaryData(buffer); items[i].binary[outputBinaryPropertyName].fileName = `${binaryData.fileName}.pgp`; items[i].binary[outputBinaryPropertyName].fileExtension = 'pgp'; items[i].binary[outputBinaryPropertyName].mimeType = 'application/pgp-encrypted'; responseData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(items[i]), { itemData: { item: i } }); } } if (operation === 'decrypt') { const privateKey = await openpgp.decryptKey({ privateKey: await openpgp.readPrivateKey({ armoredKey: credentials.key }), passphrase: credentials.passphrase, }); if (dataType === 'string') { const message = await openpgp.readMessage({ armoredMessage: this.getNodeParameter('text', i), }); const { data: decrypted } = await openpgp.decrypt({ message, decryptionKeys: privateKey, }); responseData = [{ decrypted }]; } if (dataType === 'file') { const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); const outputBinaryPropertyName = this.getNodeParameter('outputBinaryPropertyName', i, 'decrypted'); const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName); let pgpData; if (binaryData.id) { pgpData = this.helpers.getBinaryStream(binaryData.id); } else { pgpData = Buffer.from(binaryData.data, n8n_workflow_1.BINARY_ENCODING); } const encryptedMessage = await openpgp.readMessage({ binaryMessage: pgpData, }); const { data: decrypted } = await openpgp.decrypt({ message: encryptedMessage, decryptionKeys: privateKey, format: 'binary', }); const buffer = Buffer.from(decrypted); items[i].binary[outputBinaryPropertyName] = await this.helpers.prepareBinaryData(buffer); let tempFileName = binaryData.fileName || ""; if (tempFileName.endsWith('.pgp')) { tempFileName = tempFileName === null || tempFileName === void 0 ? void 0 : tempFileName.slice(0, -4); } items[i].binary[outputBinaryPropertyName].fileName = `${tempFileName}`; responseData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(items[i]), { itemData: { item: i } }); } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } } return this.prepareOutputData(returnData); } } exports.Pgp = Pgp; //# sourceMappingURL=Pgp.node.js.map