Signal-iOS/SignalServiceKit/KeyBackupService/KeyBackupServiceShims.swift
Harry a146e18aaf
Set up account state and exit registration
* Go to chat list view after registration

* sync system contacts during registration

* Use explicit local credentials for storage service operations during registration

* fix tests

* Quick hack to get through double pin confirmation

* Finishing touches

* lint

* fix build

* reload phone number discoverability after storage service sync

* fix tests again

* Take chat auth on account and contact record initializers

* Change around branches for clarity in OWSUserProfile

* pr comments

* Split ChatServiceAuth into the same and AuthedAccount

* fix tests

* merge woes
2023-03-09 21:54:51 -08:00

126 lines
4.5 KiB
Swift

//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
// This file has "shim" classes that allow KeyBackupService to talk to
// other classes which aren't protocolized and aren't stub-able in tests.
// We can easily produce stub instances of these protocols in tests,
// allowing us to control their behavior precisely.
// This also allows us to bridge from "v2" database transaction types
// which KeyBackupService uses, to sds transaction types that these
// older classes expect as parameters.
//
// Eventually, this whole file can be deleted, once:
// 1) The classes involved have been protocolized (and stub instances
// can be passed to KeyBackupService's initializer in tests).
// 2) The classes involved accept v2 transaction types.
// MARK: - Namespace
extension KBS {
public enum Shims {
public typealias TSAccountManager = _KeyBackupService_TSAccountManagerShim
public typealias StorageServiceManager = _KeyBackupService_StorageServiceManagerShim
public typealias OWS2FAManager = _KeyBackupService_OWS2FAManagerShim
public typealias RemoteAttestation = _KeyBackupService_RemoteAttestationShim
}
public enum Wrappers {
public typealias TSAccountManager = _KeyBackupService_TSAccountManagerWrapper
public typealias StorageServiceManager = _KeyBackupService_StorageServiceManagerWrapper
public typealias OWS2FAManager = _KeyBackupService_OWS2FAManagerWrapper
public typealias RemoteAttestation = _KeyBackupService_RemoteAttestationWrapper
}
}
// MARK: - TSAccountManager
public protocol _KeyBackupService_TSAccountManagerShim {
func isPrimaryDevice(transaction: DBReadTransaction) -> Bool
func isRegisteredAndReady(transaction: DBReadTransaction) -> Bool
}
public class _KeyBackupService_TSAccountManagerWrapper: KBS.Shims.TSAccountManager {
private let accountManager: TSAccountManager
public init(_ accountManager: TSAccountManager) { self.accountManager = accountManager }
public func isPrimaryDevice(transaction: DBReadTransaction) -> Bool {
return accountManager.isPrimaryDevice(transaction: SDSDB.shimOnlyBridge(transaction))
}
public func isRegisteredAndReady(transaction: DBReadTransaction) -> Bool {
return accountManager.isRegisteredAndReady(with: SDSDB.shimOnlyBridge(transaction))
}
}
// MARK: - StorageServiceManager
public protocol _KeyBackupService_StorageServiceManagerShim {
func resetLocalData(transaction: DBWriteTransaction)
func restoreOrCreateManifestIfNecessary(
authedAccount: AuthedAccount
)
}
public class _KeyBackupService_StorageServiceManagerWrapper: KBS.Shims.StorageServiceManager {
private let manager: StorageServiceManagerProtocol
public init(_ manager: StorageServiceManagerProtocol) { self.manager = manager }
public func resetLocalData(transaction: DBWriteTransaction) {
manager.resetLocalData(transaction: SDSDB.shimOnlyBridge(transaction))
}
public func restoreOrCreateManifestIfNecessary(
authedAccount: AuthedAccount
) {
manager.restoreOrCreateManifestIfNecessary(
authedAccount: authedAccount
)
}
}
// MARK: - OWS2FAManager
public protocol _KeyBackupService_OWS2FAManagerShim {
func pinCode(transaction: DBReadTransaction) -> String?
func markDisabled(transaction: DBWriteTransaction)
}
public class _KeyBackupService_OWS2FAManagerWrapper: KBS.Shims.OWS2FAManager {
private let manager: OWS2FAManager
public init(_ manager: OWS2FAManager) { self.manager = manager }
public func pinCode(transaction: DBReadTransaction) -> String? {
return manager.pinCode(with: SDSDB.shimOnlyBridge(transaction))
}
public func markDisabled(transaction: DBWriteTransaction) {
manager.markDisabled(transaction: SDSDB.shimOnlyBridge(transaction))
}
}
// MARK: - RemoteAttestation
public protocol _KeyBackupService_RemoteAttestationShim {
func performForKeyBackup(
authMethod: RemoteAttestation.KeyBackupAuthMethod,
enclave: KeyBackupEnclave
) -> Promise<RemoteAttestation>
}
public class _KeyBackupService_RemoteAttestationWrapper: _KeyBackupService_RemoteAttestationShim {
public func performForKeyBackup(
authMethod: RemoteAttestation.KeyBackupAuthMethod,
enclave: KeyBackupEnclave
) -> Promise<RemoteAttestation> {
return RemoteAttestation.performForKeyBackup(
authMethod: authMethod,
enclave: enclave
)
}
}