Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Phase 0 passes: one sign-in reaches every service
Yashwanth Kamireddi3ce304940fca
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rsindex 14bd18b..16463f9 100644--- a/crates/quince-auth/src/lib.rs+++ b/crates/quince-auth/src/lib.rs@@ -22,10 +22,55 @@ use session::{Session, Store, CAPTURED_HEADERS};use std::path::Path;use std::sync::Arc;-const AUTH_ROOT: &str = "https://idmsa.apple.com";-const AUTH: &str = "https://idmsa.apple.com/appleauth/auth";-const SETUP: &str = "https://setup.icloud.com/setup/ws/1";-const HOME: &str = "https://www.icloud.com";+/// Base URLs for Apple's auth and setup services.+///+/// Injectable so the sign-in flow can be exercised against a mock server, and so Apple's+/// China domains are a constructor rather than a fork.+#[derive(Clone, Debug)]+pub struct Endpoints {+ /// Origin for the SRP sign-in calls.+ pub auth_root: String,+ /// `{auth_root}/appleauth/auth`+ pub auth: String,+ /// `.../setup/ws/1`+ pub setup: String,+ /// Origin for everything that is not SRP sign-in.+ pub home: String,+}++impl Default for Endpoints {+ fn default() -> Self {+ Self {+ auth_root: "https://idmsa.apple.com".into(),+ auth: "https://idmsa.apple.com/appleauth/auth".into(),+ setup: "https://setup.icloud.com/setup/ws/1".into(),+ home: "https://www.icloud.com".into(),+ }+ }+}++impl Endpoints {+ /// Accounts hosted in mainland China.+ pub fn china() -> Self {+ Self {+ auth_root: "https://idmsa.apple.com.cn".into(),+ auth: "https://idmsa.apple.com.cn/appleauth/auth".into(),+ setup: "https://setup.icloud.com.cn/setup/ws/1".into(),+ home: "https://www.icloud.com.cn".into(),+ }+ }++ /// Point every service at one base URL. Test seam.+ pub fn at(base: &str) -> Self {+ let base = base.trim_end_matches('/');+ Self {+ auth_root: base.into(),+ auth: format!("{base}/appleauth/auth"),+ setup: format!("{base}/setup/ws/1"),+ home: base.into(),+ }+ }+}/// iCloud web's first-party OAuth widget key. Not a secret — it is public in the web client.const WIDGET_KEY: &str = "d39ba9916b7251055b22c7f910e2ea796ee65e98b2ddecea8f5dde8d9d1a815d";@@ -118,7 +163,12 @@ pub enum SignInOutcome {/// OAuth headers every idmsa call carries. Deliberately sets no Origin: the HTTP client/// defaults to the icloud.com origin, which is what the verify, trust and phone endpoints/// expect. Only SRP overrides it — see [`with_srp_origin`].-fn base_oauth_headers(client_id: &str, scnt: Option<&str>, session_id: Option<&str>) -> HeaderMap {+fn base_oauth_headers(+ client_id: &str,+ home: &str,+ scnt: Option<&str>,+ session_id: Option<&str>,+) -> HeaderMap {let mut h = HeaderMap::new();let mut put = |k: &'static str, v: &str| {if let Ok(val) = HeaderValue::from_str(v) {@@ -129,7 +179,7 @@ fn base_oauth_headers(client_id: &str, scnt: Option<&str>, session_id: Option<&sput("content-type", "application/json");put("x-apple-oauth-client-id", WIDGET_KEY);put("x-apple-oauth-client-type", "firstPartyAuth");- put("x-apple-oauth-redirect-uri", HOME);+ put("x-apple-oauth-redirect-uri", home);put("x-apple-oauth-require-grant-code", "true");put("x-apple-oauth-response-mode", "web_message");put("x-apple-oauth-response-type", "code");@@ -146,11 +196,11 @@ fn base_oauth_headers(client_id: &str, scnt: Option<&str>, session_id: Option<&s/// `/signin/init` and `/signin/complete` are served from idmsa and reject the icloud.com/// origin. Applying this anywhere else earns a 409.-fn with_srp_origin(mut h: HeaderMap) -> HeaderMap {- if let Ok(v) = HeaderValue::from_str(AUTH_ROOT) {+fn with_srp_origin(mut h: HeaderMap, auth_root: &str) -> HeaderMap {+ if let Ok(v) = HeaderValue::from_str(auth_root) {h.insert("origin", v);}- if let Ok(v) = HeaderValue::from_str(&format!("{AUTH_ROOT}/")) {+ if let Ok(v) = HeaderValue::from_str(&format!("{auth_root}/")) {h.insert("referer", v);}h@@ -161,11 +211,17 @@ pub struct Client {jar: Arc<CookieStoreMutex>,store: Store,session: Session,+ endpoints: Endpoints,}impl Client {pub fn new(state_dir: &Path) -> Result<Self> {- let store = Store::new(state_dir)?;+ Self::with(state_dir, Endpoints::default(), Store::keyring(state_dir)?)+ }++ /// Build a client against specific endpoints and a specific secret store.+ pub fn with(state_dir: &Path, endpoints: Endpoints, store: Store) -> Result<Self> {+ std::fs::create_dir_all(state_dir)?;let session = store.load();let jar = {@@ -180,11 +236,15 @@ impl Client {Arc::new(CookieStoreMutex::new(inner))};- // Default origin is icloud.com — what setup.icloud.com and the service endpoints expect.- // The idmsa SRP endpoints want their own origin, and override it per request.+ // Default origin is icloud.com — what setup.icloud.com and the service endpoints+ // expect. The idmsa SRP endpoints want their own origin and override it per request.let mut defaults = HeaderMap::new();- defaults.insert("origin", HeaderValue::from_static(HOME));- defaults.insert("referer", HeaderValue::from_static("https://www.icloud.com/"));+ if let Ok(v) = HeaderValue::from_str(&endpoints.home) {+ defaults.insert("origin", v);+ }+ if let Ok(v) = HeaderValue::from_str(&format!("{}/", endpoints.home)) {+ defaults.insert("referer", v);+ }defaults.insert("accept-language", HeaderValue::from_static("en-US,en;q=0.9"));let http = HttpClient::builder()@@ -193,7 +253,7 @@ impl Client {.default_headers(defaults).build()?;- Ok(Self { http, jar, store, session })+ Ok(Self { http, jar, store, session, endpoints })}pub fn session(&self) -> &Session {@@ -236,7 +296,12 @@ impl Client {fn auth_headers(&mut self) -> HeaderMap {let client_id = self.session.client_id();- base_oauth_headers(&client_id, self.session.scnt.as_deref(), self.session.session_id.as_deref())+ base_oauth_headers(+ &client_id,+ &self.endpoints.home,+ self.session.scnt.as_deref(),+ self.session.session_id.as_deref(),+ )}/// Headers for `/signin/init` and `/signin/complete` only.@@ -244,7 +309,8 @@ impl Client {/// Those two are served from idmsa and reject the icloud.com origin. Every other auth/// endpoint — verify, trust, phone — wants the icloud.com origin the client defaults to.fn srp_headers(&mut self) -> HeaderMap {- with_srp_origin(self.auth_headers())+ let h = self.auth_headers();+ with_srp_origin(h, &self.endpoints.auth_root)}/// Headers for `/signin/init` specifically.@@ -253,7 +319,8 @@ impl Client {/// sending leftovers from an abandoned challenge is a conflict.fn srp_init_headers(&mut self) -> HeaderMap {let client_id = self.session.client_id();- with_srp_origin(base_oauth_headers(&client_id, None, None))+ let h = base_oauth_headers(&client_id, &self.endpoints.home, None, None);+ with_srp_origin(h, &self.endpoints.auth_root)}/// Drop transient challenge state, keeping what identifies this install.@@ -296,7 +363,7 @@ impl Client {async fn validate(&mut self) -> Result<Value> {let r = self.http- .post(format!("{SETUP}/validate"))+ .post(format!("{}/validate", self.endpoints.setup)).header("content-type", "application/json").body("null").send()@@ -341,7 +408,7 @@ impl Client {let headers = self.srp_init_headers();let r = self.http- .post(format!("{AUTH}/signin/init"))+ .post(format!("{}/signin/init", self.endpoints.auth)).headers(headers).body(json!({@@ -404,7 +471,7 @@ impl Client {let headers = self.srp_headers();let r = self.http- .post(format!("{AUTH}/signin/complete"))+ .post(format!("{}/signin/complete", self.endpoints.auth)).query(&[("isRememberMeEnabled", "true")]).headers(headers).body(body.to_string())@@ -439,7 +506,7 @@ impl Client {pub async fn two_factor_options(&mut self) -> Result<TwoFactorOptions> {let mut headers = self.auth_headers();headers.insert("accept", HeaderValue::from_static("application/json"));- let r = self.http.get(AUTH).headers(headers).send().await?;+ let r = self.http.get(&self.endpoints.auth).headers(headers).send().await?;self.capture(&r);let text = r.text().await.unwrap_or_default();let v: Value = serde_json::from_str(&text).unwrap_or(Value::Null);@@ -475,7 +542,7 @@ impl Client {headers.insert("accept", HeaderValue::from_static("application/json"));let r = self.http- .put(format!("{AUTH}/verify/trusteddevice/securitycode"))+ .put(format!("{}/verify/trusteddevice/securitycode", self.endpoints.auth)).headers(headers).send().await?;@@ -493,7 +560,7 @@ impl Client {headers.insert("accept", HeaderValue::from_static("application/json"));let r = self.http- .put(format!("{AUTH}/verify/phone"))+ .put(format!("{}/verify/phone", self.endpoints.auth)).headers(headers).body(json!({ "phoneNumber": { "id": phone_id }, "mode": "sms" }).to_string()).send()@@ -512,7 +579,7 @@ impl Client {headers.insert("accept", HeaderValue::from_static("application/json"));let r = self.http- .post(format!("{AUTH}/verify/phone/securitycode"))+ .post(format!("{}/verify/phone/securitycode", self.endpoints.auth)).headers(headers).body(json!({@@ -553,7 +620,7 @@ impl Client {headers.insert("accept", HeaderValue::from_static("application/json"));let r = self.http- .post(format!("{AUTH}/verify/trusteddevice/securitycode"))+ .post(format!("{}/verify/trusteddevice/securitycode", self.endpoints.auth)).headers(headers).body(json!({ "securityCode": { "code": code } }).to_string()).send()@@ -589,7 +656,7 @@ impl Client {/// `GET /2sv/trust` — yields the trust token. Without it, sign-in wouldn't outlive the process.async fn trust(&mut self) -> Result<()> {let headers = self.auth_headers();- let r = self.http.get(format!("{AUTH}/2sv/trust")).headers(headers).send().await?;+ let r = self.http.get(format!("{}/2sv/trust", self.endpoints.auth)).headers(headers).send().await?;self.capture(&r);let status = r.status();let body = r.text().await.unwrap_or_default();@@ -612,7 +679,7 @@ impl Client {});let r = self.http- .post(format!("{SETUP}/accountLogin"))+ .post(format!("{}/accountLogin", self.endpoints.setup)).header("content-type", "application/json").body(body.to_string()).send()@@ -646,24 +713,27 @@ impl Client {mod tests {use super::*;+ const HOME_T: &str = "https://www.icloud.com";+ const ROOT_T: &str = "https://idmsa.apple.com";+/// Apple answers 409 when the idmsa origin is sent to an endpoint that expects the/// icloud.com one. Only the two SRP calls may carry it.#[test]fn srp_origin_is_scoped_to_sign_in() {- let base = base_oauth_headers("auth-test", None, None);+ let base = base_oauth_headers("auth-test", HOME_T, None, None);assert!(base.get("origin").is_none(),"base headers must not pin an Origin; the client default (icloud.com) applies");- let srp = with_srp_origin(base_oauth_headers("auth-test", None, None));- assert_eq!(srp.get("origin").unwrap(), AUTH_ROOT);- assert_eq!(srp.get("referer").unwrap(), &format!("{AUTH_ROOT}/")[..]);+ let srp = with_srp_origin(base_oauth_headers("auth-test", HOME_T, None, None), ROOT_T);+ assert_eq!(srp.get("origin").unwrap(), ROOT_T);+ assert_eq!(srp.get("referer").unwrap(), &format!("{ROOT_T}/")[..]);}#[test]fn session_headers_are_forwarded_when_present() {- let h = base_oauth_headers("auth-test", Some("scnt-value"), Some("session-value"));+ let h = base_oauth_headers("auth-test", HOME_T, Some("scnt-value"), Some("session-value"));assert_eq!(h.get("scnt").unwrap(), "scnt-value");assert_eq!(h.get("x-apple-id-session-id").unwrap(), "session-value");}@@ -671,16 +741,16 @@ mod tests {/// Sign-in must not advertise a session that does not exist yet.#[test]fn init_headers_omit_challenge_state() {- let stale = base_oauth_headers("auth-test", Some("stale-scnt"), Some("stale-session"));+ let stale = base_oauth_headers("auth-test", HOME_T, Some("stale-scnt"), Some("stale-session"));assert!(stale.get("scnt").is_some(), "sanity: the helper does forward them");- let init = with_srp_origin(base_oauth_headers("auth-test", None, None));+ let init = with_srp_origin(base_oauth_headers("auth-test", HOME_T, None, None), ROOT_T);assert!(init.get("scnt").is_none(), "/signin/init must not send scnt");assert!(init.get("x-apple-id-session-id").is_none(),"/signin/init must not send a session id");- assert_eq!(init.get("origin").unwrap(), AUTH_ROOT);+ assert_eq!(init.get("origin").unwrap(), ROOT_T);}#[test]diff --git a/crates/quince-auth/src/session.rs b/crates/quince-auth/src/session.rsindex 6fd141b..a6eaad1 100644--- a/crates/quince-auth/src/session.rs+++ b/crates/quince-auth/src/session.rs@@ -85,18 +85,35 @@ impl Session {}}-/// Where the session is kept.+/// Where secrets are kept.///-/// Secrets go to the OS keyring (gnome-keyring / kwallet via the Secret Service API). Cookies-/// are not independently sensitive once the tokens are protected, so they sit in a state file.+/// Normally the OS keyring (gnome-keyring / kwallet over the Secret Service API). Tests use+/// a file instead, so a test run never touches the user's real keyring.+enum Backend {+ Keyring,+ File(PathBuf),+}+pub struct Store {cookie_path: PathBuf,+ backend: Backend,}impl Store {- pub fn new(state_dir: &Path) -> std::io::Result<Self> {+ /// Production: secrets in the OS keyring.+ pub fn keyring(state_dir: &Path) -> std::io::Result<Self> {+ std::fs::create_dir_all(state_dir)?;+ Ok(Self { cookie_path: state_dir.join("cookies.json"), backend: Backend::Keyring })+ }++ /// Tests only: secrets in a file under `state_dir`. Never use for real accounts —+ /// it writes tokens to disk in the clear.+ pub fn file(state_dir: &Path) -> std::io::Result<Self> {std::fs::create_dir_all(state_dir)?;- Ok(Self { cookie_path: state_dir.join("cookies.json") })+ Ok(Self {+ cookie_path: state_dir.join("cookies.json"),+ backend: Backend::File(state_dir.join("session.json")),+ })}pub fn cookie_path(&self) -> &Path {@@ -109,28 +126,44 @@ impl Store {/// Load the stored session. A missing entry is normal (first run), not an error.pub fn load(&self) -> Session {- let Ok(entry) = Self::entry() else {- return Session::default();+ let json = match &self.backend {+ Backend::Keyring => match Self::entry().and_then(|e| e.get_password()) {+ Ok(j) => j,+ Err(_) => return Session::default(),+ },+ Backend::File(path) => match std::fs::read_to_string(path) {+ Ok(j) => j,+ Err(_) => return Session::default(),+ },};- match entry.get_password() {- Ok(json) => serde_json::from_str(&json).unwrap_or_default(),- Err(_) => Session::default(),- }+ serde_json::from_str(&json).unwrap_or_default()}pub fn save(&self, session: &Session) -> Result<(), crate::Error> {let json = serde_json::to_string(session)?;- Self::entry()- .and_then(|e| e.set_password(&json))- .map_err(|e| crate::Error::Keyring(e.to_string()))+ match &self.backend {+ Backend::Keyring => Self::entry()+ .and_then(|e| e.set_password(&json))+ .map_err(|e| crate::Error::Keyring(e.to_string())),+ Backend::File(path) => Ok(std::fs::write(path, json)?),+ }}/// Forget everything. Used by sign-out, and when Apple rejects the stored session for good.pub fn clear(&self) -> Result<(), crate::Error> {- if let Ok(entry) = Self::entry() {- match entry.delete_credential() {- Ok(()) | Err(keyring::Error::NoEntry) => {}- Err(e) => return Err(crate::Error::Keyring(e.to_string())),+ match &self.backend {+ Backend::Keyring => {+ if let Ok(entry) = Self::entry() {+ match entry.delete_credential() {+ Ok(()) | Err(keyring::Error::NoEntry) => {}+ Err(e) => return Err(crate::Error::Keyring(e.to_string())),+ }+ }+ }+ Backend::File(path) => {+ if path.exists() {+ std::fs::remove_file(path)?;+ }}}if self.cookie_path.exists() {diff --git a/probe/src/main.rs b/probe/src/main.rsindex 608bfa3..c19f014 100644--- a/probe/src/main.rs+++ b/probe/src/main.rs@@ -66,6 +66,45 @@ fn summarise(v: &Value) -> String {parts.join(" ")}+async fn probe_post(+ client: &Client,+ label: &'static str,+ url: String,+ body: Value,+) -> Outcome {+ let session = client.session();+ let q: Vec<(String, String)> = vec![+ ("clientBuildNumber".into(), "2523Project28".into()),+ ("clientMasteringNumber".into(), "2523B28".into()),+ ("clientId".into(), session.client_id.clone().unwrap_or_default()),+ ("dsid".into(), session.dsid.clone().unwrap_or_default()),+ ];+ match client.http().post(&url).query(&q).json(&body).send().await {+ Err(e) => Outcome { label, ok: false, status: 0, detail: format!("transport: {e}") },+ Ok(r) => {+ let status = r.status();+ let text = r.text().await.unwrap_or_default();+ let parsed: Option<Value> = serde_json::from_str(&text).ok();+ let detail = match (&parsed, status.is_success()) {+ // Drive answers with an array of folders; summarise the first.+ (Some(Value::Array(items)), true) => {+ let children = items+ .first()+ .and_then(|f| f.get("items"))+ .and_then(|i| i.as_array())+ .map(|a| a.len())+ .unwrap_or(0);+ format!("folders={} items={}", items.len(), children)+ }+ (Some(v), true) => summarise(v),+ (_, true) => format!("{} bytes", text.len()),+ _ => text.chars().take(160).collect::<String>().replace('\n', " "),+ };+ Outcome { label, ok: status.is_success(), status: status.as_u16(), detail }+ }+ }+}+async fn probe_get(client: &Client,label: &'static str,@@ -277,8 +316,12 @@ async fn main() -> Result<()> {&[("clientVersion", "4.0"), ("lang", "en-us"), ("usertz", "UTC")]).await);}if let Some(u) = session.service_url("drivews") {- results.push(probe_get(&client, "Drive item details",- format!("{u}/retrieveItemDetailsInFolders"), &[]).await);+ results.push(probe_post(&client, "Drive item details",+ format!("{u}/retrieveItemDetailsInFolders"),+ serde_json::json!([{+ "drivewsid": "FOLDER::com.apple.CloudDocs::root",+ "partialData": false,+ }])).await);}if let Some(u) = session.service_url("findme") {results.push(probe_get(&client, "Find My initClient",diff --git a/src/screens/Library.tsx b/src/screens/Library.tsxindex cb60c60..12a0316 100644--- a/src/screens/Library.tsx+++ b/src/screens/Library.tsx@@ -6,25 +6,38 @@ import { api, messageOf } from "../lib/ipc";const IMPLEMENTED = new Set<string>();const NAMES: Record<string, string> = {+ photos: "Photos",+ sharedlibrary: "Shared library",+ sharedstreams: "Shared albums",+ photosupload: "Photo upload",calendar: "Calendar",contacts: "Contacts",+ contactsweb: "Contacts (web)",reminders: "Reminders",- ckdatabasews: "Photos & Notes",+ notes: "Notes",+ mail: "Mail",+ premiummailsettings: "Mail settings",drivews: "iCloud Drive",docws: "Documents",+ ubiquity: "Drive (legacy)",+ archivews: "Archives",+ ckdatabasews: "CloudKit data",+ cksharews: "CloudKit sharing",+ ckdeviceservice: "CloudKit devices",findme: "Find My",- photos: "Photos",- mail: "Mail",keyvalue: "App data",- settings: "Account settings",account: "Account",- premiummailsettings: "Mail settings",- iworkthumbnailws: "iWork previews",- uploadimagews: "Photo upload",- streams: "Shared albums",+ settings: "Account settings",iwmb: "iWork",- ubiquity: "Legacy Drive",- fmf: "Find My Friends",+ iworkexportws: "iWork export",+ iworkthumbnailws: "iWork previews",+ uploadimagews: "Image upload",+ geows: "Location",+ push: "Push",+ mpp: "Private Relay",+ mcc: "Account services",+ mccgateway: "Account gateway",+ developerapi: "Developer",schoolwork: "Schoolwork",};