//! What AutoFill asks of the core: which logins fill on the site asking, and //! what to offer once a person has typed a login into a site themselves. //! //! ```text //! rank(service, logins) the logins that fill there, the same host first, then the same site //! offer(site, username, password, kept) Save (new), Update (the password changed) or Nothing //! site_of_app(app) the site an Android app asks for when it names no web domain //! ``` //! //! The site rule is [`super::domain::site_match`]'s: registrable domain by //! the Public Suffix List, never a substring. A login's extra addresses (its //! payload's `urls`, one a line) match as its `site` does. //! //! **Pure**: passwords are compared here only after the caller opened them. use super::domain::{host_of, registrable, site_match, Match}; /// One kept login, as ranking reads it. pub struct Candidate<'a> { pub id: &'a str, pub name: &'a str, pub site: Option<&'a str>, /// Its other addresses, one a line (opened), when the caller has them. pub urls: Option<&'a str>, pub last_used_at: Option<&'a str>, } /// A login that fills on the site asking, and how closely. #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)] pub struct Ranked { pub id: String, #[serde(rename = "match")] pub matched: Match, } /// The logins that fill on `service` (a domain or an address), the same host /// before the same site, then the most recently used, then by name. pub fn rank(service: &str, logins: &[Candidate<'_>]) -> Vec { let mut found: Vec<(&Candidate<'_>, Match)> = logins .iter() .filter_map(|login| { let mut best: Option = None; let sites = login.site.into_iter().chain(login.urls.into_iter().flat_map(str::lines)).map(str::trim).filter(|site| !site.is_empty()); for site in sites { if let Some(matched) = site_match(site, service) { best = Some(best.map_or(matched, |held| held.min(matched))); } } best.map(|matched| (login, matched)) }) .collect(); found.sort_by(|(a, am), (b, bm)| { am.cmp(bm) .then_with(|| b.last_used_at.unwrap_or("").cmp(a.last_used_at.unwrap_or(""))) .then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())) .then_with(|| a.id.cmp(b.id)) }); found.into_iter().map(|(login, matched)| Ranked { id: login.id.to_string(), matched }).collect() } /// The site an Android app stands for when its fill request names no web /// domain (a native app's own sign-in form, not a page): its application id /// read back to front and taken to its registrable domain by the Public /// Suffix List, as [`rank`] matches a site. `com.invented.app` → /// `invented.com`, `uk.co.invented.bank` → `invented.co.uk`. `None` for an /// id that names no domain (`android`, `co.uk`), so nothing fills there. /// Moved here from the Android provider (`Sites.callerHosts`), which took /// only the first two labels and so read `uk.co.invented` as `co.uk`. pub fn site_of_app(app: &str) -> Option { let labels: Vec = app.trim().to_ascii_lowercase().split('.').map(|label| label.replace('_', "-")).collect(); if labels.len() < 2 || labels.iter().any(|label| label.is_empty() || !label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')) { return None; } let host: Vec<&str> = labels.iter().rev().map(String::as_str).collect(); registrable(&host.join(".")) } /// A kept login for the offer, its password opened. pub struct Kept<'a> { pub id: &'a str, pub site: Option<&'a str>, pub username: Option<&'a str>, pub password: &'a str, } /// What to offer after a manual fill. #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(tag = "offer", rename_all = "snake_case")] pub enum Offer { /// No login for this site and user name: offer to save one. Save { site: String }, /// The login is kept with another password: offer to update it. Update { id: String }, /// Already kept as typed: offer nothing. Nothing { id: String }, } /// The offer for a login typed into `site`: the same host's (or same site's) /// login with the same user name is the one updated; none is a new save. pub fn offer(site: &str, username: Option<&str>, password: &str, kept: &[Kept<'_>]) -> Option { let host = host_of(site)?; let user = username.map(str::trim).unwrap_or(""); let mut same: Vec<(&Kept<'_>, Match)> = kept .iter() .filter(|login| login.username.map(str::trim).unwrap_or("") == user) .filter_map(|login| login.site.and_then(|saved| site_match(saved, &host)).map(|matched| (login, matched))) .collect(); same.sort_by_key(|(_, matched)| *matched); Some(match same.first() { None => Offer::Save { site: host }, Some((login, _)) if login.password == password => Offer::Nothing { id: login.id.to_string() }, Some((login, _)) => Offer::Update { id: login.id.to_string() }, }) } #[cfg(test)] #[path = "autofill_tests.rs"] mod tests;