mirror of
https://github.com/xtool-org/xtool.git
synced 2026-11-10 01:18:01 +03:00
e3ce2c402e
## What does this PR do? Fixes #378, solving error -80046. Seems Apple now blocks Android-identifying clients from using gsa (at least the 2fa bits). Use Mac Anisette instead. cf https://github.com/nab138/iloader/commit/902597564315442f00ab299dcc758ee0e13b8fa8 Alas, this does mean we have to retire xadi and switch back to Omnisette. ## How was it tested? - [x] Ran locally on macOS ## AI tool usage How much of this PR was AI-assisted? (check one) - [x] **0** - No AI was used to write code - [ ] **1** - I was assisted by AI. I reviewed the finished result. - [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only <!-- If an AI agent is filling this in: declare the level honestly, and open as a draft if it is #2. Do not lower the declared level to get the PR reviewed. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Two-factor requests now use the device’s configured User-Agent. * **Updates** * Anisette requests now use the Omnisette provider and a new default server. The optional server URL override remains available. * The password-login warning now explains the virtual Mac and third-party service involved, clarifies that credentials are sent only to Apple, and recommends considering a secondary Apple ID. * **Behavior Changes** * The Android-specific ADI provider is no longer available. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
218 lines
6.6 KiB
Swift
218 lines
6.6 KiB
Swift
import Foundation
|
|
import XKit
|
|
import ArgumentParser
|
|
import Crypto
|
|
import Dependencies
|
|
|
|
enum AuthMode: String, CaseIterable, CustomStringConvertible, ExpressibleByArgument {
|
|
case key
|
|
case password
|
|
|
|
var description: String {
|
|
switch self {
|
|
case .key: "API Key (requires paid Apple Developer Program membership)"
|
|
case .password: "Password (works with any Apple ID but uses private APIs)"
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AuthOperation {
|
|
var username: String?
|
|
var password: String?
|
|
var logoutFromExisting: Bool
|
|
|
|
var mode: AuthMode?
|
|
var quiet = false
|
|
|
|
func run() async throws {
|
|
if let token = try? AuthToken.saved(), !logoutFromExisting {
|
|
if !quiet {
|
|
print("Logged in.\n\(token)")
|
|
}
|
|
return
|
|
}
|
|
|
|
let mode: AuthMode
|
|
if let existing = self.mode {
|
|
mode = existing
|
|
} else {
|
|
mode = try await Console.choose(
|
|
from: AuthMode.allCases,
|
|
onNoElement: { throw Console.Error("Mode selection is required") },
|
|
multiPrompt: "Select login mode",
|
|
formatter: \.description
|
|
)
|
|
}
|
|
|
|
let token = switch mode {
|
|
case .password:
|
|
try await logInWithPassword()
|
|
case .key:
|
|
try await logInWithKey()
|
|
}
|
|
try token.save()
|
|
|
|
print("Logged in.\n\(token)")
|
|
}
|
|
|
|
private func logInWithKey() async throws -> AuthToken {
|
|
let id = try await Console.promptRequired("Key ID: ", existing: nil)
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
let issuerID = try await Console.promptRequired("Issuer ID: ", existing: nil)
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
let path = try await Console.promptRequired("Key path: ", existing: nil)
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
let pem = try String(decoding: Data(contentsOf: URL(fileURLWithPath: path)), as: UTF8.self)
|
|
do {
|
|
_ = try P256.Signing.PrivateKey(pemRepresentation: pem)
|
|
} catch {
|
|
throw Console.Error("Key is invalid: \(error)")
|
|
}
|
|
|
|
return AuthToken.appStoreConnect(.init(id: id, issuerID: issuerID, pem: pem))
|
|
}
|
|
|
|
private func logInWithPassword() async throws -> AuthToken {
|
|
print("""
|
|
Heads up:
|
|
This works by emulating a virtual Mac to interact with Apple services.
|
|
We use a third-party service to create a randomized device ID token
|
|
(Anisette Data) but your credentials are only ever sent to Apple.
|
|
Still, consider using a secondary Apple ID since this is a private API.
|
|
""")
|
|
|
|
let username = try await Console.promptRequired("Apple ID: ", existing: username)
|
|
|
|
let password: String
|
|
if let existing = self.password {
|
|
password = existing
|
|
} else {
|
|
password = try await Console.getPassword("Password: ")
|
|
}
|
|
guard !password.isEmpty else {
|
|
throw Console.Error("Password cannot be empty.")
|
|
}
|
|
|
|
print("Logging in...")
|
|
|
|
let authDelegate = XToolAuthDelegate()
|
|
let manager = DeveloperServicesLoginManager()
|
|
let token = try await manager.logIn(
|
|
withUsername: username,
|
|
password: password,
|
|
twoFactorDelegate: authDelegate
|
|
)
|
|
|
|
let client = DeveloperServicesClient(loginToken: token)
|
|
let teams = try await client.send(DeveloperServicesListTeamsRequest())
|
|
let team = try await Console.choose(
|
|
from: teams,
|
|
onNoElement: {
|
|
throw Console.Error("No development teams found")
|
|
},
|
|
multiPrompt: "\nSelect a team",
|
|
formatter: {
|
|
"\($0.name) (\($0.id.rawValue))"
|
|
}
|
|
)
|
|
|
|
@Dependency(\.anisetteDataProvider) var anisetteProvider
|
|
|
|
return AuthToken.xcode(.init(
|
|
appleID: username,
|
|
adsid: token.adsid,
|
|
token: token.token,
|
|
expiry: token.expiry,
|
|
teamID: team.id.rawValue,
|
|
anisetteProviderID: anisetteProvider.providerID
|
|
))
|
|
}
|
|
}
|
|
|
|
struct AuthLoginCommand: AsyncParsableCommand {
|
|
static let configuration = CommandConfiguration(
|
|
commandName: "login",
|
|
abstract: "Log in to Apple Developer Services"
|
|
)
|
|
|
|
@Option(name: [.short, .long], help: "Apple ID") var username: String?
|
|
@Option(name: [.short, .long]) var password: String?
|
|
@Option(name: [.short, .long]) var mode: AuthMode?
|
|
|
|
func run() async throws {
|
|
try await AuthOperation(
|
|
username: username,
|
|
password: password,
|
|
logoutFromExisting: true,
|
|
mode: mode
|
|
).run()
|
|
}
|
|
}
|
|
|
|
struct AuthLogoutCommand: AsyncParsableCommand {
|
|
static let configuration = CommandConfiguration(
|
|
commandName: "logout",
|
|
abstract: "Log out of Apple Developer Services"
|
|
)
|
|
|
|
@Flag(
|
|
name: [.short, .customLong("reset-2fa")],
|
|
help: ArgumentHelp(
|
|
"Reset 2-factor authentication data",
|
|
discussion: """
|
|
This resets the "pseudo-device" that xtool presents itself as \
|
|
when authenticating with Apple using the password login mode.
|
|
|
|
Effectively, this means you will be prompted to complete 2-factor \
|
|
authentication again the next time you log in.
|
|
"""
|
|
)
|
|
) var reset2FA = false
|
|
|
|
func run() async throws {
|
|
if (try? AuthToken.saved()) != nil {
|
|
try AuthToken.clear()
|
|
print("Logged out")
|
|
} else {
|
|
print("Already logged out")
|
|
}
|
|
|
|
if reset2FA {
|
|
@Dependency(\.anisetteDataProvider) var anisetteProvider
|
|
await anisetteProvider.resetProvisioning()
|
|
print("Forgot device")
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AuthStatusCommand: AsyncParsableCommand {
|
|
static let configuration = CommandConfiguration(
|
|
commandName: "status",
|
|
abstract: "Get Apple Developer Services auth status"
|
|
)
|
|
|
|
func run() async throws {
|
|
if let token = try? AuthToken.saved() {
|
|
print("Logged in.\n\(token)")
|
|
} else {
|
|
print("Logged out")
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AuthCommand: AsyncParsableCommand {
|
|
static let configuration = CommandConfiguration(
|
|
commandName: "auth",
|
|
abstract: "Manage Apple Developer Services authentication",
|
|
subcommands: [
|
|
AuthLoginCommand.self,
|
|
AuthLogoutCommand.self,
|
|
AuthStatusCommand.self,
|
|
],
|
|
defaultSubcommand: AuthLoginCommand.self
|
|
)
|
|
}
|