From adcd71b6f49a97fb12a44d76bcce94531ae8b819 Mon Sep 17 00:00:00 2001 From: Omar Titouhi Date: Thu, 12 Nov 2026 18:04:46 +0100 Subject: [PATCH] gsa: send SMS codes to the account's trusted phone number SMS 2FA always used phone number ID 1. When Apple rejects that with HTTP 412, read the trusted numbers from the response body and send the code to the first SMS number instead. Fixes #424 --- ...ndSlamTwoFactorAuthenticateOperation.swift | 32 +++++++++++++++++-- Tests/XToolTests/SMSPhoneNumberTests.swift | 19 +++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 Tests/XToolTests/SMSPhoneNumberTests.swift diff --git a/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift b/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift index 5a41e027..8421bfef 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) + } +}