Skip to content

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

Update kotlin native implementation (#1404)

Leendert de Borst921df2bc31a5
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
index 1f592fcde..7ec060cba 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
@@ -128,7 +128,24 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
}
/**
- * Clear the vault.
+ * Clear session data only (for forced logout).
+ * Preserves vault data on disk for recovery on next login.
+ * @param promise The promise to resolve
+ */
+ @ReactMethod
+ override fun clearSession(promise: Promise) {
+ try {
+ vaultStore.clearSession()
+ promise.resolve(null)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error clearing session", e)
+ promise.reject("ERR_CLEAR_SESSION", "Failed to clear session: ${e.message}", e)
+ }
+ }
+
+ /**
+ * Clear all vault data including from persisted storage.
+ * This is used for user-initiated logout.
* @param promise The promise to resolve
*/
@ReactMethod
@@ -1406,4 +1423,109 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
promise.reject("ERR_MARK_VAULT_CLEAN", "Failed to mark vault clean: ${e.message}", e)
}
}
+
+ // MARK: - SRP Functions (via Rust Core UniFFI)
+
+ /**
+ * Generate a cryptographic salt for SRP.
+ * @param promise The promise to resolve with the generated salt (hex string).
+ */
+ @ReactMethod
+ override fun srpGenerateSalt(promise: Promise) {
+ try {
+ val salt = uniffi.aliasvault_core.srpGenerateSalt()
+ promise.resolve(salt)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error generating SRP salt", e)
+ promise.reject("ERR_SRP_GENERATE_SALT", "Failed to generate SRP salt: ${e.message}", e)
+ }
+ }
+
+ /**
+ * Derive the SRP private key (x) from credentials.
+ * @param salt The salt (hex string).
+ * @param identity The identity (username).
+ * @param passwordHash The password hash (hex string).
+ * @param promise The promise to resolve with the private key (hex string).
+ */
+ @ReactMethod
+ override fun srpDerivePrivateKey(salt: String, identity: String, passwordHash: String, promise: Promise) {
+ try {
+ val privateKey = uniffi.aliasvault_core.srpDerivePrivateKey(salt, identity, passwordHash)
+ promise.resolve(privateKey)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error deriving SRP private key", e)
+ promise.reject("ERR_SRP_DERIVE_PRIVATE_KEY", "Failed to derive SRP private key: ${e.message}", e)
+ }
+ }
+
+ /**
+ * Derive the SRP verifier (v) from a private key.
+ * @param privateKey The private key (hex string).
+ * @param promise The promise to resolve with the verifier (hex string).
+ */
+ @ReactMethod
+ override fun srpDeriveVerifier(privateKey: String, promise: Promise) {
+ try {
+ val verifier = uniffi.aliasvault_core.srpDeriveVerifier(privateKey)
+ promise.resolve(verifier)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error deriving SRP verifier", e)
+ promise.reject("ERR_SRP_DERIVE_VERIFIER", "Failed to derive SRP verifier: ${e.message}", e)
+ }
+ }
+
+ /**
+ * Generate client ephemeral values (a, A) for SRP.
+ * @param promise The promise to resolve with JSON containing public and secret values.
+ */
+ @ReactMethod
+ override fun srpGenerateEphemeral(promise: Promise) {
+ try {
+ val ephemeral = uniffi.aliasvault_core.srpGenerateEphemeral()
+ val result = Arguments.createMap()
+ result.putString("public", ephemeral.public)
+ result.putString("secret", ephemeral.secret)
+ promise.resolve(result)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error generating SRP ephemeral", e)
+ promise.reject("ERR_SRP_GENERATE_EPHEMERAL", "Failed to generate SRP ephemeral: ${e.message}", e)
+ }
+ }
+
+ /**
+ * Derive the SRP session key and proof.
+ * @param clientSecret The client secret (a, hex string).
+ * @param serverPublic The server public value (B, hex string).
+ * @param salt The salt (hex string).
+ * @param identity The identity (username).
+ * @param privateKey The private key (x, hex string).
+ * @param promise The promise to resolve with JSON containing key and proof.
+ */
+ @ReactMethod
+ override fun srpDeriveSession(
+ clientSecret: String,
+ serverPublic: String,
+ salt: String,
+ identity: String,
+ privateKey: String,
+ promise: Promise,
+ ) {
+ try {
+ val session = uniffi.aliasvault_core.srpDeriveSession(
+ clientSecret,
+ serverPublic,
+ salt,
+ identity,
+ privateKey,
+ )
+ val result = Arguments.createMap()
+ result.putString("key", session.key)
+ result.putString("proof", session.proof)
+ promise.resolve(result)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error deriving SRP session", e)
+ promise.reject("ERR_SRP_DERIVE_SESSION", "Failed to derive SRP session: ${e.message}", e)
+ }
+ }
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCache.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCache.kt
index ae83fbd0b..ccf44f7c8 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCache.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCache.kt
@@ -27,8 +27,26 @@ class VaultCache(
crypto.clearKey()
}
+ /**
+ * Clear session data only (for forced logout).
+ * Preserves vault data on disk for recovery on next login.
+ * This is used when the user is forcibly logged out (e.g., 401, token revocation)
+ * to allow recovery of unsynced local changes.
+ */
+ fun clearSession() {
+ Log.d(TAG, "Clearing session - preserving vault data for recovery")
+
+ // Clear in-memory data only
+ clearCache()
+
+ // Clear biometric-protected key (user will need to re-authenticate)
+ keystoreProvider.clearKeys()
+ }
+
/**
* Clear all vault data including from persisted storage.
+ * This is used for user-initiated logout where they explicitly
+ * choose to clear all local data.
*/
fun clearVault() {
clearCache()
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
index 586dea677..bf59e99c8 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
@@ -683,8 +683,20 @@ class VaultStore(
cache.clearCache()
}
+ /**
+ * Clear session data only (for forced logout).
+ * Preserves vault data on disk for recovery on next login.
+ * This is used when the user is forcibly logged out (e.g., 401, token revocation)
+ * to allow recovery of unsynced local changes.
+ */
+ fun clearSession() {
+ cache.clearSession()
+ }
+
/**
* Clear all vault data including from persisted storage.
+ * This is used for user-initiated logout where they explicitly
+ * choose to clear all local data.
*/
fun clearVault() {
cache.clearVault()
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt
index 4e3a24adc..21ecb3b7d 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSync.kt
@@ -114,6 +114,21 @@ class VaultSync(
// UPLOAD: Local changes at same revision
performUploadSync(webApiService, mutate, mutationSeqAtStart, retryCount)
}
+ serverRevision < syncState.serverRevision -> {
+ /**
+ * Server revision DECREASED - server data loss/rollback detected.
+ * Client has more advanced revision - upload to recover server state.
+ *
+ * This will create a revision gap in server history (e.g., 95 → 101),
+ * which serves as an audit trail of the recovery event.
+ */
+ Log.w(
+ TAG,
+ "Server data loss detected! Server at rev $serverRevision, " +
+ "client at rev ${syncState.serverRevision}. Uploading to recover server state.",
+ )
+ performUploadSync(webApiService, mutate, mutationSeqAtStart, retryCount)
+ }
else -> {
// Already in sync
metadata.setIsSyncing(false)