xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XToolSupport / AuthCommand.swift
AuthCommand.swift219 lines · 6.7 KiBRaw
| 1 | import XUtils |
| 2 | import XCLIUtils |
| 3 | import Foundation |
| 4 | import XKit |
| 5 | import ArgumentParser |
| 6 | import Crypto |
| 7 | import Dependencies |
| 8 | |
| 9 | enum AuthMode: String, CaseIterable, CustomStringConvertible, ExpressibleByArgument { |
| 10 | case key |
| 11 | case password |
| 12 | |
| 13 | var description: String { |
| 14 | switch self { |
| 15 | case .key: "API Key (requires paid Apple Developer Program membership)" |
| 16 | case .password: "Password (works with any Apple ID but uses private APIs)" |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | struct AuthOperation { |
| 22 | var username: String? |
| 23 | var password: String? |
| 24 | var logoutFromExisting: Bool |
| 25 | |
| 26 | var mode: AuthMode? |
| 27 | var quiet = false |
| 28 | |
| 29 | func run() async throws { |
| 30 | if let token = try? AuthToken.saved(), !logoutFromExisting { |
| 31 | if !quiet { |
| 32 | d.out.print("Logged in.\n\(token)") |
| 33 | } |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | let mode: AuthMode |
| 38 | if let existing = self.mode { |
| 39 | mode = existing |
| 40 | } else { |
| 41 | mode = try await Console.choose( |
| 42 | from: AuthMode.allCases, |
| 43 | onNoElement: { throw Console.Error("Mode selection is required") }, |
| 44 | multiPrompt: "Select login mode", |
| 45 | formatter: \.description |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | let token = switch mode { |
| 50 | case .password: |
| 51 | try await logInWithPassword() |
| 52 | case .key: |
| 53 | try await logInWithKey() |
| 54 | } |
| 55 | try token.save() |
| 56 | |
| 57 | d.out.print("Logged in.\n\(token)") |
| 58 | } |
| 59 | |
| 60 | private func logInWithKey() async throws -> AuthToken { |
| 61 | let id = try await Console.promptRequired("Key ID: ", existing: nil) |
| 62 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 63 | |
| 64 | let issuerID = try await Console.promptRequired("Issuer ID: ", existing: nil) |
| 65 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 66 | |
| 67 | let path = try await Console.promptRequired("Key path: ", existing: nil) |
| 68 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 69 | |
| 70 | let pem = try String(decoding: Data(contentsOf: URL(fileURLWithPath: path)), as: UTF8.self) |
| 71 | do { |
| 72 | _ = try P256.Signing.PrivateKey(pemRepresentation: pem) |
| 73 | } catch { |
| 74 | throw Console.Error("Key is invalid: \(error)") |
| 75 | } |
| 76 | |
| 77 | return AuthToken.appStoreConnect(.init(id: id, issuerID: issuerID, pem: pem)) |
| 78 | } |
| 79 | |
| 80 | private func logInWithPassword() async throws -> AuthToken { |
| 81 | d.out.print(""" |
| 82 | Heads up: |
| 83 | This works by emulating a virtual Mac to interact with Apple services. |
| 84 | We use a third-party service to create a randomized device ID token |
| 85 | (Anisette Data) but your credentials are only ever sent to Apple. |
| 86 | Still, consider using a secondary Apple ID since this is a private API. |
| 87 | """) |
| 88 | |
| 89 | let username = try await Console.promptRequired("Apple ID: ", existing: username) |
| 90 | |
| 91 | let password: String |
| 92 | if let existing = self.password { |
| 93 | password = existing |
| 94 | } else { |
| 95 | password = try await Console.getPassword("Password: ") |
| 96 | } |
| 97 | guard !password.isEmpty else { |
| 98 | throw Console.Error("Password cannot be empty.") |
| 99 | } |
| 100 | |
| 101 | d.out.print("Logging in...") |
| 102 | |
| 103 | let authDelegate = XToolAuthDelegate() |
| 104 | let manager = DeveloperServicesLoginManager() |
| 105 | let token = try await manager.logIn( |
| 106 | withUsername: username, |
| 107 | password: password, |
| 108 | twoFactorDelegate: authDelegate |
| 109 | ) |
| 110 | |
| 111 | let client = DeveloperServicesClient(loginToken: token) |
| 112 | let teams = try await client.send(DeveloperServicesListTeamsRequest()) |
| 113 | let team = try await Console.choose( |
| 114 | from: teams, |
| 115 | onNoElement: { |
| 116 | throw Console.Error("No development teams found") |
| 117 | }, |
| 118 | multiPrompt: "\nSelect a team", |
| 119 | formatter: { |
| 120 | "\($0.name) (\($0.id.rawValue))" |
| 121 | } |
| 122 | ) |
| 123 | |
| 124 | @Dependency(\.anisetteDataProvider) var anisetteProvider |
| 125 | |
| 126 | return AuthToken.xcode(.init( |
| 127 | appleID: username, |
| 128 | adsid: token.adsid, |
| 129 | token: token.token, |
| 130 | expiry: token.expiry, |
| 131 | teamID: team.id.rawValue, |
| 132 | anisetteProviderID: anisetteProvider.providerID |
| 133 | )) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | struct AuthLoginCommand: AsyncParsableCommand { |
| 138 | static let configuration = CommandConfiguration( |
| 139 | commandName: "login", |
| 140 | abstract: "Log in to Apple Developer Services" |
| 141 | ) |
| 142 | |
| 143 | @Option(name: [.short, .long], help: "Apple ID") var username: String? |
| 144 | @Option(name: [.short, .long]) var password: String? |
| 145 | @Option(name: [.short, .long]) var mode: AuthMode? |
| 146 | |
| 147 | func run() async throws { |
| 148 | try await AuthOperation( |
| 149 | username: username, |
| 150 | password: password, |
| 151 | logoutFromExisting: true, |
| 152 | mode: mode |
| 153 | ).run() |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | struct AuthLogoutCommand: AsyncParsableCommand { |
| 158 | static let configuration = CommandConfiguration( |
| 159 | commandName: "logout", |
| 160 | abstract: "Log out of Apple Developer Services" |
| 161 | ) |
| 162 | |
| 163 | @Flag( |
| 164 | name: [.short, .customLong("reset-2fa")], |
| 165 | help: ArgumentHelp( |
| 166 | "Reset 2-factor authentication data", |
| 167 | discussion: """ |
| 168 | This resets the "pseudo-device" that xtool presents itself as \ |
| 169 | when authenticating with Apple using the password login mode. |
| 170 | |
| 171 | Effectively, this means you will be prompted to complete 2-factor \ |
| 172 | authentication again the next time you log in. |
| 173 | """ |
| 174 | ) |
| 175 | ) var reset2FA = false |
| 176 | |
| 177 | func run() async throws { |
| 178 | if (try? AuthToken.saved()) != nil { |
| 179 | try AuthToken.clear() |
| 180 | d.out.print("Logged out") |
| 181 | } else { |
| 182 | d.out.print("Already logged out") |
| 183 | } |
| 184 | |
| 185 | if reset2FA { |
| 186 | @Dependency(\.anisetteDataProvider) var anisetteProvider |
| 187 | await anisetteProvider.resetProvisioning() |
| 188 | d.out.print("Forgot device") |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | struct AuthStatusCommand: AsyncParsableCommand { |
| 194 | static let configuration = CommandConfiguration( |
| 195 | commandName: "status", |
| 196 | abstract: "Get Apple Developer Services auth status" |
| 197 | ) |
| 198 | |
| 199 | func run() async throws { |
| 200 | if let token = try? AuthToken.saved() { |
| 201 | d.out.print("Logged in.\n\(token)") |
| 202 | } else { |
| 203 | d.out.print("Logged out") |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | struct AuthCommand: AsyncParsableCommand { |
| 209 | static let configuration = CommandConfiguration( |
| 210 | commandName: "auth", |
| 211 | abstract: "Manage Apple Developer Services authentication", |
| 212 | subcommands: [ |
| 213 | AuthLoginCommand.self, |
| 214 | AuthLogoutCommand.self, |
| 215 | AuthStatusCommand.self, |
| 216 | ], |
| 217 | defaultSubcommand: AuthLoginCommand.self |
| 218 | ) |
| 219 | } |