UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

191 lines 7.29 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Totp = void 0; const n8n_workflow_1 = require("n8n-workflow"); const OTPAuth = __importStar(require("otpauth")); class Totp { description = { displayName: 'TOTP', name: 'totp', icon: 'fa:fingerprint', group: ['transform'], version: 1, subtitle: '={{ $parameter["operation"] }}', description: 'Generate a time-based one-time password', defaults: { name: 'TOTP', }, usableAsTool: true, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'totpApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Generate Secret', value: 'generateSecret', action: 'Generate secret', }, ], default: 'generateSecret', }, { displayName: 'Options', name: 'options', type: 'collection', displayOptions: { show: { operation: ['generateSecret'], }, }, default: {}, placeholder: 'Add option', options: [ { displayName: 'Algorithm', name: 'algorithm', type: 'options', default: 'SHA1', description: 'HMAC hashing algorithm. Defaults to SHA1.', options: [ { name: 'SHA1', value: 'SHA1', }, { name: 'SHA224', value: 'SHA224', }, { name: 'SHA256', value: 'SHA256', }, { name: 'SHA3-224', value: 'SHA3-224', }, { name: 'SHA3-256', value: 'SHA3-256', }, { name: 'SHA3-384', value: 'SHA3-384', }, { name: 'SHA3-512', value: 'SHA3-512', }, { name: 'SHA384', value: 'SHA384', }, { name: 'SHA512', value: 'SHA512', }, ], }, { displayName: 'Digits', name: 'digits', type: 'number', default: 6, description: 'Number of digits in the generated TOTP code. Defaults to 6 digits.', }, { displayName: 'Period', name: 'period', type: 'number', default: 30, description: 'How many seconds the generated TOTP code is valid for. Defaults to 30 seconds.', }, ], }, ], }; async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('totpApi'); if (credentials.label && !credentials.label.includes(':')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Malformed label - expected `issuer:username`'); } const options = this.getNodeParameter('options', 0); if (!options.algorithm) options.algorithm = 'SHA1'; if (!options.digits) options.digits = 6; if (!options.period) options.period = 30; const issuer = credentials.label ? credentials.label.split(':')[0] : undefined; const totpConfig = { secret: credentials.secret, algorithm: options.algorithm, digits: options.digits, period: options.period, }; if (issuer) { totpConfig.issuer = issuer; } if (credentials.label) { totpConfig.label = credentials.label; } const totp = new OTPAuth.TOTP(totpConfig); const token = totp.generate(); const secondsRemaining = (options.period * (1 - ((Date.now() / 1000 / options.period) % 1))) | 0; if (operation === 'generateSecret') { for (let i = 0; i < items.length; i++) { const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ token, secondsRemaining }), { itemData: { item: i } }); returnData.push(...executionData); } } return [returnData]; } } exports.Totp = Totp; //# sourceMappingURL=Totp.node.js.map