Skip to content

Commit

Permalink
[Bitwarden] Add lock vault command (#6627)
Browse files Browse the repository at this point in the history
* feat: implement lock-vault command

* feat: add custom icon to lock-vault command

* refactor: extract constant for vault lock messages

* chore: update changelog

* refactor: rename function

* refactor: call showToast instead of modifying existing one
  • Loading branch information
jomifepe authored May 19, 2023
1 parent 9e7e8f2 commit 82fc22b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 11 deletions.
4 changes: 4 additions & 0 deletions extensions/bitwarden/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Bitwarden Changelog

## [New Command] - 2023-05-18

- Add Lock Vault command

## [Fix] - 2023-05-16

- Fixed Generate Password (Quick) command not copying password before pasting
Expand Down
Binary file added extensions/bitwarden/assets/lock-vault.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions extensions/bitwarden/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@
]
}
]
},
{
"name": "lock-vault",
"title": "Lock Vault",
"subtitle": "Bitwarden",
"description": "Lock your bitwarden vault",
"icon": "lock-vault.png",
"mode": "no-view"
}
],
"preferences": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Action, Color, Icon, showToast, Toast } from "@raycast/api";
import { VAULT_LOCK_MESSAGES } from "~/constants/general";
import { useBitwarden } from "~/context/bitwarden";
import { useVault } from "~/context/vault";

Expand All @@ -8,7 +9,7 @@ function SearchCommonActions() {

const handleLockVault = async () => {
const toast = await showToast(Toast.Style.Animated, "Locking Vault...", "Please wait");
await bitwarden.lock("Manually locked by the user");
await bitwarden.lock(VAULT_LOCK_MESSAGES.MANUAL);
await toast.hide();
};

Expand Down
5 changes: 5 additions & 0 deletions extensions/bitwarden/src/constants/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export const LOCAL_STORAGE_KEY = {
LAST_ACTIVITY_TIME: "lastActivityTime",
VAULT_LOCK_REASON: "vaultLockReason",
};

export const VAULT_LOCK_MESSAGES = {
TIMEOUT: "Vault timed out due to inactivity",
MANUAL: "Manually locked by the user",
};
8 changes: 4 additions & 4 deletions extensions/bitwarden/src/context/session/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createContext, PropsWithChildren, useContext, useMemo } from "react";
import UnlockForm from "~/components/UnlockForm";
import { useBitwarden } from "~/context/bitwarden";
import { useSessionReducer } from "~/context/session/reducer";
import { getSavedSession, Storage } from "~/context/session/utils";
import { getSavedSession, SessionStorage } from "~/context/session/utils";
import { Cache } from "~/utils/cache";
import { captureException } from "~/utils/development";
import { VaultIsLockedError } from "~/utils/errors";
Expand Down Expand Up @@ -53,17 +53,17 @@ export function SessionProvider(props: SessionProviderProps) {

async function handleUnlock(password: string, token: string) {
const passwordHash = await hashMasterPasswordForReprompting(password);
await Storage.saveSession(token, passwordHash);
await SessionStorage.saveSession(token, passwordHash);
dispatch({ type: "unlock", token, passwordHash });
}

async function handleLock(reason?: string) {
await Storage.clearSession();
await SessionStorage.clearSession();
dispatch({ type: "lock", lockReason: reason });
}

async function handleLogout() {
await Storage.clearSession();
await SessionStorage.clearSession();
Cache.clear();
dispatch({ type: "logout" });
}
Expand Down
10 changes: 4 additions & 6 deletions extensions/bitwarden/src/context/session/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getPreferenceValues, LocalStorage } from "@raycast/api";
import { LOCAL_STORAGE_KEY } from "~/constants/general";
import { LOCAL_STORAGE_KEY, VAULT_LOCK_MESSAGES } from "~/constants/general";
import { VAULT_TIMEOUT } from "~/constants/preferences";
import { Preferences } from "~/types/preferences";
import { SessionState } from "~/types/session";

export const Storage = {
export const SessionStorage = {
getSavedSession: () => {
return Promise.all([
LocalStorage.getItem<string>(LOCAL_STORAGE_KEY.SESSION_TOKEN),
Expand Down Expand Up @@ -34,10 +34,8 @@ export type SavedSessionState = {
lockReason?: string;
};

const VAULT_TIMEOUT_MESSAGE = "Vault timed out due to inactivity";

export async function getSavedSession(): Promise<SavedSessionState> {
const [token, passwordHash, lastActivityTimeString] = await Storage.getSavedSession();
const [token, passwordHash, lastActivityTimeString] = await SessionStorage.getSavedSession();
if (!token || !passwordHash) return { shouldLockVault: true };

const loadedState: SavedSessionState = { token, passwordHash };
Expand All @@ -50,7 +48,7 @@ export async function getSavedSession(): Promise<SavedSessionState> {

const timeElapseSinceLastPasswordEnter = Date.now() - lastActivityTime.getTime();
if (vaultTimeoutMs === VAULT_TIMEOUT.IMMEDIATELY || timeElapseSinceLastPasswordEnter >= vaultTimeoutMs) {
return { ...loadedState, shouldLockVault: true, lockReason: VAULT_TIMEOUT_MESSAGE };
return { ...loadedState, shouldLockVault: true, lockReason: VAULT_LOCK_MESSAGES.TIMEOUT };
}

return { ...loadedState, shouldLockVault: false };
Expand Down
25 changes: 25 additions & 0 deletions extensions/bitwarden/src/lock-vault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { showToast, Toast } from "@raycast/api";
import { Bitwarden } from "~/api/bitwarden";
import { VAULT_LOCK_MESSAGES } from "~/constants/general";
import { SessionStorage } from "~/context/session/utils";

async function lockVaultCommand() {
try {
await showToast(Toast.Style.Animated, "Locking vault...", "Please wait");
const [token] = await SessionStorage.getSavedSession();
if (!token) {
await showToast(Toast.Style.Failure, "No session found", "Already locked or not logged in");
return;
}

const bitwarden = await new Bitwarden().initialize();
await bitwarden.withSession(token).lock(VAULT_LOCK_MESSAGES.MANUAL);
await SessionStorage.clearSession();

await showToast(Toast.Style.Success, "Vault successfully locked");
} catch (error) {
await showToast(Toast.Style.Failure, "Failed to lock vault");
}
}

export default lockVaultCommand;

0 comments on commit 82fc22b

Please sign in to comment.