Skip to content

Commit 7c135eb

Browse files
committed
Use a separate connection for each GrandSlam request
Apple's edge serves at most two requests per connection and answers everything after that with a 503 HTML error page. Authenticating takes three requests -- init, complete and apptokens -- and they all went through AppleAPI.shared.session, so apptokens was rejected whenever it landed on a reused connection and signing in failed at the last step. Give each request its own session, and therefore its own connection. The response body was also parsed as a property list before the status code was checked, so Apple's HTML error page surfaced as NSCocoaErrorDomain 3840, "The data couldn't be read because it isn't in the correct format." That describes the parser rather than the failure and sends people looking at their Apple ID, their anisette server and their OS version. Check for a 5xx first and report it as what it is. Port of rileytestut/AltSign#52
1 parent 69397b4 commit 7c135eb

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

‎Sources/StosSign_Auth/Authentication.swift‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,20 @@ public final class Authentication {
265265
request.httpBody = bodyData
266266
httpHeaders.forEach { request.addValue($0.value, forHTTPHeaderField: $0.key) }
267267

268-
let (data, _) = try await AppleAPI.shared.session.data(for: request)
269-
268+
let configuration = URLSessionConfiguration.ephemeral
269+
configuration.httpMaximumConnectionsPerHost = 1
270+
let session = URLSession(configuration: configuration)
271+
defer { session.finishTasksAndInvalidate() }
272+
273+
let (data, response) = try await session.data(for: request)
274+
275+
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode >= 500 {
276+
throw AppleAPIError.customError(
277+
code: httpResponse.statusCode,
278+
message: "Apple's authentication servers returned an error (HTTP \(httpResponse.statusCode)). This is a problem on Apple's end, not with your Apple ID or password."
279+
)
280+
}
281+
270282
guard let responseDictionary = try PropertyListSerialization.propertyList(
271283
from: data,
272284
format: nil

0 commit comments

Comments
 (0)