gsa: fall back to a trusted-device code when SMS 2FA fails (#349)
- Commit
8c46156e27fcd5874acfa3725b29defdee809184- Author
- javiertrejoyam <[email protected]>
- When
- 2026-10-19 00:55 UTC (1 month ago)
- Parents
2afd530
`secondaryAuth` can't finish logging in: GSA rejects POST
/auth/verify/phone/put?mode=sms with HTTP 403 ("Could not connect to
iCloud"). The JSON flavor of those endpoints (GET /auth, PUT
/auth/verify/phone/) answers 401 as well.
GsService2/validate still accepts a code generated on a trusted device
(Settings > Sign-In & Security > Get Verification Code), so ask for one
instead of failing. Tested on WSL (Ubuntu 26.04) with an iOS 27 device.
## What does this PR do?
<!-- Focus on *what* the PR does in essence. The *how* should come
second, if it's relevant. -->
## How was it tested?
<!-- e.g. Ran locally on macOS and added unit tests -->
## AI tool usage
How much of this PR was AI-assisted? (check one)
- [ ] **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
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Two-factor authentication can fall back to a trusted device when an
SMS request returns HTTP 403.
* If trusted-device authentication also returns HTTP 403, users receive
instructions for manually generating a verification code, and code
validation can continue. Other errors are still reported.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Kabir Oberai <[email protected]>Diff
Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift
diff --git a/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift b/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swiftindex a3a39d9..64f362b 100644--- a/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift+++ b/Sources/XKit/GrandSlam/Operations/GrandSlamTwoFactorAuthenticateOperation.swift@@ -27,7 +27,15 @@ struct GrandSlamTwoFactorAuthenticateOperation { private func performSMSAuth() async throws { // TODO: parse phone number list from o=complete req? let phoneNumberID = "1"- try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: phoneNumberID))+ do {+ try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: phoneNumberID))+ } catch let error as HTTPResponseError where error.status.code == 403 {+ print("""+ warning: Could not send verification code via SMS. Falling back to trusted device authentication...+ """)+ try await performTrustedDeviceAuth()+ return+ } try await validateCode(phoneNumberID: phoneNumberID) } @@ -51,7 +59,14 @@ struct GrandSlamTwoFactorAuthenticateOperation { private func performTrustedDeviceAuth() async throws { let request = GrandSlamTrustedDeviceRequest(loginData: loginData)- try await client.send(request)+ do {+ try await client.send(request)+ } catch let error as HTTPResponseError where error.status.code == 403 {+ print("""+ warning: Could not send verification code. Generate one manually.+ See https://github.com/xtool-org/xtool/discussions/368+ """)+ } try await validateCode() }