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.

Reject a second sign-in instead of queueing it, and prove sign-out

Yashwanth Kamireddib4070688c1ad
diff --git a/crates/quince-auth/tests/auth_flow.rs b/crates/quince-auth/tests/auth_flow.rs
index e1b74b2..dcca5a4 100644
--- a/crates/quince-auth/tests/auth_flow.rs
+++ b/crates/quince-auth/tests/auth_flow.rs
@@ -348,3 +348,54 @@ async fn a_successful_sign_in_clears_the_backoff() {
assert_eq!(client.session().consecutive_failures, 0);
assert!(client.session().retry_after.is_none());
}
+
+/// Signing out must leave nothing behind — a later client should find no session at all.
+#[tokio::test]
+async fn signing_out_forgets_everything() {
+ let server = MockServer::start().await;
+ mount_init(&server, 200).await;
+ mount_complete(&server, 409).await;
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/verify/trusteddevice/securitycode"))
+ .respond_with(ResponseTemplate::new(204))
+ .mount(&server)
+ .await;
+ mount_trust_and_login(&server).await;
+
+ let dir = tempfile::tempdir().unwrap();
+ {
+ let mut client = client_for(&server, dir.path());
+ client.sign_in("[email protected]", "hunter2").await.unwrap();
+ client.submit_two_factor("123456").await.unwrap();
+ assert!(client.session().trust_token.is_some());
+
+ client.sign_out().unwrap();
+ assert!(client.session().trust_token.is_none());
+ assert!(!client.has_stored_session());
+ }
+
+ // And nothing survives into a fresh client over the same directory.
+ let reopened = client_for(&server, dir.path());
+ assert!(!reopened.has_stored_session(), "sign-out must clear the stored session");
+ assert!(reopened.session().trust_token.is_none());
+ assert!(reopened.session().webservices.is_none());
+}
+
+/// Signing out must also clear a cooldown; otherwise a user who hit a backoff could be
+/// locked out of their own app with no way to reset it.
+#[tokio::test]
+async fn signing_out_clears_a_cooldown() {
+ let server = MockServer::start().await;
+ mount_init(&server, 429).await;
+
+ let dir = tempfile::tempdir().unwrap();
+ let mut client = client_for(&server, dir.path());
+ client.sign_in("[email protected]", "hunter2").await.unwrap_err();
+ assert!(client.session().retry_after.is_some());
+
+ client.sign_out().unwrap();
+ assert!(
+ client.session().retry_after.is_none(),
+ "a fresh start must not inherit the previous account's backoff"
+ );
+}
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 115cd35..8d1ff84 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -4,12 +4,27 @@
use quince_auth::{Client, SignInOutcome, TwoFactorOptions};
use serde::Serialize;
use std::path::PathBuf;
+use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tauri::{Manager, State};
use tokio::sync::Mutex;
-struct Auth(Arc<Mutex<Client>>);
+struct Auth {
+ client: Arc<Mutex<Client>>,
+ /// A sign-in is in flight. The mutex would serialise a second click rather than reject
+ /// it, so it would run a whole second SRP exchange and trigger another code push.
+ signing_in: AtomicBool,
+}
+
+/// Clears the in-flight flag however the guarded block exits.
+struct InFlight<'a>(&'a AtomicBool);
+
+impl Drop for InFlight<'_> {
+ fn drop(&mut self) {
+ self.0.store(false, Ordering::Release);
+ }
+}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
@@ -31,7 +46,7 @@ fn state_dir(app: &tauri::AppHandle) -> PathBuf {
#[tauri::command]
async fn auth_status(auth: State<'_, Auth>) -> UiResult<Status> {
- let client = auth.0.lock().await;
+ let client = auth.client.lock().await;
Ok(Status {
has_stored_session: client.has_stored_session(),
signed_in: client.session().webservices.is_some(),
@@ -41,7 +56,7 @@ async fn auth_status(auth: State<'_, Auth>) -> UiResult<Status> {
/// Try the stored session. `false` just means a sign-in is needed — not an error.
#[tauri::command]
async fn auth_resume(auth: State<'_, Auth>) -> UiResult<bool> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.resume().await.map_err(|e| e.to_string())
}
@@ -51,7 +66,14 @@ async fn auth_sign_in(
password: String,
auth: State<'_, Auth>,
) -> UiResult<&'static str> {
- let mut client = auth.0.lock().await;
+ // Reject rather than queue. A second attempt would mean a second code sent to the
+ // user's devices and another step toward Apple's rate limit.
+ if auth.signing_in.swap(true, Ordering::AcqRel) {
+ return Err("Already signing in.".into());
+ }
+ let _guard = InFlight(&auth.signing_in);
+
+ let mut client = auth.client.lock().await;
match client.sign_in(&apple_id, &password).await {
Ok(SignInOutcome::Complete) => Ok("complete"),
Ok(SignInOutcome::NeedsTwoFactor) => Ok("needs_two_factor"),
@@ -61,7 +83,7 @@ async fn auth_sign_in(
#[tauri::command]
async fn auth_submit_code(code: String, auth: State<'_, Auth>) -> UiResult<()> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.submit_two_factor(&code).await.map_err(|e| e.to_string())
}
@@ -69,19 +91,19 @@ async fn auth_submit_code(code: String, auth: State<'_, Auth>) -> UiResult<()> {
/// A `trustedDeviceCount` of zero means nothing was sent and waiting is pointless.
#[tauri::command]
async fn auth_two_factor_options(auth: State<'_, Auth>) -> UiResult<TwoFactorOptions> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.two_factor_options().await.map_err(|e| e.to_string())
}
#[tauri::command]
async fn auth_resend_code(auth: State<'_, Auth>) -> UiResult<()> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.resend_to_devices().await.map_err(|e| e.to_string())
}
#[tauri::command]
async fn auth_send_sms(phone_id: u64, auth: State<'_, Auth>) -> UiResult<()> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.send_sms(phone_id).await.map_err(|e| e.to_string())
}
@@ -91,20 +113,20 @@ async fn auth_submit_sms_code(
code: String,
auth: State<'_, Auth>,
) -> UiResult<()> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.submit_sms_code(phone_id, &code).await.map_err(|e| e.to_string())
}
#[tauri::command]
async fn auth_sign_out(auth: State<'_, Auth>) -> UiResult<()> {
- let mut client = auth.0.lock().await;
+ let mut client = auth.client.lock().await;
client.sign_out().map_err(|e| e.to_string())
}
/// Services Apple advertises for this account, for the sidebar.
#[tauri::command]
async fn account_services(auth: State<'_, Auth>) -> UiResult<Vec<String>> {
- let client = auth.0.lock().await;
+ let client = auth.client.lock().await;
Ok(client
.session()
.webservices
@@ -149,7 +171,7 @@ fn main() {
.setup(|app| {
let dir = state_dir(&app.handle());
let client = Arc::new(Mutex::new(Client::new(&dir)?));
- app.manage(Auth(Arc::clone(&client)));
+ app.manage(Auth { client: Arc::clone(&client), signing_in: AtomicBool::new(false) });
spawn_heartbeat(client);
Ok(())
})