xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / DeveloperServices / Certificates / DeveloperServicesFetchCertificateOperation.swift
DeveloperServicesFetchCertificateOperation.swift121 lines · 4.4 KiBRaw
| 1 | // |
| 2 | // DeveloperServicesFetchCertificateOperation.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 13/10/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import DeveloperAPI |
| 11 | import Dependencies |
| 12 | |
| 13 | public typealias DeveloperServicesCertificate = Components.Schemas.Certificate |
| 14 | |
| 15 | public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOperation { |
| 16 | |
| 17 | public enum Error: LocalizedError { |
| 18 | case csrFailed |
| 19 | case userCancelled |
| 20 | |
| 21 | public var errorDescription: String? { |
| 22 | switch self { |
| 23 | case .csrFailed: |
| 24 | return NSLocalizedString( |
| 25 | "fetch_certificate_operation.error.csr_failed", value: "CSR request failed", comment: "" |
| 26 | ) |
| 27 | case .userCancelled: |
| 28 | return NSLocalizedString( |
| 29 | "fetch_certificate_operation.error.user_cancelled", value: "The operation was cancelled", comment: "" |
| 30 | ) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | @Dependency(\.signingInfoManager) var signingInfoManager |
| 36 | |
| 37 | public let context: SigningContext |
| 38 | public let confirmRevocation: @Sendable ([DeveloperServicesCertificate]) async -> Bool |
| 39 | public init( |
| 40 | context: SigningContext, |
| 41 | confirmRevocation: @escaping @Sendable ([DeveloperServicesCertificate]) async -> Bool |
| 42 | ) { |
| 43 | self.context = context |
| 44 | self.confirmRevocation = confirmRevocation |
| 45 | } |
| 46 | |
| 47 | private func createCertificate() async throws -> SigningInfo { |
| 48 | let keypair = try Keypair() |
| 49 | let csr = try keypair.generateCSR() |
| 50 | let privateKey = try keypair.privateKey() |
| 51 | |
| 52 | let response = try await context.developerAPIClient.certificatesCreateInstance( |
| 53 | body: .json(.init(data: .init( |
| 54 | _type: .certificates, |
| 55 | attributes: .init( |
| 56 | csrContent: csr.pemString, |
| 57 | certificateType: .init(.development) |
| 58 | ) |
| 59 | ))) |
| 60 | ) |
| 61 | |
| 62 | guard let contentString = try response.created.body.json.data.attributes?.certificateContent, |
| 63 | let contentData = Data(base64Encoded: contentString) |
| 64 | else { throw Error.csrFailed } |
| 65 | |
| 66 | let certificate = try Certificate(data: contentData) |
| 67 | |
| 68 | return SigningInfo(privateKey: privateKey, certificate: certificate) |
| 69 | } |
| 70 | |
| 71 | private func replaceCertificates( |
| 72 | _ certificates: [DeveloperServicesCertificate], |
| 73 | requireConfirmation: Bool |
| 74 | ) async throws -> SigningInfo { |
| 75 | if try await context.auth.team()?.isFree == true { |
| 76 | if !certificates.isEmpty, requireConfirmation { |
| 77 | guard await confirmRevocation(certificates) |
| 78 | else { throw CancellationError() } |
| 79 | } |
| 80 | try await withThrowingTaskGroup(of: Void.self) { group in |
| 81 | for certificate in certificates { |
| 82 | group.addTask { |
| 83 | _ = try await context.developerAPIClient |
| 84 | .certificatesDeleteInstance(path: .init(id: certificate.id)) |
| 85 | .noContent |
| 86 | } |
| 87 | } |
| 88 | try await group.waitForAll() |
| 89 | } |
| 90 | } |
| 91 | let signingInfo = try await createCertificate() |
| 92 | signingInfoManager[self.context.auth.identityID] = signingInfo |
| 93 | return signingInfo |
| 94 | } |
| 95 | |
| 96 | public func perform() async throws -> SigningInfo { |
| 97 | let certificates = try await context.developerAPIClient.certificatesGetCollection().ok.body.json.data |
| 98 | |
| 99 | guard let signingInfo = signingInfoManager[self.context.auth.identityID] else { |
| 100 | return try await self.replaceCertificates(certificates, requireConfirmation: true) |
| 101 | } |
| 102 | |
| 103 | guard let certificate = certificates.first(where: { |
| 104 | $0.attributes?.serialNumber.map(signingInfo.certificate.hasSerialNumber) == true |
| 105 | }) else { |
| 106 | // we need to revoke existing certs, otherwise it doesn't always let us make a new one |
| 107 | return try await self.replaceCertificates(certificates, requireConfirmation: true) |
| 108 | } |
| 109 | |
| 110 | if let date = certificate.attributes?.expirationDate, date > Date() { |
| 111 | return signingInfo |
| 112 | } else { |
| 113 | // we have a certificate for this machine but it's not usable |
| 114 | return try await self.replaceCertificates( |
| 115 | [certificate], |
| 116 | requireConfirmation: false |
| 117 | ) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } |