Skip to content

Commit

Permalink
Fix password console output and case sensitive on dcli read (#293)
Browse files Browse the repository at this point in the history
Fix #292
  • Loading branch information
Corentin Mors authored Nov 12, 2024
1 parent b914433 commit b121e10
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/command-handlers/passwords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const runPassword = async (

if (field === 'otp') {
foundCredentials = foundCredentials.filter((credential) => credential.otpSecret);

if (foundCredentials.length === 0) {
throw new Error('No credential found with OTP.');
}
}

const selectedCredential = await selectCredential(foundCredentials, Boolean(filters?.length));
Expand Down Expand Up @@ -60,6 +64,7 @@ export const runPassword = async (

if (output === 'console') {
logger.content(result);
return;
}

const clipboard = new Clipboard();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const rootCommands = (params: { program: Command }) => {

program
.command('read')
.description('Retrieve a secret from the local vault via its path')
.description('Retrieve a secret from the local vault via its path (using <id> is much more efficient)')
.argument('<path>', 'Path to the secret (dl://<title>/<field> or dl://<id>/<field>)')
.action(runRead);

Expand Down
16 changes: 12 additions & 4 deletions src/modules/database/vaultContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export const getVaultContent = (path: string): string => {
return findVaultContent(vaultContent, parsedPath);
};

const compareStringCaseUnsensitive = (a: string | undefined, b: string | undefined): boolean => {
return a?.toLowerCase() === b?.toLowerCase();
};

export const findVaultContent = (vaultContent: VaultContent, parsedPath: ParsedPath): string => {
const filteredVaultContent: VaultContent = {
credentials: [],
Expand All @@ -67,11 +71,15 @@ export const findVaultContent = (vaultContent: VaultContent, parsedPath: ParsedP
};

if (parsedPath.title) {
filteredVaultContent.credentials = vaultContent.credentials.filter(
(credential) => credential.title === parsedPath.title
filteredVaultContent.credentials = vaultContent.credentials.filter((credential) =>
compareStringCaseUnsensitive(credential.title, parsedPath.title)
);
filteredVaultContent.notes = vaultContent.notes.filter((note) =>
compareStringCaseUnsensitive(note.title, parsedPath.title)
);
filteredVaultContent.secrets = vaultContent.secrets.filter((secret) =>
compareStringCaseUnsensitive(secret.title, parsedPath.title)
);
filteredVaultContent.notes = vaultContent.notes.filter((note) => note.title === parsedPath.title);
filteredVaultContent.secrets = vaultContent.secrets.filter((secret) => secret.title === parsedPath.title);
}

if (parsedPath.itemId) {
Expand Down

0 comments on commit b121e10

Please sign in to comment.