Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update mobile app password change logic (#2469)
Leendert de Borstfe177537e26a
diff --git a/apps/mobile-app/app/(tabs)/settings/security/change-password.tsx b/apps/mobile-app/app/(tabs)/settings/security/change-password.tsxindex 1daf36205..0915be546 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,5 @@+import { apiErrorMessage } from '@aliasvault/client/api/errors/ApiErrorMessage';+import { IncorrectPasswordError, PasswordChangedElsewhereError } from '@aliasvault/client/auth/MasterPasswordService';import { MIN_ACCEPTED_PASSWORD_LENGTH } from '@aliasvault/client/utilities/PasswordStrength';import { router } from 'expo-router';import { useState } from 'react';@@ -63,6 +65,20 @@ export default function ChangePasswordScreen(): React.ReactNode {},});+ /**+ * Map a password change failure onto the message to show.+ * @param error - the error thrown by the change flow+ */+ const errorMessage = (error: unknown): string => {+ if (error instanceof IncorrectPasswordError) {+ return t('settings.securitySettings.changePassword.currentPasswordIncorrect');+ }+ if (error instanceof PasswordChangedElsewhereError) {+ return t('common.errors.passwordChanged');+ }+ return apiErrorMessage(error, t, t('settings.securitySettings.changePassword.failedToChange'));+ };+/*** Handle the submit button press.* @returns {Promise<void>} A promise that resolves when the operation is complete@@ -92,13 +108,13 @@ export default function ChangePasswordScreen(): React.ReactNode {setIsLoading(true);setLoadingStatus(t('settings.securitySettings.changePassword.initiatingChange'));- const currentPasswordHashBase64 = await authContext.verifyPassword(currentPassword);- if (!currentPasswordHashBase64) {+ const currentUnlockKeyBase64 = await authContext.verifyPassword(currentPassword);+ if (!currentUnlockKeyBase64) {showAlert(t('common.error'), t('settings.securitySettings.changePassword.currentPasswordIncorrect'));return;}- await executeVaultPasswordChange(currentPasswordHashBase64, newPassword);+ await executeVaultPasswordChange(currentUnlockKeyBase64, newPassword);// Show confirm dialog and go back to the settings screenshowAlert(t('common.success'), t('settings.securitySettings.changePassword.passwordChangedSuccessfully'), () => {@@ -109,7 +125,7 @@ export default function ChangePasswordScreen(): React.ReactNode {});} catch (error) {console.error('Password change error:', error);- showAlert(t('common.error'), t('settings.securitySettings.changePassword.failedToChange'));+ showAlert(t('common.error'), errorMessage(error));} finally {setIsLoading(false);setLoadingStatus(null);diff --git a/apps/mobile-app/hooks/useVaultMutate.ts b/apps/mobile-app/hooks/useVaultMutate.tsindex 6568d5dd0..cd978e76b 100644--- a/apps/mobile-app/hooks/useVaultMutate.ts+++ b/apps/mobile-app/hooks/useVaultMutate.ts@@ -1,31 +1,19 @@-import { Buffer } from 'buffer';-import { useCallback, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';import Toast from 'react-native-toast-message';import type { UnlockKeyDerivationParams } from '@aliasvault/models/metadata';-import type { PasswordChangeInitiateResponse, Vault, VaultPasswordChangeRequest } from '@aliasvault/models/webapi';-import { FieldKey, getFieldValue } from '@aliasvault/models/vault';-import EncryptionUtility from '@/utils/EncryptionUtility';+import type { PasswordChangeInitiateResponse, PasswordChangeRequest } from '@aliasvault/models/webapi';+import { MasterPasswordService, PasswordChangedElsewhereError } from '@aliasvault/client/auth/MasterPasswordService';import { SrpAuthService } from '@aliasvault/client/auth/SrpAuthService';-import { DEFAULT_VAULT_MUTATION_SCOPE } from '@aliasvault/client/sync/VaultMutationScope';import { useVaultSync } from '@/hooks/useVaultSync';-import { AppErrorCode, formatErrorWithCode } from '@aliasvault/client/api/errors/AppErrorCodes';-import { PayloadTooLargeError } from '@aliasvault/client/api/errors/PayloadTooLargeError';-import { useApp } from '@/context/AppContext';import { useDb } from '@/context/DbContext';import { useWebApi } from '@/context/WebApiContext';import NativeVaultManager from '@/specs/NativeVaultManager';-type VaultPostResponse = {- status: number;- newRevisionNumber: number;-}-type VaultMutationOptions = {onSuccess?: () => void;onError?: (error: Error) => void;@@ -37,7 +25,7 @@ type VaultMutationOptions = {*/export function useVaultMutate() : {executeVaultMutation: (operation: () => Promise<void>, options?: VaultMutationOptions) => Promise<void>;- executeVaultPasswordChange: (currentPasswordHashBase64: string, newPasswordPlainText: string, options?: VaultMutationOptions) => Promise<void>;+ executeVaultPasswordChange: (currentUnlockKeyBase64: string, newPasswordPlainText: string) => Promise<void>;isLoading: boolean;syncStatus: string;} {@@ -51,48 +39,6 @@ export function useVaultMutate() : {const syncInProgressRef = useRef(false);const syncQueuedRef = useRef(false);- /**- * Prepare vault for password change operation.- */- const prepareVaultForPasswordChange = useCallback(async (): Promise<Vault> => {- const syncState = await NativeVaultManager.getSyncState();- const currentRevision = syncState.serverRevision;- const encryptedDb = await NativeVaultManager.getEncryptedDatabase();- if (!encryptedDb) {- throw new Error(t('vault.errors.failedToGetEncryptedDatabase'));- }-- const privateEmailDomains = await dbContext.sqliteClient!.getPrivateEmailDomains();- const items = await dbContext.sqliteClient!.items.getAll();- const privateEmailAddresses = items- .map(item => getFieldValue(item, FieldKey.LoginEmail))- .filter((email): email is string => email != null && email !== '')- .filter((email, index, self) => self.indexOf(email) === index)- .filter(email => {- return privateEmailDomains.some(domain => email.toLowerCase().endsWith(`@${domain.toLowerCase()}`));- });-- const username = authContext.username;- if (!username) {- throw new Error(t('vault.errors.usernameNotFound'));- }-- return {- blob: encryptedDb,- createdAt: new Date().toISOString(),- credentialsCount: items.length,- currentRevisionNumber: currentRevision,- emailAddressList: privateEmailAddresses,- privateEmailDomainList: [],- hiddenPrivateEmailDomainList: [],- publicEmailDomainList: [],- encryptionPublicKey: '',- updatedAt: new Date().toISOString(),- username: username,- version: (await dbContext.sqliteClient!.getDatabaseVersion())?.version ?? '0.0.0'- };- }, [dbContext, authContext, t]);-/*** Trigger background sync without blocking the UI.* This is fire-and-forget - the ServerSyncIndicator shows progress.@@ -175,120 +121,46 @@ export function useVaultMutate() : {}, [dbContext, triggerBackgroundSync]);/**- * Execute the provided operation (e.g. create/update/delete credential)+ * Change the master password: prove the current one, re-encrypt the Account Key with the new password's KEK, commit+ * on the server, then refresh the local key chain and derivation params. The vault itself is not re-encrypted.*/- const executePasswordChangeOperation = useCallback(async (- currentPasswordHashBase64: string,- newPasswordPlainText: string,- options: VaultMutationOptions- ) : Promise<void> => {- setSyncStatus('Saving changes to vault');-- const data = await webApi.authFetch<PasswordChangeInitiateResponse>('Auth/change-password/initiate');- const currentSalt = data.salt;- const currentServerEphemeral = data.serverEphemeral;-- // The SRP password hash the account's encryption type makes from the unlock key.- const currentPasswordHashString = await SrpAuthService.srpPasswordHash(currentPasswordHashBase64, data.encryptionType);+ const executePasswordChangeOperation = useCallback(async (currentUnlockKeyBase64: string, newPasswordPlainText: string): Promise<void> => {+ const chainJson = await NativeVaultManager.getAccountKeyChain();+ const encryptedAccountKey = chainJson ? (JSON.parse(chainJson) as { encryptedAccountKey?: string }).encryptedAccountKey : undefined;+ const storedParams = await NativeVaultManager.getUnlockKeyDerivationParams();+ if (!encryptedAccountKey || !storedParams) {+ throw new Error('Password change requires an unlocked account-key vault');+ }- // Get username from the auth context, always lowercase and trimmed which is required for the argon2id key derivation- const username = authContext.username?.toLowerCase().trim();- if (!username) {- throw new Error(t('common.errors.unknownError'));+ const challenge = await webApi.authFetch<PasswordChangeInitiateResponse>('Auth/change-password/initiate');+ if (challenge.salt !== (JSON.parse(storedParams) as UnlockKeyDerivationParams).salt) {+ throw new PasswordChangedElsewhereError();}/*** Use srpIdentity from server response if available, otherwise fall back to username.* Note: the fallback can be removed in the future after 0.26.0+ is deployed.*/- const srpIdentity = data.srpIdentity ?? username;-- // Derive the SRP client proof for the current password to authorize the change.- const currentClientProof = await SrpAuthService.deriveClientProof(- currentSalt,- srpIdentity,- currentPasswordHashString,- currentServerEphemeral- );-- // Generate salt and verifier for new password using native SRP- const newSalt = await SrpAuthService.generateSalt();- const newPasswordHash = await EncryptionUtility.deriveKeyFromPassword(newPasswordPlainText, newSalt, data.encryptionType, data.encryptionSettings);- const newPasswordHashString = Buffer.from(newPasswordHash).toString('hex').toUpperCase();-- // Store the new encryption key and derivation parameters locally- try {- const newUnlockKeyDerivationParams : UnlockKeyDerivationParams = {- encryptionType: data.encryptionType,- encryptionSettings: data.encryptionSettings,- salt: newSalt,- };-- await dbContext.storeUnlockKey(Buffer.from(newPasswordHash).toString('base64'));- await dbContext.storeUnlockKeyDerivationParams(newUnlockKeyDerivationParams);-- /**- * Persist the new encrypted database with the new encryption key by starting and committing a transaction.- * which simulates a vault mutation operation. As part of this operation, a new encrypted database is created- * locally which can then be uploaded to the server.- */- await NativeVaultManager.beginTransaction();- await NativeVaultManager.commitTransaction(DEFAULT_VAULT_MUTATION_SCOPE);-- // Unlock the newly persisted database to ensure it works and the new encryption key will be persisted in the keychain.- await NativeVaultManager.unlockVault();- } catch {- // If any part of this fails, we need logout the user as the local vault and stored encryption key are now potentially corrupt.- await authContext.logout(t('common.errors.unknownErrorTryAgain'));- }-- // Generate SRP password change data (verifier for the new password) using native SRP- const newVerifier = await SrpAuthService.deriveVerifier(await SrpAuthService.derivePrivateKey(newSalt, srpIdentity, newPasswordHashString));-- // Prepare vault for password change- const vault = await prepareVaultForPasswordChange();- setSyncStatus(t('vault.uploadingVaultToServer'));-- // Convert default vault object to password change vault object- const passwordChangeVault : VaultPasswordChangeRequest = {- ...vault,- currentClientPublicEphemeral: currentClientProof.clientPublicEphemeral,- currentClientSessionProof: currentClientProof.clientSessionProof,- newPasswordSalt: newSalt,- newPasswordVerifier: newVerifier- };-- try {- // Capture mutation sequence before upload for atomic state update- const syncState = await NativeVaultManager.getSyncState();-- // Upload to server- const response = await webApi.post<typeof passwordChangeVault, VaultPostResponse>('Vault/change-password', passwordChangeVault);-- /**- * Determine if the server responds with vault revision number, as API < 0.17.0 did not.- * TODO: Remove this once we have a minimum required API version of 0.17.0.- */- const newRevisionNumber = response.newRevisionNumber ?? passwordChangeVault.currentRevisionNumber + 1;-- // If we get here, it means we have a valid connection to the server.- await NativeVaultManager.setOfflineMode(false);-- // Update revision atomically with sync state (clears dirty flag if no mutations during upload)- await NativeVaultManager.markVaultClean(syncState.mutationSequence, newRevisionNumber);- options.onSuccess?.();- } catch (error) {- console.error('Error during password change operation:', error);- if (error instanceof PayloadTooLargeError) {- /*- * Server rejected the upload with HTTP 413. Re-throw with a translated, coded error- * so the UI can show a targeted "vault too large" message instead of a generic failure.- */- throw new Error(formatErrorWithCode(t('common.errors.vaultTooLarge'), AppErrorCode.UPLOAD_TOO_LARGE));- }- throw error;- }- }, [dbContext, authContext, webApi, prepareVaultForPasswordChange, t]);+ const srpIdentity = challenge.srpIdentity ?? SrpAuthService.normalizeUsername(authContext.username ?? '');+ const currentPasswordHashString = await SrpAuthService.srpPasswordHash(currentUnlockKeyBase64, challenge.encryptionType);+ const currentProof = await SrpAuthService.deriveClientProof(challenge.salt, srpIdentity, currentPasswordHashString, challenge.serverEphemeral);+ const next = await SrpAuthService.prepareNewPassword(newPasswordPlainText, srpIdentity);+ const { newEncryptedAccountKey } = await MasterPasswordService.reencryptAccountKey(encryptedAccountKey, currentUnlockKeyBase64, next.unlockKeyBase64);++ await webApi.post<PasswordChangeRequest, void>('Auth/change-password', {+ currentClientPublicEphemeral: currentProof.clientPublicEphemeral,+ currentClientSessionProof: currentProof.clientSessionProof,+ newPasswordSalt: next.salt,+ newPasswordVerifier: next.verifier,+ newEncryptedAccountKey,+ newEncryptionType: next.encryptionType,+ newEncryptionSettings: next.encryptionSettings,+ }, false);++ // Like a login: new params first (the sync compares the salt), then cache the server's new chain and reopen the session.+ await dbContext.storeUnlockKeyDerivationParams({ salt: next.salt, encryptionType: next.encryptionType, encryptionSettings: next.encryptionSettings });+ await NativeVaultManager.resolveVaultKey(next.unlockKeyBase64);+ }, [dbContext, authContext, webApi]);/*** Hook to execute a vault mutation which uploads a new encrypted vault to the server.@@ -311,69 +183,22 @@ export function useVaultMutate() : {}, [executeMutateOperation, t]);/**- * Hook to execute a password change which uploads a new encrypted vault to the server- * with updated SRP verifier and salt.- *- * Unlike regular mutations, password change REQUIRES a pre-sync to ensure we're- * re-encrypting the latest vault. Cannot proceed offline.+ * Change the master password, then sync in the background.+ * @throws {IncorrectPasswordError} when the current password does not open the Account Key.+ * @throws {PasswordChangedElsewhereError} when the password was already changed on another device.+ * @throws {ApiRequestError} when the server rejects the change.*/- const executeVaultPasswordChange = useCallback(async (- currentPasswordHashBase64: string,- newPasswordPlainText: string,- options: VaultMutationOptions = {}- ) => {+ const executeVaultPasswordChange = useCallback(async (currentUnlockKeyBase64: string, newPasswordPlainText: string): Promise<void> => {try {setIsLoading(true);- setSyncStatus(t('vault.checkingForVaultUpdates'));-- // Password change requires online - must sync first to get latest vault before re-encryption- const syncSuccess = await new Promise<boolean>((resolve) => {- syncVault({- onStatus: (message) => setSyncStatus(message),- onSuccess: () => resolve(true),- onError: (error) => {- Toast.show({- type: 'error',- text1: t('common.error'),- text2: error,- position: 'bottom'- });- options.onError?.(new Error(error));- resolve(false);- },- onOffline: () => {- Toast.show({- type: 'error',- text1: t('common.error'),- text2: t('vault.errors.passwordChangeRequiresOnline'),- position: 'bottom'- });- options.onError?.(new Error(t('vault.errors.passwordChangeRequiresOnline')));- resolve(false);- }- });- });-- if (!syncSuccess) {- return;- }-- // Now execute the password change operation- await executePasswordChangeOperation(currentPasswordHashBase64, newPasswordPlainText, options);- } catch (error) {- console.error('Error during password change:', error);- Toast.show({- type: 'error',- text1: t('common.error'),- text2: t('common.errors.unknownError'),- position: 'bottom'- });- options.onError?.(error instanceof Error ? error : new Error(t('common.errors.unknownError')));+ setSyncStatus(t('settings.securitySettings.changePassword.initiatingChange'));+ await executePasswordChangeOperation(currentUnlockKeyBase64, newPasswordPlainText);} finally {setIsLoading(false);setSyncStatus('');}- }, [syncVault, executePasswordChangeOperation, t]);+ void triggerBackgroundSync({});+ }, [executePasswordChangeOperation, triggerBackgroundSync, t]);return {executeVaultMutation,