Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XKit / DeveloperServices / Profiles / DeveloperServicesFetchProfileOperation.swift

DeveloperServicesFetchProfileOperation.swift121 lines · 4.2 KiBRaw
1//
2// DeveloperServicesFetchProfileOperation.swift
3// XKit
4//
5// Created by Kabir Oberai on 13/10/19.
6// Copyright © 2019 Kabir Oberai. All rights reserved.
7//
8
9import Foundation
10import Superutils
11import DeveloperAPI
12
13public struct DeveloperServicesFetchProfileOperation: DeveloperServicesOperation {
14
15 public enum Errors: Error {
16 case bundleIDNotFound
17 case tooManyMatchingBundleIDs
18 case invalidProfileData
19 }
20
21 public let context: SigningContext
22 public let bundleID: String
23 public let signingInfo: SigningInfo
24 public init(context: SigningContext, bundleID: String, signingInfo: SigningInfo) {
25 self.context = context
26 self.bundleID = bundleID
27 self.signingInfo = signingInfo
28 }
29
30 public func perform() async throws -> Mobileprovision {
31 let bundleIDs = try await context.developerAPIClient
32 .bundleIdsGetCollection(query: .init(
33 filter_lbrack_identifier_rbrack_: [bundleID],
34 fields_lbrack_profiles_rbrack_: [.bundleId],
35 include: [.profiles]
36 ))
37 .ok.body.json
38
39 // filter[identifier] is a prefix filter so we need to manually upgrade to equality
40 let filtered = bundleIDs.data.filter { $0.attributes?.identifier == self.bundleID }
41
42 let bundleID: Components.Schemas.BundleId
43 switch filtered.count {
44 case 0:
45 throw Errors.bundleIDNotFound
46 case 1:
47 bundleID = filtered[0]
48 default:
49 throw Errors.tooManyMatchingBundleIDs
50 }
51
52 let profiles = bundleID.relationships?.profiles?.data ?? []
53 switch profiles.count {
54 case 0:
55 // we're good
56 break
57 case 1:
58 _ = try await context.developerAPIClient.profilesDeleteInstance(path: .init(id: profiles[0].id)).noContent
59 default:
60 // if the user has >1 profile, it's probably okay to add another one (acceptable for non-free accounts?)
61 break
62 }
63
64 // Match locally rather than with filter[serialNumber]: the API lists serials with a
65 // leading zero nibble ("0E41…") but its filter only matches them without it ("E41…").
66 let certs = try await DeveloperAPIPages {
67 try await context.developerAPIClient.certificatesGetCollection().ok.body.json
68 } next: {
69 $0.links.next
70 }
71 .map(\.data)
72 .reduce(into: [], +=)
73 .filter { $0.attributes?.serialNumber.map(signingInfo.certificate.hasSerialNumber) == true }
74
75 let allDevices = try await context.developerAPIClient.devicesGetCollection()
76 .ok.body.json.data
77 .filter { $0.attributes?.deviceClass?.value1?.supportsIOSApps == true }
78
79 let response = try await context.developerAPIClient.profilesCreateInstance(
80 body: .json(
81 .init(
82 data: .init(
83 _type: .profiles,
84 attributes: .init(
85 name: "XTL profile \(bundleID.id)",
86 profileType: .init(.iosAppDevelopment)
87 ),
88 relationships: .init(
89 bundleId: .init(
90 data: .init(_type: .bundleIds, id: bundleID.id)
91 ),
92 devices: .init(data: allDevices.map {
93 .init(_type: .devices, id: $0.id)
94 }),
95 certificates: .init(data: certs.map {
96 .init(_type: .certificates, id: $0.id)
97 })
98 )
99 )
100 )
101 )
102 )
103 .created.body.json.data
104
105 guard let contentString = response.attributes?.profileContent,
106 let contentData = Data(base64Encoded: contentString)
107 else { throw Errors.invalidProfileData }
108
109 return try Mobileprovision(data: contentData)
110 }
111
112}
113
114extension Components.Schemas.Device.AttributesPayload.DeviceClassPayload.Value1Payload {
115 fileprivate var supportsIOSApps: Bool {
116 switch self {
117 case .ipad, .iphone, .ipod: true
118 default: false
119 }
120 }
121}