Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Use Rust password generator in context menu (#776)

Leendert de Borst272690204e44
diff --git a/apps/browser-extension/src/entrypoints/background/ContextMenu.ts b/apps/browser-extension/src/entrypoints/background/ContextMenu.ts
index 8b3255a1e..c9cd9bcdc 100644
--- a/apps/browser-extension/src/entrypoints/background/ContextMenu.ts
+++ b/apps/browser-extension/src/entrypoints/background/ContextMenu.ts
@@ -1,8 +1,11 @@
import { type Browser } from '@wxt-dev/browser';
+import { handleGetPasswordSettings } from '@/entrypoints/background/VaultMessageHandler';
+
import { POPUP_TYPES, type PopupType, isPopupType } from '@/utils/autofill/PopupTypes';
-import { PasswordGenerator } from '@/utils/dist/core/password-generator';
+import type { PasswordSettings } from '@/utils/dist/core/models/vault';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
+import * as RustCore from '@/utils/RustCore';
import { t } from '@/i18n/StandaloneI18n';
@@ -104,25 +107,49 @@ export async function setupContextMenus() : Promise<void> {
}
}
+/**
+ * Get the user's saved password settings when the vault is unlocked, otherwise fall back to
+ * defaults so password generation still works while locked.
+ */
+async function getPasswordSettingsOrDefault(): Promise<PasswordSettings> {
+ const defaults: PasswordSettings = {
+ Length: 18,
+ UseLowercase: true,
+ UseUppercase: true,
+ UseNumbers: true,
+ UseSpecialChars: true,
+ UseNonAmbiguousChars: false
+ };
+
+ try {
+ const response = await handleGetPasswordSettings();
+ return response.success && response.settings ? response.settings : defaults;
+ } catch {
+ return defaults;
+ }
+}
+
/**
* Handle context menu clicks.
*/
export function handleContextMenuClick(info: Browser.contextMenus.OnClickData, tab?: Browser.tabs.Tab) : void {
if (info.menuItemId === "aliasvault-generate-password") {
- // Initialize password generator
- const passwordGenerator = new PasswordGenerator();
- const password = passwordGenerator.generateRandomPassword();
-
- // Use browser.scripting to write password to clipboard from active tab
+ /*
+ * Generate a password and copy it to the clipboard of the active tab. Use the user's saved
+ * settings when the vault is unlocked; otherwise fall back to defaults.
+ */
if (tab?.id) {
- // Get confirm text translation.
- t('content.passwordCopiedToClipboard').then((message) => {
+ const tabId = tab.id;
+ void (async (): Promise<void> => {
+ const settings = await getPasswordSettingsOrDefault();
+ const password = await RustCore.generatePassword(settings);
+ const message = await t('content.passwordCopiedToClipboard');
browser.scripting.executeScript({
- target: { tabId: tab.id },
+ target: { tabId },
func: copyPasswordToClipboard,
args: [message, password]
});
- });
+ })();
}
} else if (tab?.id) {
const popupType = popupTypeForMenuId(info.menuItemId);
diff --git a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
index bdcd4af02..708f6d0ac 100644
--- a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
+++ b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
@@ -10,6 +10,7 @@ import type { TwoFactorState } from '@/entrypoints/background/TwoFactorStateHand
import type { SyncStatusCheckResult, FullVaultSyncResult } from '@/entrypoints/background/VaultMessageHandler';
import type { EncryptionKeyDerivationParams } from '@/utils/dist/core/models/metadata';
+import type { PasswordSettings } from '@/utils/dist/core/models/vault';
import type { LoginResponse } from '@/utils/dist/core/models/webapi';
import type { SavePromptPersistedState, LastAutofilledCredential } from '@/utils/loginDetector';
import type { PendingPasskeyRequest, WebAuthnSettingsResponse, WebAuthnPublicKeyGetPayload, MatchingPasskeysResponse, WebAuthnAssertionResponse } from '@/utils/passkey/types';
@@ -49,6 +50,7 @@ export interface IExtensionMessageProtocol {
CLIPBOARD_COUNTDOWN_CANCELLED(data: Record<string, never>): void;
CREATE_ITEM(data: any): BoolResponse;
FULL_VAULT_SYNC(): FullVaultSyncResult;
+ GENERATE_PASSWORD(data: { settings: PasswordSettings }): { success: boolean; password?: string; error?: string };
GENERATE_TOTP_CODE(data: { itemId: string }): { success: boolean; code?: string; error?: string };
GET_CLIPBOARD_CLEAR_TIMEOUT(): number;
GET_CLIPBOARD_COUNTDOWN_STATE(): { remaining: number; total: number; id: number } | null;