Skip to content

Bitwarden Extension: Add Support for Apple Silicon Macs, Add Lock Vault command - #263

Merged
PitNikola merged 9 commits into
raycast:mainfrom
pomdtr:fix-path
Nov 2, 2021
Merged

PitNikola merged 9 commits into
raycast:mainfrom
pomdtr:fix-path

Conversation

@pomdtr

@pomdtr pomdtr commented Oct 29, 2021 •

Copy link
Copy Markdown
Contributor

Description

Search the bitwarden cli in multiple dirs, including the default brew install dir for M1 macs

Type of change

Bug fix / improvement for my extension

Screencast

image

@PSalant726 PSalant726 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this is a step backwards. It makes the preference more confusing for non-developers, and setting a custom PATH could have unforeseen user-specific side effects when executing commands via the extension. I think if you want to support M1 macs by default, changing the setting's default value is all that should be done here.

As an alternative solution to support both homebrew default installation paths: you could have the Bitwarden constructor run a bw status command using the old default path, and if the CLI isn't found, try again with the new default. Then, the installation path could be saved as state on the Bitwarden instance that gets created.

@pomdtr

pomdtr commented Oct 31, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, your raised valid points

@pomdtr

pomdtr commented Nov 1, 2021 •

Copy link
Copy Markdown
Contributor Author

@PSalant726 Please look at this second iteration. I reverted back the cli path pref, but without a default value. Instead, bw dynamically find the command if this setting is not set (and looks at both the /usr/local/bin and /opt/homebrew/bin directories).

I'm not sure why but I'm unable to rerequest a review.

@pernielsentikaer pernielsentikaer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pomdtr 👋 💪

I tested this extension on M1 and I can confirm that everything works as expected, didn't have the bitwarden-cli, but was guided and it was an easy setup.

A few NITs and suggestions:

  • package.json line 29/37 (Add linebreak for better readability)
"description": "The client_id value seen in the My Account section of a Bitwarden web vault.\r\nMore info: https://bitwarden.com/help/article/personal-api-key/",
  • hooks.ts line 8/12 (Corrected language)
const toast = await showToast(ToastStyle.Animated, "Logging in...", "It may take some time");
showToast(ToastStyle.Failure, "An error occurred during login!", "Please check your credentials");
  • api.ts line 46 (Redundant return await)
return execa(this.cliPath, args, {env: this.env});
  • It could be cool with a way to "log out" of the vault again to ensure security if needed.

Request a new review when you are ready, feel free to contact me here or at Slack if you have any questions.

@pernielsentikaer pernielsentikaer self-assigned this Nov 2, 2021
@pernielsentikaer pernielsentikaer added the extension fix / improvement Label for PRs with extension's fix improvements label Nov 2, 2021
@pomdtr pomdtr changed the title Bitwarden Extension: Add Support for Apple Silicon Macs Bitwarden Extension: Add Support for Apple Silicon Macs, Add Lock Vault command Nov 2, 2021
@pomdtr

pomdtr commented Nov 2, 2021

Copy link
Copy Markdown
Contributor Author

@pernielsentikaer I added the ability to lock the vault using a second command.

Comment thread extensions/bitwarden/src/hooks.ts Resolved
@PitNikola
PitNikola merged commit 2aa3585 into raycast:main Nov 2, 2021
@pernielsentikaer

Copy link
Copy Markdown
Contributor

I found a few word translations that can be corrected for the next version @pomdtr 💪

package.json
Line 37: "description": "The client_secret value seen in the My Account section of a Bitwarden web vault.\r\nMore info: https://bitwarden.com/help/article/personal-api-key/",
Line 62: "label": "Fetch vault items icon",
Line 63: "description": "Use Google to fetch icons. If left unchecked, built-in icons will be used."

hooks.ts
Line 9: const toast = await showToast(ToastStyle.Animated, "Logging in...", "It may take some time");

Tried your extension directly from the store and it worked very well! 💯

