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 cooldown from a clock that ran ahead is over

Yashwanth Kamireddi2c074e2395be
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rs
index 426beca..cd02061 100644
--- a/crates/quince-auth/src/lib.rs
+++ b/crates/quince-auth/src/lib.rs
@@ -1041,13 +1041,8 @@ impl Client {
) -> Result<SignInOutcome> {
// Refuse before touching Apple. Hammering sign-in is what gets an account
// temporarily locked, and an impatient user clicking twice should not risk that.
- if let Some(until) = self.session.retry_after {
- let now = unix_now();
- if now < until {
- return Err(Error::CoolingDown {
- seconds: until - now,
- });
- }
+ if let Some(seconds) = session::wait_left(self.session.retry_after, unix_now()) {
+ return Err(Error::CoolingDown { seconds });
}
// Start clean. An interrupted previous attempt would otherwise announce a
diff --git a/crates/quince-auth/src/session.rs b/crates/quince-auth/src/session.rs
index 5bb9aaf..6507c43 100644
--- a/crates/quince-auth/src/session.rs
+++ b/crates/quince-auth/src/session.rs
@@ -153,6 +153,18 @@ pub fn backoff_seconds(failures: u32) -> u64 {
}
}
+/// How long is left of a cooldown that runs until `until`, at `now`; `None` when it is over.
+///
+/// Never longer than the longest wait [`backoff_seconds`] ever sets. One that seems to be is
+/// over: it was set while the clock ran ahead — a computer that dual-boots, its hardware clock
+/// in local time (+5:30 and the like) — and once the clock was put right, sign-in stayed
+/// refused for hours, saying to wait, with nothing anyone could do about it.
+#[must_use]
+pub fn wait_left(until: Option<u64>, now: u64) -> Option<u64> {
+ let left = until?.checked_sub(now).filter(|s| *s > 0)?;
+ (left <= backoff_seconds(u32::MAX)).then_some(left)
+}
+
pub fn unix_now() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
diff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rs
index 084746a..a064528 100644
--- a/crates/quince-auth/tests/auth_flow.rs
+++ b/crates/quince-auth/tests/auth_flow.rs
@@ -1552,3 +1552,71 @@ async fn a_trusted_computer_signing_in_again_is_asked_for_no_code() {
"and the trust is kept as it was"
);
}
+
+/// A cooldown set while the clock ran ahead — a dual-boot computer whose hardware clock keeps
+/// local time, +5:30 — seemed to have hours left once the clock was put right, and sign-in
+/// stayed refused all that time. No wait Skykeep sets is longer than ten minutes, so one that
+/// seems longer is over; a real one still holds, and still spares Apple.
+#[test]
+fn a_cooldown_from_a_clock_that_ran_ahead_is_over() {
+ use quince_auth::session::wait_left;
+ let now = 1_800_000_000;
+ assert_eq!(
+ wait_left(Some(now + 30), now),
+ Some(30),
+ "a real wait holds"
+ );
+ assert_eq!(
+ wait_left(Some(now + 600), now),
+ Some(600),
+ "the longest one Skykeep sets"
+ );
+ assert_eq!(
+ wait_left(Some(now + 5 * 3600 + 30 * 60), now),
+ None,
+ "five and a half hours ahead: over"
+ );
+ assert_eq!(wait_left(Some(now - 1), now), None, "run out");
+ assert_eq!(wait_left(Some(now), now), None);
+ assert_eq!(wait_left(None, now), None);
+}
+
+#[tokio::test]
+async fn sign_in_is_not_held_up_by_a_cooldown_from_a_clock_that_ran_ahead() {
+ let server = MockServer::start().await;
+ mount_init(&server, 200).await;
+ mount_complete(&server, 409).await;
+ Mock::given(method("PUT"))
+ .and(path("/appleauth/auth/verify/trusteddevice/securitycode"))
+ .respond_with(ResponseTemplate::new(202))
+ .mount(&server)
+ .await;
+ let dir = tempfile::tempdir().unwrap();
+ let store = Store::file(dir.path()).unwrap();
+ let now = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_secs();
+ store
+ .save_cooldown(&quince_auth::session::Cooldown {
+ account: Some(quince_auth::session::account_fingerprint(
+ )),
+ consecutive_failures: 3,
+ retry_after: Some(now + 5 * 3600 + 30 * 60),
+ })
+ .unwrap();
+ let mut client = client_for(&server, dir.path());
+ assert_eq!(
+ client
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap(),
+ SignInOutcome::NeedsTwoFactor,
+ "the sign-in goes ahead"
+ );
+ assert_eq!(
+ requests_to(&server.received_requests().await.unwrap(), "/signin/init").len(),
+ 1
+ );
+}