Skip to content

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

3.4 KB
Raw Blame
import Foundation
import VaultModels
import VaultUtils

/// The sync operations of the store, forwarded to VaultSync (the wrapper around the Rust sync engine).
extension VaultStore {
    /// Full vault sync: status check, then pull (and merge) or push as the server and local revisions decide.
    public func syncVaultWithServer(using webApiService: WebApiService) async -> VaultSyncResult {
        return await sync.syncVaultWithServer(using: webApiService)
    }

    /// How long a sync hold stays valid without being released, so a crashed holder is forgotten.
    private static let vaultSyncHoldTtl: TimeInterval = 2 * 60

    /// Hold vault syncing while an operation runs that a sync must not race (e.g. a password change), or release it with nil.
    public func setVaultSyncHold(_ reason: String?) {
        if let reason = reason {
            userDefaults.set(["reason": reason, "heldAt": Date().timeIntervalSince1970], forKey: VaultConstants.vaultSyncHoldKey)
        } else {
            userDefaults.removeObject(forKey: VaultConstants.vaultSyncHoldKey)
        }
    }

    /// The reason syncing is on hold, or nil when no unexpired hold is held.
    public func getVaultSyncHoldReason() -> String? {
        guard let hold = userDefaults.dictionary(forKey: VaultConstants.vaultSyncHoldKey),
              let reason = hold["reason"] as? String,
              let heldAt = hold["heldAt"] as? TimeInterval,
              Date().timeIntervalSince1970 - heldAt < Self.vaultSyncHoldTtl else {
            return nil
        }
        return reason
    }

    /// One status call: whether the server holds newer state than this device.
    public func checkVaultVersion(using webApiService: WebApiService) async throws -> VaultVersionCheckResult {
        return try await sync.checkVaultVersion(using: webApiService)
    }

    /// Resolve and store the vault key right after login from the password-derived key (see VaultSync.resolveVaultKey).
    public func resolveVaultKey(using webApiService: WebApiService, derivedKeyBase64: String) async throws {
        try await sync.resolveVaultKey(using: webApiService, derivedKeyBase64: derivedKeyBase64)
    }

    /// Classify the pending manifest migration (see VaultSync.getVaultMigrationStatus).
    public func getVaultMigrationStatus(using webApiService: WebApiService) async throws -> String {
        return try await sync.getVaultMigrationStatus(using: webApiService)
    }

    /// Run the pending manifest migration and push it (see VaultSync.migrateVaultManifest).
    public func migrateVaultManifest(using webApiService: WebApiService) async -> VaultMigrationResult {
        return await sync.migrateVaultManifest(using: webApiService)
    }

    /// Run a sharing operation of the sync engine (see VaultSync.runSharingOperation).
    public func runSharingOperation(_ operation: String, params: [String: Any], using webApiService: WebApiService) async -> VaultSharingResult {
        return await sync.runSharingOperation(operation, params: params, using: webApiService)
    }

    /// Push the pending local changes (after a native mutation such as an autofill link or a passkey creation).
    public func mutateVault(using webApiService: WebApiService) async throws {
        try await sync.mutateVault(using: webApiService)
    }

    /// The logs of the recent sync engine runs as JSON text, newest first, for the developer tools.
    public func getVaultSyncLogs() -> String {
        return VaultSyncRunLog.persistedLogs(in: userDefaults)
    }
}