Skip to content

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

Make PIN unlock errors translatable (#1338)

Leendert de Borst27e411f4851f
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
index aacdc9fd4..befebf9a2 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
@@ -26,6 +26,9 @@ import {
isPinEnabled,
isPinLocked,
PinLockedError,
+ IncorrectPinError,
+ InvalidPinFormatError,
+ PinNotConfiguredError,
resetFailedAttempts,
unlockWithPin
} from '@/utils/PinUnlockService';
@@ -326,21 +329,35 @@ const Unlock: React.FC = () => {
navigate('/reinitialize', { replace: true });
hideLoading();
- } catch (err: any) {
+ } catch (err: unknown) {
if (err instanceof PinLockedError) {
setIsLocked(true);
setPinAvailable(false);
setUnlockMode('password');
setError(t('settings.unlockMethod.pinLocked'));
- } else {
- console.error('PIN unlock failed:', err);
- setError((err as Error).message);
+ } else if (err instanceof IncorrectPinError) {
+ /* Show translatable error with attempts remaining */
+ const attemptsRemaining = err.attemptsRemaining;
+ if (attemptsRemaining === 1) {
+ setError(t('settings.unlockMethod.incorrectPinSingular'));
+ } else {
+ setError(t('settings.unlockMethod.incorrectPin', { attemptsRemaining }));
+ }
setPin('');
- hideLoading();
- // Update failed attempts
+ /* Update failed attempts counter */
const newAttempts = await getFailedAttempts();
setFailedAttempts(newAttempts);
+ } else if (err instanceof InvalidPinFormatError) {
+ setError(t('settings.unlockMethod.invalidPinFormat'));
+ setPin('');
+ } else if (err instanceof PinNotConfiguredError) {
+ setError(t('settings.unlockMethod.pinNotConfigured'));
+ setPin('');
+ } else {
+ console.error('PIN unlock failed:', err);
+ setError(t('common.errors.unknownErrorTryAgain'));
+ setPin('');
}
hideLoading();
}
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx
index af0b0966c..ad772d4c4 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/VaultUnlockSettings.tsx
@@ -11,7 +11,8 @@ import {
setupPin,
disablePin,
isValidPin,
- isPinLocked
+ isPinLocked,
+ InvalidPinFormatError
} from '@/utils/PinUnlockService';
import { storage } from '#imports';
@@ -110,17 +111,17 @@ const VaultUnlockSettings: React.FC = () => {
try {
showLoading();
- // Get the encryption key from session storage
+ /* Get the encryption key from session storage */
const encryptionKeyResponse = await storage.getItem('session:encryptionKey') as string | undefined;
const encryptionKey = encryptionKeyResponse as string;
if (!encryptionKey) {
- setError(t('common.errors.unknownErrorTryAgain'));
+ setError(t('settings.unlockMethod.unlockVaultFirst'));
hideLoading();
return;
}
- // Setup PIN with the encryption key
+ /* Setup PIN with the encryption key */
await setupPin(newPin, encryptionKey);
setPinEnabled(true);
@@ -130,9 +131,15 @@ const VaultUnlockSettings: React.FC = () => {
setConfirmPin('');
setSuccess(t('settings.unlockMethod.enableSuccess'));
hideLoading();
- } catch (err: any) {
+ } catch (err: unknown) {
console.error('Failed to enable PIN:', err);
- setError(t('common.errors.unknownErrorTryAgain'));
+
+ if (err instanceof InvalidPinFormatError) {
+ setError(t('settings.unlockMethod.invalidPinFormat'));
+ } else {
+ setError(t('common.errors.unknownErrorTryAgain'));
+ }
+
hideLoading();
}
};
diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.json
index 9f393d2c3..99cd5250d 100644
--- a/apps/browser-extension/src/i18n/locales/en.json
+++ b/apps/browser-extension/src/i18n/locales/en.json
@@ -369,17 +369,21 @@
"password": "Master Password",
"pin": "PIN Code",
"pinDescription": "Unlock vault with PIN code",
- "pinSecurityWarning": "PIN unlock is less secure than your master password. PINs have lower entropy and can be brute-forced if your device is compromised. Use it only on devices you fully trust.",
- "setupPin": "Enable PIN Code",
+ "setupPin": "Setup PIN Code",
"enterNewPin": "Enter new PIN",
- "enterNewPinDescription": "Choose a 4-8 digit PIN code",
+ "enterNewPinDescription": "Enter a PIN code consisting of minimum 4 digits",
"confirmPin": "Confirm PIN",
"confirmPinDescription": "Enter your PIN again to confirm",
"pinPlaceholder": "••••",
- "invalidPinFormat": "PIN must be 4-8 digits",
+ "invalidPinFormat": "Invalid PIN format",
"pinMismatch": "PINs do not match",
+ "incorrectPin": "Incorrect PIN. {{attemptsRemaining}} attempts remaining.",
+ "incorrectPinSingular": "Incorrect PIN. 1 attempt remaining.",
+ "pinNotConfigured": "PIN unlock is not configured.",
"enableSuccess": "PIN unlock enabled successfully!",
- "pinLocked": "PIN unlock has been disabled. Please use your master password to unlock your vault."
+ "pinLocked": "PIN unlock has been disabled. Please use your master password to unlock your vault.",
+ "unlockVaultFirst": "Please unlock your vault first before setting up PIN.",
+ "pinSecurityWarning": "PIN unlock is less secure than your master password. PINs have lower entropy and can be brute-forced if your device is compromised. Use it only on devices you fully trust."
}
},
"passkeys": {
diff --git a/apps/browser-extension/src/utils/PinUnlockService.ts b/apps/browser-extension/src/utils/PinUnlockService.ts
index 129dbc235..be7589bca 100644
--- a/apps/browser-extension/src/utils/PinUnlockService.ts
+++ b/apps/browser-extension/src/utils/PinUnlockService.ts
@@ -28,24 +28,83 @@ const PIN_ENCRYPTED_KEY_KEY = 'local:aliasvault_pin_encrypted_key';
const PIN_SALT_KEY = 'local:aliasvault_pin_salt';
const PIN_LENGTH_KEY = 'local:aliasvault_pin_length';
const PIN_FAILED_ATTEMPTS_KEY = 'local:aliasvault_pin_failed_attempts';
-const MAX_PIN_ATTEMPTS = 3;
-const MIN_PIN_LENGTH = 4;
-const MAX_PIN_LENGTH = 8;
+const MAX_PIN_ATTEMPTS = 4;
/**
* Error thrown when PIN is locked after too many failed attempts.
+ * Translation key: settings.unlockMethod.pinLocked
*/
export class PinLockedError extends Error {
/**
* Creates a new instance of PinLockedError.
- * @param message - The error message.
*/
- public constructor(message: string = 'PIN locked after too many failed attempts') {
- super(message);
+ public constructor() {
+ super('PIN locked after too many failed attempts');
this.name = 'PinLockedError';
}
}
+/**
+ * Error thrown when PIN format is invalid.
+ * Translation key: settings.unlockMethod.invalidPinFormat
+ */
+export class InvalidPinFormatError extends Error {
+ /**
+ * Creates a new instance of InvalidPinFormatError.
+ */
+ public constructor() {
+ super('Invalid PIN format');
+ this.name = 'InvalidPinFormatError';
+ }
+}
+
+/**
+ * Error thrown when PIN is incorrect.
+ * Includes remaining attempts count.
+ * Translation key: settings.unlockMethod.incorrectPin
+ */
+export class IncorrectPinError extends Error {
+ public readonly attemptsRemaining: number;
+
+ /**
+ * Creates a new instance of IncorrectPinError.
+ * @param attemptsRemaining - Number of attempts remaining
+ */
+ public constructor(attemptsRemaining: number) {
+ super(`Incorrect PIN. ${attemptsRemaining} attempts remaining.`);
+ this.name = 'IncorrectPinError';
+ this.attemptsRemaining = attemptsRemaining;
+ }
+}
+
+/**
+ * Error thrown when PIN unlock is not configured.
+ * Translation key: settings.unlockMethod.pinNotConfigured
+ */
+export class PinNotConfiguredError extends Error {
+ /**
+ * Creates a new instance of PinNotConfiguredError.
+ */
+ public constructor() {
+ super('PIN unlock is not configured');
+ this.name = 'PinNotConfiguredError';
+ }
+}
+
+/**
+ * Error thrown when encryption key is not available for PIN setup.
+ * Translation key: settings.unlockMethod.unlockVaultFirst
+ */
+export class EncryptionKeyNotAvailableError extends Error {
+ /**
+ * Creates a new instance of EncryptionKeyNotAvailableError.
+ */
+ public constructor() {
+ super('Encryption key not available');
+ this.name = 'EncryptionKeyNotAvailableError';
+ }
+}
+
/**
* Check if PIN unlock is enabled
*/
@@ -107,7 +166,7 @@ export async function isPinLocked(): Promise<boolean> {
*/
export async function setupPin(pin: string, vaultEncryptionKey: string): Promise<void> {
if (!isValidPin(pin)) {
- throw new Error(`PIN must be ${MIN_PIN_LENGTH}-${MAX_PIN_LENGTH} digits`);
+ throw new InvalidPinFormatError();
}
try {
@@ -142,10 +201,13 @@ export async function setupPin(pin: string, vaultEncryptionKey: string): Promise
]);
} catch (error: unknown) {
- if (error instanceof Error) {
- throw new Error(`Failed to setup PIN: ${error.message}`);
+ /* Re-throw custom errors as-is */
+ if (error instanceof InvalidPinFormatError) {
+ throw error;
}
- throw new Error('Failed to setup PIN');
+ /* Log internal errors and throw generic error for user */
+ console.error('[PinUnlockService] Failed to setup PIN:', error);
+ throw error;
}
}
@@ -158,12 +220,12 @@ export async function setupPin(pin: string, vaultEncryptionKey: string): Promise
*/
export async function unlockWithPin(pin: string): Promise<string> {
if (!isValidPin(pin)) {
- throw new Error('Invalid PIN format');
+ throw new InvalidPinFormatError();
}
- // Check if locked due to too many attempts
+ /* Check if locked due to too many attempts */
if (await isPinLocked()) {
- throw new Error('Too many failed attempts. Please use your master password.');
+ throw new PinLockedError();
}
try {
@@ -174,7 +236,7 @@ export async function unlockWithPin(pin: string): Promise<string> {
]);
if (!encryptedKeyBase64 || !saltBase64) {
- throw new Error('PIN unlock is not configured');
+ throw new PinNotConfiguredError();
}
// Decode encrypted package
@@ -214,7 +276,7 @@ export async function unlockWithPin(pin: string): Promise<string> {
throw new PinLockedError();
}
- throw new Error(`Incorrect PIN. ${MAX_PIN_ATTEMPTS - newAttempts} attempts remaining.`);
+ throw new IncorrectPinError(MAX_PIN_ATTEMPTS - newAttempts);
}
}