From 3b052efead2ffe2544bea6ce3b85503a9645a31c Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Tue, 12 May 2026 20:15:32 +0200 Subject: [PATCH] Move VaultConstants to VaultUtils shared project (#2006) --- ...ialProviderViewController+Credential.swift | 1 - .../ios/NativeVaultManager/VaultManager.swift | 17 ++-------- .../Constants/VaultConstants.swift | 34 ------------------- .../Services/WebApiService.swift | 1 + .../ios/VaultStoreKit/VaultStore+Auth.swift | 1 + .../ios/VaultStoreKit/VaultStore+Cache.swift | 1 + .../ios/VaultStoreKit/VaultStore+Crypto.swift | 1 + .../VaultStoreKit/VaultStore+Database.swift | 1 + .../VaultStoreKit/VaultStore+Metadata.swift | 1 + .../ios/VaultStoreKit/VaultStore+Mutate.swift | 1 + .../ios/VaultStoreKit/VaultStore+Pin.swift | 1 + .../ios/VaultStoreKit/VaultStore.swift | 1 + .../ios/VaultUtils/AutofillSettings.swift | 28 +++++++-------- .../ios/VaultUtils/VaultConstants.swift | 34 +++++++++++++++++++ 14 files changed, 58 insertions(+), 65 deletions(-) delete mode 100644 apps/mobile-app/ios/VaultStoreKit/Constants/VaultConstants.swift create mode 100644 apps/mobile-app/ios/VaultUtils/VaultConstants.swift diff --git a/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Credential.swift b/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Credential.swift index 28422df7c..d9031c57a 100644 --- a/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Credential.swift +++ b/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Credential.swift @@ -12,7 +12,6 @@ import VaultUtils extension CredentialProviderViewController: CredentialProviderDelegate { // MARK: - CredentialProviderDelegate Implementation - func setupCredentialView(vaultStore: VaultStore, serviceUrl: String?) throws -> UIViewController { // Create the ViewModel with injected behaviors let viewModel = CredentialProviderViewModel( diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift index b5344b5d4..6b71c8b31 100644 --- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift +++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift @@ -396,27 +396,14 @@ public class VaultManager: NSObject { @objc func getAutofillCopyTotpOnFill(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { - guard let defaults = UserDefaults(suiteName: VaultConstants.userDefaultsSuite) else { - resolve(true) - return - } - // Default to true when key has never been written. - if defaults.object(forKey: VaultConstants.autofillCopyTotpOnFillKey) == nil { - resolve(true) - return - } - resolve(defaults.bool(forKey: VaultConstants.autofillCopyTotpOnFillKey)) + resolve(AutofillSettings.shouldCopyTotpOnFill) } @objc func setAutofillCopyTotpOnFill(_ enabled: Bool, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { - guard let defaults = UserDefaults(suiteName: VaultConstants.userDefaultsSuite) else { - reject("AUTOFILL_SETTING_ERROR", "App Group UserDefaults unavailable", nil) - return - } - defaults.set(enabled, forKey: VaultConstants.autofillCopyTotpOnFillKey) + AutofillSettings.shouldCopyTotpOnFill = enabled resolve(nil) } diff --git a/apps/mobile-app/ios/VaultStoreKit/Constants/VaultConstants.swift b/apps/mobile-app/ios/VaultStoreKit/Constants/VaultConstants.swift deleted file mode 100644 index 0fdd3db5a..000000000 --- a/apps/mobile-app/ios/VaultStoreKit/Constants/VaultConstants.swift +++ /dev/null @@ -1,34 +0,0 @@ -import Foundation -import VaultModels - -/// Constants used for userDefaults keys and other things. -public struct VaultConstants { - static let keychainService = "net.aliasvault.autofill" - static let keychainAccessGroup = "group.net.aliasvault.autofill" - public static let userDefaultsSuite = "group.net.aliasvault.autofill" - - static let vaultMetadataKey = "aliasvault_vault_metadata" - static let encryptionKeyKey = "aliasvault_encryption_key" - static let encryptedDbFileName = "encrypted_db.sqlite" - static let authMethodsKey = "aliasvault_auth_methods" - static let autoLockTimeoutKey = "aliasvault_auto_lock_timeout" - static let encryptionKeyDerivationParamsKey = "aliasvault_encryption_key_derivation_params" - static let usernameKey = "aliasvault_username" - static let offlineModeKey = "aliasvault_offline_mode" - static let pinEnabledKey = "aliasvault_pin_enabled" - static let serverVersionKey = "aliasvault_server_version" - public static let autofillCopyTotpOnFillKey = "aliasvault_autofill_copy_totp_on_fill" - - // Sync state keys (for offline sync and race detection) - static let isDirtyKey = "aliasvault_is_dirty" - static let mutationSequenceKey = "aliasvault_mutation_sequence" - static let isSyncingKey = "aliasvault_is_syncing" - - static let defaultAutoLockTimeout: Int = 3600 // 1 hour in seconds - - // Trash retention. Soft-deleted items stay in the recycle bin for this many - // days before the Rust pruner permanently removes them on the next sync. - // This value is declared in other places as well, make sure to update them - // when updating this value. - static let trashRetentionDays: Int = 30 -} diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift b/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift index 8df127b37..dad580db7 100644 --- a/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift +++ b/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift @@ -1,4 +1,5 @@ import Foundation +import VaultUtils /** * Native Swift WebAPI service for making HTTP requests to the AliasVault server. diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift index 775fb397b..dcf613985 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift @@ -2,6 +2,7 @@ import Foundation import LocalAuthentication import Security import VaultModels +import VaultUtils /// Extension for the VaultStore class to handle authentication methods extension VaultStore { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift index 0640e2670..652c1821f 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift @@ -1,5 +1,6 @@ import Foundation import Security +import VaultUtils /// Extension for the VaultStore class to handle cache management extension VaultStore { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift index e9c7e38e4..0bcdfaa4c 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift @@ -3,6 +3,7 @@ import CryptoKit import LocalAuthentication import Security import SignalArgon2 +import VaultUtils /// Extension for the VaultStore class to handle encryption/decryption extension VaultStore { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift index 1fbefe588..a3f9b020e 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift @@ -1,5 +1,6 @@ import Foundation import SQLite +import VaultUtils /// Extension for the VaultStore class to handle database management extension VaultStore { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift index 9a874daf4..250585840 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift @@ -1,5 +1,6 @@ import Foundation import VaultModels +import VaultUtils /// Extension for the VaultStore class to handle metadata management extension VaultStore { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Mutate.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Mutate.swift index 8953aab9f..783049dab 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Mutate.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Mutate.swift @@ -1,5 +1,6 @@ import Foundation import VaultModels +import VaultUtils /// Vault upload model that matches the API contract public struct VaultUpload: Codable { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift index 98cc2c4b8..0f3c8e158 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift @@ -3,6 +3,7 @@ import CryptoKit import Security import SignalArgon2 import VaultModels +import VaultUtils /// Extension for the VaultStore class to handle PIN unlock functionality extension VaultStore { diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift index fe43407e6..8a374ae02 100644 --- a/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift +++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift @@ -5,6 +5,7 @@ import CryptoKit import CommonCrypto import Security import VaultModels +import VaultUtils /// This class is used to store and retrieve the encrypted AliasVault database and encryption key. /// It also handles executing queries against the SQLite database and biometric authentication. diff --git a/apps/mobile-app/ios/VaultUtils/AutofillSettings.swift b/apps/mobile-app/ios/VaultUtils/AutofillSettings.swift index ea8e85852..ff253d913 100644 --- a/apps/mobile-app/ios/VaultUtils/AutofillSettings.swift +++ b/apps/mobile-app/ios/VaultUtils/AutofillSettings.swift @@ -1,30 +1,28 @@ import Foundation -/// Read-only access to autofill-related preferences shared between the main app and +/// Read/write access to autofill-related preferences shared between the main app and /// the iOS Autofill extension via the App Group UserDefaults suite. /// -/// The setter side lives in NativeVaultManager (main app target) and writes to the -/// same suite. Keys and the suite identifier must stay in sync with VaultConstants. +/// All underlying identifiers (suite name + key names) come from `VaultConstants` +/// so they are defined exactly once across the project. public enum AutofillSettings { - /// App Group identifier — must match `VaultConstants.userDefaultsSuite` and - /// the autofill entitlements. - private static let suiteName = "group.net.aliasvault.autofill" - - /// Key — must match `VaultConstants.autofillCopyTotpOnFillKey`. - private static let copyTotpOnFillKey = "aliasvault_autofill_copy_totp_on_fill" - private static var sharedDefaults: UserDefaults? { - UserDefaults(suiteName: suiteName) + UserDefaults(suiteName: VaultConstants.userDefaultsSuite) } /// Whether the autofill extension should copy a credential's current TOTP code to /// the clipboard when the user selects it for autofill. /// Defaults to `true` when the key has never been written. public static var shouldCopyTotpOnFill: Bool { - guard let defaults = sharedDefaults else { return true } - if defaults.object(forKey: copyTotpOnFillKey) == nil { - return true + get { + guard let defaults = sharedDefaults else { return true } + if defaults.object(forKey: VaultConstants.autofillCopyTotpOnFillKey) == nil { + return true + } + return defaults.bool(forKey: VaultConstants.autofillCopyTotpOnFillKey) + } + set { + sharedDefaults?.set(newValue, forKey: VaultConstants.autofillCopyTotpOnFillKey) } - return defaults.bool(forKey: copyTotpOnFillKey) } } diff --git a/apps/mobile-app/ios/VaultUtils/VaultConstants.swift b/apps/mobile-app/ios/VaultUtils/VaultConstants.swift new file mode 100644 index 000000000..28bb6f8a9 --- /dev/null +++ b/apps/mobile-app/ios/VaultUtils/VaultConstants.swift @@ -0,0 +1,34 @@ +import Foundation + +/// Constants used for userDefaults keys, keychain identifiers, and other shared +/// identifiers across the app, autofill extension, and shared frameworks. +public struct VaultConstants { + public static let keychainService = "net.aliasvault.autofill" + public static let keychainAccessGroup = "group.net.aliasvault.autofill" + public static let userDefaultsSuite = "group.net.aliasvault.autofill" + + public static let vaultMetadataKey = "aliasvault_vault_metadata" + public static let encryptionKeyKey = "aliasvault_encryption_key" + public static let encryptedDbFileName = "encrypted_db.sqlite" + public static let authMethodsKey = "aliasvault_auth_methods" + public static let autoLockTimeoutKey = "aliasvault_auto_lock_timeout" + public static let encryptionKeyDerivationParamsKey = "aliasvault_encryption_key_derivation_params" + public static let usernameKey = "aliasvault_username" + public static let offlineModeKey = "aliasvault_offline_mode" + public static let pinEnabledKey = "aliasvault_pin_enabled" + public static let serverVersionKey = "aliasvault_server_version" + public static let autofillCopyTotpOnFillKey = "aliasvault_autofill_copy_totp_on_fill" + + // Sync state keys (for offline sync and race detection) + public static let isDirtyKey = "aliasvault_is_dirty" + public static let mutationSequenceKey = "aliasvault_mutation_sequence" + public static let isSyncingKey = "aliasvault_is_syncing" + + public static let defaultAutoLockTimeout: Int = 3600 // 1 hour in seconds + + // Trash retention. Soft-deleted items stay in the recycle bin for this many + // days before the Rust pruner permanently removes them on the next sync. + // This value is declared in other places as well, make sure to update them + // when updating this value. + public static let trashRetentionDays: Int = 30 +}