Skip to content

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

Update password strength indicator (#2413)

Leendert de Borst61d9f1798dd1
diff --git a/apps/browser-extension/src/entrypoints/popup/components/PasswordStrengthIndicator.tsx b/apps/browser-extension/src/entrypoints/popup/components/PasswordStrengthIndicator.tsx
index 5c5c727b4..a4d68332f 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/PasswordStrengthIndicator.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/PasswordStrengthIndicator.tsx
@@ -1,30 +1,7 @@
+import { getPasswordStrength } from '@aliasvault/client/utilities/PasswordStrength';
import React from 'react';
import { useTranslation } from 'react-i18next';
-/** Minimum password length that reaches the "Good" strength level. */
-export const MIN_GOOD_PASSWORD_LENGTH = 12;
-
-/**
- * Calculate the strength of a password.
- * @param password - the password to rate
- */
-export const getPasswordStrength = (password: string): number => {
- const length = password.length;
- if (length < 8) {
- return 0;
- }
- if (length < MIN_GOOD_PASSWORD_LENGTH) {
- return 1;
- }
- if (length < 16) {
- return 2;
- }
- if (length < 20) {
- return 3;
- }
- return 4;
-};
-
const LABEL_KEYS = ['veryWeak', 'weak', 'fair', 'good', 'strong'];
const BAR_COLORS = ['bg-orange-400 dark:bg-orange-500', 'bg-yellow-500 dark:bg-yellow-600', 'bg-green-500 dark:bg-green-600', 'bg-green-600 dark:bg-green-700', 'bg-green-700 dark:bg-green-800'];
const TEXT_COLORS = ['text-orange-600 dark:text-orange-400', 'text-yellow-600 dark:text-yellow-400', 'text-green-600 dark:text-green-400', 'text-green-700 dark:text-green-300', 'text-green-800 dark:text-green-200'];
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx
index b9e514d4a..e77957f69 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx
@@ -1,5 +1,6 @@
import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';
import { IncorrectPasswordError, MasterPasswordService, PasswordChangedElsewhereError } from '@aliasvault/client/auth/MasterPasswordService';
+import { MIN_ACCEPTED_PASSWORD_LENGTH } from '@aliasvault/client/utilities/PasswordStrength';
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -7,7 +8,7 @@ import AlertMessage from '@/entrypoints/popup/components/AlertMessage';
import Button from '@/entrypoints/popup/components/Button';
import { HeaderIcon, HeaderIconType } from '@/entrypoints/popup/components/Icons/HeaderIcons';
import PageTitle from '@/entrypoints/popup/components/PageTitle';
-import PasswordStrengthIndicator, { MIN_GOOD_PASSWORD_LENGTH } from '@/entrypoints/popup/components/PasswordStrengthIndicator';
+import PasswordStrengthIndicator from '@/entrypoints/popup/components/PasswordStrengthIndicator';
import { useDb } from '@/entrypoints/popup/context/DbContext';
import { useLoading } from '@/entrypoints/popup/context/LoadingContext';
import { useWebApi } from '@/entrypoints/popup/context/WebApiContext';
@@ -118,8 +119,8 @@ const ChangePasswordSettings: React.FC = () => {
setError(t('settings.securitySettings.changePassword.passwordsDoNotMatch'));
return;
}
- if (newPassword.length < MIN_GOOD_PASSWORD_LENGTH) {
- setError(t('settings.securitySettings.changePassword.passwordTooShort', { minLength: MIN_GOOD_PASSWORD_LENGTH }));
+ if (newPassword.length < MIN_ACCEPTED_PASSWORD_LENGTH) {
+ setError(t('settings.securitySettings.changePassword.passwordTooShort', { minLength: MIN_ACCEPTED_PASSWORD_LENGTH }));
return;
}
diff --git a/apps/mobile-app/app/(tabs)/settings/security/change-password.tsx b/apps/mobile-app/app/(tabs)/settings/security/change-password.tsx
index 78e4eac0a..1daf36205 100644
--- a/apps/mobile-app/app/(tabs)/settings/security/change-password.tsx
+++ b/apps/mobile-app/app/(tabs)/settings/security/change-password.tsx
@@ -1,3 +1,4 @@
+import { MIN_ACCEPTED_PASSWORD_LENGTH } from '@aliasvault/client/utilities/PasswordStrength';
import { router } from 'expo-router';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -6,6 +7,7 @@ import { StyleSheet, View, KeyboardAvoidingView, Platform } from 'react-native';
import { useColors } from '@/hooks/useColorScheme';
import { useVaultMutate } from '@/hooks/useVaultMutate';
+import { PasswordStrengthIndicator } from '@/components/form/PasswordStrengthIndicator';
import LoadingOverlay from '@/components/LoadingOverlay';
import { ThemedButton } from '@/components/themed/ThemedButton';
import { ThemedContainer } from '@/components/themed/ThemedContainer';
@@ -71,6 +73,11 @@ export default function ChangePasswordScreen(): React.ReactNode {
return;
}
+ if (newPassword.length < MIN_ACCEPTED_PASSWORD_LENGTH) {
+ showAlert(t('common.error'), t('settings.securitySettings.changePassword.passwordTooShort', { minLength: MIN_ACCEPTED_PASSWORD_LENGTH }));
+ return;
+ }
+
if (newPassword !== confirmPassword) {
showAlert(t('common.error'), t('settings.securitySettings.changePassword.passwordsDoNotMatch'));
return;
@@ -145,6 +152,7 @@ export default function ChangePasswordScreen(): React.ReactNode {
onChangeText={setNewPassword}
placeholder={t('settings.securitySettings.changePassword.enterNewPassword')}
/>
+ <PasswordStrengthIndicator password={newPassword} />
</View>
<View style={styles.inputContainer}>
diff --git a/apps/mobile-app/components/form/PasswordStrengthIndicator.tsx b/apps/mobile-app/components/form/PasswordStrengthIndicator.tsx
new file mode 100644
index 000000000..a9a5b30fa
--- /dev/null
+++ b/apps/mobile-app/components/form/PasswordStrengthIndicator.tsx
@@ -0,0 +1,95 @@
+import { getPasswordStrength } from '@aliasvault/client/utilities/PasswordStrength';
+import { useEffect, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
+import { Animated, StyleSheet, View } from 'react-native';
+
+import { useColors, useColorScheme } from '@/hooks/useColorScheme';
+
+import { ThemedText } from '@/components/themed/ThemedText';
+
+const LABEL_KEYS = ['veryWeak', 'weak', 'fair', 'good', 'strong'];
+
+/**
+ * Bar, label and track colors per strength level, the same Tailwind shades the web app and browser extension use.
+ */
+const STRENGTH_COLORS = {
+ light: {
+ bar: ['#fb923c', '#eab308', '#22c55e', '#16a34a', '#15803d'],
+ text: ['#ea580c', '#ca8a04', '#16a34a', '#15803d', '#166534'],
+ track: '#e5e7eb',
+ },
+ dark: {
+ bar: ['#f97316', '#ca8a04', '#16a34a', '#15803d', '#166534'],
+ text: ['#fb923c', '#facc15', '#4ade80', '#86efac', '#bbf7d0'],
+ track: '#374151',
+ },
+};
+
+type PasswordStrengthIndicatorProps = {
+ /** The password to rate, the indicator hides itself while this is empty. */
+ password: string;
+};
+
+/**
+ * Strength bar shown underneath a new password input, mirroring the web app and browser extension.
+ * @returns {React.ReactNode} The rendered component
+ */
+export function PasswordStrengthIndicator({ password }: PasswordStrengthIndicatorProps): React.ReactNode {
+ const { t } = useTranslation();
+ const colors = useColors();
+ const palette = STRENGTH_COLORS[useColorScheme()];
+ const strength = getPasswordStrength(password);
+ const width = useRef(new Animated.Value(strength + 1)).current;
+
+ useEffect(() => {
+ Animated.timing(width, { toValue: strength + 1, duration: 300, useNativeDriver: false }).start();
+ }, [strength, width]);
+
+ if (password.length === 0) {
+ return null;
+ }
+
+ const styles = StyleSheet.create({
+ bar: {
+ backgroundColor: palette.bar[strength],
+ borderRadius: 5,
+ height: 10,
+ },
+ container: {
+ marginTop: 12,
+ },
+ header: {
+ flexDirection: 'row',
+ justifyContent: 'space-between',
+ marginBottom: 8,
+ },
+ label: {
+ color: colors.textMuted,
+ fontSize: 12,
+ fontWeight: '500',
+ },
+ strength: {
+ color: palette.text[strength],
+ fontSize: 12,
+ fontWeight: '600',
+ },
+ track: {
+ backgroundColor: palette.track,
+ borderRadius: 5,
+ height: 10,
+ overflow: 'hidden',
+ },
+ });
+
+ return (
+ <View style={styles.container} testID="password-strength-indicator">
+ <View style={styles.header}>
+ <ThemedText style={styles.label}>{t('common.passwordStrength.title')}</ThemedText>
+ <ThemedText style={styles.strength}>{t(`common.passwordStrength.${LABEL_KEYS[strength]}`)}</ThemedText>
+ </View>
+ <View style={styles.track}>
+ <Animated.View style={[styles.bar, { width: width.interpolate({ inputRange: [0, 5], outputRange: ['0%', '100%'] }) }]} />
+ </View>
+ </View>
+ );
+}
diff --git a/apps/mobile-app/i18n/locales/en.json b/apps/mobile-app/i18n/locales/en.json
index cc787c4d5..a558756bf 100644
--- a/apps/mobile-app/i18n/locales/en.json
+++ b/apps/mobile-app/i18n/locales/en.json
@@ -30,6 +30,14 @@
"or": "or",
"retry": "Retry",
"resizeField": "Drag to resize field",
+ "passwordStrength": {
+ "title": "Password Strength",
+ "veryWeak": "Keep going",
+ "weak": "Getting better",
+ "fair": "Good",
+ "good": "Great",
+ "strong": "Excellent"
+ },
"deleteItemConfirmTitle": "Delete Item",
"deleteItemConfirmDescription": "Are you sure you want to delete this item?",
"errors": {
@@ -274,6 +282,7 @@
"changePassword": "Change Password",
"fillAllFields": "Please fill in all fields",
"passwordsDoNotMatch": "New passwords do not match",
+ "passwordTooShort": "Password must be at least {{minLength}} characters long.",
"userNotAuthenticated": "User not authenticated",
"initiatingChange": "Initiating password change...",
"currentPasswordIncorrect": "Current password is not correct",
diff --git a/apps/web/src/components/auth/setup/SetupSteps.tsx b/apps/web/src/components/auth/setup/SetupSteps.tsx
index 4770e209d..9c6d5a02a 100644
--- a/apps/web/src/components/auth/setup/SetupSteps.tsx
+++ b/apps/web/src/components/auth/setup/SetupSteps.tsx
@@ -1,11 +1,12 @@
import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';
+import { MIN_ACCEPTED_PASSWORD_LENGTH } from '@aliasvault/client/utilities/PasswordStrength';
import React, { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import GlobalNotificationDisplay from '@/components/alerts/GlobalNotificationDisplay';
import PasswordInputField from '@/components/auth/PasswordInputField';
import EditFormRow from '@/components/forms/EditFormRow';
-import PasswordStrengthIndicator, { MIN_GOOD_PASSWORD_LENGTH } from '@/components/shared/PasswordStrengthIndicator';
+import PasswordStrengthIndicator from '@/components/shared/PasswordStrengthIndicator';
import { useApp } from '@/context/AppContext';
import { useNotifications } from '@/context/NotificationContext';
import { useWebApi } from '@/context/WebApiContext';
@@ -185,8 +186,8 @@ export const PasswordStep: React.FC<{ onPasswordChange: (password: string) => vo
* Length and match checks.
*/
const validate = (value: string, confirm: string): void => {
- if (value.length < MIN_GOOD_PASSWORD_LENGTH) {
- setErrorMessage(t('validationMessages.PasswordMinLengthGeneric', { 0: MIN_GOOD_PASSWORD_LENGTH }));
+ if (value.length < MIN_ACCEPTED_PASSWORD_LENGTH) {
+ setErrorMessage(t('validationMessages.PasswordMinLengthGeneric', { 0: MIN_ACCEPTED_PASSWORD_LENGTH }));
onPasswordChange('');
return;
}
diff --git a/apps/web/src/components/shared/PasswordStrengthIndicator.tsx b/apps/web/src/components/shared/PasswordStrengthIndicator.tsx
index 1b337ebe6..fc21f6172 100644
--- a/apps/web/src/components/shared/PasswordStrengthIndicator.tsx
+++ b/apps/web/src/components/shared/PasswordStrengthIndicator.tsx
@@ -1,29 +1,7 @@
+import { getPasswordStrength } from '@aliasvault/client/utilities/PasswordStrength';
import React from 'react';
import { useTranslation } from 'react-i18next';
-/** Passwords shorter than this are rejected; it is also where the strength scale reaches "Fair". */
-export const MIN_PASSWORD_LENGTH = 12;
-
-/**
- * Strength score of a password, 0 to 4, by length.
- */
-export function getPasswordStrength(password: string): number {
- const length = password.length;
- if (length < 8) {
- return 0;
- }
- if (length < MIN_PASSWORD_LENGTH) {
- return 1;
- }
- if (length < 16) {
- return 2;
- }
- if (length < 20) {
- return 3;
- }
- return 4;
-}
-
const LABEL_KEYS = ['StrengthVeryWeak', 'StrengthWeak', 'StrengthFair', 'StrengthGood', 'StrengthStrong'];
const BAR_CLASSES = ['bg-orange-400 dark:bg-orange-500', 'bg-yellow-500 dark:bg-yellow-600', 'bg-green-500 dark:bg-green-600', 'bg-green-600 dark:bg-green-700', 'bg-green-700 dark:bg-green-800'];
const TEXT_CLASSES = ['text-orange-600 dark:text-orange-400', 'text-yellow-600 dark:text-yellow-400', 'text-green-600 dark:text-green-400', 'text-green-700 dark:text-green-300', 'text-green-800 dark:text-green-200'];
diff --git a/core/client/src/utilities/PasswordStrength.ts b/core/client/src/utilities/PasswordStrength.ts
new file mode 100644
index 000000000..593024f52
--- /dev/null
+++ b/core/client/src/utilities/PasswordStrength.ts
@@ -0,0 +1,26 @@
+/**
+ * Minimum length the register, change password and export password forms accept; it is also where the strength
+ * scale reaches "Fair". Not to be confused with the password generator's MIN_PASSWORD_LENGTH.
+ */
+export const MIN_ACCEPTED_PASSWORD_LENGTH = 12;
+
+/**
+ * Strength score of a password, 0 (very weak) to 4 (strong), by length.
+ * @param password - the password to rate
+ */
+export function getPasswordStrength(password: string): number {
+ const length = password.length;
+ if (length < 8) {
+ return 0;
+ }
+ if (length < MIN_ACCEPTED_PASSWORD_LENGTH) {
+ return 1;
+ }
+ if (length < 16) {
+ return 2;
+ }
+ if (length < 20) {
+ return 3;
+ }
+ return 4;
+}