From 1b8b043290505e76bf21cd6d3346d4b59b8b1366 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Sun, 17 May 2020 23:47:24 -0700 Subject: [PATCH] Supports resigning apps with multiple app groups --- AltServer/Devices/ALTDeviceManager.mm | 2 +- .../FetchProvisioningProfilesOperation.swift | 85 +++++++++++-------- Dependencies/AltSign | 2 +- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/AltServer/Devices/ALTDeviceManager.mm b/AltServer/Devices/ALTDeviceManager.mm index 38f1fefd..89b3b2dd 100644 --- a/AltServer/Devices/ALTDeviceManager.mm +++ b/AltServer/Devices/ALTDeviceManager.mm @@ -1117,7 +1117,7 @@ NSNotificationName const ALTDeviceManagerDeviceDidDisconnectNotification = @"ALT NSString *name = [NSString stringWithCString:device_name encoding:NSUTF8StringEncoding]; NSString *identifier = [NSString stringWithCString:udid encoding:NSUTF8StringEncoding]; - ALTDevice *altDevice = [[ALTDevice alloc] initWithName:name identifier:identifier]; + ALTDevice *altDevice = [[ALTDevice alloc] initWithName:name identifier:identifier type:ALTDeviceTypeiPhone]; [connectedDevices addObject:altDevice]; if (device_name != NULL) diff --git a/AltStore/Operations/FetchProvisioningProfilesOperation.swift b/AltStore/Operations/FetchProvisioningProfilesOperation.swift index 2110e0a4..239faf2d 100644 --- a/AltStore/Operations/FetchProvisioningProfilesOperation.swift +++ b/AltStore/Operations/FetchProvisioningProfilesOperation.swift @@ -361,57 +361,70 @@ extension FetchProvisioningProfilesOperation { entitlements[key] = value } - - // TODO: Handle apps belonging to more than one app group. - guard let applicationGroups = entitlements[.appGroups] as? [String], let groupIdentifier = applicationGroups.first else { - return completionHandler(.success(appID)) - } - - func finish(_ result: Result) - { - switch result - { - case .failure(let error): completionHandler(.failure(error)) - case .success(let group): - // Assign App Group - // TODO: Determine whether app already belongs to app group. - ALTAppleAPI.shared.add(appID, to: group, team: team, session: session) { (success, error) in - let result = result.map { _ in appID } - completionHandler(result) - } - } - } + guard let applicationGroups = entitlements[.appGroups] as? [String] else { return completionHandler(.success(appID)) } // Dispatch onto global queue to prevent appGroupsLock deadlock. DispatchQueue.global().async { - let adjustedGroupIdentifier = groupIdentifier + "." + team.identifier // Ensure we're not concurrently fetching and updating app groups, // which can lead to race conditions such as adding an app group twice. self.appGroupsLock.lock() + func finish(_ result: Result) + { + self.appGroupsLock.unlock() + completionHandler(result) + } + ALTAppleAPI.shared.fetchAppGroups(for: team, session: session) { (groups, error) in switch Result(groups, error) { - case .failure(let error): - self.appGroupsLock.unlock() - completionHandler(.failure(error)) + case .failure(let error): finish(.failure(error)) + case .success(let fetchedGroups): + let dispatchGroup = DispatchGroup() - case .success(let groups): - if let group = groups.first(where: { $0.groupIdentifier == adjustedGroupIdentifier }) + var groups = [ALTAppGroup]() + var errors = [Error]() + + for groupIdentifier in applicationGroups { - self.appGroupsLock.unlock() - finish(.success(group)) - } - else - { - // Not all characters are allowed in group names, so we replace periods with spaces (like Apple does). - let name = "AltStore " + groupIdentifier.replacingOccurrences(of: ".", with: " ") + let adjustedGroupIdentifier = groupIdentifier + "." + team.identifier - ALTAppleAPI.shared.addAppGroup(withName: name, groupIdentifier: adjustedGroupIdentifier, team: team, session: session) { (group, error) in - self.appGroupsLock.unlock() - finish(Result(group, error)) + if let group = fetchedGroups.first(where: { $0.groupIdentifier == adjustedGroupIdentifier }) + { + groups.append(group) + } + else + { + dispatchGroup.enter() + + // Not all characters are allowed in group names, so we replace periods with spaces (like Apple does). + let name = "AltStore " + groupIdentifier.replacingOccurrences(of: ".", with: " ") + + ALTAppleAPI.shared.addAppGroup(withName: name, groupIdentifier: adjustedGroupIdentifier, team: team, session: session) { (group, error) in + switch Result(group, error) + { + case .success(let group): groups.append(group) + case .failure(let error): errors.append(error) + } + + dispatchGroup.leave() + } + } + } + + dispatchGroup.notify(queue: .global()) { + if let error = errors.first + { + finish(.failure(error)) + } + else + { + ALTAppleAPI.shared.assign(appID, to: Array(groups), team: team, session: session) { (success, error) in + let result = Result(success, error) + finish(result.map { _ in appID }) + } } } } diff --git a/Dependencies/AltSign b/Dependencies/AltSign index 59050382..db6cadf0 160000 --- a/Dependencies/AltSign +++ b/Dependencies/AltSign @@ -1 +1 @@ -Subproject commit 5905038272c4b2ccee4390a0b830bfa84001f273 +Subproject commit db6cadf0210e150594f1b50a89fe15c64d9ffeee