first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"node": "n8n-nodes-base.totp",
|
||||
"nodeVersion": "1.0",
|
||||
"codexVersion": "1.0",
|
||||
"categories": ["Core Nodes"],
|
||||
"subcategories": ["Helpers"],
|
||||
"details": "Generate a time-based one-time password",
|
||||
"alias": ["2FA", "MFA", "authentication", "Security", "OTP", "password", "multi", "factor"],
|
||||
"resources": {
|
||||
"primaryDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.totp/"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
|
||||
import * as OTPAuth from 'otpauth';
|
||||
|
||||
export class Totp implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
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: [NodeConnectionTypes.Main],
|
||||
outputs: [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(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
const credentials = await this.getCredentials<{ label?: string; secret: string }>('totpApi');
|
||||
|
||||
if (credentials.label && !credentials.label.includes(':')) {
|
||||
throw new NodeOperationError(this.getNode(), 'Malformed label - expected `issuer:username`');
|
||||
}
|
||||
|
||||
const options = this.getNodeParameter('options', 0) as {
|
||||
algorithm?: string;
|
||||
digits?: number;
|
||||
period?: number;
|
||||
};
|
||||
|
||||
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: {
|
||||
issuer?: string;
|
||||
label?: string;
|
||||
secret: string;
|
||||
algorithm: string;
|
||||
digits: number;
|
||||
period: number;
|
||||
} = {
|
||||
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];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { NodeTestHarness } from '@nodes-testing/node-test-harness';
|
||||
import type { WorkflowTestData } from 'n8n-workflow';
|
||||
import * as OTPAuth from 'otpauth';
|
||||
|
||||
describe('Execute TOTP node', () => {
|
||||
const testHarness = new NodeTestHarness();
|
||||
|
||||
// Test constants
|
||||
const FIXED_TIMESTAMP = 1640000000000; // 2021-12-20T11:33:20.000Z
|
||||
const TEST_SECRET = 'BVDRSBXQB2ZEL5HE';
|
||||
const TEST_LABEL = 'GitHub:john-doe';
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(Date, 'now').mockReturnValue(FIXED_TIMESTAMP);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
// Pre-calculate expected token using the real OTPAuth library
|
||||
// Note: The token value is the same whether label/issuer is present or not,
|
||||
// since those fields are metadata and don't affect the TOTP algorithm
|
||||
const expectedToken = new OTPAuth.TOTP({
|
||||
secret: TEST_SECRET,
|
||||
algorithm: 'SHA1',
|
||||
digits: 6,
|
||||
period: 30,
|
||||
}).generate({ timestamp: FIXED_TIMESTAMP });
|
||||
|
||||
const tests: WorkflowTestData[] = [
|
||||
{
|
||||
description: 'Generate TOTP Token with label',
|
||||
input: {
|
||||
workflowData: testHarness.readWorkflowJSON('Totp.workflow.test.json'),
|
||||
},
|
||||
output: {
|
||||
nodeData: {
|
||||
TOTP: [[{ json: expect.objectContaining({ token: expectedToken }) }]],
|
||||
},
|
||||
},
|
||||
credentials: {
|
||||
totpApi: {
|
||||
label: TEST_LABEL,
|
||||
secret: TEST_SECRET,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
description: 'Generate TOTP Token without label',
|
||||
input: {
|
||||
workflowData: testHarness.readWorkflowJSON('Totp.workflow.test.json'),
|
||||
},
|
||||
output: {
|
||||
nodeData: {
|
||||
// When no label is provided, the node should still generate a valid token
|
||||
TOTP: [[{ json: expect.objectContaining({ token: expectedToken }) }]],
|
||||
},
|
||||
},
|
||||
credentials: {
|
||||
totpApi: {
|
||||
secret: TEST_SECRET,
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
for (const testData of tests) {
|
||||
testHarness.setupTest(testData);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"nodes": [
|
||||
{
|
||||
"parameters": {},
|
||||
"id": "f2e03169-0e94-4a42-821b-3e8f67f449d7",
|
||||
"name": "When clicking \"Execute Workflow\"",
|
||||
"type": "n8n-nodes-base.manualTrigger",
|
||||
"typeVersion": 1,
|
||||
"position": [580, 320]
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"additionalOptions": {}
|
||||
},
|
||||
"id": "831f657d-2724-4a25-bb94-cf37355654bb",
|
||||
"name": "TOTP",
|
||||
"type": "n8n-nodes-base.totp",
|
||||
"typeVersion": 1,
|
||||
"position": [800, 320],
|
||||
"credentials": {
|
||||
"totpApi": {
|
||||
"id": "1",
|
||||
"name": "TOTP account"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"connections": {
|
||||
"When clicking \"Execute Workflow\"": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "TOTP",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user