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: a locked account refused before the password is tried is locked, not a wrong password

Yashwanth Kamireddi09775d6e2fca
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rs
index 40a3534..20c8055 100644
--- a/crates/quince-auth/src/lib.rs
+++ b/crates/quince-auth/src/lib.rs
@@ -209,6 +209,20 @@ fn apple_error(context: &str, status: StatusCode, body: &str) -> Error {
Error::Protocol(format!("{context}: HTTP {status} {snippet}"))
}
+/// Apple turning the password down (a 401, or a 403 before the password was even tried) —
+/// unless its own code in the reply says why it really refused.
+///
+/// A locked account is a 403 or 401 with code -20209 (timlaing/pyicloud#368,
+/// icloud_photos_downloader#1010). Read as a wrong password, a lapsed session signing itself
+/// back in threw away the password it keeps, so the account was locked *and* signed out, and
+/// the person was told to check a password that was right.
+fn refused_password(context: &str, status: StatusCode, body: &str) -> Error {
+ match apple_error(context, status, body) {
+ e @ (Error::AccountLocked | Error::RateLimited) => e,
+ _ => Error::BadCredentials,
+ }
+}
+
/// A phone Apple will text a code to, when no trusted device responds.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
@@ -899,7 +913,7 @@ impl Client {
let status = r.status();
let text = r.text().await.unwrap_or_default();
if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
- return Err(Error::BadCredentials);
+ return Err(refused_password("starting sign-in", status, &text));
}
if !status.is_success() {
return Err(apple_error("starting sign-in", status, &text));
@@ -994,7 +1008,7 @@ impl Client {
self.keep();
Ok(SignInOutcome::Complete)
}
- StatusCode::UNAUTHORIZED => Err(Error::BadCredentials),
+ StatusCode::UNAUTHORIZED => Err(refused_password("finishing sign-in", status, &text)),
StatusCode::PRECONDITION_FAILED => Err(Error::UnsupportedAccount(
"two-factor auth isn't switched on".into(),
)),
diff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rs
index fd58408..c4b8e73 100644
--- a/crates/quince-auth/tests/auth_flow.rs
+++ b/crates/quince-auth/tests/auth_flow.rs
@@ -1046,3 +1046,91 @@ async fn asking_for_the_code_again_too_often_says_to_wait() {
let err = client.send_sms(1).await.unwrap_err();
assert!(matches!(err, quince_auth::Error::RateLimited), "{err:?}");
}
+
+/// Apple can refuse a locked account before the password is even tried: a 403 from
+/// `/signin/init`, or a 401 from `/signin/complete`, each carrying code -20209
+/// (timlaing/pyicloud#368, icloud_photos_downloader#1010). Read as a wrong password, a lapsed
+/// session signing itself back in threw away the password it keeps.
+#[tokio::test]
+async fn a_locked_account_refused_at_any_step_keeps_the_password() {
+ let locked = json!({
+ "serviceErrors": [{
+ "code": "-20209",
+ "message": "This Apple Account has been locked for security reasons."
+ }]
+ });
+ for (step, status) in [("init", 403), ("init", 401), ("complete", 401)] {
+ 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;
+ if step == "init" {
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/signin/init"))
+ .respond_with(ResponseTemplate::new(status).set_body_json(locked.clone()))
+ .mount(&server)
+ .await;
+ } else {
+ mount_init(&server, 200).await;
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/signin/complete"))
+ .respond_with(ResponseTemplate::new(status).set_body_json(locked.clone()))
+ .mount(&server)
+ .await;
+ }
+
+ let mut client = client_for(&server, dir.path());
+ assert!(
+ !client.resume().await.unwrap(),
+ "{step} {status}: not back in"
+ );
+ assert_eq!(
+ client.session().password.as_deref(),
+ Some("hunter2"),
+ "{step} {status}: a locked account is no wrong password; the password stays kept"
+ );
+ // Signed in by hand (on another install: this one now owes a wait), it says why.
+ let fresh = tempfile::tempdir().unwrap();
+ let err = client_for(&server, fresh.path())
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap_err();
+ assert!(
+ matches!(err, quince_auth::Error::AccountLocked),
+ "{step} {status}: {err:?}"
+ );
+ }
+}
+
+/// A plain 401 or 403 with no code of Apple's is still the wrong password.
+#[tokio::test]
+async fn a_bare_refusal_is_still_a_wrong_password() {
+ for status in [401, 403] {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/signin/init"))
+ .respond_with(ResponseTemplate::new(status))
+ .mount(&server)
+ .await;
+ let dir = tempfile::tempdir().unwrap();
+ let mut client = client_for(&server, dir.path());
+ let err = client
+ .sign_in("[email protected]", "nope")
+ .await
+ .unwrap_err();
+ assert!(
+ matches!(err, quince_auth::Error::BadCredentials),
+ "{status}: {err:?}"
+ );
+ }
+}