Signal-iOS/SignalServiceKit/Network/API/Requests/AccountAttributes/AccountAttributesGenerator.swift

83 lines
3.1 KiB
Swift

//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
public struct AccountAttributesGenerator {
private let ows2FAManager: OWS2FAManager
private let profileManager: ProfileManager
private let svrLocalStorage: SVRLocalStorage
private let tsAccountManager: TSAccountManager
private let udManager: OWSUDManager
init(
ows2FAManager: OWS2FAManager,
profileManager: ProfileManager,
svrLocalStorage: SVRLocalStorage,
tsAccountManager: TSAccountManager,
udManager: OWSUDManager
) {
self.ows2FAManager = ows2FAManager
self.profileManager = profileManager
self.svrLocalStorage = svrLocalStorage
self.tsAccountManager = tsAccountManager
self.udManager = udManager
}
func generateForPrimary(
tx: DBWriteTransaction
) -> AccountAttributes {
owsAssertDebug(tsAccountManager.registrationState(tx: tx).isPrimaryDevice == true)
let sdsTx: SDSAnyWriteTransaction = SDSDB.shimOnlyBridge(tx)
let isManualMessageFetchEnabled = tsAccountManager.isManualMessageFetchEnabled(tx: tx)
let registrationId = tsAccountManager.getOrGenerateAciRegistrationId(tx: tx)
let pniRegistrationId = tsAccountManager.getOrGeneratePniRegistrationId(tx: tx)
guard let profileKey = profileManager.localUserProfile(tx: sdsTx)?.profileKey else {
owsFail("Couldn't fetch local profile key.")
}
let udAccessKey = SMKUDAccessKey(profileKey: profileKey).keyData.base64EncodedString()
let allowUnrestrictedUD = udManager.shouldAllowUnrestrictedAccessLocal(transaction: sdsTx)
let hasSVRBackups = svrLocalStorage.getIsMasterKeyBackedUp(tx)
let twoFaMode: AccountAttributes.TwoFactorAuthMode
if
let reglockToken = svrLocalStorage.getMasterKey(tx)?.data(for: .registrationLock),
ows2FAManager.isRegistrationLockV2Enabled(transaction: sdsTx)
{
twoFaMode = .v2(reglockToken: reglockToken.canonicalStringRepresentation)
} else if
let pinCode = ows2FAManager.pinCode(transaction: sdsTx),
pinCode.isEmpty.negated,
hasSVRBackups.negated
{
twoFaMode = .v1(pinCode: pinCode)
} else {
twoFaMode = .none
}
let registrationRecoveryPassword = svrLocalStorage.getMasterKey(tx)?.data(
for: .registrationRecoveryPassword
).canonicalStringRepresentation
let phoneNumberDiscoverability = tsAccountManager.phoneNumberDiscoverability(tx: tx)
return AccountAttributes(
isManualMessageFetchEnabled: isManualMessageFetchEnabled,
registrationId: registrationId,
pniRegistrationId: pniRegistrationId,
unidentifiedAccessKey: udAccessKey,
unrestrictedUnidentifiedAccess: allowUnrestrictedUD,
twofaMode: twoFaMode,
registrationRecoveryPassword: registrationRecoveryPassword,
encryptedDeviceName: nil,
discoverableByPhoneNumber: phoneNumberDiscoverability,
hasSVRBackups: hasSVRBackups
)
}
}