-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Create TOTP node #5901
Merged
Merged
feat: Create TOTP node #5901
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5157cd8
:sparkles: Create TOTP node
ivov 88ac777
:recycle: Apply feedback
ivov d51b479
:recycle: Recreate `pnpm-lock.yaml`
ivov 531dae8
:recycle: Apply Giulio's feedback
ivov 320e027
:construction: WIP node tests
ivov 468d64e
:white_check_mark: Finish node test setup
ivov 048dc41
:rewind: Restore test command
ivov 2a199f4
Merge branch 'master' of https://github.com/n8n-io/n8n into ado-517-t…
michael-radency 2d371a2
:zap: linter fixes, tweaks
michael-radency f315962
:recycle: Address Michael's feedback
ivov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { ICredentialType, INodeProperties } from 'n8n-workflow'; | ||
|
||
export class TotpApi implements ICredentialType { | ||
name = 'totpApi'; | ||
|
||
displayName = 'TOTP API'; | ||
|
||
documentationUrl = 'totp'; | ||
|
||
properties: INodeProperties[] = [ | ||
{ | ||
displayName: 'Secret', | ||
name: 'secret', | ||
type: 'string', | ||
typeOptions: { password: true }, | ||
default: '', | ||
placeholder: 'e.g. BVDRSBXQB2ZEL5HE', | ||
required: true, | ||
description: | ||
'Secret key encoded in the QR code during setup. <a href="https://github.com/google/google-authenticator/wiki/Key-Uri-Format#secret">Learn more</a>.', | ||
}, | ||
{ | ||
displayName: 'Label', | ||
name: 'label', | ||
type: 'string', | ||
default: '', | ||
required: true, | ||
placeholder: 'e.g. GitHub:john-doe', | ||
description: | ||
'Identifier for the TOTP account, in the <code>issuer:username</code> format. <a href="https://github.com/google/google-authenticator/wiki/Key-Uri-Format#label">Learn more</a>.', | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import type { | ||
IExecuteFunctions, | ||
INodeExecutionData, | ||
INodeType, | ||
INodeTypeDescription, | ||
} from 'n8n-workflow'; | ||
|
||
import { NodeOperationError } from 'n8n-workflow'; | ||
|
||
import 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', | ||
}, | ||
inputs: ['main'], | ||
outputs: ['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('totpApi')) as { label: string; secret: string }; | ||
|
||
if (!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.split(':'); | ||
|
||
const totp = new OTPAuth.TOTP({ | ||
issuer, | ||
label: credentials.label, | ||
secret: credentials.secret, | ||
algorithm: options.algorithm, | ||
digits: options.digits, | ||
period: options.period, | ||
}); | ||
|
||
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 this.prepareOutputData(returnData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as Helpers from '../../../test/nodes/Helpers'; | ||
import { executeWorkflow } from '../../../test/nodes/ExecuteWorkflow'; | ||
import type { WorkflowTestData } from '../../../test/nodes/types'; | ||
|
||
jest.mock('otpauth', () => { | ||
return { | ||
TOTP: jest.fn().mockImplementation(() => { | ||
return { | ||
generate: jest.fn().mockReturnValue('123456'), | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
describe('Execute TOTP node', () => { | ||
const tests: WorkflowTestData[] = [ | ||
{ | ||
description: 'Generate TOTP Token', | ||
input: { | ||
workflowData: Helpers.readJsonFileSync('nodes/Totp/test/Totp.workflow.test.json'), | ||
}, | ||
output: { | ||
nodeData: { | ||
TOTP: [[{ json: { token: '123456' } }]], // ignore secondsRemaining to prevent flakiness | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const nodeTypes = Helpers.setup(tests); | ||
|
||
for (const testData of tests) { | ||
// eslint-disable-next-line @typescript-eslint/no-loop-func | ||
test(testData.description, async () => { | ||
const { result } = await executeWorkflow(testData, nodeTypes); | ||
|
||
Helpers.getResultNodeData(result, testData).forEach(({ nodeName, resultData }) => { | ||
const expected = testData.output.nodeData[nodeName][0][0].json; | ||
const actual = resultData[0]?.[0].json; | ||
|
||
expect(actual?.token).toEqual(expected.token); | ||
}); | ||
|
||
expect(result.finished).toEqual(true); | ||
}); | ||
} | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/nodes-base/nodes/Totp/test/Totp.workflow.test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { IDataObject } from 'n8n-workflow'; | ||
|
||
// If your test needs data from credentials, you can add it here. | ||
// as JSON.stringify({ id: 'credentials_ID', name: 'credentials_name' }) for specific credentials | ||
// or as 'credentials_type' for all credentials of that type | ||
// expected keys for credentials can be found in packages/nodes-base/credentials/[credentials_type].credentials.ts | ||
export const FAKE_CREDENTIALS_DATA: IDataObject = { | ||
[JSON.stringify({ id: '20', name: 'Airtable account' })]: { | ||
apiKey: 'key456', | ||
}, | ||
airtableApi: { | ||
apiKey: 'key123', | ||
}, | ||
n8nApi: { | ||
apiKey: 'key123', | ||
baseUrl: 'https://test.app.n8n.cloud/api/v1', | ||
}, | ||
totpApi: { | ||
label: 'GitHub:john-doe', | ||
secret: 'BVDRSBXQB2ZEL5HE', | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a cleaner way to specify default options? Adding
default: { algorithm: 'SHA1', period: 30, digits: 6 }
causes the options to start off revealed, which is not what we want, but otherwiseoptions
defaults to{}
, which is also not what we want.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I should've been clearer. By "cleaner" I meant to avoid repeating all these defaults that were already specified in the node params above, for maintainability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not that I'm aware of, we could use some const/enum
defaultOptions
to make it dryerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think the ideal would be for
getNodeParameter
to return an object with every field set to its default value, so that making it dryer does not introduce too much indirection. Let's ignore for now then.