|
| 1 | +// |
| 2 | +// Auth.swift |
| 3 | +// IPATool |
| 4 | +// |
| 5 | +// Created by Majd Alfhaily on 21.03.22. |
| 6 | +// |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import Foundation |
| 10 | +import Networking |
| 11 | +import StoreAPI |
| 12 | +import Persistence |
| 13 | + |
| 14 | +struct Auth: AsyncParsableCommand { |
| 15 | + static var configuration: CommandConfiguration { |
| 16 | + return .init( |
| 17 | + commandName: "auth", |
| 18 | + abstract: "Authenticate with the App Store.", |
| 19 | + subcommands: [Login.self, Revoke.self], |
| 20 | + defaultSubcommand: nil |
| 21 | + ) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +extension Auth { |
| 26 | + struct Login: AsyncParsableCommand { |
| 27 | + static var configuration: CommandConfiguration { |
| 28 | + return .init(abstract: "Login to the App Store.") |
| 29 | + } |
| 30 | + |
| 31 | + @Option(name: [.short, .customLong("email")], help: "The email address for the Apple ID.") |
| 32 | + private var emailArgument: String? |
| 33 | + |
| 34 | + @Option(name: [.short, .customLong("password")], help: "The password for the Apple ID.") |
| 35 | + private var passwordArgument: String? |
| 36 | + |
| 37 | + @Option(name: [.customLong("auth-code")], help: "The 2FA code for the Apple ID.") |
| 38 | + private var authCodeArgument: String? |
| 39 | + |
| 40 | + @Option(name: [.long], help: "The log level.") |
| 41 | + private var logLevel: LogLevel = .info |
| 42 | + |
| 43 | + lazy var logger = ConsoleLogger(level: logLevel) |
| 44 | + } |
| 45 | + |
| 46 | + struct Revoke: AsyncParsableCommand { |
| 47 | + static var configuration: CommandConfiguration { |
| 48 | + return .init(abstract: "Revoke your App Store credentials.") |
| 49 | + } |
| 50 | + |
| 51 | + @Option(name: [.long], help: "The log level.") |
| 52 | + private var logLevel: LogLevel = .info |
| 53 | + |
| 54 | + lazy var logger = ConsoleLogger(level: logLevel) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +extension Auth.Login { |
| 59 | + private mutating func email() -> String { |
| 60 | + if let email = emailArgument { |
| 61 | + return email |
| 62 | + } else if let email = ProcessInfo.processInfo.environment["IPATOOL_EMAIL"] { |
| 63 | + return email |
| 64 | + } else if let email = String(validatingUTF8: UnsafePointer<CChar>(getpass(logger.compile("Enter Apple ID email: ", level: .warning)))) { |
| 65 | + return email |
| 66 | + } else { |
| 67 | + logger.log("An Apple ID email address is required.", level: .error) |
| 68 | + _exit(1) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private mutating func password() -> String { |
| 73 | + if let password = passwordArgument { |
| 74 | + return password |
| 75 | + } else if let password = ProcessInfo.processInfo.environment["IPATOOL_PASSWORD"] { |
| 76 | + return password |
| 77 | + } else if let password = String(validatingUTF8: UnsafePointer<CChar>(getpass(logger.compile("Enter Apple ID password: ", level: .warning)))) { |
| 78 | + return password |
| 79 | + } else { |
| 80 | + logger.log("An Apple ID password is required.", level: .error) |
| 81 | + _exit(1) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private mutating func authCode() -> String { |
| 86 | + if let authCode = authCodeArgument { |
| 87 | + return authCode |
| 88 | + } else if let authCode = ProcessInfo.processInfo.environment["IPATOOL_2FA_CODE"] { |
| 89 | + return authCode |
| 90 | + } else if let authCode = String(validatingUTF8: UnsafePointer<CChar>(getpass(logger.compile("Enter 2FA code: ", level: .warning)))) { |
| 91 | + return authCode |
| 92 | + } else { |
| 93 | + logger.log("A 2FA auth-code is required.", level: .error) |
| 94 | + _exit(1) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private mutating func authenticate(email: String, password: String) async -> Account { |
| 99 | + logger.log("Creating HTTP client...", level: .debug) |
| 100 | + let httpClient = HTTPClient(session: URLSession.shared) |
| 101 | + |
| 102 | + logger.log("Creating App Store client...", level: .debug) |
| 103 | + let storeClient = StoreClient(httpClient: httpClient) |
| 104 | + |
| 105 | + do { |
| 106 | + logger.log("Authenticating with the App Store...", level: .info) |
| 107 | + let account = try await storeClient.authenticate(email: email, password: password, code: nil) |
| 108 | + return Account( |
| 109 | + name: "\(account.firstName) \(account.lastName)", |
| 110 | + email: email, |
| 111 | + passwordToken: account.passwordToken, |
| 112 | + directoryServicesIdentifier: account.directoryServicesIdentifier |
| 113 | + ) |
| 114 | + } catch { |
| 115 | + switch error { |
| 116 | + case StoreResponse.Error.codeRequired: |
| 117 | + do { |
| 118 | + let account = try await storeClient.authenticate(email: email, password: password, code: authCode()) |
| 119 | + return Account( |
| 120 | + name: "\(account.firstName) \(account.lastName)", |
| 121 | + email: email, |
| 122 | + passwordToken: account.passwordToken, |
| 123 | + directoryServicesIdentifier: account.directoryServicesIdentifier |
| 124 | + ) |
| 125 | + } catch { |
| 126 | + logger.log("\(error)", level: .debug) |
| 127 | + |
| 128 | + switch error { |
| 129 | + case StoreClient.Error.invalidResponse: |
| 130 | + logger.log("Received invalid response.", level: .error) |
| 131 | + case StoreResponse.Error.invalidAccount: |
| 132 | + logger.log("This Apple ID has not been set up to use the App Store.", level: .error) |
| 133 | + case StoreResponse.Error.invalidCredentials: |
| 134 | + logger.log("Invalid credentials.", level: .error) |
| 135 | + case StoreResponse.Error.lockedAccount: |
| 136 | + logger.log("This Apple ID has been disabled for security reasons.", level: .error) |
| 137 | + default: |
| 138 | + logger.log("An unknown error has occurred.", level: .error) |
| 139 | + } |
| 140 | + |
| 141 | + _exit(1) |
| 142 | + } |
| 143 | + default: |
| 144 | + logger.log("\(error)", level: .debug) |
| 145 | + |
| 146 | + switch error { |
| 147 | + case StoreClient.Error.invalidResponse: |
| 148 | + logger.log("Received invalid response.", level: .error) |
| 149 | + case StoreResponse.Error.invalidAccount: |
| 150 | + logger.log("This Apple ID has not been set up to use the App Store.", level: .error) |
| 151 | + case StoreResponse.Error.invalidCredentials: |
| 152 | + logger.log("Invalid credentials.", level: .error) |
| 153 | + case StoreResponse.Error.lockedAccount: |
| 154 | + logger.log("This Apple ID has been disabled for security reasons.", level: .error) |
| 155 | + default: |
| 156 | + logger.log("An unknown error has occurred.", level: .error) |
| 157 | + } |
| 158 | + |
| 159 | + _exit(1) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + mutating func run() async throws { |
| 165 | + // Get Apple ID email |
| 166 | + let email: String = email() |
| 167 | + |
| 168 | + // Get Apple ID password |
| 169 | + let password: String = password() |
| 170 | + |
| 171 | + // Authenticate with the App Store |
| 172 | + let account: Account = await authenticate(email: email, password: password) |
| 173 | + |
| 174 | + // Store data in keychain |
| 175 | + do { |
| 176 | + let keychainStore = KeychainStore(service: "ipatool.service") |
| 177 | + try keychainStore.setValue(account, forKey: "account") |
| 178 | + |
| 179 | + logger.log("Authenticated as '\(account.name)'.", level: .info) |
| 180 | + } catch { |
| 181 | + logger.log("Failed to save account data in keychain.", level: .error) |
| 182 | + logger.log("\(error)", level: .debug) |
| 183 | + |
| 184 | + _exit(1) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +extension Auth.Revoke { |
| 190 | + mutating func run() async throws { |
| 191 | + let keychainStore = KeychainStore(service: "ipatool.service") |
| 192 | + |
| 193 | + guard let account: Account = try keychainStore.value(forKey: "account") else { |
| 194 | + logger.log("No credentials available to revoke.", level: .error) |
| 195 | + _exit(1) |
| 196 | + } |
| 197 | + |
| 198 | + try keychainStore.remove("account") |
| 199 | + logger.log("Revoked credentials for '\(account.name)'.", level: .info) |
| 200 | + } |
| 201 | +} |
0 commit comments