UNPKG

n8n-nodes-gpg

Version:

This package contains the GPG node for n8n. It allows you to encrypt, decrypt, sign and verify files using openpgp keys.

263 lines 12.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.Gpg = void 0; const n8n_workflow_1 = require("n8n-workflow"); const openpgp = __importStar(require("openpgp")); class Gpg { constructor() { this.description = { displayName: 'GPG', name: 'gpg', icon: 'file:gpg.svg', group: ['input'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Use GPG to encrypt or decrypt data', defaults: { name: 'GPG', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'gpgKeyApi', 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'], }, }, }, { displayName: 'Add Additional Properties', name: 'addAdditionalProperties', type: 'boolean', default: false, displayOptions: { show: { type: ['string'], }, }, }, { displayName: 'Additional Properties', name: 'additionalProperties', type: 'json', default: '', description: 'Any data you wish to add to the returned object', displayOptions: { show: { type: ['string'], addAdditionalProperties: [true], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('gpgKeyApi'); 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 addAdditionalProperties = this.getNodeParameter('addAdditionalProperties', i); const additionalProperties = this.getNodeParameter('additionalProperties', i); const data = this.getNodeParameter('text', i); const encrypted = await openpgp.encrypt({ message: await openpgp.createMessage({ text: data }), encryptionKeys: publicKey, }); responseData = addAdditionalProperties ? [ { additionalProperties, encrypted, }, ] : [ { 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 = await 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}.gpg`; items[i].binary[outputBinaryPropertyName].fileExtension = 'gpg'; 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 addAdditionalProperties = this.getNodeParameter('addAdditionalProperties', i); const additionalProperties = this.getNodeParameter('additionalProperties', i); const message = await openpgp.readMessage({ armoredMessage: this.getNodeParameter('text', i), }); const { data: decrypted } = await openpgp.decrypt({ message, decryptionKeys: privateKey, config: { allowInsecureDecryptionWithSigningKeys: true }, }); responseData = addAdditionalProperties ? [{ additionalProperties, decrypted }] : [{ 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 = await 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', config: { allowInsecureDecryptionWithSigningKeys: true }, }); const buffer = Buffer.from(decrypted); items[i].binary[outputBinaryPropertyName] = await this.helpers.prepareBinaryData(buffer); items[i].binary[outputBinaryPropertyName].fileName = `${binaryData.fileName}`; 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.Gpg = Gpg; //# sourceMappingURL=Gpg.node.js.map