Skip to content

Commit

Permalink
Fix otpauth parsing issue (#301)
Browse files Browse the repository at this point in the history
Fix #299
  • Loading branch information
Corentin Mors authored Nov 22, 2024
1 parent b84d24b commit 8a2613f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dashlane/cli",
"version": "6.2447.1",
"version": "6.2447.2",
"description": "Manage your Dashlane vault through a CLI tool",
"type": "module",
"main": "dist/index.cjs",
Expand Down
2 changes: 1 addition & 1 deletion src/cliVersion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CliVersion } from './types.js';

export const CLI_VERSION: CliVersion = { major: 6, minor: 2447, patch: 1 };
export const CLI_VERSION: CliVersion = { major: 6, minor: 2447, patch: 2 };
export const breakingChangesVersions: CliVersion[] = [];

export const cliVersionToString = (version: CliVersion): string => {
Expand Down
2 changes: 1 addition & 1 deletion src/command-handlers/passwords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const runPassword = async (
result = selectedCredential.password;
break;
case 'otp':
if (!selectedCredential.otpSecret || !selectedCredential.otpUrl) {
if (!selectedCredential.otpSecret && !selectedCredential.otpUrl) {
throw new Error('No OTP found for this credential.');
}
if (selectedCredential.otpSecret) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
import { Command } from 'commander';
import process from 'process';

import { cliVersionToString, CLI_VERSION } from './cliVersion.js';
import { rootCommands } from './commands/index.js';
import { initDeviceCredentials, initStagingCheck, initTeamDeviceCredentials } from './utils/index.js';
import { errorColor, initLogger } from './logger.js';

process.removeAllListeners('warning');

const debugLevel = process.argv.indexOf('--debug') !== -1 ? 'debug' : 'info';

initLogger({ debugLevel });
Expand Down
13 changes: 8 additions & 5 deletions src/modules/crypto/otpauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ interface Otpauth {
secret: string;
algorithm: HashAlgorithms;
digits: number;
period: number;
counter: number;
period?: number;
counter?: number;
}

const matchAlgorithm = (algorithm: string): HashAlgorithms => {
Expand All @@ -40,9 +40,9 @@ const parseOtpauth = (uri: string): Otpauth => {
issuer: searchParams.get('issuer') ?? '',
secret: searchParams.get('secret') ?? '',
algorithm: matchAlgorithm(searchParams.get('algorithm') ?? 'SHA1'),
digits: Number(searchParams.get('digits') ?? 0),
period: Number(searchParams.get('period') ?? 0),
counter: Number(searchParams.get('counter') ?? 0),
digits: Number(searchParams.get('digits') ?? 6),
period: Number(searchParams.get('period')),
counter: Number(searchParams.get('counter')),
};
};

Expand All @@ -66,6 +66,9 @@ export const generateOtpFromUri = (uri: string): GenerateOtpOutput => {
};
return { token: authenticator.generate(otpauth.secret), remainingTime: authenticator.timeRemaining() };
case 'hotp':
if (otpauth.counter === undefined) {
throw new Error('Counter is required for HOTP');
}
hotp.options = { algorithm: otpauth.algorithm, digits: otpauth.digits };
return { token: hotp.generate(otpauth.secret, otpauth.counter), remainingTime: null };
default:
Expand Down

0 comments on commit 8a2613f

Please sign in to comment.