Fix bugs in FetchCertOp, asycify
- Commit
14cef5cf428d1db647385d084bdf18f973329a1e- Author
- Kabir Oberai <[email protected]>
- When
- 2023-09-01 17:12 UTC (3 years ago)
- Parents
2b2ee98
Diff
Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift
diff --git a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swiftindex 3601f89..6591896 100644--- a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift+++ b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift@@ -29,27 +29,19 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera } public let context: SigningContext- public let confirmRevocation: ([DeveloperServicesCertificate], @escaping (Bool) -> Void) -> Void+ public let confirmRevocation: ([DeveloperServicesCertificate]) async -> Bool public init( context: SigningContext,- confirmRevocation: @escaping ([DeveloperServicesCertificate], @escaping (Bool) -> Void) -> Void+ confirmRevocation: @escaping ([DeveloperServicesCertificate]) async -> Bool ) { self.context = context self.confirmRevocation = confirmRevocation } - private func createCertificate(- completion: @escaping (Result<SigningInfo, Swift.Error>) -> Void- ) {- let csr: CSR- let privateKey: PrivateKey- do {- let keypair = try Keypair()- csr = try keypair.generateCSR()- privateKey = try keypair.privateKey()- } catch {- return completion(.failure(error))- }+ private func createCertificate() async throws -> SigningInfo {+ let keypair = try Keypair()+ let csr = try keypair.generateCSR()+ let privateKey = try keypair.privateKey() let request = DeveloperServicesSubmitCSRRequest( platform: context.platform,@@ -58,72 +50,65 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera machineName: "Supercharge: \(SigningContext.hostName)", machineID: context.client.deviceInfo.deviceID )- context.client.send(request) { result in- guard let partialCert = result.get(withErrorHandler: completion) else { return }- let serialNumber = partialCert.serialNumber+ let partialCert = try await context.client.send(request) - let request = DeveloperServicesListCertificatesRequest(- teamID: self.context.teamID, certificateKind: .init(platform: self.context.platform)- )- self.context.client.send(request) { result in- guard let certificates = result.get(withErrorHandler: completion) else { return }- guard let fullCert = certificates.first(where: { $0.attributes.serialNumber == serialNumber })- else { return completion(.failure(Error.csrFailed)) }- completion(.success(.init(privateKey: privateKey, certificate: fullCert.attributes.content)))- }- }+ let serialNumber = partialCert.serialNumber+ let listRequest = DeveloperServicesListCertificatesRequest(+ teamID: self.context.teamID, certificateKind: .init(platform: self.context.platform)+ )+ let certificates = try await context.client.send(listRequest)+ guard let fullCert = certificates.first(where: { $0.attributes.serialNumber == serialNumber })+ else { throw Error.csrFailed }+ return .init(privateKey: privateKey, certificate: fullCert.attributes.content) } - private func createAndSaveCertificate(- completion: @escaping (Result<SigningInfo, Swift.Error>) -> Void- ) {- createCertificate { result in- guard let value = result.get(withErrorHandler: completion) else { return }- self.context.signingInfoManager[self.context.teamID] = value- completion(.success(value))+ private func revokeCreateSaveCertificate(certificates: [DeveloperServicesCertificate]) async throws -> SigningInfo {+ if !certificates.isEmpty {+ guard await confirmRevocation(certificates) + else { throw CancellationError() } }- }-- private func revokeCreateSaveCertificate(- certificates: [DeveloperServicesCertificate],- completion: @escaping (Result<SigningInfo, Swift.Error>) -> Void- ) {- confirmRevocation(certificates) { confirmation in- guard confirmation else { return completion(.failure(Error.userCancelled)) }- let grouper = RequestGrouper<EmptyResponse, Swift.Error>()- certificates.forEach {- let request = DeveloperServicesRevokeCertificateRequest(teamID: context.teamID, certificateID: $0.id)- grouper.add { context.client.send(request, completion: $0) }- }- grouper.onComplete { result in- guard result.get(withErrorHandler: completion) != nil else { return }- self.createAndSaveCertificate(completion: completion)+ try await withThrowingTaskGroup(of: Void.self) { group in+ for certificate in certificates {+ group.addTask {+ let request = DeveloperServicesRevokeCertificateRequest(+ teamID: context.teamID, certificateID: certificate.id+ )+ _ = try await context.client.send(request)+ } }+ try await group.waitForAll() }+ let signingInfo = try await createCertificate()+ self.context.signingInfoManager[self.context.teamID] = signingInfo+ return signingInfo } - public func perform(completion: @escaping (Result<SigningInfo, Swift.Error>) -> Void) {+ private func perform() async throws -> SigningInfo { let request = DeveloperServicesListCertificatesRequest( teamID: context.teamID, certificateKind: .init(platform: context.platform) )- context.client.send(request) { result in- guard let certificates = result.get(withErrorHandler: completion) else { return }+ let certificates = try await context.client.send(request) - guard let certificate = certificates.first(where: {- $0.attributes.machineID == self.context.client.deviceInfo.deviceID- }) else {- // we need to revoke existing certs, otherwise it doesn't always let us make a new one- return self.revokeCreateSaveCertificate(certificates: certificates, completion: completion)- }+ guard let certificate = certificates.first(where: {+ $0.attributes.machineID == self.context.client.deviceInfo.deviceID+ }) else {+ // we need to revoke existing certs, otherwise it doesn't always let us make a new one+ return try await self.revokeCreateSaveCertificate(certificates: certificates)+ } - guard let signingInfo = self.context.signingInfoManager[self.context.teamID],- certificate.attributes.serialNumber.rawValue == signingInfo.certificate.serialNumber(),- certificate.attributes.expiry > Date()- else { return self.revokeCreateSaveCertificate(- certificates: [certificate], completion: completion- ) }+ guard let signingInfo = self.context.signingInfoManager[self.context.teamID],+ certificate.attributes.serialNumber.rawValue == signingInfo.certificate.serialNumber(),+ certificate.attributes.expiry > Date() else {+ // we have a certificate for this machine but it's not usable+ return try await self.revokeCreateSaveCertificate(certificates: [certificate])+ }++ return signingInfo+ } - completion(.success(signingInfo))+ public func perform(completion: @escaping (Result<SigningInfo, Swift.Error>) -> Void) {+ Task {+ completion(await Result { try await perform() }) } }
Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift
diff --git a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swiftindex dcde4ce..075bd8a 100644--- a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift+++ b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesProvisioningOperation.swift@@ -23,12 +23,12 @@ public struct DeveloperServicesProvisioningOperation: DeveloperServicesOperation public let context: SigningContext public let app: URL- public let confirmRevocation: ([DeveloperServicesCertificate], @escaping (Bool) -> Void) -> Void+ public let confirmRevocation: ([DeveloperServicesCertificate]) async -> Bool public let progress: (Double) -> Void public init( context: SigningContext, app: URL,- confirmRevocation: @escaping ([DeveloperServicesCertificate], @escaping (Bool) -> Void) -> Void,+ confirmRevocation: @escaping ([DeveloperServicesCertificate]) async -> Bool, progress: @escaping (Double) -> Void ) { self.context = context
Supersign/Sources/Supersign/Integration/SuperchargeInstaller.swift
diff --git a/Supersign/Sources/Supersign/Integration/SuperchargeInstaller.swift b/Supersign/Sources/Supersign/Integration/SuperchargeInstaller.swiftindex 34b767c..642c861 100644--- a/Supersign/Sources/Supersign/Integration/SuperchargeInstaller.swift+++ b/Supersign/Sources/Supersign/Integration/SuperchargeInstaller.swift@@ -24,7 +24,7 @@ public protocol IntegratedInstallerDelegate: AnyObject { func installerDidComplete(withResult result: Result<String, Error>) // defaults to always returning true- func confirmRevocation(of certificates: [DeveloperServicesCertificate], completion: @escaping (Bool) -> Void)+ func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool /// Decompress the zipped ipa file ///@@ -62,8 +62,8 @@ public protocol IntegratedInstallerDelegate: AnyObject { } extension IntegratedInstallerDelegate {- public func confirmRevocation(of certificates: [DeveloperServicesCertificate], completion: @escaping (Bool) -> Void) {- completion(true)+ public func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool {+ true } } @@ -374,11 +374,9 @@ public final class IntegratedInstaller { } catch { return completion(.failure(error)) }- let signer = Signer(context: context) { certs, completion in- guard let delegate = self.delegate else {- return completion(false)- }- delegate.confirmRevocation(of: certs, completion: completion)+ let signer = Signer(context: context) { certs in+ guard let delegate = self.delegate else { return false }+ return await delegate.confirmRevocation(of: certs) } signer.sign( app: appDir,
Supersign/Sources/Supersign/Signer/Signer.swift
diff --git a/Supersign/Sources/Supersign/Signer/Signer.swift b/Supersign/Sources/Supersign/Signer/Signer.swiftindex 6144be6..1eb5bec 100644--- a/Supersign/Sources/Supersign/Signer/Signer.swift+++ b/Supersign/Sources/Supersign/Signer/Signer.swift@@ -40,10 +40,10 @@ public struct Signer { } public let context: SigningContext- public let confirmRevocation: ([DeveloperServicesCertificate], @escaping (Bool) -> Void) -> Void+ public let confirmRevocation: ([DeveloperServicesCertificate]) async -> Bool public init( context: SigningContext,- confirmRevocation: @escaping ([DeveloperServicesCertificate], @escaping (Bool) -> Void) -> Void+ confirmRevocation: @escaping ([DeveloperServicesCertificate]) async -> Bool ) { self.context = context self.confirmRevocation = confirmRevocation
SupersignCLISupport/Sources/SupersignCLISupport/SupersignCLIDelegate.swift
diff --git a/SupersignCLISupport/Sources/SupersignCLISupport/SupersignCLIDelegate.swift b/SupersignCLISupport/Sources/SupersignCLISupport/SupersignCLIDelegate.swiftindex 66a09a2..b5ca601 100644--- a/SupersignCLISupport/Sources/SupersignCLISupport/SupersignCLIDelegate.swift+++ b/SupersignCLISupport/Sources/SupersignCLISupport/SupersignCLIDelegate.swift@@ -127,14 +127,14 @@ final class SupersignCLIDelegate: IntegratedInstallerDelegate, TwoFactorAuthDele return formatter }() - func confirmRevocation(of certificates: [DeveloperServicesCertificate], completion: @escaping (Bool) -> Void) {+ func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool { print("\nThe following certificates must be revoked:") print( certificates.map { "- \($0.attributes.name) (expires \(Self.expiryFormatter.string(from: $0.attributes.expiry)))" }.joined(separator: "\n") )- completion(Console.confirm("Continue?"))+ return await Task.detached { Console.confirm("Continue?") }.value } // TODO: Use `powershell Compress-Archive` and `powershell Expand-Archive` on Windows