Skip to content
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: introduce totp command for OTP token #975

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
e && error(e);
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
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();
}