Skip to content

Commit 32a2c55

Browse files
Add BackupIdManager
1 parent 940d6f4 commit 32a2c55

File tree

10 files changed

+236
-134
lines changed

10 files changed

+236
-134
lines changed

Signal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,7 @@
24412441
D91D9C8C2C3F06400009E4F7 /* MessageBackupExpirationTimerChatUpdateArchiver.swift in Sources */ = {isa = PBXBuildFile; fileRef = D91D9C8B2C3F06400009E4F7 /* MessageBackupExpirationTimerChatUpdateArchiver.swift */; };
24422442
D91F0B4E2B193A5C0086DB30 /* GroupCallRecordRingUpdateDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9B91D8D2B17E2A600BCB11A /* GroupCallRecordRingUpdateDelegate.swift */; };
24432443
D91F0B4F2B193A7A0086DB30 /* GroupCallRecordRingUpdateDelegateTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D91F0B4B2B1939B60086DB30 /* GroupCallRecordRingUpdateDelegateTest.swift */; };
2444+
D923DF9C2DC135D200CDAFC3 /* BackupIdManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D923DF9B2DC135A000CDAFC3 /* BackupIdManager.swift */; };
24442445
D9247EA62BFBFFCA00DFEF6F /* DeleteForMeIncomingSyncMessageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9247EA42BFBFFCA00DFEF6F /* DeleteForMeIncomingSyncMessageManager.swift */; };
24452446
D9247EA82BFD28E800DFEF6F /* DeleteForMeSyncMessageReceiver.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9247EA72BFD28E800DFEF6F /* DeleteForMeSyncMessageReceiver.swift */; };
24462447
D9247EAA2BFD424800DFEF6F /* InteractionDeleteManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9247EA92BFD424800DFEF6F /* InteractionDeleteManager.swift */; };
@@ -6351,6 +6352,7 @@
63516352
D91D9C8B2C3F06400009E4F7 /* MessageBackupExpirationTimerChatUpdateArchiver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageBackupExpirationTimerChatUpdateArchiver.swift; sourceTree = "<group>"; };
63526353
D91F0B4B2B1939B60086DB30 /* GroupCallRecordRingUpdateDelegateTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupCallRecordRingUpdateDelegateTest.swift; sourceTree = "<group>"; };
63536354
D91F7A2C2935A32F00012C64 /* DonationMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DonationMode.swift; sourceTree = "<group>"; };
6355+
D923DF9B2DC135A000CDAFC3 /* BackupIdManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackupIdManager.swift; sourceTree = "<group>"; };
63546356
D9247EA42BFBFFCA00DFEF6F /* DeleteForMeIncomingSyncMessageManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeleteForMeIncomingSyncMessageManager.swift; sourceTree = "<group>"; };
63556357
D9247EA72BFD28E800DFEF6F /* DeleteForMeSyncMessageReceiver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteForMeSyncMessageReceiver.swift; sourceTree = "<group>"; };
63566358
D9247EA92BFD424800DFEF6F /* InteractionDeleteManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InteractionDeleteManager.swift; sourceTree = "<group>"; };
@@ -12388,6 +12390,7 @@
1238812390
D9388C8F2DA4751A0048D4F9 /* Settings */ = {
1238912391
isa = PBXGroup;
1239012392
children = (
12393+
D923DF9B2DC135A000CDAFC3 /* BackupIdManager.swift */,
1239112394
D9388C902DA4751F0048D4F9 /* BackupSettingsStore.swift */,
1239212395
);
1239312396
path = Settings;
@@ -17495,6 +17498,7 @@
1749517498
667BB2082C580C1400E79B57 /* BackupAttachmentDownloadStore.swift in Sources */,
1749617499
66734F012CA1ED3F00558494 /* BackupAttachmentUploadManager.swift in Sources */,
1749717500
66C7952D2C9B78E900C13937 /* BackupAttachmentUploadStore.swift in Sources */,
17501+
D923DF9C2DC135D200CDAFC3 /* BackupIdManager.swift in Sources */,
1749817502
D970541F2CFE4D0200AC7954 /* BackupPaymentMethod.swift in Sources */,
1749917503
D97054202CFE4D0200AC7954 /* BackupPaymentProcessor.swift in Sources */,
1750017504
D9EE79112CF525A4007F581B /* BackupReceiptCredentialRedemptionJobQueue.swift in Sources */,

Signal/src/ViewControllers/AppSettings/Internal/InternalSettingsViewController.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ private extension InternalSettingsViewController {
397397
}
398398

399399
func exportMessageBackupProtoRemotely() async throws {
400+
let backupIdManager = DependenciesBridge.shared.backupIdManager
400401
let messageBackupKeyMaterial = DependenciesBridge.shared.messageBackupKeyMaterial
401402
let messageBackupManager = DependenciesBridge.shared.messageBackupManager
402403
let tsAccountManager = DependenciesBridge.shared.tsAccountManager
@@ -418,8 +419,15 @@ private extension InternalSettingsViewController {
418419
backupPurpose: .remoteBackup,
419420
progress: nil
420421
)
422+
423+
let registeredBackupIDToken = try await backupIdManager.registerBackupId(
424+
localIdentifiers: localIdentifiers,
425+
auth: .implicit()
426+
)
427+
421428
_ = try await messageBackupManager.uploadEncryptedBackup(
422429
metadata: metadata,
430+
registeredBackupIDToken: registeredBackupIDToken,
423431
localIdentifiers: localIdentifiers,
424432
auth: .implicit()
425433
)

SignalServiceKit/Dependencies/DependenciesBridge.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class DependenciesBridge {
6565
public let backupAttachmentDownloadQueueStatusManager: BackupAttachmentDownloadQueueStatusManager
6666
public let backupAttachmentDownloadStore: BackupAttachmentDownloadStore
6767
public let backupAttachmentUploadManager: BackupAttachmentUploadManager
68+
public let backupIdManager: BackupIdManager
6869
public let backupSubscriptionManager: BackupSubscriptionManager
6970
public let badgeCountFetcher: BadgeCountFetcher
7071
public let callLinkStore: any CallLinkRecordStore
@@ -121,6 +122,7 @@ public class DependenciesBridge {
121122
public let messageBackupErrorPresenter: MessageBackupErrorPresenter
122123
public let messageBackupKeyMaterial: MessageBackupKeyMaterial
123124
public let messageBackupManager: MessageBackupManager
125+
public let messageBackupRequestManager: MessageBackupRequestManager
124126
public let messageStickerManager: MessageStickerManager
125127
public let nicknameManager: any NicknameManager
126128
public let orphanedBackupAttachmentManager: OrphanedBackupAttachmentManager
@@ -188,6 +190,7 @@ public class DependenciesBridge {
188190
backupAttachmentDownloadQueueStatusManager: BackupAttachmentDownloadQueueStatusManager,
189191
backupAttachmentDownloadStore: BackupAttachmentDownloadStore,
190192
backupAttachmentUploadManager: BackupAttachmentUploadManager,
193+
backupIdManager: BackupIdManager,
191194
backupSubscriptionManager: BackupSubscriptionManager,
192195
badgeCountFetcher: BadgeCountFetcher,
193196
callLinkStore: any CallLinkRecordStore,
@@ -244,6 +247,7 @@ public class DependenciesBridge {
244247
messageBackupErrorPresenter: MessageBackupErrorPresenter,
245248
messageBackupKeyMaterial: MessageBackupKeyMaterial,
246249
messageBackupManager: MessageBackupManager,
250+
messageBackupRequestManager: MessageBackupRequestManager,
247251
messageStickerManager: MessageStickerManager,
248252
nicknameManager: any NicknameManager,
249253
orphanedBackupAttachmentManager: OrphanedBackupAttachmentManager,
@@ -310,6 +314,7 @@ public class DependenciesBridge {
310314
self.backupAttachmentDownloadQueueStatusManager = backupAttachmentDownloadQueueStatusManager
311315
self.backupAttachmentDownloadStore = backupAttachmentDownloadStore
312316
self.backupAttachmentUploadManager = backupAttachmentUploadManager
317+
self.backupIdManager = backupIdManager
313318
self.backupSubscriptionManager = backupSubscriptionManager
314319
self.badgeCountFetcher = badgeCountFetcher
315320
self.callLinkStore = callLinkStore
@@ -366,6 +371,7 @@ public class DependenciesBridge {
366371
self.messageBackupErrorPresenter = messageBackupErrorPresenter
367372
self.messageBackupKeyMaterial = messageBackupKeyMaterial
368373
self.messageBackupManager = messageBackupManager
374+
self.messageBackupRequestManager = messageBackupRequestManager
369375
self.messageStickerManager = messageStickerManager
370376
self.nicknameManager = nicknameManager
371377
self.orphanedBackupAttachmentManager = orphanedBackupAttachmentManager

SignalServiceKit/Environment/AppSetup.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,13 @@ public class AppSetup {
978978
tsAccountManager: tsAccountManager
979979
)
980980

981+
let backupIdManager = BackupIdManager(
982+
api: BackupIdManager.NetworkAPI(networkManager: networkManager),
983+
backupRequestManager: messageBackupRequestManager,
984+
backupKeyMaterial: messageBackupKeyMaterial,
985+
db: db
986+
)
987+
981988
let backupReceiptCredentialRedemptionJobQueue = BackupReceiptCredentialRedemptionJobQueue(
982989
authCredentialStore: authCredentialStore,
983990
db: db,
@@ -1282,6 +1289,7 @@ public class AppSetup {
12821289
backupAttachmentDownloadQueueStatusManager: backupAttachmentDownloadQueueStatusManager,
12831290
backupAttachmentDownloadStore: backupAttachmentDownloadStore,
12841291
backupAttachmentUploadManager: backupAttachmentUploadManager,
1292+
backupIdManager: backupIdManager,
12851293
backupSubscriptionManager: backupSubscriptionManager,
12861294
badgeCountFetcher: badgeCountFetcher,
12871295
callLinkStore: callLinkStore,
@@ -1338,6 +1346,7 @@ public class AppSetup {
13381346
messageBackupErrorPresenter: messageBackupErrorPresenter,
13391347
messageBackupKeyMaterial: messageBackupKeyMaterial,
13401348
messageBackupManager: messageBackupManager,
1349+
messageBackupRequestManager: messageBackupRequestManager,
13411350
messageStickerManager: messageStickerManager,
13421351
nicknameManager: nicknameManager,
13431352
orphanedBackupAttachmentManager: orphanedBackupAttachmentManager,

SignalServiceKit/MessageBackup/MessageBackupManager.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ public protocol MessageBackupManager {
1111
// MARK: - Interact with remotes
1212

1313
/// Download the encrypted backup for the current user to a local file.
14-
func downloadEncryptedBackup(localIdentifiers: LocalIdentifiers, auth: ChatServiceAuth) async throws -> URL
14+
func downloadEncryptedBackup(
15+
localIdentifiers: LocalIdentifiers,
16+
auth: ChatServiceAuth
17+
) async throws -> URL
1518

1619
/// Upload the local encrypted backup identified by the given metadata for
1720
/// the current user.
1821
func uploadEncryptedBackup(
1922
metadata: Upload.EncryptedBackupUploadMetadata,
23+
registeredBackupIDToken: BackupIdManager.RegisteredBackupIDToken,
2024
localIdentifiers: LocalIdentifiers,
2125
auth: ChatServiceAuth
2226
) async throws -> Upload.Result<Upload.EncryptedBackupUploadMetadata>
2327

2428
// MARK: - Export
2529

2630
/// Export an encrypted backup binary to a local file.
27-
/// - SeeAlso ``uploadEncryptedBackup(metadata:localIdentifiers:auth:)``
31+
/// - SeeAlso `uploadEncryptedBackup`
2832
func exportEncryptedBackup(
2933
localIdentifiers: LocalIdentifiers,
3034
backupKey: BackupKey,

SignalServiceKit/MessageBackup/MessageBackupManagerImpl.swift

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -145,34 +145,6 @@ public class MessageBackupManagerImpl: MessageBackupManager {
145145

146146
// MARK: - Remote backups
147147

148-
/// Initialize Message Backups by reserving a backup ID and registering a public key used to sign backup auth credentials.
149-
/// These registration calls are safe to call multiple times, but to avoid unecessary network calls, the app will remember if
150-
/// backups have been successfully registered on this device and will no-op in this case.
151-
private func reserveAndRegister(localIdentifiers: LocalIdentifiers, auth: ChatServiceAuth) async throws {
152-
let (hasReservedBackupKey, hasReservedMediaBackupKey) = db.read { tx in
153-
return (
154-
kvStore.getBool(Constants.keyValueStoreHasReservedBackupKey, transaction: tx) ?? false,
155-
kvStore.getBool(Constants.keyValueStoreHasReservedMediaBackupKey, transaction: tx) ?? false
156-
)
157-
}
158-
159-
if hasReservedBackupKey && hasReservedMediaBackupKey {
160-
return
161-
}
162-
163-
// Both reserveBackupId and registerBackupKeys can be called multiple times, so if
164-
// we think the backupId needs to be registered, register the public key at the same time.
165-
let localAci = localIdentifiers.aci
166-
try await backupRequestManager.reserveBackupId(localAci: localAci, auth: auth)
167-
try await backupRequestManager.registerBackupKeys(localAci: localAci, auth: auth)
168-
169-
// Remember this device has registered for backups
170-
await db.awaitableWrite { [weak self] tx in
171-
self?.kvStore.setBool(true, key: Constants.keyValueStoreHasReservedBackupKey, transaction: tx)
172-
self?.kvStore.setBool(true, key: Constants.keyValueStoreHasReservedMediaBackupKey, transaction: tx)
173-
}
174-
}
175-
176148
public func downloadEncryptedBackup(localIdentifiers: LocalIdentifiers, auth: ChatServiceAuth) async throws -> URL {
177149
let backupAuth = try await backupRequestManager.fetchBackupServiceAuth(
178150
for: .messages,
@@ -190,11 +162,10 @@ public class MessageBackupManagerImpl: MessageBackupManager {
190162

191163
public func uploadEncryptedBackup(
192164
metadata: Upload.EncryptedBackupUploadMetadata,
165+
registeredBackupIDToken: BackupIdManager.RegisteredBackupIDToken,
193166
localIdentifiers: LocalIdentifiers,
194167
auth: ChatServiceAuth
195168
) async throws -> Upload.Result<Upload.EncryptedBackupUploadMetadata> {
196-
// This will return early if this device has already registered the backup ID.
197-
try await reserveAndRegister(localIdentifiers: localIdentifiers, auth: auth)
198169
let backupAuth = try await backupRequestManager.fetchBackupServiceAuth(
199170
for: .messages,
200171
localAci: localIdentifiers.aci,

SignalServiceKit/MessageBackup/MessageBackupManagerMock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ open class MessageBackupManagerMock: MessageBackupManager {
1818

1919
public func uploadEncryptedBackup(
2020
metadata: Upload.EncryptedBackupUploadMetadata,
21+
registeredBackupIDToken: BackupIdManager.RegisteredBackupIDToken,
2122
localIdentifiers: LocalIdentifiers,
2223
auth: ChatServiceAuth
2324
) async throws -> Upload.Result<Upload.EncryptedBackupUploadMetadata> {

SignalServiceKit/MessageBackup/MessageBackupRequestManager.swift

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ public protocol MessageBackupRequestManager {
9090
auth: ChatServiceAuth
9191
) async throws -> MessageBackupServiceAuth
9292

93-
func reserveBackupId(localAci: Aci, auth: ChatServiceAuth) async throws
94-
95-
func registerBackupKeys(localAci: Aci, auth: ChatServiceAuth) async throws
96-
9793
func fetchBackupUploadForm(auth: MessageBackupServiceAuth) async throws -> Upload.Form
9894

9995
func fetchBackupMediaAttachmentUploadForm(auth: MessageBackupServiceAuth) async throws -> Upload.Form
@@ -187,33 +183,6 @@ public struct MessageBackupRequestManagerImpl: MessageBackupRequestManager {
187183
self.networkManager = networkManager
188184
}
189185

190-
// MARK: - Reserve Backup
191-
192-
/// Onetime request to reserve this backup ID.
193-
public func reserveBackupId(localAci: Aci, auth: ChatServiceAuth) async throws {
194-
let messageBackupRequestContext = try db.read { tx in
195-
BackupAuthCredentialRequestContext.create(
196-
backupKey: try messageBackupKeyMaterial.backupKey(type: .messages, tx: tx).serialize(),
197-
aci: localAci.rawUUID
198-
)
199-
}
200-
let mediaBackupRequestContext = try db.read { tx in
201-
return BackupAuthCredentialRequestContext.create(
202-
backupKey: try messageBackupKeyMaterial.backupKey(type: .media, tx: tx).serialize(),
203-
aci: localAci.rawUUID
204-
)
205-
}
206-
let base64MessageRequestContext = messageBackupRequestContext.getRequest().serialize().asData.base64EncodedString()
207-
let base64MediaRequestContext = mediaBackupRequestContext.getRequest().serialize().asData.base64EncodedString()
208-
let request = try OWSRequestFactory.reserveBackupId(
209-
backupId: base64MessageRequestContext,
210-
mediaBackupId: base64MediaRequestContext,
211-
auth: auth
212-
)
213-
// TODO: Switch this back to true when reg supports websockets
214-
_ = try await networkManager.asyncRequest(request, canUseWebSocket: false)
215-
}
216-
217186
// MARK: - Backup Auth
218187

219188
public func fetchBackupServiceAuth(
@@ -242,31 +211,6 @@ public struct MessageBackupRequestManagerImpl: MessageBackupRequestManager {
242211
)
243212
}
244213

245-
// MARK: - Register Backup
246-
247-
/// Onetime request to register the backup public key.
248-
public func registerBackupKeys(localAci: Aci, auth: ChatServiceAuth) async throws {
249-
let backupAuth = try await fetchBackupServiceAuth(
250-
for: .messages,
251-
localAci: localAci,
252-
auth: auth
253-
)
254-
_ = try await executeBackupServiceRequest(
255-
auth: backupAuth,
256-
requestFactory: OWSRequestFactory.backupSetPublicKeyRequest(auth:)
257-
)
258-
259-
let mediaBackupAuth = try await fetchBackupServiceAuth(
260-
for: .media,
261-
localAci: localAci,
262-
auth: auth
263-
)
264-
_ = try await executeBackupServiceRequest(
265-
auth: mediaBackupAuth,
266-
requestFactory: OWSRequestFactory.backupSetPublicKeyRequest(auth:)
267-
)
268-
}
269-
270214
// MARK: - Upload Forms
271215

272216
/// CDN upload form for uploading a backup
@@ -351,15 +295,6 @@ public struct MessageBackupRequestManagerImpl: MessageBackupRequestManager {
351295
)
352296
}
353297

354-
/// Delete the current backup
355-
public func deleteBackup(auth: MessageBackupServiceAuth) async throws {
356-
owsAssertDebug(auth.type == .messages)
357-
_ = try await executeBackupServiceRequest(
358-
auth: auth,
359-
requestFactory: OWSRequestFactory.deleteBackupRequest(auth:)
360-
)
361-
}
362-
363298
// MARK: - Media
364299

365300
/// Retrieve credentials used for reading from the CDN

0 commit comments

Comments
 (0)