Modern Apps git

← xtool.git

Port Devices, Certificates to ASC API

Commit
14511f470897d94094b94f6f96df732bb4427b0b
Author
Kabir Oberai <[email protected]>
When
2024-12-21 20:04 UTC (1 year ago)
Parents
b6a40ca

Diff

Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCSRResponse.swift
diff --git a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCSRResponse.swift b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCSRResponse.swiftdeleted file mode 100644index 03310eb..0000000--- a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCSRResponse.swift+++ /dev/null@@ -1,35 +0,0 @@-//-//  DeveloperServicesCSRResponse.swift-//  Supersign-//-//  Created by Kabir Oberai on 09/10/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public struct DeveloperServicesCSRResponse: Decodable, Sendable {-    public struct ID: RawRepresentable, Decodable, Sendable {-        public let rawValue: String-        public init(rawValue: String) { self.rawValue = rawValue }-    }-    public let id: ID-    public let certificateID: DeveloperServicesCertificate.ID-    public let serialNumber: DeveloperServicesCertificate.SerialNumber-    public let status: String-    public let statusCode: Int-    public let platform: DeveloperServicesPlatform-    public let machineID: String?-    public let machineName: String?--    private enum CodingKeys: String, CodingKey {-        case id = "certRequestId"-        case certificateID = "certificateId"-        case serialNumber = "serialNum"-        case status = "statusString"-        case statusCode-        case platform = "csrPlatform"-        case machineID = "machineId"-        case machineName-    }-}
Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCertificate.swift
diff --git a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCertificate.swift b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCertificate.swiftdeleted file mode 100644index 189cdee..0000000--- a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesCertificate.swift+++ /dev/null@@ -1,67 +0,0 @@-//-//  DeveloperServicesCertificate.swift-//  Supercharge-//-//  Created by Kabir Oberai on 29/07/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public struct DeveloperServicesCertificate: Decodable, Sendable {-    public struct ID: RawRepresentable, Decodable, Sendable {-        public let rawValue: String-        public init(rawValue: String) { self.rawValue = rawValue }-    }-    public let id: ID--    public struct SerialNumber: RawRepresentable, Decodable, Sendable {-        public let rawValue: String-        public init(rawValue: String) { self.rawValue = rawValue }-    }--    public enum Kind: String, Decodable, CaseIterable, Sendable {-        case development = "DEVELOPMENT"-        case distribution = "DISTRIBUTION"-        case iOSDevelopment = "IOS_DEVELOPMENT"-        case iOSDistribution = "IOS_DISTRIBUTION"-        case macAppDistribution = "MAC_APP_DISTRIBUTION"-        case macInstallerDistribution = "MAC_INSTALLER_DISTRIBUTION"-        case developerIDKext = "DEVELOPER_ID_KEXT"-        case developerIDApplication = "DEVELOPER_ID_APPLICATION"-        case unknown--        public init(platform: DeveloperServicesPlatform) {-            switch platform {-            case .iOS: self = .iOSDevelopment-            default: self = .development-            }-        }--        public init(from decoder: Decoder) throws {-            let rawValue = try String(from: decoder)-            self = Self.allCases.first { $0.rawValue == rawValue } ?? .unknown-        }-    }--    public struct Attributes: Decodable, Sendable {-        public let serialNumber: SerialNumber-        public let name: String-        public let kind: Kind-        public let expiry: Date-        public let machineID: String?-        public let machineName: String?-        public let content: Certificate--        private enum CodingKeys: String, CodingKey {-            case serialNumber-            case name-            case kind = "certificateType"-            case expiry = "expirationDate"-            case content = "certificateContent"-            case machineID = "machineId"-            case machineName-        }-    }-    public let attributes: Attributes-}
Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift
diff --git a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swiftindex cf5287b..c3f4cc9 100644--- a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift+++ b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift@@ -7,6 +7,9 @@ //  import Foundation+import DeveloperAPI++public typealias DeveloperServicesCertificate = Components.Schemas.Certificate  public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOperation { @@ -43,37 +46,39 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera         let csr = try keypair.generateCSR()         let privateKey = try keypair.privateKey() -        let request = DeveloperServicesSubmitCSRRequest(-            platform: context.platform,-            teamID: context.teamID,-            csr: csr,-            machineName: "Supercharge: \(SigningContext.hostName)",-            machineID: context.client.deviceInfo.deviceID+        let response = try await context.developerAPIClient.certificatesCreateInstance(+            body: .json(.init(data: .init(+                _type: .certificates,+                attributes: .init(+                    csrContent: csr.pemString,+                    certificateType: .development+                )+            )))         )-        let partialCert = try await context.client.send(request) -        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)+        guard let contentString = try response.created.body.json.data.attributes?.certificateContent,+              let contentData = Data(base64Encoded: contentString)+              else { throw Error.csrFailed }++        let certificate = try Certificate(data: contentData)++        return SigningInfo(privateKey: privateKey, certificate: certificate)     } -    private func revokeCreateSaveCertificate(certificates: [DeveloperServicesCertificate]) async throws -> SigningInfo {-        if !certificates.isEmpty {+    private func replaceCertificates(+        _ certificates: [DeveloperServicesCertificate],+        requireConfirmation: Bool+    ) async throws -> SigningInfo {+        if !certificates.isEmpty, requireConfirmation {             guard await confirmRevocation(certificates)                 else { throw CancellationError() }         }         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 context.developerAPIClient+                        .certificatesDeleteInstance(path: .init(id: certificate.id))+                        .noContent                 }             }             try await group.waitForAll()@@ -84,26 +89,27 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera     }      public func perform() async throws -> SigningInfo {-        let request = DeveloperServicesListCertificatesRequest(-            teamID: context.teamID, certificateKind: .init(platform: context.platform)-        )-        let certificates = try await context.client.send(request)+        let certificates = try await context.developerAPIClient.certificatesGetCollection().ok.body.json.data++        guard let signingInfo = self.context.signingInfoManager[self.context.teamID] else {+            return try await self.replaceCertificates(certificates, requireConfirmation: true)+        } -        guard let certificate = certificates.first(where: {-            $0.attributes.machineID == self.context.client.deviceInfo.deviceID-        }) else {+        let knownSerialNumber = signingInfo.certificate.serialNumber()+        guard let certificate = certificates.first(where: { $0.attributes?.serialNumber == knownSerialNumber }) 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)+            return try await self.replaceCertificates(certificates, requireConfirmation: true)         } -        guard let signingInfo = self.context.signingInfoManager[self.context.teamID],-              certificate.attributes.serialNumber.rawValue == signingInfo.certificate.serialNumber(),-              certificate.attributes.expiry > Date() else {+        if let date = certificate.attributes?.expirationDate, date > Date() {+            return signingInfo+        } else {             // we have a certificate for this machine but it's not usable-            return try await self.revokeCreateSaveCertificate(certificates: [certificate])+            return try await self.replaceCertificates(+                [certificate],+                requireConfirmation: false+            )         }--        return signingInfo     }  }
Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesListCertificatesRequest.swift
diff --git a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesListCertificatesRequest.swift b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesListCertificatesRequest.swiftdeleted file mode 100644index f927908..0000000--- a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesListCertificatesRequest.swift+++ /dev/null@@ -1,35 +0,0 @@-//-//  DeveloperServicesListCertificatesRequest.swift-//  Supercharge-//-//  Created by Kabir Oberai on 24/07/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public struct DeveloperServicesListCertificatesRequest: DeveloperServicesRequest, Sendable {--    public typealias Response = [DeveloperServicesCertificate]-    public typealias Value = Response--    public var apiVersion: DeveloperServicesAPIVersion { DeveloperServicesAPIVersionV1() }-    public var methodOverride: String? { "GET" }--    public let teamID: DeveloperServicesTeam.ID-    public let certificateKind: DeveloperServicesCertificate.Kind--    public var action: String { "certificates" }-    public var parameters: [String: Any] {-        [-            "teamId": teamID.rawValue,-            "filter[certificateType]": certificateKind.rawValue-        ]-    }--    public init(teamID: DeveloperServicesTeam.ID, certificateKind: DeveloperServicesCertificate.Kind) {-        self.teamID = teamID-        self.certificateKind = certificateKind-    }--}
Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesRevokeCertificateRequest.swift
diff --git a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesRevokeCertificateRequest.swift b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesRevokeCertificateRequest.swiftdeleted file mode 100644index 7b0543e..0000000--- a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesRevokeCertificateRequest.swift+++ /dev/null@@ -1,36 +0,0 @@-//-//  DeveloperServicesRevokeCertificateRequest.swift-//  Supercharge-//-//  Created by Kabir Oberai on 24/07/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--// swiftlint:disable:next type_name-public struct DeveloperServicesRevokeCertificateRequest: DeveloperServicesRequest {--    public typealias Response = EmptyResponse-    public typealias Value = Response--    public var apiVersion: DeveloperServicesAPIVersion { DeveloperServicesAPIVersionV1() }-    public var methodOverride: String? { "DELETE" }--    public let teamID: DeveloperServicesTeam.ID-    public let certificateID: DeveloperServicesCertificate.ID--    public var action: String { "certificates/\(certificateID.rawValue)" }-    public var parameters: [String: Any] {-        ["teamId": teamID.rawValue]-    }--    public init(-        teamID: DeveloperServicesTeam.ID,-        certificateID: DeveloperServicesCertificate.ID-    ) {-        self.teamID = teamID-        self.certificateID = certificateID-    }--}
Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift
diff --git a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift b/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swiftdeleted file mode 100644index 010104b..0000000--- a/Sources/Supersign/DeveloperServices/Certificates/DeveloperServicesSubmitCSRRequest.swift+++ /dev/null@@ -1,52 +0,0 @@-//-//  DeveloperServicesSubmitCSRRequest.swift-//  Supercharge-//-//  Created by Kabir Oberai on 24/07/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public final class DeveloperServicesSubmitCSRRequest: DeveloperServicesPlatformRequest {--    public struct Response: Decodable, Sendable {-        public let certRequest: DeveloperServicesCSRResponse-    }-    public typealias Value = DeveloperServicesCSRResponse--    public let platform: DeveloperServicesPlatform-    public let teamID: DeveloperServicesTeam.ID-    public let csr: CSR-    public let machineName: String-    public let machineID: String--    var subAction: String { return "submitDevelopmentCSR" }-    var subParameters: [String: Any] {-        return [-            "teamId": teamID.rawValue,-            "csrContent": csr.pemString,-            "machineId": machineID,-            "machineName": machineName-        ]-    }--    public func parse(_ response: Response) -> DeveloperServicesCSRResponse {-        response.certRequest-    }--    public init(-        platform: DeveloperServicesPlatform,-        teamID: DeveloperServicesTeam.ID,-        csr: CSR,-        machineName: String,-        machineID: String-    ) {-        self.platform = platform-        self.teamID = teamID-        self.csr = csr-        self.machineName = machineName-        self.machineID = machineID-    }--}
Sources/Supersign/DeveloperServices/Devices/DeveloperServicesAddDeviceRequest.swift
diff --git a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesAddDeviceRequest.swift b/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesAddDeviceRequest.swiftdeleted file mode 100644index a4ebbf6..0000000--- a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesAddDeviceRequest.swift+++ /dev/null@@ -1,48 +0,0 @@-//-//  DeveloperServicesAddDeviceRequest.swift-//  Supercharge-//-//  Created by Kabir Oberai on 24/07/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public struct DeveloperServicesAddDeviceRequest: DeveloperServicesPlatformRequest {--    public struct Response: Decodable, Sendable {-        let device: DeveloperServicesDevice-    }-    public typealias Value = DeveloperServicesDevice--    public let platform: DeveloperServicesPlatform-    public let teamID: DeveloperServicesTeam.ID-    public let udid: String-    public let name: String--    var subAction: String { return "addDevice" }-    var subParameters: [String: Any] {-        return [-            "teamId": teamID.rawValue,-            "deviceNumber": udid,-            "name": name-        ]-    }--    public func parse(_ response: Response) -> DeveloperServicesDevice {-        response.device-    }--    public init(-        platform: DeveloperServicesPlatform,-        teamID: DeveloperServicesTeam.ID,-        udid: String,-        name: String-    ) {-        self.platform = platform-        self.teamID = teamID-        self.udid = udid-        self.name = name-    }--}
Sources/Supersign/DeveloperServices/Devices/DeveloperServicesDevice.swift
diff --git a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesDevice.swift b/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesDevice.swiftdeleted file mode 100644index 5cfe7de..0000000--- a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesDevice.swift+++ /dev/null@@ -1,32 +0,0 @@-//-//  DeveloperServicesDevice.swift-//  Supercharge-//-//  Created by Kabir Oberai on 07/10/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public struct DeveloperServicesDevice: Decodable, Sendable {-    public struct ID: RawRepresentable, Decodable, Sendable {-        public let rawValue: String-        public init(rawValue: String) { self.rawValue = rawValue }-    }--    public let id: ID-    public let name: String-    public let udid: String-    public let platform: DeveloperServicesPlatform-    public let type: String // TODO: Create enum-    public let model: String?--    private enum CodingKeys: String, CodingKey {-        case id = "deviceId"-        case name-        case udid = "deviceNumber"-        case platform = "devicePlatform"-        case type = "deviceClass"-        case model-    }-}
Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift
diff --git a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift b/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swiftindex 5b21e99..ff488e0 100644--- a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift+++ b/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift@@ -7,6 +7,7 @@ //  import Foundation+import DeveloperAPI  public struct DeveloperServicesFetchDeviceOperation: DeveloperServicesOperation {     public let context: SigningContext@@ -14,20 +15,25 @@ public struct DeveloperServicesFetchDeviceOperation: DeveloperServicesOperation         self.context = context     } -    public func perform() async throws -> DeveloperServicesDevice {-        let listRequest = DeveloperServicesListDevicesRequest(platform: context.platform, teamID: context.teamID)-        let devices = try await context.client.send(listRequest)-        if let device = devices.first(where: { $0.udid == self.context.udid }) {+    public func perform() async throws -> Components.Schemas.Device {+        let devices = try await context.developerAPIClient.devicesGetCollection().ok.body.json.data++        if let device = devices.first(where: { $0.attributes?.udid == self.context.udid }) {             return device         } -        let addRequest = DeveloperServicesAddDeviceRequest(-            platform: self.context.platform,-            teamID: self.context.teamID,-            udid: self.context.udid,-            name: self.context.deviceName+        let response = try await context.developerAPIClient.devicesCreateInstance(+            body: .json(.init(data: .init(+                _type: .devices,+                attributes: .init(+                    name: self.context.deviceName,+                    platform: .ios,+                    udid: self.context.udid+                )+            )))         )-        return try await context.client.send(addRequest)++        return try response.created.body.json.data     }  }
Sources/Supersign/DeveloperServices/Devices/DeveloperServicesListDevicesRequest.swift
diff --git a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesListDevicesRequest.swift b/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesListDevicesRequest.swiftdeleted file mode 100644index 2801104..0000000--- a/Sources/Supersign/DeveloperServices/Devices/DeveloperServicesListDevicesRequest.swift+++ /dev/null@@ -1,39 +0,0 @@-//-//  DeveloperServicesListDevicesRequest.swift-//  Supercharge-//-//  Created by Kabir Oberai on 24/07/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation--public struct DeveloperServicesListDevicesRequest: DeveloperServicesPlatformRequest {--    public struct Response: Decodable, Sendable {-        public let devices: [DeveloperServicesDevice]-    }-    public typealias Value = [DeveloperServicesDevice]--    public let platform: DeveloperServicesPlatform-    public let teamID: DeveloperServicesTeam.ID--    var subAction: String { return "listDevices" }-    var subParameters: [String: Any] {-        return [-            "teamId": teamID.rawValue,-            "pageSize": "500",-            "includeRemovedDevices": "false"-        ]-    }--    public func parse(_ response: Response) -> Value {-        response.devices-    }--    public init(platform: DeveloperServicesPlatform, teamID: DeveloperServicesTeam.ID) {-        self.platform = platform-        self.teamID = teamID-    }--}
Sources/Supersign/Signer/SigningContext.swift
diff --git a/Sources/Supersign/Signer/SigningContext.swift b/Sources/Supersign/Signer/SigningContext.swiftindex 76cfde3..d793908 100644--- a/Sources/Supersign/Signer/SigningContext.swift+++ b/Sources/Supersign/Signer/SigningContext.swift@@ -7,6 +7,7 @@ //  import Foundation+import DeveloperAPI  public struct SigningContext: Sendable { @@ -18,6 +19,17 @@ public struct SigningContext: Sendable {     public let platform: DeveloperServicesPlatform     public let signerImpl: SignerImpl +    public var developerAPIClient: DeveloperAPIClient {+        DeveloperAPIClient(+            xcodeAPI: DeveloperAPIXcodeMiddleware(+                anisetteDataProvider: client.anisetteDataProvider,+                loginToken: client.loginToken,+                deviceInfo: client.deviceInfo,+                teamID: teamID.rawValue+            )+        )+    }+     public init(         udid: String,         deviceName: String,
Sources/SupersignCLISupport/SupersignCLIDelegate.swift
diff --git a/Sources/SupersignCLISupport/SupersignCLIDelegate.swift b/Sources/SupersignCLISupport/SupersignCLIDelegate.swiftindex f64abe9..b7a9214 100644--- a/Sources/SupersignCLISupport/SupersignCLIDelegate.swift+++ b/Sources/SupersignCLISupport/SupersignCLIDelegate.swift@@ -87,7 +87,7 @@ actor SupersignCLIDelegate: IntegratedInstallerDelegate {         print("\nThe following certificates must be revoked:")         print(             certificates.map {-                "- \($0.attributes.name) (expires \($0.attributes.expiry.formatted(date: .abbreviated, time: .shortened)))"+                "- \($0.attributes!.name!) (expires \($0.attributes!.expirationDate!.formatted(date: .abbreviated, time: .shortened)))"             }.joined(separator: "\n")         )         do {