Skip to content

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

Update Android Kotlin scaffolding to work with new Rust vault sync engine (#2159)

Leendert de Borstb7efd72d1b98
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
deleted file mode 100644
index 4c70e4124..000000000
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
+++ /dev/null
@@ -1,339 +0,0 @@
-package net.aliasvault.app.rustcore
-
-import android.database.sqlite.SQLiteDatabase
-import android.util.Base64
-import android.util.Log
-import net.aliasvault.app.vaultstore.VaultCrypto
-import org.json.JSONArray
-import org.json.JSONObject
-import java.io.File
-
-/**
- * Service for vault merge operations using the Rust core library.
- * Wraps UniFFI-generated bindings for LWW merge operations on SQLite vault databases.
- */
-object VaultMergeService {
- private const val TAG = "VaultMergeService"
-
- init {
- // Ensure JNA is initialized before loading the Rust library
- JnaInitializer.ensureInitialized()
- System.loadLibrary("aliasvault_core")
- Log.d(TAG, "Rust core library loaded")
- }
-
- /**
- * Base exception for vault merge operations.
- */
- sealed class VaultMergeException(message: String) : Exception(message) {
- /**
- * Exception thrown when JSON parsing or serialization fails.
- */
- class JsonError(message: String) : VaultMergeException("JSON error: $message")
-
- /**
- * Exception thrown when Rust core operations fail.
- */
- class RustError(message: String) : VaultMergeException("Rust error: $message")
-
- /**
- * Exception thrown when database operations fail.
- */
- class DatabaseError(message: String) : VaultMergeException("Database error: $message")
-
- /**
- * Exception thrown when input validation fails.
- */
- class InvalidInput(message: String) : VaultMergeException("Invalid input: $message")
- }
-
- /**
- * Get the list of syncable table names from Rust.
- */
- fun getTableNames(): List<String> {
- return uniffi.aliasvault_core.getSyncableTableNames()
- }
-
- /**
- * Merge local and server vaults using LWW strategy.
- */
- @Suppress("SwallowedException") // Exceptions are logged and re-thrown with context
- @Throws(VaultMergeException::class)
- fun mergeVaults(
- localVaultBase64: String,
- serverVaultBase64: String,
- encryptionKey: ByteArray,
- tempDir: File,
- ): String {
- // Decrypt both vaults
- // Vault format: base64(encrypted(base64(sqlite_bytes)))
- // After AES decrypt we get base64(sqlite_bytes), need to decode again
- val localDecrypted = try {
- val decryptedBase64 = VaultCrypto.decrypt(Base64.decode(localVaultBase64, Base64.DEFAULT), encryptionKey)
- Base64.decode(decryptedBase64, Base64.NO_WRAP)
- } catch (e: Exception) {
- Log.e(TAG, "Failed to decrypt local vault", e)
- throw VaultMergeException.InvalidInput("Failed to decrypt local vault: ${e.message}")
- }
-
- val serverDecrypted = try {
- val decryptedBase64 = VaultCrypto.decrypt(Base64.decode(serverVaultBase64, Base64.DEFAULT), encryptionKey)
- Base64.decode(decryptedBase64, Base64.NO_WRAP)
- } catch (e: Exception) {
- Log.e(TAG, "Failed to decrypt server vault", e)
- throw VaultMergeException.InvalidInput("Failed to decrypt server vault: ${e.message}")
- }
-
- // Create temporary database files
- // Ensure the temp directory exists (may not on first app run or after cache clear)
- if (!tempDir.exists()) {
- tempDir.mkdirs()
- }
-
- // Verify directory exists and is writable
- if (!tempDir.exists() || !tempDir.isDirectory) {
- throw VaultMergeException.DatabaseError("Temp directory does not exist or is not a directory: ${tempDir.absolutePath}")
- }
- if (!tempDir.canWrite()) {
- throw VaultMergeException.DatabaseError("Temp directory is not writable: ${tempDir.absolutePath}")
- }
-
- val localDbFile = File(tempDir, "local_merge_temp.db")
- val serverDbFile = File(tempDir, "server_merge_temp.db")
-
- try {
- // Write decrypted databases to temp files using FileOutputStream to ensure proper flushing
- java.io.FileOutputStream(localDbFile).use { fos ->
- fos.write(localDecrypted)
- fos.fd.sync()
- }
-
- java.io.FileOutputStream(serverDbFile).use { fos ->
- fos.write(serverDecrypted)
- fos.fd.sync()
- }
-
- // Verify files were written
- if (!localDbFile.exists() || localDbFile.length() == 0L) {
- throw VaultMergeException.DatabaseError(
- "Failed to write local temp db: exists=${localDbFile.exists()}, len=${localDbFile.length()}",
- )
- }
- if (!serverDbFile.exists() || serverDbFile.length() == 0L) {
- throw VaultMergeException.DatabaseError(
- "Failed to write server temp db: exists=${serverDbFile.exists()}, len=${serverDbFile.length()}",
- )
- }
-
- // The merge base is the SERVER database (opened read-write so the merge statements land on
- // it); the local database is only read for its offline changes.
- val localDb = SQLiteDatabase.openDatabase(localDbFile.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
- val serverDb = SQLiteDatabase.openDatabase(serverDbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)
-
- try {
- // Read all syncable tables
- val tableNames = getTableNames()
- val localTables = JSONArray()
- val serverTables = JSONArray()
-
- for (tableName in tableNames) {
- localTables.put(
- JSONObject().apply {
- put("name", tableName)
- put("records", readTable(localDb, tableName))
- },
- )
- serverTables.put(
- JSONObject().apply {
- put("name", tableName)
- put("records", readTable(serverDb, tableName))
- },
- )
- }
-
- // Call Rust merge
- val mergeInput = JSONObject().apply {
- put("local_tables", localTables)
- put("server_tables", serverTables)
- }
-
- val outputJson = uniffi.aliasvault_core.mergeVaultsJson(mergeInput.toString())
- val statements = parseStatements(outputJson)
-
- // Apply SQL statements to the SERVER database (the merge base). The exported server DB
- // carries the newest codec overflow carrier untouched.
- serverDb.beginTransaction()
- try {
- for ((sql, params) in statements) {
- serverDb.execSQL(sql, params.toTypedArray())
- }
- serverDb.setTransactionSuccessful()
- } finally {
- serverDb.endTransaction()
- }
-
- localDb.close()
- serverDb.close()
-
- // Encrypt and return
- // Output format must match input: base64(encrypted(base64(sqlite_bytes)))
- val mergedData = serverDbFile.readBytes()
- val mergedBase64 = Base64.encodeToString(mergedData, Base64.NO_WRAP)
- val encrypted = VaultCrypto.encrypt(mergedBase64.toByteArray(Charsets.UTF_8), encryptionKey)
- return Base64.encodeToString(encrypted, Base64.DEFAULT)
- } finally {
- if (localDb.isOpen) localDb.close()
- if (serverDb.isOpen) serverDb.close()
- }
- } finally {
- localDbFile.delete()
- serverDbFile.delete()
- }
- }
-
- /**
- * Prune expired items from trash.
- */
- @Suppress("SwallowedException") // Exceptions are logged and re-thrown with context
- @Throws(VaultMergeException::class)
- fun pruneVault(
- vaultBase64: String,
- retentionDays: Int = 30,
- encryptionKey: ByteArray,
- tempDir: File,
- ): Pair<String, Int> {
- // Vault format: base64(encrypted(base64(sqlite_bytes)))
- // After AES decrypt we get base64(sqlite_bytes), need to decode again
- val decrypted = try {
- val decryptedBase64 = VaultCrypto.decrypt(Base64.decode(vaultBase64, Base64.DEFAULT), encryptionKey)
- Base64.decode(decryptedBase64, Base64.NO_WRAP)
- } catch (e: Exception) {
- Log.e(TAG, "Failed to decrypt vault for pruning", e)
- throw VaultMergeException.InvalidInput("Failed to decrypt vault: ${e.message}")
- }
-
- // Ensure the temp directory exists (may not on first app run or after cache clear)
- if (!tempDir.exists()) {
- tempDir.mkdirs()
- }
- val dbFile = File(tempDir, "prune_temp.db")
-
- try {
- dbFile.writeBytes(decrypted)
- val db = SQLiteDatabase.openDatabase(dbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)
-
- try {
- // Get the per-table SELECT queries clients should run to build `PruneInput`.
- val pruneTableQueries = uniffi.aliasvault_core.getPruneTableQueries()
- val tables = JSONArray()
-
- for (tableQuery in pruneTableQueries) {
- tables.put(
- JSONObject().apply {
- put("name", tableQuery.name)
- put("records", readQuery(db, tableQuery.name, tableQuery.query))
- },
- )
- }
-
- // Call Rust prune - use ISO8601 format: YYYY-MM-DDTHH:MM:SS.sssZ
- val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", java.util.Locale.US)
- dateFormat.timeZone = java.util.TimeZone.getTimeZone("UTC")
- val currentTime = dateFormat.format(java.util.Date())
-
- val pruneInput = JSONObject().apply {
- put("tables", tables)
- put("retention_days", retentionDays)
- put("current_time", currentTime)
- }
-
- val outputJson = uniffi.aliasvault_core.pruneVaultJson(pruneInput.toString())
- val statements = parseStatements(outputJson)
-
- // Apply SQL statements
- db.beginTransaction()
- try {
- for ((sql, params) in statements) {
- db.execSQL(sql, params.toTypedArray())
- }
- db.setTransactionSuccessful()
- } finally {
- db.endTransaction()
- }
-
- db.close()
-
- // Encrypt and return
- // Output format must match input: base64(encrypted(base64(sqlite_bytes)))
- val prunedData = dbFile.readBytes()
- val prunedBase64 = Base64.encodeToString(prunedData, Base64.NO_WRAP)
- val encrypted = VaultCrypto.encrypt(prunedBase64.toByteArray(Charsets.UTF_8), encryptionKey)
- return Pair(Base64.encodeToString(encrypted, Base64.DEFAULT), statements.size)
- } finally {
- if (db.isOpen) db.close()
- }
- } finally {
- dbFile.delete()
- }
- }
-
- private fun readTable(db: SQLiteDatabase, tableName: String): JSONArray {
- return readQuery(db, tableName, "SELECT * FROM $tableName")
- }
-
- private fun readQuery(db: SQLiteDatabase, tableName: String, query: String): JSONArray {
- val records = JSONArray()
-
- // Check if table exists
- val checkCursor = db.rawQuery(
- "SELECT name FROM sqlite_master WHERE type='table' AND name=?",
- arrayOf(tableName),
- )
- val tableExists = checkCursor.moveToFirst()
- checkCursor.close()
- if (!tableExists) return records
-
- // Read all records
- val cursor = db.rawQuery(query, null)
- try {
- while (cursor.moveToNext()) {
- val record = JSONObject()
- for (i in 0 until cursor.columnCount) {
- val columnName = cursor.getColumnName(i)
- val value = when (cursor.getType(i)) {
- android.database.Cursor.FIELD_TYPE_INTEGER -> cursor.getLong(i)
- android.database.Cursor.FIELD_TYPE_FLOAT -> cursor.getDouble(i)
- android.database.Cursor.FIELD_TYPE_STRING -> cursor.getString(i)
- android.database.Cursor.FIELD_TYPE_BLOB -> Base64.encodeToString(cursor.getBlob(i), Base64.DEFAULT)
- else -> JSONObject.NULL
- }
- record.put(columnName, value)
- }
- records.put(record)
- }
- } finally {
- cursor.close()
- }
-
- return records
- }
-
- private fun parseStatements(json: String): List<Pair<String, List<String?>>> {
- val output = JSONObject(json)
- val statementsArray = output.optJSONArray("statements") ?: JSONArray()
- val statements = mutableListOf<Pair<String, List<String?>>>()
-
- for (i in 0 until statementsArray.length()) {
- val stmt = statementsArray.getJSONObject(i)
- val sql = stmt.getString("sql")
- val paramsArray = stmt.optJSONArray("params") ?: JSONArray()
- val params = (0 until paramsArray.length()).map { j ->
- val param = paramsArray.opt(j)
- if (param == JSONObject.NULL || param == null) null else param.toString()
- }
- statements.add(Pair(sql, params))
- }
-
- return statements
- }
-}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/AppError.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/AppError.kt
index 5b103dd76..925c2b234 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/AppError.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/AppError.kt
@@ -63,6 +63,14 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
cause: Throwable? = null,
) : AppError(message, cause)
+ /**
+ * Error indicating the server answered a request with an unexpected HTTP failure.
+ */
+ class ServerError(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Server error: $message", cause)
+
// Version/compatibility errors
/**
* Error indicating client version not supported.
@@ -105,6 +113,14 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
cause: Throwable? = null,
) : AppError(message, cause)
+ /**
+ * Error indicating the server's vault snapshot could not be assembled into a vault.
+ */
+ class SyncVaultFetchFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Server vault could not be assembled: $message", cause)
+
// Decryption errors
/**
* Error indicating failed to decrypt vault.
@@ -235,6 +251,30 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
cause: Throwable? = null,
) : AppError(message, cause)
+ /**
+ * Error indicating a storage read (state, database, at-rest vault) failed.
+ */
+ class StorageReadFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Storage read failed: $message", cause)
+
+ /**
+ * Error indicating a storage write (state, database, at-rest vault) failed.
+ */
+ class StorageWriteFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Storage write failed: $message", cause)
+
+ /**
+ * Error indicating the staging database could not be opened.
+ */
+ class DatabaseInitFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Database init failed: $message", cause)
+
/**
* Error indicating failed to store vault.
*/
@@ -286,6 +326,14 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
cause: Throwable? = null,
) : AppError(message, cause)
+ /**
+ * Error indicating the vault migration check failed.
+ */
+ class MigrationCheckFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Migration check failed: $message", cause)
+
// Generic errors
/**
* Error indicating unknown error.
@@ -303,6 +351,31 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
cause: Throwable? = null,
) : AppError("Parse error: $message", cause)
+ // Sync engine failures that name their cause in the message
+ /**
+ * Error indicating a server response is not the JSON shape this client expects.
+ */
+ class SyncResponseInvalid(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Invalid server response: $message", cause)
+
+ /**
+ * Error indicating the core library (codec, merge or crypto) refused the vault data.
+ */
+ class SyncCodecFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Vault codec failed: $message", cause)
+
+ /**
+ * Error indicating the sync engine hit a state it has no rule for.
+ */
+ class SyncEngineFailed(
+ message: String,
+ cause: Throwable? = null,
+ ) : AppError("Sync engine failed: $message", cause)
+
/**
* Get the error code string for React Native bridge.
*
@@ -326,11 +399,13 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
is ServerUnavailable -> "E-201"
is NetworkError -> "E-202"
is Timeout -> "E-203"
+ is ServerError -> "E-204"
is ClientVersionNotSupported -> "E-301"
is ServerVersionNotSupported -> "E-302"
is VaultVersionIncompatible -> "E-303"
is VaultMergeRequired -> "E-401"
is VaultOutdated -> "E-402"
+ is SyncVaultFetchFailed -> "E-404"
is VaultDecryptFailed -> "E-501"
is EncryptionKeyNotFound -> "E-502"
is Base64DecodeFailed -> "E-503"
@@ -347,14 +422,21 @@ sealed class AppError(message: String, cause: Throwable? = null) : Exception(mes
is BiometricNotAvailable -> "E-514"
is BiometricNotEnrolled -> "E-515"
is BiometricLockout -> "E-516"
+ is StorageReadFailed -> "E-601"
+ is StorageWriteFailed -> "E-602"
+ is DatabaseInitFailed -> "E-603"
is VaultStoreFailed -> "E-604"
is VaultMergeFailed -> "E-701"
is MergeUploadFailed -> "E-705"
is VaultUploadFailed -> "E-801"
is VaultTooLarge -> "E-804"
is MaxRetriesReached -> "E-901"
+ is MigrationCheckFailed -> "E-903"
is UnknownError -> "E-001"
is ParseError -> "E-002"
+ is SyncResponseInvalid -> "E-003"
+ is SyncCodecFailed -> "E-004"
+ is SyncEngineFailed -> "E-005"
}
/**
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 ccf44f7c8..25eb9899a 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
@@ -54,6 +54,7 @@ class VaultCache(
keystoreProvider.clearKeys()
storageProvider.clearStorage()
+ storageProvider.clearSyncEngineState()
}
// endregion
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt
index a62a09356..a7d648e5b 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt
@@ -25,7 +25,7 @@ class VaultCrypto(
private const val BIOMETRICS_AUTH_METHOD = "faceid"
/**
- * Raw AES-GCM encryption (for VaultMergeService).
+ * Raw AES-GCM encryption with a caller-supplied key.
* Encrypts data using AES-256-GCM with a provided key.
*/
fun encrypt(data: ByteArray, key: ByteArray): ByteArray {
@@ -49,7 +49,7 @@ class VaultCrypto(
}
/**
- * Raw AES-GCM decryption (for VaultMergeService).
+ * Raw AES-GCM decryption with a caller-supplied key.
* Decrypts data using AES-256-GCM with a provided key.
*/
fun decrypt(encryptedData: ByteArray, key: ByteArray): ByteArray {
@@ -76,6 +76,11 @@ class VaultCrypto(
*/
internal var encryptionKey: ByteArray? = null
+ /**
+ * The account private key (JWK) of the unlocked session, when the sync engine opened it; memory only.
+ */
+ internal var accountPrivateKey: String? = null
+
// region Key Derivation
/**
@@ -155,6 +160,7 @@ class VaultCrypto(
*/
fun clearEncryptionKeyFromMemory() {
this.encryptionKey = null
+ this.accountPrivateKey = null
}
/**
@@ -171,6 +177,49 @@ class VaultCrypto(
return storageProvider.getKeyDerivationParams()
}
+ /**
+ * Store the account-key chain the native password unlock unwraps: JSON with the Account Key wrapped by the
+ * password-derived KEK ("encryptedAccountKey") and the VEK wrapped by the Account Key ("encryptedVek").
+ * Null means a legacy account whose KEK encrypts the vault directly.
+ */
+ fun storeAccountKeyChain(chainJson: String?) {
+ storageProvider.setAccountKeyChain(chainJson?.takeIf { it.isNotEmpty() })
+ }
+
+ /**
+ * The stored account-key chain JSON, or null for a legacy account.
+ */
+ fun getAccountKeyChain(): String? {
+ return storageProvider.getAccountKeyChain()
+ }
+
+ /**
+ * Turn a password-derived key (KEK) into the vault encryption key: unwrap KEK > Account Key > VEK when the
+ * account has a key chain, otherwise the derived key is the vault key itself (legacy account).
+ */
+ fun resolveVaultEncryptionKey(derivedKey: ByteArray): ByteArray {
+ val chainJson = getAccountKeyChain() ?: return derivedKey
+ val chain = JSONObject(chainJson)
+ val encryptedAccountKey = chain.optString("encryptedAccountKey").takeIf { it.isNotEmpty() } ?: return derivedKey
+ val encryptedVek = chain.optString("encryptedVek").takeIf { it.isNotEmpty() } ?: error("Account key chain is missing the encrypted VEK")
+
+ val accountKey = decrypt(Base64.decode(encryptedAccountKey, Base64.NO_WRAP), derivedKey)
+ return decrypt(Base64.decode(encryptedVek, Base64.NO_WRAP), accountKey)
+ }
+
+ /**
+ * The vault key an unlock method (PIN) stored, as base64. A snapshot taken before the account moved onto the key
+ * hierarchy still holds the password-derived key; when that opens the stored chain, the VEK behind it is returned.
+ */
+ @Suppress("SwallowedException")
+ fun resolveStoredUnlockKey(base64Key: String): String {
+ return try {
+ Base64.encodeToString(resolveVaultEncryptionKey(Base64.decode(base64Key, Base64.NO_WRAP)), Base64.NO_WRAP)
+ } catch (e: Exception) {
+ base64Key
+ }
+ }
+
/**
* Check if biometric authentication is enabled and available.
*/
@@ -217,6 +266,7 @@ class VaultCrypto(
*/
fun clearKey() {
encryptionKey = null
+ accountPrivateKey = null
}
// endregion
@@ -224,10 +274,17 @@ class VaultCrypto(
// region Encryption/Decryption
/**
- * Decrypt data.
+ * Decrypt data to text.
*/
fun decryptData(encryptedData: String, authMethods: String): String {
- var decryptedResult: String? = null
+ return String(decryptDataBytes(encryptedData, authMethods), Charsets.UTF_8)
+ }
+
+ /**
+ * Decrypt data to raw bytes.
+ */
+ fun decryptDataBytes(encryptedData: String, authMethods: String): ByteArray {
+ var decryptedResult: ByteArray? = null
var error: Exception? = null
val latch = java.util.concurrent.CountDownLatch(1)
@@ -247,8 +304,7 @@ class VaultCrypto(
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmSpec)
- val decrypted = cipher.doFinal(encryptedContent)
- decryptedResult = String(decrypted, Charsets.UTF_8)
+ decryptedResult = cipher.doFinal(encryptedContent)
} catch (e: Exception) {
error = AppError.VaultDecryptFailed(cause = e)
Log.e(TAG, "Error decrypting data", e)
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultData.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultData.kt
index a04dd4b04..8d932efd2 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultData.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultData.kt
@@ -1,81 +1,18 @@
package net.aliasvault.app.vaultstore
/**
- * Vault data from API.
+ * Outcome of a status check: whether the server holds newer state than this device.
*
- * @property username The username associated with the vault.
- * @property blob The encrypted vault blob.
- * @property version The vault version.
- * @property currentRevisionNumber The current revision number of the vault.
- * @property encryptionPublicKey The public key used for encryption.
- * @property credentialsCount The number of credentials in the vault.
- * @property emailAddressList The list of email addresses.
- * @property privateEmailDomainList The list of private email domains.
- * @property hiddenPrivateEmailDomainList The list of hidden private email domains.
- * @property publicEmailDomainList The list of public email domains.
- * @property createdAt The timestamp when the vault was created.
- * @property updatedAt The timestamp when the vault was last updated.
- */
-data class VaultData(
- val username: String,
- val blob: String,
- val version: String,
- val currentRevisionNumber: Int,
- val encryptionPublicKey: String,
- val credentialsCount: Int,
- val emailAddressList: List<String>,
- val privateEmailDomainList: List<String>,
- val hiddenPrivateEmailDomainList: List<String>,
- val publicEmailDomainList: List<String>,
- val createdAt: String,
- val updatedAt: String,
-)
-
-/**
- * Vault response from Vault GET endpoint.
- *
- * @property status The HTTP status code of the response.
- * @property vault The vault data returned from the server.
- */
-data class VaultResponse(
- val status: Int,
- val vault: VaultData,
-)
-
-/**
- * Result of checking for new vault version.
- *
- * @property isNewVersionAvailable Whether a new version is available on the server.
- * @property newRevision The new revision number if available.
- * @property serverRevision The current revision number on the server.
+ * @property isNewVersionAvailable Whether the server holds newer state.
* @property syncState The current sync state of the vault.
*/
data class VaultVersionCheckResult(
val isNewVersionAvailable: Boolean,
- val newRevision: Int?,
- val serverRevision: Int,
val syncState: net.aliasvault.app.vaultstore.models.SyncState,
)
/**
- * Result of vault upload.
- *
- * @property success Whether the upload was successful.
- * @property status The HTTP status code of the upload response.
- * @property newRevisionNumber The new revision number after upload.
- * @property mutationSeqAtStart The mutation sequence number at the start of upload.
- * @property error The error message if upload failed.
- */
-data class VaultUploadResult(
- val success: Boolean,
- val status: Int,
- val newRevisionNumber: Int,
- val mutationSeqAtStart: Int,
- val error: String? = null,
-)
-
-/**
- * Action taken during sync.
+ * What a sync did.
*
* @property value The string value of the sync action.
*/
@@ -88,13 +25,13 @@ enum class SyncAction(val value: String) {
}
/**
- * Result of syncVaultWithServer operation.
+ * Result of a full vault sync.
*
* @property success Whether the sync was successful.
* @property action The action taken during sync.
- * @property newRevision The new revision number after sync.
- * @property wasOffline Whether the sync occurred while offline.
- * @property error The error message if sync failed.
+ * @property newRevision The vault revision after the sync.
+ * @property wasOffline Whether the sync ran offline.
+ * @property error The error code, if any.
*/
data class VaultSyncResult(
val success: Boolean,
@@ -102,4 +39,5 @@ data class VaultSyncResult(
val newRevision: Int,
val wasOffline: Boolean,
val error: String? = null,
+ val errorMessage: String? = null,
)
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt
index aaed32c81..e67febe99 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt
@@ -47,6 +47,15 @@ class VaultDatabase(
// endregion
+ /**
+ * The id of the user's personal manifest as the last sync recorded it (engine state, stored as `{"v": id}`),
+ * or null before the first pull.
+ */
+ fun getPersonalManifestId(): String? {
+ val json = storageProvider.getSyncEngineState("vaultPersonalManifestId") ?: return null
+ return org.json.JSONObject(json).optString("v").takeIf { it.isNotEmpty() }
+ }
+
// region Vault Unlock
/**
@@ -54,10 +63,10 @@ class VaultDatabase(
*/
fun unlockVault(authMethods: String) {
val encryptedDbBase64 = getEncryptedDatabase()
- val decryptedDbBase64 = crypto.decryptData(encryptedDbBase64, authMethods)
+ val decrypted = crypto.decryptDataBytes(encryptedDbBase64, authMethods)
try {
- setupDatabaseWithDecryptedData(decryptedDbBase64)
+ setupDatabaseWithDecryptedData(decrypted)
} catch (e: Exception) {
Log.e(TAG, "Error unlocking vault", e)
throw e
@@ -81,16 +90,24 @@ class VaultDatabase(
* foreign keys, indexes, triggers, and views from file to memory.
* This is equivalent to the Swift implementation using the backup API.
*/
- private fun setupDatabaseWithDecryptedData(decryptedDbBase64: String) {
+ /**
+ * Setup the database with the decrypted data. The plaintext is either the raw SQLite bytes (what the app
+ * writes now, same as the browser extension) or base64 text of them (what older builds wrote).
+ */
+ private fun setupDatabaseWithDecryptedData(decrypted: ByteArray) {
var tempDbFile: File? = null
var sourceDb: io.requery.android.database.sqlite.SQLiteDatabase? = null
try {
- // Step 1: Decode base64
- val decryptedDbData = try {
- Base64.decode(decryptedDbBase64, Base64.NO_WRAP)
- } catch (e: Exception) {
- Log.e(TAG, "Failed to decode base64 data after decryption", e)
- throw AppError.Base64DecodeFailed(cause = e)
+ // Step 1: Take the SQLite bytes as-is, or decode the legacy base64 text.
+ val decryptedDbData = if (isSqliteDatabase(decrypted)) {
+ decrypted
+ } else {
+ try {
+ Base64.decode(String(decrypted, Charsets.UTF_8), Base64.NO_WRAP)
+ } catch (e: Exception) {
+ Log.e(TAG, "Failed to decode base64 data after decryption", e)
+ throw AppError.Base64DecodeFailed(cause = e)
+ }
}
// Step 2: Write decrypted data to temp file
@@ -359,4 +376,12 @@ class VaultDatabase(
}
// endregion
+
+ /**
+ * Whether these plaintext bytes are a SQLite database rather than base64 text of one.
+ */
+ private fun isSqliteDatabase(bytes: ByteArray): Boolean {
+ val header = "SQLite format 3\u0000".toByteArray(Charsets.UTF_8)
+ return bytes.size >= header.size && header.indices.all { bytes[it] == header[it] }
+ }
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMutate.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMutate.kt
deleted file mode 100644
index aed5f474e..000000000
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMutate.kt
+++ /dev/null
@@ -1,308 +0,0 @@
-package net.aliasvault.app.vaultstore
-
-import android.util.Log
-import net.aliasvault.app.exceptions.SerializationException
-import net.aliasvault.app.exceptions.VaultOperationException
-import net.aliasvault.app.rustcore.VaultMergeService
-import net.aliasvault.app.vaultstore.repositories.ItemRepository
-import net.aliasvault.app.vaultstore.storageprovider.StorageProvider
-import org.json.JSONArray
-import org.json.JSONObject
-
-/**
- * Handles vault mutation operations (uploading changes to server).
- */
-@Suppress("LongParameterList") // Aggregates the collaborators required for vault mutation and pruning
-class VaultMutate(
- private val database: VaultDatabase,
- private val itemRepository: ItemRepository,
- private val metadata: VaultMetadataManager,
- private val crypto: VaultCrypto,
- private val auth: VaultAuth,
- private val storageProvider: StorageProvider,
-) {
- companion object {
- private const val TAG = "VaultMutate"
-
- // Trash retention. Soft-deleted items stay in the recycle bin for this many
- // days before the Rust pruner permanently removes them on the next upload.
- // This value is declared in other places as well, make sure to update them
- // when updating this value.
- private const val TRASH_RETENTION_DAYS = 30
- }
-
- // region Vault Mutation
-
- /**
- * Execute a vault mutation operation.
- * This captures mutation sequence for race detection and clears dirty after successful upload.
- */
- suspend fun mutateVault(webApiService: net.aliasvault.app.webapi.WebApiService): Boolean {
- // Capture mutation sequence for race detection
- val mutationSeqAtStart = metadata.getMutationSequence()
-
- // Prune expired trash items and cleanup orphaned logos
- pruneLocalVault()
-
- try {
- val vault = prepareVault()
-
- val json = JSONObject()
- json.put("blob", vault.blob)
- json.put("createdAt", vault.createdAt)
- json.put("credentialsCount", vault.credentialsCount)
- json.put("currentRevisionNumber", vault.currentRevisionNumber)
- json.put("emailAddressList", JSONArray(vault.emailAddressList))
- json.put("encryptionPublicKey", vault.encryptionPublicKey)
- json.put("updatedAt", vault.updatedAt)
- json.put("username", vault.username)
- json.put("version", vault.version)
-
- val response = webApiService.executeRequest(
- method = "POST",
- endpoint = "Vault",
- body = json.toString(),
- headers = mapOf("Content-Type" to "application/json"),
- requiresAuth = true,
- )
-
- if (response.statusCode != 200) {
- Log.e(TAG, "Server rejected vault upload with status ${response.statusCode}")
- throw VaultOperationException("Server returned error: ${response.statusCode}")
- }
-
- val vaultResponse = try {
- val responseJson = JSONObject(response.body)
- VaultPostResponse(
- status = responseJson.getInt("status"),
- newRevisionNumber = responseJson.getInt("newRevisionNumber"),
- )
- } catch (e: Exception) {
- Log.e(TAG, "Failed to parse vault upload response", e)
- throw SerializationException("Failed to parse vault upload response: ${e.message}", e)
- }
-
- when (vaultResponse.status) {
- 0 -> {
- // Success - update revision and clear dirty (if no mutations during upload)
- metadata.markVaultClean(mutationSeqAtStart, vaultResponse.newRevisionNumber)
- metadata.setOfflineMode(false)
- return true
- }
- 1 -> throw VaultOperationException("Vault merge required")
- 2 -> throw VaultOperationException("Vault is outdated, please sync first")
- else -> throw VaultOperationException("Failed to upload vault")
- }
- } catch (e: Exception) {
- Log.e(TAG, "Error mutating vault", e)
- throw e
- }
- }
-
- /**
- * Upload the vault to the server and return detailed result.
- * This is used for sync operations where race detection is needed.
- */
- suspend fun uploadVault(webApiService: net.aliasvault.app.webapi.WebApiService): VaultUploadResult {
- val mutationSeqAtStart = metadata.getMutationSequence()
-
- // Prune expired trash items and cleanup orphaned logos
- pruneLocalVault()
-
- return try {
- val vault = prepareVault()
-
- val json = JSONObject()
- json.put("blob", vault.blob)
- json.put("createdAt", vault.createdAt)
- json.put("credentialsCount", vault.credentialsCount)
- json.put("currentRevisionNumber", vault.currentRevisionNumber)
- json.put("emailAddressList", JSONArray(vault.emailAddressList))
- json.put("encryptionPublicKey", vault.encryptionPublicKey)
- json.put("updatedAt", vault.updatedAt)
- json.put("username", vault.username)
- json.put("version", vault.version)
-
- val response = try {
- webApiService.executeRequest(
- method = "POST",
- endpoint = "Vault",
- body = json.toString(),
- headers = mapOf("Content-Type" to "application/json"),
- requiresAuth = true,
- )
- } catch (e: Exception) {
- return VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Network error: ${e.message}",
- )
- }
-
- if (response.statusCode != 200) {
- return VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Server returned error: ${response.statusCode}",
- )
- }
-
- val vaultResponse = try {
- val responseJson = JSONObject(response.body)
- VaultPostResponse(
- status = responseJson.getInt("status"),
- newRevisionNumber = responseJson.getInt("newRevisionNumber"),
- )
- } catch (e: Exception) {
- return VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Failed to parse response: ${e.message}",
- )
- }
-
- if (vaultResponse.status == 0) {
- // Success - update local revision number and clear offline mode
- metadata.setVaultRevisionNumber(vaultResponse.newRevisionNumber)
- metadata.setOfflineMode(false)
- }
-
- VaultUploadResult(
- success = vaultResponse.status == 0,
- status = vaultResponse.status,
- newRevisionNumber = vaultResponse.newRevisionNumber,
- mutationSeqAtStart = mutationSeqAtStart,
- error = if (vaultResponse.status != 0) "Vault upload returned status ${vaultResponse.status}" else null,
- )
- } catch (e: Exception) {
- Log.e(TAG, "Error uploading vault", e)
- VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Error uploading vault: ${e.message}",
- )
- }
- }
-
- // endregion
-
- // region Internal Helpers
-
- private fun prepareVault(): VaultUpload {
- val currentRevision = metadata.getVaultRevisionNumber()
-
- val encryptedDb = database.getEncryptedDatabase()
-
- val username = metadata.getUsername()
- ?: throw VaultOperationException("Username not found")
-
- if (!database.isVaultUnlocked()) {
- throw VaultOperationException("Vault must be unlocked to prepare for upload")
- }
-
- // Get all items to count them and extract private email addresses
- val items = itemRepository.getAll()
-
- val metadataObj = metadata.getVaultMetadataObject()
- val privateEmailDomains = metadataObj?.privateEmailDomains ?: emptyList()
-
- // Extract private email addresses from items using the email field
- val privateEmailAddresses = items
- .mapNotNull { it.email }
- .filter { email ->
- privateEmailDomains.any { domain ->
- email.lowercase().endsWith("@${domain.lowercase()}")
- }
- }
- .distinct()
-
- val dbVersion = itemRepository.getDatabaseVersion()
-
- @Suppress("SwallowedException")
- val version = try {
- // Try to get version from storage provider context
- val context = database.javaClass.getDeclaredField("storageProvider")
- .get(database) as? net.aliasvault.app.vaultstore.storageprovider.AndroidStorageProvider
- val pm = context?.javaClass?.getDeclaredField("context")?.get(context)
- as? android.content.Context
- pm?.packageManager?.getPackageInfo(pm.packageName, 0)?.versionName ?: "0.0.0"
- } catch (e: Exception) {
- "0.0.0"
- }
-
- val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", java.util.Locale.US)
- dateFormat.timeZone = java.util.TimeZone.getTimeZone("UTC")
- val now = dateFormat.format(java.util.Date())
-
- return VaultUpload(
- blob = encryptedDb,
- createdAt = now,
- credentialsCount = items.size,
- currentRevisionNumber = currentRevision,
- emailAddressList = privateEmailAddresses,
- // TODO: add public RSA encryption key to payload when implementing vault creation from mobile app. Currently only web app does this.
- encryptionPublicKey = "",
- updatedAt = now,
- username = username,
- version = dbVersion,
- )
- }
-
- /**
- * Run the Rust vault pruner against the locally-stored encrypted vault and,
- * if any rows were pruned, persist the cleaned version and reload the
- * in-memory database so subsequent reads see the pruned state.
- *
- * All errors are swallowed: pruning is best-effort and must never block
- * the surrounding upload.
- */
- private fun pruneLocalVault() {
- val encryptionKey = crypto.encryptionKey ?: return
-
- try {
- val (prunedBase64, prunedCount) = VaultMergeService.pruneVault(
- vaultBase64 = database.getEncryptedDatabase(),
- retentionDays = TRASH_RETENTION_DAYS,
- encryptionKey = encryptionKey,
- tempDir = storageProvider.getCacheDir(),
- )
- if (prunedCount == 0) return
-
- database.storeEncryptedDatabase(prunedBase64)
- database.unlockVault(auth.getAuthMethods())
- } catch (e: Exception) {
- Log.w(TAG, "Vault prune failed, continuing with upload", e)
- }
- }
-
- // endregion
-
- // region Data Models
-
- private data class VaultUpload(
- val blob: String,
- val createdAt: String,
- val credentialsCount: Int,
- val currentRevisionNumber: Int,
- val emailAddressList: List<String>,
- val encryptionPublicKey: String,
- val updatedAt: String,
- val username: String,
- val version: String,
- )
-
- private data class VaultPostResponse(
- val status: Int,
- val newRevisionNumber: Int,
- )
-
- // endregion
-}
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 8b0dccb9a..ebcc0f517 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
@@ -125,8 +125,7 @@ class VaultStore(
},
onBackground = { clearLastSuccessfulAuth() },
)
- private val sync = VaultSync(database, metadata, crypto, storageProvider, itemRepository)
- private val mutate = VaultMutate(database, itemRepository, metadata, crypto, auth, storageProvider)
+ private val sync = VaultSync(this, storageProvider)
private val cache = VaultCache(crypto, database, keystoreProvider, storageProvider)
private val passkey = VaultPasskey(database)
private val pin by lazy {
@@ -218,6 +217,29 @@ class VaultStore(
return crypto.getEncryptionKeyDerivationParams()
}
+ /**
+ * Store the account-key chain the native password unlock unwraps (null for a legacy account).
+ */
+ fun storeAccountKeyChain(chainJson: String?) {
+ crypto.storeAccountKeyChain(chainJson)
+ }
+
+ /**
+ * Get the stored account-key chain JSON, or null for a legacy account.
+ */
+ fun getAccountKeyChain(): String? {
+ return crypto.getAccountKeyChain()
+ }
+
+ /**
+ * The account private key (JWK) of the unlocked session, or null until a sync opened it; memory only.
+ */
+ internal var accountPrivateKey: String?
+ get() = crypto.accountPrivateKey
+ set(value) {
+ crypto.accountPrivateKey = value
+ }
+
/**
* Derive a key from a password using Argon2Id.
*/
@@ -238,11 +260,11 @@ class VaultStore(
}
/**
- * Verify password and return encryption key if correct.
- * Returns null if password is incorrect.
+ * Verify the password and return the vault encryption key if correct: the VEK unwrapped through the
+ * account-key chain, or the derived key itself for a legacy account. Returns null if the password is incorrect.
*
* @param password The password to verify
- * @return The base64-encoded encryption key if password is correct, null otherwise
+ * @return The base64-encoded vault encryption key if password is correct, null otherwise
*/
@Suppress("SwallowedException")
fun verifyPassword(password: String): String? {
@@ -254,18 +276,19 @@ class VaultStore(
val encryptionType = paramsJson.getString("encryptionType")
val encryptionSettings = paramsJson.getString("encryptionSettings")
- // Derive key from password
+ // Derive the KEK from the password and unwrap the chain; a wrong password fails the unwrap.
val derivedKey = crypto.deriveKeyFromPassword(password, salt, encryptionType, encryptionSettings)
+ val vaultKey = crypto.resolveVaultEncryptionKey(derivedKey)
// Try to decrypt the vault to verify the password is correct
val encryptedDb = database.getEncryptedDatabase()
val encryptedDbBytes = android.util.Base64.decode(encryptedDb, android.util.Base64.NO_WRAP)
// Attempt decryption to verify password is correct
- VaultCrypto.decrypt(encryptedDbBytes, derivedKey)
+ VaultCrypto.decrypt(encryptedDbBytes, vaultKey)
- // If decryption succeeded, return the key as base64
- android.util.Base64.encodeToString(derivedKey, android.util.Base64.NO_WRAP)
+ // If decryption succeeded, return the vault key as base64
+ android.util.Base64.encodeToString(vaultKey, android.util.Base64.NO_WRAP)
} catch (e: Exception) {
// Password incorrect or decryption failed - intentionally return null
// We don't log the error as this is expected when password is incorrect
@@ -679,25 +702,6 @@ class VaultStore(
// region Sync Methods
- /**
- * Check if a new vault version is available on the server.
- */
- suspend fun isNewVaultVersionAvailable(webApiService: net.aliasvault.app.webapi.WebApiService): Map<String, Any?> {
- return sync.isNewVaultVersionAvailable(webApiService)
- }
-
- /**
- * Download and store the vault from the server.
- */
- suspend fun downloadVault(webApiService: net.aliasvault.app.webapi.WebApiService, newRevision: Int): Boolean {
- val result = sync.downloadVault(webApiService, newRevision)
- // Re-unlock vault if it was unlocked before download
- if (result && isVaultUnlocked()) {
- unlockVault()
- }
- return result
- }
-
/**
* Get the sync state.
*/
@@ -765,36 +769,48 @@ class VaultStore(
}
/**
- * Upload the vault to the server.
+ * The vault encryption key held in memory, as base64, or null while the vault is locked.
+ */
+ fun getEncryptionKeyBase64(): String? {
+ return encryptionKey?.let { android.util.Base64.encodeToString(it, android.util.Base64.NO_WRAP) }
+ }
+
+ /**
+ * Adopt a new vault encryption key for this session (the sync engine swapped a password-derived key for the
+ * account's VEK), persisting it to the keystore when biometrics are enabled.
*/
- suspend fun uploadVault(webApiService: net.aliasvault.app.webapi.WebApiService): VaultUploadResult {
- return mutate.uploadVault(webApiService)
+ fun adoptEncryptionKey(base64EncryptionKey: String) {
+ crypto.storeEncryptionKey(base64EncryptionKey, auth.getAuthMethods())
}
/**
- * Fetch the server vault (encrypted blob).
+ * Forget what the sync engine learned about the stored vault (revisions, fingerprints, key chain), so the next
+ * sync starts from the server as if this device had never pulled. Used when the vault is discarded.
*/
- suspend fun fetchServerVault(webApiService: net.aliasvault.app.webapi.WebApiService): VaultResponse {
- return sync.fetchServerVault(webApiService)
+ fun clearSyncEngineState() {
+ storageProvider.clearSyncEngineState()
+ crypto.storeAccountKeyChain(null)
}
/**
- * Check vault version including sync state.
+ * One status call: whether the server holds newer state than this device.
*/
suspend fun checkVaultVersion(webApiService: net.aliasvault.app.webapi.WebApiService): VaultVersionCheckResult {
return sync.checkVaultVersion(webApiService)
}
/**
- * Unified vault sync method that handles all sync scenarios.
+ * Resolve and store the vault key right after login from the password-derived key (see VaultSync.resolveVaultKey).
+ */
+ suspend fun resolveVaultKey(webApiService: net.aliasvault.app.webapi.WebApiService, derivedKeyBase64: String): String {
+ return sync.resolveVaultKey(webApiService, derivedKeyBase64)
+ }
+
+ /**
+ * Full vault sync through the Rust sync engine.
*/
suspend fun syncVaultWithServer(webApiService: net.aliasvault.app.webapi.WebApiService): VaultSyncResult {
- val result = sync.syncVaultWithServer(webApiService)
- // Re-unlock vault if it was unlocked before sync and action was download/merge
- if (result.success && (result.action == SyncAction.DOWNLOADED || result.action == SyncAction.MERGED) && isVaultUnlocked()) {
- unlockVault()
- }
- return result
+ return sync.syncVaultWithServer(webApiService)
}
// endregion
@@ -802,10 +818,10 @@ class VaultStore(
// region Mutate Methods
/**
- * Execute a vault mutation operation.
+ * Push the pending local changes (after a native mutation).
*/
suspend fun mutateVault(webApiService: net.aliasvault.app.webapi.WebApiService): Boolean {
- return mutate.mutateVault(webApiService)
+ return sync.mutateVault(webApiService)
}
// endregion
@@ -995,7 +1011,8 @@ class VaultStore(
*/
@Throws(Exception::class)
fun unlockWithPin(pinValue: String): String {
- val key = pin.unlockWithPin(pinValue)
+ // A PIN set up before the account moved onto the key hierarchy still holds the password-derived key.
+ val key = crypto.resolveStoredUnlockKey(pin.unlockWithPin(pinValue))
markSuccessfulAuth()
return key
}
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 4bc051d48..9bf252a6a 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
@@ -1,922 +1,219 @@
package net.aliasvault.app.vaultstore
import android.util.Log
-import net.aliasvault.app.exceptions.SerializationException
-import net.aliasvault.app.exceptions.VaultOperationException
-import net.aliasvault.app.rustcore.VaultMergeService
-import net.aliasvault.app.utils.AppInfo
-import net.aliasvault.app.vaultstore.repositories.ItemRepository
+import net.aliasvault.app.vaultstore.models.VaultMetadata
import net.aliasvault.app.vaultstore.storageprovider.StorageProvider
-import net.aliasvault.app.vaultstore.utils.VersionComparison
+import net.aliasvault.app.webapi.WebApiService
import org.json.JSONArray
import org.json.JSONObject
/**
- * Handles vault synchronization with the server.
+ * Vault sync runs through the Rust sync engine which handles the decision making and is cross-platform.
*/
class VaultSync(
- private val database: VaultDatabase,
- private val metadata: VaultMetadataManager,
- private val crypto: VaultCrypto,
+ private val vaultStore: VaultStore,
private val storageProvider: StorageProvider,
- private val itemRepository: ItemRepository,
) {
companion object {
private const val TAG = "VaultSync"
}
- // region Sync Methods
-
/**
- * Check if a new vault version is available on the server.
+ * Full vault sync: status check, then pull (and merge) or push as the server and local revisions decide.
*/
- suspend fun isNewVaultVersionAvailable(webApiService: net.aliasvault.app.webapi.WebApiService): Map<String, Any?> {
- val status = fetchAndValidateStatus(webApiService)
- metadata.setOfflineMode(false)
-
- val currentRevision = metadata.getVaultRevisionNumber()
- return if (status.vaultRevision > currentRevision) {
- mapOf(
- "isNewVersionAvailable" to true,
- "newRevision" to status.vaultRevision,
- )
- } else {
- mapOf(
- "isNewVersionAvailable" to false,
- "newRevision" to null,
- )
- }
- }
-
- /**
- * Download and store the vault from the server.
- */
- suspend fun downloadVault(webApiService: net.aliasvault.app.webapi.WebApiService, newRevision: Int): Boolean {
- try {
- downloadAndStoreVault(webApiService, newRevision)
- metadata.setOfflineMode(false)
- return true
- } catch (e: Exception) {
- Log.e(TAG, "Error downloading vault", e)
- throw e
- }
- }
-
- /**
- * Unified vault sync method that handles all sync scenarios:
- * 1. Server has newer vault → download (or merge if local changes exist)
- * 2. Local has changes at same revision → upload
- * 3. Both have changes → merge using LWW strategy, then upload
- * 4. Already in sync → no action needed
- *
- * This method handles race detection and retries automatically.
- * The merge logic uses the Rust core library for LWW merge.
- */
- suspend fun syncVaultWithServer(
- webApiService: net.aliasvault.app.webapi.WebApiService,
- retryCount: Int = 0,
- ): VaultSyncResult {
- val maxRetries = 3
-
- // Prevent infinite recursion
- if (retryCount >= maxRetries) {
- return handleSyncError(AppError.MaxRetriesReached())
- }
-
- // Mark as syncing
+ @Suppress("TooGenericExceptionCaught")
+ suspend fun syncVaultWithServer(webApiService: WebApiService): VaultSyncResult {
+ val metadata = vaultStore.metadata
metadata.setIsSyncing(true)
-
- return try {
- // Step 1: Check vault version and get sync state
- val versionCheck = checkVaultVersion(webApiService)
- val serverRevision = versionCheck.serverRevision
- val syncState = versionCheck.syncState
- val mutationSeqAtStart = syncState.mutationSequence
- val isDirty = syncState.isDirty
-
- // Step 2: Determine sync action needed
- when {
- serverRevision > syncState.serverRevision -> {
- // Server has newer vault
- if (isDirty) {
- // MERGE: Local changes + server changes
- performMergeSync(webApiService, serverRevision, mutationSeqAtStart, retryCount)
- } else {
- // DOWNLOAD: No local changes, safe to overwrite
- performDownloadSync(webApiService, serverRevision, mutationSeqAtStart, retryCount)
- }
- }
- serverRevision == syncState.serverRevision && isDirty -> {
- // UPLOAD: Local changes at same revision
- performUploadSync(webApiService, 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, mutationSeqAtStart, retryCount)
- }
- else -> {
- // Already in sync
- metadata.setIsSyncing(false)
- VaultSyncResult(
- success = true,
- action = SyncAction.ALREADY_IN_SYNC,
- newRevision = syncState.serverRevision,
- wasOffline = false,
- error = null,
- )
- }
- }
- } catch (e: AppError) {
- metadata.setIsSyncing(false)
- handleSyncError(e)
- } catch (e: Exception) {
- metadata.setIsSyncing(false)
- Log.e(TAG, "Sync error: ${e.message}", e)
- handleSyncError(AppError.UnknownError(e.message ?: "Unknown sync error", e))
- }
- }
-
- // region Sync Helpers
-
- /**
- * Perform download-only sync (no local changes).
- */
- private suspend fun performDownloadSync(
- webApiService: net.aliasvault.app.webapi.WebApiService,
- serverRevision: Int,
- mutationSeqAtStart: Int,
- retryCount: Int,
- ): VaultSyncResult {
- return try {
- val serverVault = fetchServerVault(webApiService)
-
- // Store with race detection
- val storeResult = storeEncryptedVaultWithSyncState(
- encryptedVault = serverVault.vault.blob,
- markDirty = false,
- serverRevision = serverRevision,
- expectedMutationSeq = mutationSeqAtStart,
- )
-
- if (!storeResult.success) {
- // Race detected - retry
- Log.d(TAG, "Race detected during download, retrying")
- metadata.setIsSyncing(false)
- return syncVaultWithServer(webApiService, retryCount + 1)
- }
-
- // Store vault metadata
- val vaultMetadata = net.aliasvault.app.vaultstore.models.VaultMetadata(
- publicEmailDomains = serverVault.vault.publicEmailDomainList,
- privateEmailDomains = serverVault.vault.privateEmailDomainList,
- hiddenPrivateEmailDomains = serverVault.vault.hiddenPrivateEmailDomainList,
- vaultRevisionNumber = serverRevision,
- )
- storeVaultMetadata(vaultMetadata)
-
- // Clear dirty flag - we just downloaded fresh from server, no local changes
- metadata.setIsDirty(false)
-
- // Re-unlock if was unlocked
- if (database.isVaultUnlocked()) {
- // Note: unlock requires auth methods from VaultStore
- }
-
- metadata.setIsSyncing(false)
- VaultSyncResult(
- success = true,
- action = SyncAction.DOWNLOADED,
- newRevision = serverRevision,
- wasOffline = false,
- error = null,
- )
- } catch (e: AppError) {
- metadata.setIsSyncing(false)
- handleSyncError(e)
- } catch (e: Exception) {
- metadata.setIsSyncing(false)
- Log.e(TAG, "Download sync error: ${e.message}", e)
- handleSyncError(AppError.VaultStoreFailed("Download sync failed: ${e.message}", e))
- }
- }
-
- /**
- * Perform upload-only sync (local changes, no server changes).
- */
- private suspend fun performUploadSync(
- webApiService: net.aliasvault.app.webapi.WebApiService,
- mutationSeqAtStart: Int,
- retryCount: Int,
- ): VaultSyncResult {
- return try {
- val uploadResult = uploadVault(webApiService)
-
- when {
- uploadResult.success -> {
- // Mark clean if no new mutations during upload
- metadata.markVaultClean(mutationSeqAtStart, uploadResult.newRevisionNumber)
- metadata.setIsSyncing(false)
- VaultSyncResult(
- success = true,
- action = SyncAction.UPLOADED,
- newRevision = uploadResult.newRevisionNumber,
- wasOffline = false,
- error = null,
- )
- }
- uploadResult.status == 2 -> {
- // Vault outdated - server moved forward, retry to merge
- Log.d(TAG, "Vault outdated during upload, retrying")
- metadata.setIsSyncing(false)
- syncVaultWithServer(webApiService, retryCount + 1)
- }
- else -> {
- metadata.setIsSyncing(false)
- Log.e(TAG, "Upload failed: ${uploadResult.error}")
- handleSyncError(AppError.VaultUploadFailed("Upload failed: ${uploadResult.error}"))
- }
+ try {
+ val wasDirty = metadata.getIsDirty()
+ val result = try {
+ VaultSyncEngine(vaultStore, storageProvider, webApiService).run("fullSync")
+ } catch (e: Exception) {
+ return failedSync(driverError(e), metadata.getOfflineMode())
}
- } catch (e: AppError) {
- // Preserve typed AppError (e.g. VaultTooLarge → E-804) so the JS layer can show the targeted message.
- metadata.setIsSyncing(false)
- Log.e(TAG, "Upload sync error: ${e.message}", e)
- handleSyncError(e)
- } catch (e: Exception) {
- metadata.setIsSyncing(false)
- Log.e(TAG, "Upload sync error: ${e.message}", e)
- handleSyncError(AppError.VaultUploadFailed("Upload sync error: ${e.message}", e))
- }
- }
- /**
- * Perform merge sync (both local and server have changes).
- */
- @Suppress("UnusedParameter") // serverRevision will be used for conflict resolution metadata in the future
- private suspend fun performMergeSync(
- webApiService: net.aliasvault.app.webapi.WebApiService,
- serverRevision: Int,
- mutationSeqAtStart: Int,
- retryCount: Int,
- ): VaultSyncResult {
- return try {
- val serverVault = fetchServerVault(webApiService)
+ adoptSyncSideEffects(result)
- val localVault = database.getEncryptedDatabase()
- if (localVault.isEmpty()) {
- metadata.setIsSyncing(false)
- Log.e(TAG, "No local vault available for merge")
- return handleSyncError(AppError.VaultMergeFailed("No local vault available for merge"))
+ val wasOffline = result.optBoolean("wasOffline", false)
+ if (!result.optBoolean("success", false)) {
+ return failedSync(syncError(result), wasOffline)
}
-
- // Perform LWW merge using Rust core library
- val mergedVault = performLWWMerge(localVault, serverVault.vault.blob)
-
- // Store merged vault with race detection
- // Set serverRevision to the server's revision so prepareVault() sends correct revision when uploading
- val storeResult = storeEncryptedVaultWithSyncState(
- encryptedVault = mergedVault,
- markDirty = false,
- serverRevision = serverVault.vault.currentRevisionNumber,
- expectedMutationSeq = mutationSeqAtStart,
- )
-
- if (!storeResult.success) {
- // Race detected - retry
- Log.d(TAG, "Race detected during merge, retrying")
- metadata.setIsSyncing(false)
- return syncVaultWithServer(webApiService, retryCount + 1)
+ if (wasOffline) {
+ val code = AppError.NetworkError(IllegalStateException("offline")).code
+ return VaultSyncResult(false, SyncAction.ERROR, metadata.getVaultRevisionNumber(), true, code)
}
-
- // Re-unlock immediately after merge to load merged vault into memory
- // This ensures prepareVault() gets correct item counts and the UI shows merged data
- if (database.isVaultUnlocked()) {
- // Note: unlock requires auth methods from VaultStore - handled there
+ if (result.optBoolean("manifestMigrationRequired", false)) {
+ runPendingManifestMigration(webApiService)
}
- // Upload merged vault
- val uploadResult = uploadVault(webApiService)
-
- when {
- uploadResult.success -> {
- metadata.markVaultClean(mutationSeqAtStart, uploadResult.newRevisionNumber)
- metadata.setIsSyncing(false)
- VaultSyncResult(
- success = true,
- action = SyncAction.MERGED,
- newRevision = uploadResult.newRevisionNumber,
- wasOffline = false,
- error = null,
- )
- }
- uploadResult.status == 2 -> {
- // Vault outdated again - retry
- Log.d(TAG, "Vault outdated after merge, retrying")
- metadata.setIsSyncing(false)
- syncVaultWithServer(webApiService, retryCount + 1)
- }
- else -> {
- metadata.setIsSyncing(false)
- Log.e(TAG, "Upload after merge failed: ${uploadResult.error}")
- handleSyncError(AppError.MergeUploadFailed("Upload after merge failed: ${uploadResult.error}"))
- }
+ val hasNewVault = result.optBoolean("hasNewVault", false)
+ val action = when {
+ hasNewVault && wasDirty -> SyncAction.MERGED
+ hasNewVault -> SyncAction.DOWNLOADED
+ wasDirty -> SyncAction.UPLOADED
+ else -> SyncAction.ALREADY_IN_SYNC
}
- } catch (e: AppError) {
- metadata.setIsSyncing(false)
- handleSyncError(e)
- } catch (e: Exception) {
+ return VaultSyncResult(true, action, metadata.getVaultRevisionNumber(), false, null)
+ } finally {
metadata.setIsSyncing(false)
- Log.e(TAG, "Merge sync error: ${e.message}", e)
- handleSyncError(AppError.VaultMergeFailed("Merge sync error: ${e.message}", e))
}
}
/**
- * Perform Last-Write-Wins merge between local and server vaults using Rust core library.
- *
- * @param localVault Base64-encoded encrypted local vault
- * @param serverVault Base64-encoded encrypted server vault
- * @return Base64-encoded encrypted merged vault
+ * A failed sync return.
*/
- private fun performLWWMerge(localVault: String, serverVault: String): String {
- val encryptionKey = crypto.encryptionKey
- ?: throw AppError.EncryptionKeyNotFound("Encryption key not available for merge")
-
- return try {
- VaultMergeService.mergeVaults(
- localVaultBase64 = localVault,
- serverVaultBase64 = serverVault,
- encryptionKey = encryptionKey,
- tempDir = storageProvider.getCacheDir(),
- )
- } catch (e: VaultMergeService.VaultMergeException) {
- Log.e(TAG, "Rust merge failed: ${e.message}", e)
- throw AppError.VaultMergeFailed("Rust merge failed: ${e.message}", e)
- }
+ private fun failedSync(error: AppError, wasOffline: Boolean): VaultSyncResult {
+ Log.e(TAG, "Sync failed (${error.code}): ${error.message}", error.cause)
+ return VaultSyncResult(false, SyncAction.ERROR, vaultStore.metadata.getVaultRevisionNumber(), wasOffline, error.code, error.message)
}
/**
- * Upload the vault to the server and return detailed result.
- * This is used for sync operations where race detection is needed.
+ * An error the driver itself threw (a request it could not build, a command it could not decode).
*/
- private suspend fun uploadVault(webApiService: net.aliasvault.app.webapi.WebApiService): VaultUploadResult {
- val mutationSeqAtStart = metadata.getMutationSequence()
-
- return try {
- val vault = prepareVault()
-
- val json = JSONObject()
- json.put("blob", vault.blob)
- json.put("createdAt", vault.createdAt)
- json.put("credentialsCount", vault.credentialsCount)
- json.put("currentRevisionNumber", vault.currentRevisionNumber)
- json.put("emailAddressList", JSONArray(vault.emailAddressList))
- json.put("encryptionPublicKey", vault.encryptionPublicKey)
- json.put("updatedAt", vault.updatedAt)
- json.put("username", vault.username)
- json.put("version", vault.version)
-
- val response = try {
- webApiService.executeRequest(
- method = "POST",
- endpoint = "Vault",
- body = json.toString(),
- headers = mapOf("Content-Type" to "application/json"),
- requiresAuth = true,
- )
- } catch (e: Exception) {
- return VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Network error: ${e.message}",
- )
- }
-
- if (response.statusCode != 200) {
- // 413: server / reverse-proxy rejected the upload because the vault exceeded
- // MAX_UPLOAD_SIZE_MB. Throw a typed error so the JS layer gets E-804 and can
- // surface the targeted "vault too large" message instead of a generic failure.
- if (response.statusCode == 413) {
- throw AppError.VaultTooLarge()
- }
- // 426: server no longer supports this app version for this account.
- if (response.statusCode == 426) {
- throw AppError.ClientVersionNotSupported()
- }
- return VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Server returned error: ${response.statusCode}",
- )
- }
-
- val vaultResponse = try {
- val responseJson = JSONObject(response.body)
- VaultPostResponse(
- status = responseJson.getInt("status"),
- newRevisionNumber = responseJson.getInt("newRevisionNumber"),
- )
- } catch (e: Exception) {
- return VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Failed to parse response: ${e.message}",
- )
- }
-
- if (vaultResponse.status == 0) {
- // Success - update local revision number and clear offline mode
- metadata.setVaultRevisionNumber(vaultResponse.newRevisionNumber)
- metadata.setOfflineMode(false)
- }
-
- VaultUploadResult(
- success = vaultResponse.status == 0,
- status = vaultResponse.status,
- newRevisionNumber = vaultResponse.newRevisionNumber,
- mutationSeqAtStart = mutationSeqAtStart,
- error = if (vaultResponse.status != 0) "Vault upload returned status ${vaultResponse.status}" else null,
- )
- } catch (e: AppError) {
- // Let typed AppError (e.g. VaultTooLarge → E-804) propagate so performUploadSync can pass
- // it to handleSyncError with the correct error code instead of wrapping it as a generic upload failure.
- throw e
- } catch (e: Exception) {
- Log.e(TAG, "Error uploading vault", e)
- VaultUploadResult(
- success = false,
- status = -1,
- newRevisionNumber = 0,
- mutationSeqAtStart = mutationSeqAtStart,
- error = "Error uploading vault: ${e.message}",
- )
- }
- }
+ private fun driverError(e: Exception): AppError = e as? AppError ?: AppError.SyncEngineFailed(e.toString(), e)
/**
- * Prepare the vault for upload by assembling all metadata.
+ * Resolve the vault key right after login: the account's key chain is opened with the password-derived key and the VEK
+ * is stored as the session key; a legacy account keeps the derived key. Every sync assumes the key this stored. Returns the stored key (base64).
*/
- private fun prepareVault(): VaultUpload {
- val currentRevision = metadata.getVaultRevisionNumber()
-
- val encryptedDb = database.getEncryptedDatabase()
-
- val username = metadata.getUsername()
- ?: throw VaultOperationException("Username not found")
-
- if (!database.isVaultUnlocked()) {
- throw VaultOperationException("Vault must be unlocked to prepare for upload")
- }
-
- // Get all items to count them and extract private email addresses
- val items = itemRepository.getAll()
-
- val metadataObj = metadata.getVaultMetadataObject()
- val privateEmailDomains = metadataObj?.privateEmailDomains ?: emptyList()
-
- // Extract private email addresses from items using the email field
- val privateEmailAddresses = items
- .mapNotNull { it.email }
- .filter { email ->
- privateEmailDomains.any { domain ->
- email.lowercase().endsWith("@${domain.lowercase()}")
- }
- }
- .distinct()
-
- val dbVersion = itemRepository.getDatabaseVersion()
-
- @Suppress("SwallowedException")
- val version = try {
- // Try to get version from storage provider context
- val context = database.javaClass.getDeclaredField("storageProvider")
- .get(database) as? net.aliasvault.app.vaultstore.storageprovider.AndroidStorageProvider
- val pm = context?.javaClass?.getDeclaredField("context")?.get(context)
- as? android.content.Context
- pm?.packageManager?.getPackageInfo(pm.packageName, 0)?.versionName ?: "0.0.0"
+ @Suppress("TooGenericExceptionCaught")
+ suspend fun resolveVaultKey(webApiService: WebApiService, derivedKeyBase64: String): String {
+ val result = try {
+ VaultSyncEngine(vaultStore, storageProvider, webApiService).run("resolveVaultKey", encryptionKey = derivedKeyBase64)
} catch (e: Exception) {
- "0.0.0"
+ throw driverError(e)
}
-
- val dateFormat = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", java.util.Locale.US)
- dateFormat.timeZone = java.util.TimeZone.getTimeZone("UTC")
- val now = dateFormat.format(java.util.Date())
-
- return VaultUpload(
- blob = encryptedDb,
- createdAt = now,
- credentialsCount = items.size,
- currentRevisionNumber = currentRevision,
- emailAddressList = privateEmailAddresses,
- // TODO: add public RSA encryption key to payload when implementing vault creation from mobile app.
- encryptionPublicKey = "",
- updatedAt = now,
- username = username,
- version = dbVersion,
- )
- }
-
- /**
- * Store encrypted vault with sync state atomically.
- */
- private fun storeEncryptedVaultWithSyncState(
- encryptedVault: String,
- markDirty: Boolean,
- serverRevision: Int?,
- expectedMutationSeq: Int?,
- ): net.aliasvault.app.vaultstore.models.StoreVaultResult {
- var mutationSequence = metadata.getMutationSequence()
-
- // Race detection for sync operations
- if (expectedMutationSeq != null && expectedMutationSeq != mutationSequence) {
- return net.aliasvault.app.vaultstore.models.StoreVaultResult(success = false, mutationSequence = mutationSequence)
+ if (!result.optBoolean("success", false)) {
+ throw syncError(result)
}
-
- if (markDirty) {
- mutationSequence += 1
- }
-
- // Store vault
- database.storeEncryptedDatabase(encryptedVault)
-
- if (markDirty) {
- metadata.setMutationSequence(mutationSequence)
- metadata.setIsDirty(true)
- }
-
- if (serverRevision != null) {
- metadata.setVaultRevisionNumber(serverRevision)
- }
-
- return net.aliasvault.app.vaultstore.models.StoreVaultResult(success = true, mutationSequence = mutationSequence)
- }
-
- /**
- * Store vault metadata as JSON string.
- */
- private fun storeVaultMetadata(vaultMetadata: net.aliasvault.app.vaultstore.models.VaultMetadata) {
- val json = JSONObject().apply {
- put("publicEmailDomains", org.json.JSONArray(vaultMetadata.publicEmailDomains))
- put("privateEmailDomains", org.json.JSONArray(vaultMetadata.privateEmailDomains))
- put("hiddenPrivateEmailDomains", org.json.JSONArray(vaultMetadata.hiddenPrivateEmailDomains))
- put("vaultRevisionNumber", vaultMetadata.vaultRevisionNumber)
- }
- metadata.storeMetadata(json.toString())
+ val key = result.optString("encryptionKey").takeIf { it.isNotEmpty() } ?: derivedKeyBase64
+ vaultStore.adoptEncryptionKey(key)
+ result.optJSONObject("sessionUpdates")?.optString("accountPrivateKey")?.takeIf { it.isNotEmpty() }?.let { vaultStore.accountPrivateKey = it }
+ return key
}
/**
- * Handle sync errors and return appropriate result.
+ * Push any pending local changes.
*/
- private fun handleSyncError(error: AppError): VaultSyncResult {
- Log.e(TAG, "Sync error: ${error.message}", error)
-
- return when (error) {
- is AppError.NetworkError,
- is AppError.ServerUnavailable,
- is AppError.Timeout,
- -> {
- metadata.setOfflineMode(true)
- VaultSyncResult(
- success = false,
- action = SyncAction.ERROR,
- newRevision = metadata.getVaultRevisionNumber(),
- wasOffline = true,
- error = error.code,
- )
- }
- else -> {
- VaultSyncResult(
- success = false,
- action = SyncAction.ERROR,
- newRevision = metadata.getVaultRevisionNumber(),
- wasOffline = metadata.getOfflineMode(),
- error = error.code,
- )
- }
+ suspend fun mutateVault(webApiService: WebApiService): Boolean {
+ val result = syncVaultWithServer(webApiService)
+ if (!result.success && !result.wasOffline) {
+ throw AppError.VaultUploadFailed(result.errorMessage ?: result.error ?: "Vault sync failed")
}
- }
-
- // endregion
-
- /**
- * Check if a new vault version is available, including sync state for merge decision.
- * This enhanced version returns sync state so the caller can decide whether to merge.
- */
- suspend fun checkVaultVersion(webApiService: net.aliasvault.app.webapi.WebApiService): VaultVersionCheckResult {
- val status = fetchAndValidateStatus(webApiService)
- metadata.setOfflineMode(false)
-
- val syncState = metadata.getSyncState()
- val isNewVersionAvailable = status.vaultRevision > syncState.serverRevision
-
- return VaultVersionCheckResult(
- isNewVersionAvailable = isNewVersionAvailable,
- newRevision = if (isNewVersionAvailable) status.vaultRevision else null,
- serverRevision = status.vaultRevision,
- syncState = syncState,
- )
+ return result.success
}
/**
- * Fetch the server vault (encrypted blob).
- * Use this for merge operations where you need both local and server vaults.
+ * Issue a status call.
*/
- suspend fun fetchServerVault(webApiService: net.aliasvault.app.webapi.WebApiService): VaultResponse {
- val vaultResponse = try {
- webApiService.executeRequest(
- method = "GET",
- endpoint = "Vault",
- body = null,
- headers = emptyMap(),
- requiresAuth = true,
- )
+ @Suppress("TooGenericExceptionCaught")
+ suspend fun checkVaultVersion(webApiService: WebApiService): VaultVersionCheckResult {
+ val result = try {
+ VaultSyncEngine(vaultStore, storageProvider, webApiService).run("statusCheck")
} catch (e: Exception) {
- throw AppError.NetworkError(e)
+ throw driverError(e)
}
-
- if (vaultResponse.statusCode != 200) {
- if (vaultResponse.statusCode == 401) {
- throw AppError.SessionExpired()
- }
- if (vaultResponse.statusCode == 426) {
- // Server no longer supports this app version for this account
- throw AppError.ClientVersionNotSupported()
- }
- throw AppError.ServerUnavailable(vaultResponse.statusCode)
+ result.optString("serverVersion").takeIf { it.isNotEmpty() }?.let { vaultStore.metadata.setServerVersion(it) }
+ if (result.optBoolean("isOffline", false)) {
+ vaultStore.metadata.setOfflineMode(true)
+ throw AppError.ServerUnavailable(0)
}
-
- return parseVaultResponsePublic(vaultResponse.body)
+ if (result.optBoolean("requiresLogout", false) || !result.optBoolean("success", false)) {
+ throw syncError(result)
+ }
+ vaultStore.metadata.setOfflineMode(false)
+ return VaultVersionCheckResult(result.optBoolean("hasNewerVault", false), vaultStore.getSyncState())
}
- // endregion
-
- // region Internal Helpers
-
- private suspend fun fetchAndValidateStatus(webApiService: net.aliasvault.app.webapi.WebApiService): StatusResponse {
- val statusResponse = try {
- webApiService.executeRequest(
- method = "GET",
- endpoint = "Auth/status",
- body = null,
- headers = emptyMap(),
- requiresAuth = true,
+ /**
+ * Persist sync results.
+ */
+ private fun adoptSyncSideEffects(result: JSONObject) {
+ result.optString("serverVersion").takeIf { it.isNotEmpty() }?.let { vaultStore.metadata.setServerVersion(it) }
+ if (result.has("isOfflineMode")) {
+ vaultStore.metadata.setOfflineMode(result.optBoolean("isOfflineMode", false))
+ }
+ result.optJSONObject("sessionUpdates")?.let { updates ->
+ updates.optString("encryptionKey").takeIf { it.isNotEmpty() }?.let { vaultStore.adoptEncryptionKey(it) }
+ updates.optString("accountPrivateKey").takeIf { it.isNotEmpty() }?.let { vaultStore.accountPrivateKey = it }
+ }
+ result.optJSONObject("emailRouting")?.let { routing ->
+ val metadata = VaultMetadata(
+ publicEmailDomains = routing.optJSONArray("publicEmailDomainList").toStringList(),
+ privateEmailDomains = routing.optJSONArray("privateEmailDomainList").toStringList(),
+ hiddenPrivateEmailDomains = routing.optJSONArray("hiddenPrivateEmailDomainList").toStringList(),
+ vaultRevisionNumber = if (result.has("pulledRevision")) result.optInt("pulledRevision") else vaultStore.metadata.getVaultRevisionNumber(),
)
- } catch (e: Exception) {
- throw AppError.NetworkError(e)
- }
-
- if (statusResponse.statusCode != 200) {
- if (statusResponse.statusCode == 401) {
- Log.e(TAG, "Authentication failed (401) - token refresh also failed")
- throw AppError.SessionExpired()
- }
- if (statusResponse.statusCode == 426) {
- // Server no longer supports this app version for this account
- throw AppError.ClientVersionNotSupported()
- }
- metadata.setOfflineMode(true)
- throw AppError.ServerUnavailable(statusResponse.statusCode)
- }
-
- val status = try {
- val json = JSONObject(statusResponse.body)
- StatusResponse(
- clientVersionSupported = json.getBoolean("clientVersionSupported"),
- serverVersion = json.getString("serverVersion"),
- vaultRevision = json.getInt("vaultRevision"),
- srpSalt = json.getString("srpSalt"),
+ vaultStore.metadata.storeMetadata(
+ JSONObject().apply {
+ put("publicEmailDomains", JSONArray(metadata.publicEmailDomains))
+ put("privateEmailDomains", JSONArray(metadata.privateEmailDomains))
+ put("hiddenPrivateEmailDomains", JSONArray(metadata.hiddenPrivateEmailDomains))
+ put("vaultRevisionNumber", metadata.vaultRevisionNumber)
+ }.toString(),
)
- } catch (e: Exception) {
- Log.e(TAG, "Failed to decode status response", e)
- Log.e(TAG, "Response body: '${statusResponse.body}'")
- throw AppError.ParseError("Failed to decode status response: ${e.message}", e)
- }
-
- if (!status.clientVersionSupported) {
- throw AppError.ClientVersionNotSupported()
}
-
- if (!VersionComparison.isServerVersionSupported(status.serverVersion)) {
- Log.e(TAG, "Server version ${status.serverVersion} does not meet minimum requirement ${AppInfo.MIN_SERVER_VERSION}")
- throw AppError.ServerVersionNotSupported()
- }
-
- // Store server version in metadata
- metadata.setServerVersion(status.serverVersion)
-
- validateSrpSalt(status.srpSalt)
- return status
}
- private fun validateSrpSalt(srpSalt: String) {
- val keyDerivationParams = crypto.getEncryptionKeyDerivationParams()
- if (keyDerivationParams.isEmpty()) {
- return
- }
-
- @Suppress("SwallowedException")
+ /**
+ * Bring the local vault onto the current storage model when the engine reports that it has to (a schema
+ * rebuild after an app update, or the one-time account-key upgrade of a legacy vault), then push it.
+ */
+ @Suppress("TooGenericExceptionCaught")
+ private suspend fun runPendingManifestMigration(webApiService: WebApiService) {
try {
- val json = JSONObject(keyDerivationParams)
- val salt = json.optString("salt", "")
- if (srpSalt.isNotEmpty() && srpSalt != salt) {
- throw AppError.PasswordChanged()
- }
- } catch (e: AppError.PasswordChanged) {
- throw e
+ val status = VaultSyncEngine(vaultStore, storageProvider, webApiService).run("migrationStatus")
+ val kind = status.optString("kind", "none")
+ if (kind == "none") {
+ return
+ }
+ val result = VaultSyncEngine(vaultStore, storageProvider, webApiService).run("migrateManifest")
+ adoptSyncSideEffects(result)
+ Log.i(TAG, "Manifest migration ($kind): success=${result.optBoolean("success")} pushed=${result.optBoolean("pushed")}")
} catch (e: Exception) {
- // Ignore generic parsing errors
+ Log.w(TAG, "Manifest migration failed, the vault stays as it is until the next sync", e)
}
}
- private suspend fun downloadAndStoreVault(webApiService: net.aliasvault.app.webapi.WebApiService, newRevision: Int) {
- val vaultResponse = try {
- webApiService.executeRequest(
- method = "GET",
- endpoint = "Vault",
- body = null,
- headers = emptyMap(),
- requiresAuth = true,
- )
- } catch (e: Exception) {
- throw VaultOperationException("Network error: ${e.message}", e)
- }
-
- if (vaultResponse.statusCode != 200) {
- if (vaultResponse.statusCode == 401) {
- throw VaultOperationException("Session expired")
- }
- if (vaultResponse.statusCode == 426) {
- // Typed error so the JS layer gets E-301 and shows the "update your app" message.
- throw AppError.ClientVersionNotSupported()
- }
- throw VaultOperationException("Server unavailable: ${vaultResponse.statusCode}")
- }
-
- val vault = parseVaultResponse(vaultResponse.body)
- validateVaultStatus(vault.status)
- database.storeEncryptedDatabase(vault.vault.blob)
- metadata.setVaultRevisionNumber(newRevision)
-
- // Store vault metadata (public/private email domains)
- val vaultMetadata = net.aliasvault.app.vaultstore.models.VaultMetadata(
- publicEmailDomains = vault.vault.publicEmailDomainList,
- privateEmailDomains = vault.vault.privateEmailDomainList,
- hiddenPrivateEmailDomains = vault.vault.hiddenPrivateEmailDomainList,
- vaultRevisionNumber = newRevision,
- )
- storeVaultMetadata(vaultMetadata)
-
- if (database.isVaultUnlocked()) {
- // Re-unlock with new data
- // Note: This requires auth methods to be passed, handled by VaultStore
- }
- }
-
- private fun parseVaultResponse(body: String): InternalVaultResponse {
- return try {
- val json = JSONObject(body)
- val vaultJson = json.getJSONObject("vault")
-
- val emailList = mutableListOf<String>()
- val emailArray = vaultJson.getJSONArray("emailAddressList")
- for (i in 0 until emailArray.length()) {
- emailList.add(emailArray.getString(i))
- }
-
- val privateList = mutableListOf<String>()
- val privateArray = vaultJson.getJSONArray("privateEmailDomainList")
- for (i in 0 until privateArray.length()) {
- privateList.add(privateArray.getString(i))
- }
-
- val hiddenPrivateList = mutableListOf<String>()
- val hiddenPrivateArray = vaultJson.getJSONArray("hiddenPrivateEmailDomainList")
- for (i in 0 until hiddenPrivateArray.length()) {
- hiddenPrivateList.add(hiddenPrivateArray.getString(i))
- }
-
- val publicList = mutableListOf<String>()
- val publicArray = vaultJson.getJSONArray("publicEmailDomainList")
- for (i in 0 until publicArray.length()) {
- publicList.add(publicArray.getString(i))
- }
-
- InternalVaultResponse(
- status = json.getInt("status"),
- vault = InternalVaultData(
- username = vaultJson.getString("username"),
- blob = vaultJson.getString("blob"),
- version = vaultJson.getString("version"),
- currentRevisionNumber = vaultJson.getInt("currentRevisionNumber"),
- encryptionPublicKey = vaultJson.getString("encryptionPublicKey"),
- credentialsCount = vaultJson.getInt("credentialsCount"),
- emailAddressList = emailList,
- privateEmailDomainList = privateList,
- hiddenPrivateEmailDomainList = hiddenPrivateList,
- publicEmailDomainList = publicList,
- createdAt = vaultJson.getString("createdAt"),
- updatedAt = vaultJson.getString("updatedAt"),
- ),
- )
- } catch (e: Exception) {
- Log.e(TAG, "Failed to decode vault response", e)
- throw SerializationException("Failed to decode vault response: ${e.message}", e)
- }
- }
-
- private fun parseVaultResponsePublic(body: String): VaultResponse {
- val internal = parseVaultResponse(body)
- return VaultResponse(
- status = internal.status,
- vault = VaultData(
- username = internal.vault.username,
- blob = internal.vault.blob,
- version = internal.vault.version,
- currentRevisionNumber = internal.vault.currentRevisionNumber,
- encryptionPublicKey = internal.vault.encryptionPublicKey,
- credentialsCount = internal.vault.credentialsCount,
- emailAddressList = internal.vault.emailAddressList,
- privateEmailDomainList = internal.vault.privateEmailDomainList,
- hiddenPrivateEmailDomainList = internal.vault.hiddenPrivateEmailDomainList,
- publicEmailDomainList = internal.vault.publicEmailDomainList,
- createdAt = internal.vault.createdAt,
- updatedAt = internal.vault.updatedAt,
- ),
- )
- }
-
- private fun validateVaultStatus(status: Int) {
- when (status) {
- 0 -> return
- 1 -> throw VaultOperationException("Vault merge required")
- 2 -> throw VaultOperationException("Vault outdated")
- else -> throw VaultOperationException("Unknown vault status: $status")
- }
+ /**
+ * Convert JSON result to AppError.
+ */
+ private fun syncError(result: JSONObject): AppError {
+ when (result.optString("errorKey")) {
+ "clientVersionNotSupported" -> return AppError.ClientVersionNotSupported()
+ "serverVersionNotSupported" -> return AppError.ServerVersionNotSupported()
+ "sessionExpired" -> return AppError.SessionExpired()
+ "passwordChanged" -> return AppError.PasswordChanged()
+ "vaultVersionIncompatible" -> return AppError.VaultVersionIncompatible()
+ }
+ // The engine's message is diagnostic detail; the code decides what the user sees.
+ val message = result.optString("error", "Vault sync failed")
+ return when (result.optString("errorCode")) {
+ "E-805" -> AppError.Timeout()
+ "E-804" -> AppError.VaultTooLarge()
+ "E-903" -> AppError.ServerVersionNotSupported()
+ "E-901" -> AppError.MigrationCheckFailed(message)
+ "E-505" -> AppError.ServerUnavailable(0)
+ "E-506" -> AppError.ServerError(message)
+ "E-507" -> AppError.SyncResponseInvalid(message)
+ "E-508" -> AppError.SyncCodecFailed(message)
+ "E-509" -> AppError.SyncEngineFailed(message)
+ "E-502" -> AppError.SyncVaultFetchFailed(message)
+ "E-202" -> AppError.EncryptionKeyNotFound()
+ "E-203", "E-503" -> AppError.VaultDecryptFailed()
+ "E-601" -> AppError.StorageReadFailed(message)
+ "E-602" -> AppError.StorageWriteFailed(message)
+ "E-603" -> AppError.DatabaseInitFailed(message)
+ "E-702" -> AppError.MaxRetriesReached()
+ "E-701" -> AppError.VaultMergeFailed(message)
+ "E-801" -> AppError.VaultUploadFailed(message)
+ else -> AppError.UnknownError(message)
+ }
+ }
+
+ private fun JSONArray?.toStringList(): List<String> {
+ if (this == null) return emptyList()
+ return (0 until length()).map { optString(it) }
}
-
- // endregion
-
- // region Data Models
-
- private data class StatusResponse(
- val clientVersionSupported: Boolean,
- val serverVersion: String,
- val vaultRevision: Int,
- val srpSalt: String,
- )
-
- private data class InternalVaultData(
- val username: String,
- val blob: String,
- val version: String,
- val currentRevisionNumber: Int,
- val encryptionPublicKey: String,
- val credentialsCount: Int,
- val emailAddressList: List<String>,
- val privateEmailDomainList: List<String>,
- val hiddenPrivateEmailDomainList: List<String>,
- val publicEmailDomainList: List<String>,
- val createdAt: String,
- val updatedAt: String,
- )
-
- private data class InternalVaultResponse(
- val status: Int,
- val vault: InternalVaultData,
- )
-
- private data class VaultUpload(
- val blob: String,
- val createdAt: String,
- val credentialsCount: Int,
- val currentRevisionNumber: Int,
- val emailAddressList: List<String>,
- val encryptionPublicKey: String,
- val updatedAt: String,
- val username: String,
- val version: String,
- )
-
- private data class VaultPostResponse(
- val status: Int,
- val newRevisionNumber: Int,
- )
-
- // endregion
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
new file mode 100644
index 000000000..629d43224
--- /dev/null
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
@@ -0,0 +1,383 @@
+package net.aliasvault.app.vaultstore
+
+import android.database.Cursor
+import android.util.Base64
+import android.util.Log
+import io.requery.android.database.sqlite.SQLiteDatabase
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import net.aliasvault.app.rustcore.JnaInitializer
+import net.aliasvault.app.utils.AppInfo
+import net.aliasvault.app.vaultstore.models.VaultSql
+import net.aliasvault.app.vaultstore.storageprovider.StorageProvider
+import net.aliasvault.app.webapi.WebApiService
+import org.json.JSONArray
+import org.json.JSONObject
+import uniffi.aliasvault_core.VaultSyncSession
+import java.io.File
+import java.net.SocketTimeoutException
+
+/**
+ * Drives one operation of the Rust vault sync engine against this vault store.
+ *
+ * The engine owns the sync algorithm and emits commands (HTTP, state, SQLite, vault storage); this class carries
+ * each one out and feeds the response back until the engine reports `done`. See the `vault_sync` module in
+ * `core/rust` for the command and response contract.
+ */
+class VaultSyncEngine(
+ private val vaultStore: VaultStore,
+ private val storageProvider: StorageProvider,
+ private val webApiService: WebApiService,
+) {
+ companion object {
+ private const val TAG = "VaultSyncEngine"
+
+ /** Engine state keys mirrored into the native account-key chain, so the native password unlock can unwrap it. */
+ private const val ENCRYPTED_ACCOUNT_KEY_STATE_KEY = "encryptedAccountKey"
+ private const val ENCRYPTED_VEK_STATE_KEY = "encryptedVek"
+
+ /** Engine state key that lives in the native store instead (the login writes it there), routed on read and write. */
+ private const val DERIVATION_PARAMS_STATE_KEY = "encryptionKeyDerivationParams"
+ }
+
+ private var staging: SQLiteDatabase? = null
+ private var stagingFile: File? = null
+
+ /**
+ * Run one engine operation (`fullSync`, `statusCheck`, `migrationStatus`, `migrateManifest`, `resolveVaultKey`) and
+ * return its result. [encryptionKey] overrides the store's key for the one operation that runs before it is known.
+ */
+ suspend fun run(operation: String, forcePull: Boolean = false, encryptionKey: String? = null): JSONObject {
+ JnaInitializer.ensureInitialized()
+ val session = VaultSyncSession(buildRequest(operation, forcePull, encryptionKey))
+ try {
+ while (true) {
+ val command = JSONObject(session.nextCommand())
+ val kind = command.optString("kind")
+ if (kind == "done") {
+ return command.optJSONObject("result") ?: JSONObject()
+ }
+ val response = handle(kind, command)
+ session.resume(response.toString())
+ }
+ } finally {
+ closeStaging()
+ session.destroy()
+ }
+ }
+
+ // region Request
+
+ private fun buildRequest(operation: String, forcePull: Boolean, encryptionKey: String?): String {
+ val metadata = vaultStore.metadata.getVaultMetadataObject()
+ val syncState = vaultStore.getSyncState()
+ val request = JSONObject().apply {
+ put("operation", operation)
+ put("username", vaultStore.getUsername() ?: "")
+ put("isDirty", syncState.isDirty)
+ put("mutationSequence", syncState.mutationSequence)
+ put("dirtyScopes", JSONArray(if (syncState.isDirty) listOf("Main") else emptyList<String>()))
+ put("privateEmailDomains", JSONArray(metadata?.privateEmailDomains ?: emptyList<String>()))
+ put("forcePull", forcePull)
+ put("minServerVersion", AppInfo.MIN_SERVER_VERSION)
+ put("isOfflineMode", vaultStore.getOfflineMode())
+ put("unnamedSharedVaultName", "Shared vault")
+ }
+ (encryptionKey ?: vaultStore.getEncryptionKeyBase64())?.let { request.put("encryptionKey", it) }
+ (state("accountPublicKey") as? String)?.let { request.put("accountPublicKey", it) }
+ vaultStore.accountPrivateKey?.let { request.put("accountPrivateKey", it) }
+ return request.toString()
+ }
+
+ // endregion
+
+ // region Commands
+
+ @Suppress("TooGenericExceptionCaught")
+ private suspend fun handle(kind: String, command: JSONObject): JSONObject {
+ return try {
+ when (kind) {
+ "http" -> http(command)
+ "stateGet" -> JSONObject().put("value", state(command.optString("key")) ?: JSONObject.NULL)
+ "stateSet" -> {
+ setState(command.optString("key"), command.opt("value"))
+ JSONObject()
+ }
+ "stateRemove" -> {
+ setState(command.optString("key"), null)
+ JSONObject()
+ }
+ "dbOpen" -> {
+ openStaging(command.optString("bytes", null).takeIf { command.has("bytes") && !command.isNull("bytes") })
+ JSONObject()
+ }
+ "dbQuery" -> {
+ val rows = query(database(command.optString("db")), command.optString("sql"), command.optJSONArray("params") ?: JSONArray())
+ JSONObject().put("rows", rows)
+ }
+ "dbExec" -> {
+ exec(database(command.optString("db")), command.optJSONArray("statements") ?: JSONArray())
+ JSONObject()
+ }
+ "dbExport" -> JSONObject().put("bytes", Base64.encodeToString(export(database(command.optString("db"))), Base64.NO_WRAP))
+ "vaultStore" -> storeVault(command)
+ "vaultLoad" -> JSONObject().put("encryptedBlob", if (vaultStore.hasEncryptedDatabase()) vaultStore.getEncryptedDatabase() else JSONObject.NULL)
+ "markClean" -> {
+ val cleared = vaultStore.markVaultClean(command.optInt("mutationSeqAtStart"), vaultStore.metadata.getVaultRevisionNumber())
+ JSONObject().put("cleared", cleared)
+ }
+ "log" -> {
+ Log.d(TAG, command.optString("message"))
+ JSONObject()
+ }
+ else -> JSONObject().put("error", "Unknown engine command $kind")
+ }
+ } catch (e: Exception) {
+ Log.w(TAG, "Engine command $kind failed", e)
+ JSONObject().put("error", e.message ?: e.toString())
+ }
+ }
+
+ @Suppress("TooGenericExceptionCaught")
+ private suspend fun http(command: JSONObject): JSONObject = withContext(Dispatchers.IO) {
+ val body = if (command.has("body") && !command.isNull("body")) command.getString("body") else null
+ val headers = mutableMapOf("Accept" to "application/json")
+ if (body != null) {
+ headers["Content-Type"] = "application/json"
+ }
+ try {
+ val response = webApiService.executeVersionedRequest(
+ method = command.optString("method", "GET"),
+ path = command.optString("path"),
+ body = body,
+ headers = headers,
+ requiresAuth = command.optBoolean("auth", true),
+ )
+ JSONObject().put("status", response.statusCode).put("body", response.body)
+ } catch (e: SocketTimeoutException) {
+ JSONObject().put("status", 0).put("transportError", e.toString()).put("timedOut", true)
+ } catch (e: Exception) {
+ JSONObject().put("status", 0).put("transportError", e.toString()).put("timedOut", false)
+ }
+ }
+
+ // endregion
+
+ // region State
+
+ /** Engine state is stored as the JSON text of `{"v": value}`, so any JSON value round-trips. */
+ private fun state(key: String): Any? {
+ if (key == DERIVATION_PARAMS_STATE_KEY) {
+ return storageProvider.getKeyDerivationParams().takeIf { it.isNotBlank() }?.let { JSONObject(it) }
+ }
+ val json = storageProvider.getSyncEngineState(key) ?: return null
+ return JSONObject(json).opt("v")?.takeIf { it != JSONObject.NULL }
+ }
+
+ private fun setState(key: String, value: Any?) {
+ if (key == DERIVATION_PARAMS_STATE_KEY) {
+ (value as? JSONObject)?.let { storageProvider.setKeyDerivationParams(it.toString()) }
+ return
+ }
+ if (value == null || value == JSONObject.NULL) {
+ storageProvider.setSyncEngineState(key, null)
+ } else {
+ storageProvider.setSyncEngineState(key, JSONObject().put("v", value).toString())
+ }
+ if (key == ENCRYPTED_ACCOUNT_KEY_STATE_KEY || key == ENCRYPTED_VEK_STATE_KEY) {
+ mirrorAccountKeyChain()
+ }
+ }
+
+ /**
+ * Keep the native account-key chain in step with the engine's cached key blobs.
+ */
+ private fun mirrorAccountKeyChain() {
+ val encryptedAccountKey = state(ENCRYPTED_ACCOUNT_KEY_STATE_KEY) as? String
+ val encryptedVek = state(ENCRYPTED_VEK_STATE_KEY) as? String
+ if (encryptedAccountKey == null || encryptedVek == null) {
+ vaultStore.storeAccountKeyChain(null)
+ return
+ }
+ val chain = JSONObject().put("encryptedAccountKey", encryptedAccountKey).put("encryptedVek", encryptedVek)
+ vaultStore.storeAccountKeyChain(chain.toString())
+ }
+
+ // endregion
+
+ // region Vault storage
+
+ private fun storeVault(command: JSONObject): JSONObject {
+ val encryptedBlob = command.getString("encryptedBlob")
+ if (command.has("encryptionKey") && !command.isNull("encryptionKey")) {
+ // The blob is encrypted under a key this session did not start with (KEK to VEK migration): adopt it first.
+ vaultStore.adoptEncryptionKey(command.getString("encryptionKey"))
+ }
+ val result = vaultStore.storeEncryptedVaultWithSyncState(
+ encryptedVault = encryptedBlob,
+ markDirty = command.optBoolean("markDirty", false),
+ serverRevision = optionalInt(command, "revision"),
+ expectedMutationSeq = optionalInt(command, "expectedMutationSeq"),
+ )
+ if (result.success && vaultStore.isVaultUnlocked()) {
+ // The contract: after a store, the live database is the vault just stored.
+ vaultStore.unlockVault()
+ }
+ return JSONObject().put("success", result.success).put("mutationSequence", result.mutationSequence)
+ }
+
+ private fun optionalInt(command: JSONObject, key: String): Int? {
+ return if (command.has(key) && !command.isNull(key)) command.getInt(key) else null
+ }
+
+ // endregion
+
+ // region SQLite
+
+ private fun database(name: String): SQLiteDatabase = when (name) {
+ "local" -> vaultStore.database.dbConnection ?: error("The vault is not unlocked")
+ "staging" -> staging ?: error("The staging database is not open")
+ else -> error("Unknown database $name")
+ }
+
+ /**
+ * Open the staging database: from SQLite bytes (as a temporary file), or fresh in memory with the current schema.
+ */
+ private fun openStaging(bytesBase64: String?) {
+ closeStaging()
+ if (bytesBase64 != null) {
+ val file = File(storageProvider.getRandomTempFilePath())
+ file.writeBytes(Base64.decode(bytesBase64, Base64.NO_WRAP))
+ stagingFile = file
+ staging = SQLiteDatabase.openDatabase(file.path, null, SQLiteDatabase.OPEN_READWRITE).also {
+ it.rawQuery("PRAGMA foreign_keys = OFF", null)?.close()
+ }
+ } else {
+ staging = SQLiteDatabase.create(null).also {
+ executeScript(it, VaultSql.completeSchema)
+ // The schema script ends by turning foreign keys on; the engine inserts rows in codec order, not FK order.
+ it.rawQuery("PRAGMA foreign_keys = OFF", null)?.close()
+ }
+ }
+ }
+
+ private fun closeStaging() {
+ staging?.close()
+ staging = null
+ stagingFile?.delete()
+ stagingFile = null
+ }
+
+ /**
+ * Run a multi-statement SQL script. Statements split on semicolons, except inside a trigger body.
+ */
+ private fun executeScript(db: SQLiteDatabase, script: String) {
+ for (statement in splitStatements(script)) {
+ val upper = statement.uppercase()
+ when {
+ upper.startsWith("PRAGMA") -> db.rawQuery(statement, null)?.close()
+ upper.startsWith("BEGIN") || upper.startsWith("COMMIT") || upper.startsWith("ROLLBACK") -> db.execSQL(statement)
+ else -> db.compileStatement(statement).use { it.execute() }
+ }
+ }
+ }
+
+ /**
+ * Split a script into statements on semicolons, keeping a trigger body (`CREATE TRIGGER ... END`) whole.
+ */
+ private fun splitStatements(script: String): List<String> {
+ val statements = mutableListOf<String>()
+ val trigger = StringBuilder()
+ val chunks = script.trimStart('\uFEFF').split(";").map { it.trim().trimStart('\uFEFF') }
+ for (chunk in chunks.filter { it.isNotEmpty() && !it.startsWith("--") }) {
+ if (trigger.isNotEmpty()) {
+ trigger.append(chunk).append(";")
+ if (chunk.uppercase() == "END") {
+ statements.add(trigger.toString())
+ trigger.clear()
+ }
+ } else if (chunk.uppercase().contains("CREATE TRIGGER")) {
+ trigger.append(chunk).append(";")
+ } else {
+ statements.add(chunk)
+ }
+ }
+ return statements
+ }
+
+ private fun export(db: SQLiteDatabase): ByteArray {
+ val file = File(storageProvider.getRandomTempFilePath())
+ file.delete()
+ try {
+ val quoted = file.absolutePath.replace("'", "''")
+ db.compileStatement("VACUUM INTO '$quoted'").use { it.execute() }
+ return file.readBytes()
+ } finally {
+ file.delete()
+ }
+ }
+
+ private fun bindValue(value: Any?): Any? = when (value) {
+ null, JSONObject.NULL -> null
+ is Boolean -> if (value) 1L else 0L
+ is Int -> value.toLong()
+ is Long -> value
+ is Double -> value
+ is Float -> value.toDouble()
+ is String -> value
+ is JSONObject -> if (value.has("__b64")) Base64.decode(value.getString("__b64"), Base64.NO_WRAP) else value.toString()
+ else -> value.toString()
+ }
+
+ private fun query(db: SQLiteDatabase, sql: String, params: JSONArray): JSONArray {
+ val args = Array<Any?>(params.length()) { bindValue(params.opt(it)) }
+ val rows = JSONArray()
+ db.rawQuery(sql, args).use { cursor ->
+ while (cursor.moveToNext()) {
+ val row = JSONObject()
+ for (index in 0 until cursor.columnCount) {
+ val name = cursor.getColumnName(index)
+ when (cursor.getType(index)) {
+ Cursor.FIELD_TYPE_INTEGER -> row.put(name, cursor.getLong(index))
+ Cursor.FIELD_TYPE_FLOAT -> row.put(name, cursor.getDouble(index))
+ Cursor.FIELD_TYPE_STRING -> row.put(name, cursor.getString(index))
+ Cursor.FIELD_TYPE_BLOB -> row.put(name, JSONObject().put("__b64", Base64.encodeToString(cursor.getBlob(index), Base64.NO_WRAP)))
+ else -> row.put(name, JSONObject.NULL)
+ }
+ }
+ rows.put(row)
+ }
+ }
+ return rows
+ }
+
+ private fun exec(db: SQLiteDatabase, statements: JSONArray) {
+ db.beginTransaction()
+ try {
+ for (index in 0 until statements.length()) {
+ val entry = statements.getJSONObject(index)
+ db.compileStatement(entry.getString("sql")).use { statement ->
+ bindParams(statement, entry.optJSONArray("params") ?: JSONArray())
+ statement.execute()
+ }
+ }
+ db.setTransactionSuccessful()
+ } finally {
+ db.endTransaction()
+ }
+ }
+
+ private fun bindParams(statement: io.requery.android.database.sqlite.SQLiteStatement, params: JSONArray) {
+ for (position in 0 until params.length()) {
+ when (val value = bindValue(params.opt(position))) {
+ null -> statement.bindNull(position + 1)
+ is Long -> statement.bindLong(position + 1, value)
+ is Double -> statement.bindDouble(position + 1, value)
+ is ByteArray -> statement.bindBlob(position + 1, value)
+ else -> statement.bindString(position + 1, value.toString())
+ }
+ }
+ }
+
+ // endregion
+}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/queries/PasskeyQueries.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/queries/PasskeyQueries.kt
index 1e44415c3..86eed7838 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/queries/PasskeyQueries.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/queries/PasskeyQueries.kt
@@ -55,8 +55,8 @@ object PasskeyQueries {
*/
const val INSERT = """
INSERT INTO Passkeys (Id, ItemId, RpId, UserHandle, PublicKey, PrivateKey, PrfKey,
- DisplayName, CreatedAt, UpdatedAt, IsDeleted)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ DisplayName, CreatedAt, UpdatedAt, IsDeleted, ManifestId)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE((SELECT ManifestId FROM Items WHERE Id = ?), ?))
"""
/**
@@ -141,16 +141,16 @@ object PasskeyQueries {
* Create an Item record for passkey registration.
*/
const val CREATE_ITEM = """
- INSERT INTO Items (Id, Name, ItemType, LogoId, FolderId, CreatedAt, UpdatedAt, IsDeleted, DeletedAt)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ INSERT INTO Items (Id, Name, ItemType, LogoId, FolderId, CreatedAt, UpdatedAt, IsDeleted, DeletedAt, ManifestId)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE((SELECT ManifestId FROM Folders WHERE Id = ?), ?))
"""
/**
* Insert a URL field value.
*/
const val INSERT_FIELD_VALUE = """
- INSERT INTO FieldValues (Id, ItemId, FieldDefinitionId, FieldKey, Value, Weight, CreatedAt, UpdatedAt, IsDeleted)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ INSERT INTO FieldValues (Id, ItemId, FieldDefinitionId, FieldKey, Value, Weight, CreatedAt, UpdatedAt, IsDeleted, ManifestId)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE((SELECT ManifestId FROM Items WHERE Id = ?), ?))
"""
/**
@@ -176,8 +176,8 @@ object LogoQueries {
* Insert a new logo.
*/
const val INSERT = """
- INSERT INTO Logos (Id, Source, FileData, MimeType, FetchedAt, CreatedAt, UpdatedAt, IsDeleted)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+ INSERT INTO Logos (Id, Kind, Source, ManifestId, FileData, MimeType, FetchedAt, CreatedAt, UpdatedAt, IsDeleted)
+ VALUES (?, 'favicon', ?, ?, ?, ?, ?, ?, ?, ?)
"""
/**
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt
index 74fb2fe2c..7bca4fe3f 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt
@@ -12,6 +12,14 @@ open class BaseRepository(
/** The database component used for executing queries. */
protected val database: VaultDatabase,
) {
+ /**
+ * The manifest new rows outside any folder or item are stamped with: the personal manifest. Rows inside a
+ * folder or item take that parent's manifest through the SQL instead.
+ */
+ protected fun activeManifestId(): String {
+ return database.getPersonalManifestId() ?: ""
+ }
+
// MARK: - Transaction Helpers
/**
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt
index 007079842..33464c101 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt
@@ -518,7 +518,7 @@ class ItemRepository(database: VaultDatabase) : BaseRepository(database) {
// Soft delete attachments AND zero their blob bytes — tombstone stays
// for sync but storage is reclaimed immediately.
executeUpdate(
- "UPDATE Attachments SET IsDeleted = 1, Blob = X'', UpdatedAt = ? WHERE ItemId = ? AND IsDeleted = 0",
+ "UPDATE Attachments SET IsDeleted = 1, Blob = NULL, UpdatedAt = ? WHERE ItemId = ? AND IsDeleted = 0",
arrayOf(now, itemId),
)
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt
index 75f59cc96..bf57a54e7 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt
@@ -11,6 +11,7 @@ import net.aliasvault.app.vaultstore.models.Passkey
import net.aliasvault.app.vaultstore.passkey.PasskeyHelper
import net.aliasvault.app.vaultstore.queries.LogoQueries
import net.aliasvault.app.vaultstore.queries.PasskeyQueries
+import uniffi.aliasvault_core.vaultCodecLogoIdFor
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
@@ -125,6 +126,8 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {
it.bindString(9, DateHelpers.toStandardFormat(passkey.createdAt))
it.bindString(10, DateHelpers.toStandardFormat(passkey.updatedAt))
it.bindLong(11, if (passkey.isDeleted) 1 else 0)
+ it.bindString(12, passkey.parentItemId.toString().lowercase())
+ it.bindString(13, activeManifestId())
it.executeInsert()
}
}
@@ -171,6 +174,8 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {
timestamp,
0,
null,
+ null, // FolderId again, for the manifest lookup
+ activeManifestId(),
),
)
@@ -188,6 +193,8 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {
timestamp,
timestamp,
0,
+ itemId.toString().lowercase(),
+ activeManifestId(),
),
)
}
@@ -206,6 +213,8 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {
timestamp,
timestamp,
0,
+ itemId.toString().lowercase(),
+ activeManifestId(),
),
)
}
@@ -560,11 +569,13 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {
return existingLogoId
}
- // Create new logo entry
- val logoId = generateId()
+ // Create new logo entry. The id is derived from (manifest, kind, source) by the Rust core, like every other
+ // client does, so the same favicon never produces two rows across devices.
+ val manifestId = activeManifestId()
+ val logoId = vaultCodecLogoIdFor(manifestId, "favicon", source)
executeUpdate(
LogoQueries.INSERT,
- arrayOf(logoId, source, logoData, "image/png", null, timestamp, timestamp, 0),
+ arrayOf(logoId, source, manifestId, logoData, "image/png", null, timestamp, timestamp, 0),
)
return logoId
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/AndroidStorageProvider.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/AndroidStorageProvider.kt
index 1daed2f31..188634ab0 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/AndroidStorageProvider.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/AndroidStorageProvider.kt
@@ -53,6 +53,22 @@ class AndroidStorageProvider(private val context: Context) : StorageProvider {
return sharedPreferences.getString("key_derivation_params", "") ?: ""
}
+ override fun getAccountKeyChain(): String? {
+ val sharedPreferences = context.getSharedPreferences("aliasvault", Context.MODE_PRIVATE)
+ return sharedPreferences.getString("account_key_chain", null)
+ }
+
+ override fun setAccountKeyChain(chainJson: String?) {
+ val sharedPreferences = context.getSharedPreferences("aliasvault", Context.MODE_PRIVATE)
+ sharedPreferences.edit {
+ if (chainJson == null) {
+ remove("account_key_chain")
+ } else {
+ putString("account_key_chain", chainJson)
+ }
+ }
+ }
+
override fun setAuthMethods(authMethods: String) {
val sharedPreferences = context.getSharedPreferences("aliasvault", Context.MODE_PRIVATE)
sharedPreferences.edit {
@@ -203,4 +219,25 @@ class AndroidStorageProvider(private val context: Context) : StorageProvider {
override fun getCacheDir(): File {
return context.cacheDir
}
+
+ // region Sync engine state
+
+ override fun getSyncEngineState(key: String): String? {
+ val sharedPreferences = context.getSharedPreferences("aliasvault_sync_state", Context.MODE_PRIVATE)
+ return sharedPreferences.getString(key, null)
+ }
+
+ override fun setSyncEngineState(key: String, json: String?) {
+ val sharedPreferences = context.getSharedPreferences("aliasvault_sync_state", Context.MODE_PRIVATE)
+ sharedPreferences.edit {
+ if (json == null) remove(key) else putString(key, json)
+ }
+ }
+
+ override fun clearSyncEngineState() {
+ val sharedPreferences = context.getSharedPreferences("aliasvault_sync_state", Context.MODE_PRIVATE)
+ sharedPreferences.edit { clear() }
+ }
+
+ // endregion
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt
index 0d16186a0..bf5bec3a6 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/StorageProvider.kt
@@ -37,6 +37,18 @@ interface StorageProvider {
*/
fun setKeyDerivationParams(keyDerivationParams: String)
+ /**
+ * Get the account-key chain JSON, or null for a legacy account.
+ * @return The account-key chain JSON or null
+ */
+ fun getAccountKeyChain(): String?
+
+ /**
+ * Set the account-key chain JSON. Null clears it.
+ * @param chainJson The account-key chain JSON or null
+ */
+ fun setAccountKeyChain(chainJson: String?)
+
/**
* Get the metadata.
* @return The metadata as a string
@@ -174,4 +186,27 @@ interface StorageProvider {
* @return The cache directory
*/
fun getCacheDir(): File
+
+ // region Sync engine state
+
+ /**
+ * Read one persisted value of the Rust sync engine (revisions, fingerprints, key blobs), as JSON text.
+ * @param key The engine's storage key
+ * @return The JSON text, or null when absent
+ */
+ fun getSyncEngineState(key: String): String?
+
+ /**
+ * Write (or with null, delete) one persisted value of the Rust sync engine.
+ * @param key The engine's storage key
+ * @param json The JSON text, or null to delete
+ */
+ fun setSyncEngineState(key: String, json: String?)
+
+ /**
+ * Forget every persisted value of the Rust sync engine (revisions, fingerprints, blob cache, key chain).
+ */
+ fun clearSyncEngineState()
+
+ // endregion
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/TestStorageProvider.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/TestStorageProvider.kt
index 968ce7f24..5d39f85c5 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/TestStorageProvider.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/storageprovider/TestStorageProvider.kt
@@ -11,6 +11,7 @@ class TestStorageProvider : StorageProvider {
private val tempFile = File.createTempFile("encrypted_database", ".db")
private var tempMetadata = String()
private var tempKeyDerivationParams = String()
+ private var tempAccountKeyChain: String? = null
private var tempAuthMethods = "[]"
private var tempAutoLockTimeout = defaultAutoLockTimeout
private var username: String? = null
@@ -48,6 +49,14 @@ class TestStorageProvider : StorageProvider {
return tempKeyDerivationParams
}
+ override fun getAccountKeyChain(): String? {
+ return tempAccountKeyChain
+ }
+
+ override fun setAccountKeyChain(chainJson: String?) {
+ tempAccountKeyChain = chainJson
+ }
+
override fun setAuthMethods(authMethods: String) {
tempAuthMethods = authMethods
}
@@ -68,6 +77,7 @@ class TestStorageProvider : StorageProvider {
tempFile.delete()
tempMetadata = ""
tempKeyDerivationParams = ""
+ tempAccountKeyChain = null
tempAuthMethods = "[]"
tempAutoLockTimeout = defaultAutoLockTimeout
}
@@ -141,4 +151,22 @@ class TestStorageProvider : StorageProvider {
override fun getCacheDir(): File {
return File(System.getProperty("java.io.tmpdir") ?: "/tmp")
}
+
+ // region Sync engine state
+
+ private val syncEngineState = mutableMapOf<String, String>()
+
+ override fun getSyncEngineState(key: String): String? {
+ return syncEngineState[key]
+ }
+
+ override fun setSyncEngineState(key: String, json: String?) {
+ if (json == null) syncEngineState.remove(key) else syncEngineState[key] = json
+ }
+
+ override fun clearSyncEngineState() {
+ syncEngineState.clear()
+ }
+
+ // endregion
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
index f50cca840..030f2f28b 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
@@ -5,6 +5,7 @@ import android.content.pm.PackageManager
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
+import net.aliasvault.app.utils.AppInfo
import org.json.JSONArray
import org.json.JSONObject
import java.io.BufferedReader
@@ -37,7 +38,7 @@ class WebApiService(private val context: Context) {
private const val REFRESH_TOKEN_KEY = "refreshToken"
private const val APP_INSTANCE_ID_KEY = "appInstanceId"
private const val CUSTOM_PROXY_HEADERS_KEY = "customProxyHeaders"
- private const val DEFAULT_API_URL = "https://app.aliasvault.com/api"
+ private const val DEFAULT_API_URL = AppInfo.DEFAULT_API_URL
private const val SHARED_PREFS_NAME = "aliasvault"
/**
@@ -141,6 +142,13 @@ class WebApiService(private val context: Context) {
return name to value
}
+ /**
+ * Get the API root URL (no version segment), for paths that carry their own.
+ */
+ private fun getApiRootUrl(): String {
+ return getApiUrl().trimEnd('/') + "/"
+ }
+
/**
* Get the base URL with /v1/ appended.
*/
@@ -191,12 +199,14 @@ class WebApiService(private val context: Context) {
/**
* Execute a WebAPI request with support for authentication and token refresh.
*/
+ @Suppress("LongParameterList") // One optional flag past the threshold; splitting the signature would hurt every caller
suspend fun executeRequest(
method: String,
endpoint: String,
body: String?,
headers: Map<String, String>,
requiresAuth: Boolean,
+ versioned: Boolean = false,
): WebApiResponse = withContext(Dispatchers.IO) {
val requestHeaders = headers.toMutableMap()
@@ -217,6 +227,7 @@ class WebApiService(private val context: Context) {
endpoint = endpoint,
body = body,
headers = requestHeaders,
+ versioned = versioned,
)
// Handle 401 Unauthorized - attempt token refresh
@@ -236,6 +247,7 @@ class WebApiService(private val context: Context) {
endpoint = endpoint,
body = body,
headers = retryHeaders,
+ versioned = versioned,
)
return@withContext retryResponse
@@ -249,6 +261,20 @@ class WebApiService(private val context: Context) {
response
}
+ /**
+ * Execute a request whose path carries its own API version segment (e.g. `v2/Vault`), with
+ * authentication and token refresh. This is what the Rust sync engine's HTTP commands go through.
+ */
+ suspend fun executeVersionedRequest(
+ method: String,
+ path: String,
+ body: String?,
+ headers: Map<String, String>,
+ requiresAuth: Boolean,
+ ): WebApiResponse {
+ return executeRequest(method, path, body, headers, requiresAuth, versioned = true)
+ }
+
/**
* Execute a raw HTTP request without token refresh logic.
*/
@@ -257,8 +283,9 @@ class WebApiService(private val context: Context) {
endpoint: String,
body: String?,
headers: Map<String, String>,
+ versioned: Boolean = false,
): WebApiResponse = withContext(Dispatchers.IO) {
- val baseUrl = getBaseUrl()
+ val baseUrl = if (versioned) getApiRootUrl() else getBaseUrl()
val urlString = "$baseUrl$endpoint"
var connection: HttpURLConnection? = null
@@ -341,7 +368,9 @@ class WebApiService(private val context: Context) {
}
/**
- * Refresh the access token using the refresh token.
+ * Refresh the access token using the refresh token. Returns null when the server refused the refresh (the
+ * session is over); a refresh that never reached the server throws, so the caller can go offline instead of
+ * treating a network blip as an expired session.
*/
private suspend fun refreshAccessToken(): String? = withContext(Dispatchers.IO) {
val refreshToken = getRefreshToken()
@@ -352,48 +381,47 @@ class WebApiService(private val context: Context) {
return@withContext null
}
- try {
- // Prepare refresh request body
- val refreshBody = JSONObject()
- refreshBody.put("token", accessToken)
- refreshBody.put("refreshToken", refreshToken)
-
- val headers = mutableMapOf(
- "Content-Type" to "application/json",
- "X-Ignore-Failure" to "true",
- )
- headers["X-AliasVault-Client"] = getClientVersionHeader()
- headers["X-AliasVault-AppInstanceId"] = appInstanceId
+ // Prepare refresh request body
+ val refreshBody = JSONObject()
+ refreshBody.put("token", accessToken)
+ refreshBody.put("refreshToken", refreshToken)
- val response = executeRawRequest(
- method = "POST",
- endpoint = "Auth/refresh",
- body = refreshBody.toString(),
- headers = headers,
- )
+ val headers = mutableMapOf(
+ "Content-Type" to "application/json",
+ "X-Ignore-Failure" to "true",
+ )
+ headers["X-AliasVault-Client"] = getClientVersionHeader()
+ headers["X-AliasVault-AppInstanceId"] = appInstanceId
- if (response.statusCode != 200) {
- Log.w(TAG, "Token refresh failed with status ${response.statusCode}")
- return@withContext null
- }
+ val response = executeRawRequest(
+ method = "POST",
+ endpoint = "Auth/refresh",
+ body = refreshBody.toString(),
+ headers = headers,
+ )
- // Parse the response JSON
- val json = JSONObject(response.body)
- val newToken = if (json.has("token")) json.getString("token") else null
- val newRefreshToken = if (json.has("refreshToken")) json.getString("refreshToken") else null
+ if (response.statusCode == 401 || response.statusCode == 403) {
+ Log.w(TAG, "Token refresh refused with status ${response.statusCode}")
+ return@withContext null
+ }
+ if (response.statusCode != 200) {
+ Log.w(TAG, "Token refresh failed with status ${response.statusCode}, treating the server as unreachable")
+ throw java.io.IOException("Token refresh failed with status ${response.statusCode}")
+ }
- if (newToken == null || newRefreshToken == null) {
- Log.w(TAG, "Token refresh response missing tokens")
- return@withContext null
- }
+ // Parse the response JSON
+ val json = JSONObject(response.body)
+ val newToken = if (json.has("token")) json.getString("token") else null
+ val newRefreshToken = if (json.has("refreshToken")) json.getString("refreshToken") else null
- // Update stored tokens
- setAuthTokens(accessToken = newToken, refreshToken = newRefreshToken)
- newToken
- } catch (e: Exception) {
- Log.e(TAG, "Token refresh failed", e)
- null
+ if (newToken == null || newRefreshToken == null) {
+ Log.w(TAG, "Token refresh response missing tokens")
+ return@withContext null
}
+
+ // Update stored tokens
+ setAuthTokens(accessToken = newToken, refreshToken = newRefreshToken)
+ newToken
}
// MARK: - Helper Methods
diff --git a/core/client/src/platform/ClientPlatform.ts b/core/client/src/platform/ClientPlatform.ts
index 5a0168baf..9f3b3de27 100644
--- a/core/client/src/platform/ClientPlatform.ts
+++ b/core/client/src/platform/ClientPlatform.ts
@@ -22,7 +22,7 @@ export interface IClientPlatform {
/** The Rust core: WebAssembly on the web hosts (see WasmRustCore), the native uniffi bindings on mobile. */
rustCore: IRustCore;
- /** The SQLite engine the vault is opened with: sql.js on the web hosts (see SqlJsEngine), expo-sqlite on mobile. */
+ /** The SQLite engine the vault is opened with: sql.js on the web hosts (see SqlJsEngine), unavailable on mobile. */
sqlite: ISqliteEngine;
/**
diff --git a/core/client/src/platform/SqliteEngine.ts b/core/client/src/platform/SqliteEngine.ts
index 11dea36f6..02ed73310 100644
--- a/core/client/src/platform/SqliteEngine.ts
+++ b/core/client/src/platform/SqliteEngine.ts
@@ -56,8 +56,8 @@ export interface ISqliteDatabase {
}
/**
- * The SQLite engine a host provides. The browser and web hosts use sql.js (see SqlJsEngine); the mobile host uses
- * expo-sqlite. Databases live in memory only: the vault at rest is the encrypted blob, never a database file.
+ * The in-JS SQLite engine a host provides: sql.js on the browser and web hosts (see SqlJsEngine). Databases are opened
+ * in memory only. The mobile host provides none, since its vault database is owned by the native vault store.
*/
export interface ISqliteEngine {
/**