Skip to content

Commit

Permalink
feat: introduce totp command for OTP token (#975)
Browse files Browse the repository at this point in the history
* feat: introduce totp command for OTP token

* fix wording and test

* print error

Co-authored-by: Emilio Alvarez Piñeiro <[email protected]>

---------

Co-authored-by: Emilio Alvarez Piñeiro <[email protected]>
  • Loading branch information
vigneshshanmugam and emilioalvap authored Oct 28, 2024
1 parent 22fb009 commit 79fe27d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
22 changes: 21 additions & 1 deletion __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('CLI', () => {
});

it('generate mfa totp token', async () => {
const cli = new CLIMock(true)
const cli = new CLIMock()
.stdin(
`step('gen mfa totp', async () => {
const token = mfa.totp('FLIIOLP3IR3W');
Expand Down Expand Up @@ -491,4 +491,24 @@ describe('CLI', () => {
});
});
});

describe('TOTP token', () => {
async function runTotp(args) {
const cli = new CLIMock()
.args(['totp', ...args])
.run({});
await cli.exitCode;
return cli.stderr();
}

it('error when secret is not provided', async () => {
const output = await runTotp('');
expect(output).toContain(`error: missing required argument 'secret'`);
});

it('generates otp token', async () => {
const output = await runTotp('FLIIOLP3IR3W');
expect(output).toContain('OTP Token: ');
});
});
});
23 changes: 20 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import { program, Option } from 'commander';
import { cwd } from 'process';
import { bold } from 'kleur/colors';
import { resolve } from 'path';
import { CliArgs, PushOptions } from './common_types';
import { reporters } from './reporters';
import {
Expand All @@ -45,13 +47,13 @@ import {
renderLocations,
LocationCmdOptions,
} from './locations';
import { resolve } from 'path';
import { Generator } from './generator';
import { error } from './helpers';
import { error, write } from './helpers';
import { LocationsMap } from './locations/public-locations';
import { createLightweightMonitors } from './push/monitor';
import { getVersion } from './push/kibana_api';
import { installTransform } from './core/transform';
import { totp, TOTPCmdOptions } from './core/mfa';

/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { name, version } = require('../package.json');
Expand Down Expand Up @@ -192,7 +194,6 @@ program
'the target Kibana spaces for the pushed monitors — spaces help you organise pushed monitors.'
)
.option('-y, --yes', 'skip all questions and run non-interactively')

.addOption(pattern)
.addOption(tags)
.addOption(fields)
Expand Down Expand Up @@ -274,4 +275,20 @@ program
}
});

// TOTP command
program
.command('totp <secret>')
.description('Generate a Time-based One-Time token using the provided secret.')
.option('--issuer <issuer>', 'Provider or Service the secret is associated with.')
.option('--label <label>', 'Account Identifier (default: SyntheticsTOTP)')
.action((secret, cmdOpts: TOTPCmdOptions) => {
try {
const token = totp(secret, cmdOpts)
write(bold(`OTP Token: ${token}`));
} catch (e) {
error(e);
process.exit(1);
}
});

program.parse(process.argv);
5 changes: 5 additions & 0 deletions src/core/mfa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ type TOTPOptions = {
period?: number
};

export type TOTPCmdOptions = {
issuer?: string
label?: string
};

export function totp(secret?: string, options: TOTPOptions = {}) {
return new TOTP({ label: "SyntheticsTOTP", secret, ...options }).generate();
}

0 comments on commit 79fe27d

Please sign in to comment.