Modern Apps git

← xtool.git

More concurrency

Commit
873c9ef9d802a181fdab561efeeaf28c4a78c6f8
Author
Kabir Oberai <[email protected]>
When
2024-11-11 03:42 UTC (2 years ago)
Parents
191530b

Diff

Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
diff --git a/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift b/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swiftindex 07f4e6f..1737089 100644--- a/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift	+++ b/Supersign/Sources/Supersign/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift	@@ -164,7 +164,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {     }      /// Registers the app + its extensions, returning the profile and entitlements of each-    public func perform() async throws -> [URL : ProvisioningInfo] {+    public func perform() async throws -> [URL: ProvisioningInfo] {         var apps: [URL] = [root]         let plugins = root.appendingPathComponent("PlugIns")         if plugins.dirExists {
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 ad088f9..57ab49f 100644--- a/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift+++ b/Supersign/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift@@ -64,7 +64,7 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera      private func revokeCreateSaveCertificate(certificates: [DeveloperServicesCertificate]) async throws -> SigningInfo {         if !certificates.isEmpty {-            guard await confirmRevocation(certificates) +            guard await confirmRevocation(certificates)                 else { throw CancellationError() }         }         try await withThrowingTaskGroup(of: Void.self) { group in
Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesLoginManager.swift
diff --git a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesLoginManager.swift b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesLoginManager.swiftindex 68570fd..7ce2837 100644--- a/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesLoginManager.swift+++ b/Supersign/Sources/Supersign/DeveloperServices/DeveloperServicesLoginManager.swift@@ -45,35 +45,47 @@ public struct DeveloperServicesLoginManager {     }      private func logIn(-        withLoginData loginData: GrandSlamLoginData,-        completion: @escaping (Result<DeveloperServicesLoginToken, Swift.Error>) -> Void-    ) {-        GrandSlamFetchAppTokensOperation(+        withLoginData loginData: GrandSlamLoginData+    ) async throws -> DeveloperServicesLoginToken {+        let tokens = try await GrandSlamFetchAppTokensOperation(             client: client,             apps: [.xcode],             loginData: loginData-        ).perform { result in-            guard let tokens = result.get(withErrorHandler: completion) else { return }-            guard let token = tokens[.xcode]-                else { return completion(.failure(Error.missingLoginToken)) }-            completion(.success(.init(adsid: loginData.adsid, token: token.value, expiry: token.expiry)))-        }+        ).perform()+        guard let token = tokens[.xcode]+            else { throw Error.missingLoginToken }+        return .init(adsid: loginData.adsid, token: token.value, expiry: token.expiry)     }      public func logIn(         withUsername username: String,         password: String,-        twoFactorDelegate: TwoFactorAuthDelegate,-        completion: @escaping (Result<DeveloperServicesLoginToken, Swift.Error>) -> Void-    ) {-        GrandSlamAuthenticateOperation(+        twoFactorDelegate: TwoFactorAuthDelegate+    ) async throws -> DeveloperServicesLoginToken {+        let loginData = try await GrandSlamAuthenticateOperation(             client: client,             username: username,             password: password,             twoFactorDelegate: twoFactorDelegate-        ).authenticate { result in-            guard let loginData = result.get(withErrorHandler: completion) else { return }-            self.logIn(withLoginData: loginData, completion: completion)+        ).authenticate()+        return try await self.logIn(withLoginData: loginData)+    }++    @available(*, deprecated, message: "Use async overload")+    public func logIn(+        withUsername username: String,+        password: String,+        twoFactorDelegate: TwoFactorAuthDelegate,+        completion: @escaping (Result<DeveloperServicesLoginToken, Swift.Error>) -> Void+    ) {+        Task {+            completion(await Result {+                try await logIn(+                    withUsername: username,+                    password: password,+                    twoFactorDelegate: twoFactorDelegate+                )+            })         }     } 
Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swiftindex 37849b6..fa6e615 100644--- a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift@@ -64,34 +64,30 @@ class GrandSlamAuthenticateOperation {         self.twoFactorDelegate = twoFactorDelegate     } -    private func restartAuth(completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void) {+    private func restartAuth() async throws -> GrandSlamLoginData {         srpClient = .init()         isSecondAttempt = true-        authenticate(completion: completion)+        return try await authenticate()     }      private func authenticateTwoFactor(         mode: GrandSlamAuthMode,-        loginData: GrandSlamLoginData,-        completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void-    ) {-        guard !isSecondAttempt else { return completion(.failure(Error.failedSecondaryAuth)) }+        loginData: GrandSlamLoginData+    ) async throws -> GrandSlamLoginData {+        guard !isSecondAttempt else { throw Error.failedSecondaryAuth }         let operation = GrandSlamTwoFactorAuthenticateOperation(             client: client,             mode: mode,             loginData: loginData,             delegate: twoFactorDelegate         )-        operation.perform { result in-            guard result.get(withErrorHandler: completion) != nil else { return }-            self.restartAuth(completion: completion)-        }+        try await operation.perform()+        return try await self.restartAuth()     }      private func authenticateStage3(-        completeResponse: GrandSlamAuthCompleteRequest.Decoder.Value,-        completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void-    ) {+        completeResponse: GrandSlamAuthCompleteRequest.Decoder.Value+    ) async throws -> GrandSlamLoginData {         srpClient.add(string: "|")         srpClient.add(data: completeResponse.encryptedResponse)         srpClient.add(string: "|")@@ -99,44 +95,32 @@ class GrandSlamAuthenticateOperation {         srpClient.add(string: "|")          guard srpClient.verify(hamk: completeResponse.hamk) && srpClient.verify(negProto: completeResponse.negProto)-            else { return completion(.failure(Error.internalError)) }+            else { throw Error.internalError }          guard let rawLoginResponse = srpClient.decrypt(cbc: completeResponse.encryptedResponse) else {-            return completion(.failure(Error.internalError))+            throw Error.internalError         } -        let response: Response-        do {-            response = try decoder.decode(Response.self, from: rawLoginResponse)-        } catch {-            return completion(.failure(error))-        }+        let response = try decoder.decode(Response.self, from: rawLoginResponse) -        let loginData: GrandSlamLoginData-        do {-            loginData = try decoder.decode(GrandSlamLoginData.self, from: rawLoginResponse)-        } catch {-            return completion(.failure(error))-        }+        let loginData = try decoder.decode(GrandSlamLoginData.self, from: rawLoginResponse)          if response.statusCode == 409,             let secondaryURL = response.url,             let authMode = GrandSlamAuthMode(rawValue: secondaryURL) {             // 2FA-            return authenticateTwoFactor(+            return try await authenticateTwoFactor(                 mode: authMode,-                loginData: loginData,-                completion: completion+                loginData: loginData             )         } -        completion(.success(loginData))+        return loginData     }      private func authenticateStage2(-        initResponse: GrandSlamAuthInitRequest.Decoder.Value,-        completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void-    ) {+        initResponse: GrandSlamAuthInitRequest.Decoder.Value+    ) async throws -> GrandSlamLoginData {         let isS2K = initResponse.selectedProtocol == .s2k         srpClient.add(string: "|")         srpClient.add(string: initResponse.selectedProtocol.rawValue)@@ -149,30 +133,24 @@ class GrandSlamAuthenticateOperation {             serverPublicKey: initResponse.serverPublicKey,             isS2K: isS2K         )-        guard let m1Data = mDataRaw else { return completion(.failure(Error.internalError)) }+        guard let m1Data = mDataRaw else { throw Error.internalError }          let completeRequest = GrandSlamAuthCompleteRequest(             username: username, cookie: initResponse.cookie, m1Data: m1Data         )-        client.send(completeRequest) { result in-            guard let response = result.get(withErrorHandler: completion) else { return }-            self.authenticateStage3(completeResponse: response, completion: completion)-        }+        let response = try await client.send(completeRequest)+        return try await self.authenticateStage3(completeResponse: response)     } -    func authenticate(-        completion: @escaping (Result<GrandSlamLoginData, Swift.Error>) -> Void-    ) {+    func authenticate() async throws -> GrandSlamLoginData {         srpClient.add(string: GrandSlamAuthInitRequest.protocols.joined(separator: ","))         srpClient.add(string: "|")          let publicKey = srpClient.publicKey()          let initRequest = GrandSlamAuthInitRequest(username: username, publicKey: publicKey)-        client.send(initRequest) { result in-            guard let response = result.get(withErrorHandler: completion) else { return }-            self.authenticateStage2(initResponse: response, completion: completion)-        }+        let response = try await client.send(initRequest)+        return try await self.authenticateStage2(initResponse: response)     }  }
Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swiftindex 0affea5..763da71 100644--- a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift@@ -58,25 +58,17 @@ class GrandSlamFetchAppTokensOperation {     }      private func handle(-        response: GrandSlamAppTokensRequest.Decoder.Value,-        completion: @escaping (Result<[AppTokenKey: Token], Swift.Error>) -> Void-    ) {-        let decrypted: Data-        do {-            decrypted = try AppTokens.decrypt(gcm: response.encryptedToken, withSK: loginData.sk)-        } catch {-            return completion(.failure(error))-        }-        completion(Result {-            let pairs = try decoder-                .decode(Response.self, from: decrypted)-                .tokens-                .map { (AppTokenKey($0), $1) }-            return Dictionary(uniqueKeysWithValues: pairs)-        })+        response: GrandSlamAppTokensRequest.Decoder.Value+    ) async throws -> [AppTokenKey: Token] {+        let decrypted = try AppTokens.decrypt(gcm: response.encryptedToken, withSK: loginData.sk)+        let pairs = try decoder+            .decode(Response.self, from: decrypted)+            .tokens+            .map { (AppTokenKey($0), $1) }+        return Dictionary(uniqueKeysWithValues: pairs)     } -    func perform(completion: @escaping (Result<[AppTokenKey: Token], Swift.Error>) -> Void) {+    func perform() async throws -> [AppTokenKey: Token] {         let checksum = AppTokens.checksum(withSK: loginData.sk, adsid: loginData.adsid, apps: apps.map { $0.rawValue })         let appTokensRequest = GrandSlamAppTokensRequest(             username: loginData.adsid,@@ -85,10 +77,8 @@ class GrandSlamFetchAppTokensOperation {             idmsToken: loginData.idmsToken,             checksum: checksum         )-        client.send(appTokensRequest) { result in-            guard let response = result.get(withErrorHandler: completion) else { return }-            self.handle(response: response, completion: completion)-        }+        let response = try await client.send(appTokensRequest)+        return try await self.handle(response: response)     }  }
Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swiftindex e0735a0..12ac243 100644--- a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift@@ -35,46 +35,46 @@ class GrandSlamTwoFactorAuthenticateOperation {         self.delegate = delegate     } -    private func performSecondaryAuth(completion: @escaping (Result<(), Swift.Error>) -> Void) {-        let request = GrandSlamSecondaryAuthRequest(loginData: loginData)-        client.send(request) { result in-            guard result.get(withErrorHandler: completion) != nil else { return }-            self.delegate.fetchCode { self.validate(code: $0, completion: completion) }+    private func performSecondaryAuth() async throws {+        try await client.send(GrandSlamSecondaryAuthRequest(loginData: loginData))+        let code = await withCheckedContinuation { continuation in+            self.delegate.fetchCode {+                continuation.resume(returning: $0)+            }         }+        try await self.validate(code: code)     } -    private func validate(code: String?, completion: @escaping (Result<(), Swift.Error>) -> Void) {-        guard let code = code else { return completion(.failure(Error.userCancelled)) }+    private func validate(code: String?) async throws {+        guard let code = code else { throw Error.userCancelled }         let request = GrandSlamValidateRequest(loginData: loginData, verificationCode: code)-        client.send(request) { result in-            switch result {-            case .success:-                completion(.success(()))-            case .failure(let error as GrandSlamOperationError) where error.code == -21669:-                completion(.failure(Error.incorrectVerificationCode))-            case .failure(let error):-                completion(.failure(error))-            }+        do {+            try await client.send(request)+        } catch let error as GrandSlamOperationError where error.code == -21669 {+            throw Error.incorrectVerificationCode         }     } -    private func performTrustedDeviceAuth(completion: @escaping (Result<(), Swift.Error>) -> Void) {+    private func performTrustedDeviceAuth() async throws {         let request = GrandSlamTrustedDeviceRequest(loginData: loginData)-        client.send(request) { result in-            guard result.get(withErrorHandler: completion) != nil else { return }-            self.delegate.fetchCode { self.validate(code: $0, completion: completion) }+        try await client.send(request)+        let code = await withCheckedContinuation { continuation in+            self.delegate.fetchCode {+                continuation.resume(returning: $0)+            }         }+        try await self.validate(code: code)     } -    func perform(completion: @escaping (Result<(), Swift.Error>) -> Void) {+    func perform() async throws {         switch mode {         case .secondaryAuth:             // TODO: We *should* be calling performSecondaryAuth – it does             // seem like performTrustedDeviceAuth sometimes works here but             // other times re-authenticating continues to 409-            self.performTrustedDeviceAuth(completion: completion)+            try await self.performTrustedDeviceAuth()         case .trustedDeviceSecondaryAuth:-            self.performTrustedDeviceAuth(completion: completion)+            try await self.performTrustedDeviceAuth()         }     } 
SupersignCLISupport/Sources/SupersignCLISupport/DSCommand.swift
diff --git a/SupersignCLISupport/Sources/SupersignCLISupport/DSCommand.swift b/SupersignCLISupport/Sources/SupersignCLISupport/DSCommand.swiftindex 0311216..91ee68d 100644--- a/SupersignCLISupport/Sources/SupersignCLISupport/DSCommand.swift+++ b/SupersignCLISupport/Sources/SupersignCLISupport/DSCommand.swift@@ -38,14 +38,11 @@ private extension AuthToken {             deviceInfo: deviceInfo,             anisetteProvider: provider         )-        let token: DeveloperServicesLoginToken = try withSyncContinuation { cont in-            manager.logIn(-                withUsername: username,-                password: password,-                twoFactorDelegate: authDelegate,-                completion: cont-            )-        }+        let token = try await manager.logIn(+            withUsername: username,+            password: password,+            twoFactorDelegate: authDelegate+        )         _ = authDelegate         return AuthToken(appleID: username, dsToken: token)     }