diff --git a/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift b/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift index f185276b..3ab96e46 100644 --- a/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift +++ b/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift @@ -26,10 +26,9 @@ struct GrandSlamTwoFactorAuthenticateOperation { unowned let delegate: TwoFactorAuthDelegate private func performSMSAuth() async throws { - // TODO: parse phone number list from o=complete req? - let phoneNumberID = 1 + let phoneNumberID: Int do { - try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: phoneNumberID)) + phoneNumberID = try await sendSMSCode() } catch let error as HTTPResponseError where error.status.code == 403 { d.out.print(""" warning: Could not send verification code via SMS. Falling back to trusted device authentication... @@ -40,6 +39,33 @@ struct GrandSlamTwoFactorAuthenticateOperation { try await validateCode(phoneNumberID: phoneNumberID) } + /// Sends an SMS code and returns the ID of the phone number it was sent to. + private func sendSMSCode() async throws -> Int { + do { + try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: 1)) + return 1 + } catch let error as HTTPResponseError where error.status.code == 412 { + // Apple rejects an unknown phone number ID with 412 and lists the account's trusted numbers. + guard let phoneNumberID = Self.smsPhoneNumberID(in: error.body) else { throw error } + try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: phoneNumberID)) + return phoneNumberID + } + } + + static func smsPhoneNumberID(in body: String?) -> Int? { + struct Response: Decodable { + struct PhoneNumber: Decodable { + let id: Int + let pushMode: String? + } + let trustedPhoneNumbers: [PhoneNumber] + } + guard let data = body?.data(using: .utf8), + let numbers = try? JSONDecoder().decode(Response.self, from: data).trustedPhoneNumbers + else { return nil } + return (numbers.first { $0.pushMode == "sms" } ?? numbers.first)?.id + } + private func validateCode(phoneNumberID: Int? = nil) async throws { guard let code = await self.delegate.fetchCode() else { throw Error.userCancelled } let request: any GrandSlamRequest = if let phoneNumberID { diff --git a/Tests/XToolTests/SMSPhoneNumberTests.swift b/Tests/XToolTests/SMSPhoneNumberTests.swift new file mode 100644 index 00000000..017b1e32 --- /dev/null +++ b/Tests/XToolTests/SMSPhoneNumberTests.swift @@ -0,0 +1,19 @@ +import Testing +@testable import XKit + +struct SMSPhoneNumberTests { + @Test(arguments: [ + // the body from #424: Apple lists the only trusted number, with ID 2 + (#"{"success":0,"trustedPhoneNumbers":[{"nonFTEU":true,"pushMode":"sms","id":2}],"mode":"sms"}"#, 2), + (#"{"trustedPhoneNumbers":[{"pushMode":"voice","id":3},{"pushMode":"sms","id":4}]}"#, 4), + (#"{"trustedPhoneNumbers":[{"id":5}]}"#, 5), + ]) + func picksTrustedNumber(body: String, expected: Int) { + #expect(GrandSlamTwoFactorAuthenticateOperation.smsPhoneNumberID(in: body) == expected) + } + + @Test(arguments: [nil, "", "not json", #"{"trustedPhoneNumbers":[]}"#, #"{"success":0}"#]) + func noTrustedNumber(body: String?) { + #expect(GrandSlamTwoFactorAuthenticateOperation.smsPhoneNumberID(in: body) == nil) + } +}