Comment on lines +22 to +27
{
"name": "lock-vault",
"title": "Lock Vault",
"subtitle": "Bitwarden",
"description": "Lock the vault.",
"mode": "no-view"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem for this PR, but in the future I'd prefer if a new command were added in it's own PR. It keeps changes cleaner and much simpler to review.

Since the change are on this PR, I would just reiterate that the concept of authentication (including locking and unlocking), is not limited to just vault actions. If we later plan to add commands related to the Bitwarden Send functionality, then we should drop the word "vault" from this command.

import which from "which";
import { VaultStatus } from "./types";

const PATH = "/usr/local/bin:/opt/homebrew/bin"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested this extensively, but it's possible that setting a custom PATH value could have unforeseen per-user side effects. I'm not sure how the PATH variable is handled in shell sessions launched by Raycast.

@pomdtr pomdtr Nov 3, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to check a set of expected bw cli path, but the bw cli does not work if /usr/local/bin is not in path :/

constructor() {
const { cliPath, clientId, clientSecret } = getPreferenceValues()
this.cliPath = cliPath ? cliPath : which.sync('bw', {path: PATH})
if (!existsSync(this.cliPath)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the previous line guarantee that this.cliPath will exist?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it could be:

  • an empty string
  • a faulty path (if the user provides a wrong path.

return JSON.parse(stdout).status
}

private async exec(args: string[]): Promise<execa.ExecaChildProcess> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the async keyword necessary here if no await exists within the function body?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure 😅

1. The Bitwarden CLI is [correctly installed](https://bitwarden.com/help/article/cli/#download-and-install)
1. The path of the installation matches the Bitwarden CLI Installation Path extension setting
> Currently set to: \`${path}\`
1. If you did not install bitwarden using brew, please check that path of the installation matches the \`Bitwarden CLI Installation Path\` extension setting

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. If you did not install bitwarden using brew, please check that path of the installation matches the \`Bitwarden CLI Installation Path\` extension setting
1. If you did not install the Bitwarden CLI using homebrew, verify that the installation path matches the \`Bitwarden CLI Installation Path\` extension setting.

@@ -0,0 +1 @@
export const SESSION_KEY = "sessionToken";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be stored on the Bitwarden instance, instead of in it's own file?

toast.hide();
} catch (error) {
showToast(ToastStyle.Failure, "An error occurred during login!", "Please check your crendentials");
showToast(ToastStyle.Failure, "An error occurred during login!", "Please check your credentials");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
showToast(ToastStyle.Failure, "An error occurred during login!", "Please check your credentials");
showToast(ToastStyle.Failure, "An error occurred during login!", "Invalid credentials");

Comment on lines +81 to +83
for (const item of state?.items || []) {
item.favorite ? favoriteItems.push(item) : regularItems.push(item);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to map over both the favorites and regular items below. We can DRY things up and slightly improve the latency by doing it here:

Suggested change
for (const item of state?.items || []) {
item.favorite ? favoriteItems.push(item) : regularItems.push(item);
}
for (const item of state?.items || []) {
const listItem = <ItemListItem
key={item.id}
item={item}
folder={item.folderId ? folderMap[item.folderId] : undefined}
refreshItems={refreshItems}
/>
item.favorite ? favoriteItems.push(listItem) : regularItems.push(listItem);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this is much better.

FezVrasta pushed a commit to FezVrasta/extensions that referenced this pull request Nov 24, 2021
…lt command (raycast#263)

* Add support apple silicon macs

* Update pref title

* search bw cli in brew directories by default

* extract PATH to a global var

* fix @pernielsentikaer comments

* Update troubleshooting guide

* Add ability to lock the vault

* Group favorites to the top

* Update extensions/bitwarden/src/hooks.ts

Co-authored-by: Petr Nikolaev <[email protected]>
colebemis added a commit to colebemis/raycast-extensions that referenced this pull request Nov 6, 2024