Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions Tests/XToolTests/SMSPhoneNumberTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}