Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Sign-in: an Apple outage is not a sign-out

Yashwanth Kamireddie6388f8305fe
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rs
index 3c3648c..dd305ff 100644
--- a/crates/quince-auth/src/lib.rs
+++ b/crates/quince-auth/src/lib.rs
@@ -107,7 +107,7 @@ pub enum Error {
Protocol(String),
/// Apple's servers answered but could not serve now — an outage, maintenance, too many
/// requests (5xx, 429). Like being offline: try again later, never "signed out".
- #[error("Apple's servers are busy right now ({0}). Skykeep will try again shortly.")]
+ #[error("Apple’s servers are busy right now. Try again in a few minutes.")]
Unavailable(u16),
#[error("Too many attempts. Try again in {seconds} seconds.")]
CoolingDown { seconds: u64 },
@@ -153,6 +153,10 @@ fn apple_error(context: &str, status: StatusCode, body: &str) -> Error {
if status.as_u16() == 450 {
return Error::ReauthRequired;
}
+ // An outage or maintenance: its body is a web page, never words for the person signing in.
+ if status.is_server_error() {
+ return Error::Unavailable(status.as_u16());
+ }
let parsed: Option<Value> = serde_json::from_str(body).ok();
if let Some(v) = &parsed {
if let Some(errs) = v["serviceErrors"].as_array().filter(|a| !a.is_empty()) {
@@ -701,11 +705,19 @@ impl Client {
let before = self.session.clone();
match self.sign_in(&apple_id, &password).await {
Ok(SignInOutcome::Complete) => return Ok(true),
- // Lost the network part way: what was kept is put back as it was.
- Err(Error::Network(e)) => {
+ // Lost the network part way, or Apple busy, down or asking to wait: not a
+ // refusal. What was kept is put back as it was and the next try is left to
+ // later — read as signed out, an outage told the person "Apple signed you
+ // out" and dropped the session token.
+ Err(
+ e @ (Error::Network(_)
+ | Error::Unavailable(_)
+ | Error::RateLimited
+ | Error::CoolingDown { .. }),
+ ) => {
self.session = before;
let _ = self.persist();
- return Err(Error::Network(e));
+ return Err(e);
}
Ok(SignInOutcome::NeedsTwoFactor) => {
self.awaiting_code = true;
@@ -1699,6 +1711,16 @@ mod tests {
Error::RateLimited
));
+ // An outage at sign-in is said as one, not as Apple's error page.
+ let page = "<html><head><title>502 Bad Gateway</title></head><body>nginx</body></html>";
+ let said = apple_error("starting sign-in", StatusCode::BAD_GATEWAY, page);
+ assert!(matches!(said, Error::Unavailable(502)), "got {said:?}");
+ assert!(!said.to_string().contains('<'), "no markup: {said}");
+ assert!(
+ !said.to_string().contains("Skykeep will"),
+ "nothing it won't do"
+ );
+
// Find My answers 450 on a resumed session; that is "sign in again", not a failure.
assert!(matches!(
apple_error("x", StatusCode::from_u16(450).unwrap(), ""),
diff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rs
index 33fb586..1fac3a0 100644
--- a/crates/quince-auth/tests/auth_flow.rs
+++ b/crates/quince-auth/tests/auth_flow.rs
@@ -742,6 +742,62 @@ async fn a_lapsed_session_signs_itself_back_in_without_a_code() {
);
}
+/// Apple busy or down just as a lapsed session signs itself back in: that is "not now", not
+/// "signed out". Read as signed out, every sync during an outage told the person Apple had
+/// signed them out, and the session token was dropped.
+#[tokio::test]
+async fn an_outage_while_signing_back_in_is_not_a_sign_out() {
+ for (init, complete) in [(503, 200), (200, 502)] {
+ let dir = tempfile::tempdir().unwrap();
+ signed_in(dir.path()).await;
+
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/setup/ws/1/validate"))
+ .respond_with(ResponseTemplate::new(421))
+ .mount(&server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/setup/ws/1/accountLogin"))
+ .respond_with(ResponseTemplate::new(421))
+ .mount(&server)
+ .await;
+ mount_init(&server, init).await;
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/signin/complete"))
+ .respond_with(
+ ResponseTemplate::new(complete).set_body_string("<html>Bad Gateway</html>"),
+ )
+ .mount(&server)
+ .await;
+
+ let mut client = client_for(&server, dir.path());
+ let err = client
+ .resume()
+ .await
+ .expect_err("an outage is said, not read as signed out");
+ assert!(
+ matches!(
+ err,
+ quince_auth::Error::Unavailable(_) | quince_auth::Error::RateLimited
+ ),
+ "{init}/{complete}: {err:?}"
+ );
+ assert!(!err.to_string().contains('<'), "no error page: {err}");
+ assert!(
+ client.session().session_token.is_some(),
+ "the session is kept"
+ );
+
+ let back = client_for(&server, dir.path());
+ assert!(
+ back.has_stored_session(),
+ "and still kept on the next launch"
+ );
+ assert!(back.session().session_token.is_some());
+ }
+}
+
/// When the trust itself has run out, Apple wants a code: the code screen is next, not the
/// sign-in screen, and the code finishes it.
#[tokio::test]
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 04a77c7..d9a8fb1 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -1283,12 +1283,13 @@ async fn resume_session(client: &mut Client) -> UiResult<&'static str> {
Ok(resumed) => resumed,
// Offline with a session kept: the library opens on what it holds, as a local-first
// app should, and the next refresh — on the timer, or when the network is back —
- // signs back in.
- Err(quince_auth::Error::Network(_) | quince_auth::Error::Unavailable(_))
- if client.has_stored_session() =>
- {
- true
- }
+ // signs back in. Apple busy, or asking to wait, is the same: not now, not signed out.
+ Err(
+ quince_auth::Error::Network(_)
+ | quince_auth::Error::Unavailable(_)
+ | quince_auth::Error::RateLimited
+ | quince_auth::Error::CoolingDown { .. },
+ ) if client.has_stored_session() => true,
Err(e) => return Err(e.to_string()),
};
// Back in; or back in but for the code Apple wants before trusting this computer again;