model.rs (7231B)
1 //! What the greeter decides without drawing anything: whether a name or a password is acceptable, 2 //! the user name to suggest from a full name, and wiping a secret. Tested on its own. 3 4 /// Longer than any password; what is typed past it is dropped (as patina-lock does). 5 pub const MAX_PASSWORD: usize = 1024; 6 7 /// What AccountsService accepts as a user name, checked first so the person typing is told why. 8 pub fn check_username(name: &str) -> Result<(), &'static str> { 9 let mut chars = name.chars(); 10 match chars.next() { 11 None => return Err("Choose a user name"), 12 Some(c) if !c.is_ascii_lowercase() => { 13 return Err("The user name has to start with a lower case letter"); 14 } 15 Some(_) => {} 16 } 17 if name.len() > 31 { 18 return Err("The user name is too long"); 19 } 20 if !chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_') { 21 return Err("The user name can use lower case letters, digits, - and _"); 22 } 23 Ok(()) 24 } 25 26 pub fn check_full_name(name: &str) -> Result<(), &'static str> { 27 let name = name.trim(); 28 if name.is_empty() { 29 return Err("Enter your name"); 30 } 31 if name.chars().count() > 64 { 32 return Err("The name is too long"); 33 } 34 if name.chars().any(|c| c == ':' || c.is_control()) { 35 return Err("The name can't have a colon in it"); 36 } 37 Ok(()) 38 } 39 40 /// A user name to start from: the first word of the full name, lower case, letters and digits 41 /// only, starting with a letter. Empty when nothing usable is left, and then the person types one. 42 pub fn suggest_username(full_name: &str) -> String { 43 let first = full_name.split_whitespace().next().unwrap_or(""); 44 let mut name: String = first 45 .chars() 46 .filter(char::is_ascii_alphanumeric) 47 .map(|c| c.to_ascii_lowercase()) 48 .collect(); 49 while name.chars().next().is_some_and(|c| c.is_ascii_digit()) { 50 name.remove(0); 51 } 52 name.truncate(31); 53 name 54 } 55 56 /// Clears a secret, zeroing its bytes first, and keeps its room so typing again does not move it. 57 pub fn wipe(secret: &mut String) { 58 let mut bytes = std::mem::take(secret).into_bytes(); 59 bytes.fill(0); 60 std::hint::black_box(&bytes); 61 secret.reserve(MAX_PASSWORD); 62 } 63 64 /// A computer's name as hostnamed takes it for a static host name: one DNS label, so letters, 65 /// numbers and hyphens, not starting or ending with a hyphen, up to 63 characters. (Case is 66 /// folded before this is asked.) 67 pub fn check_hostname(name: &str) -> Result<(), &'static str> { 68 if name.is_empty() { 69 return Err("Give the computer a name"); 70 } 71 if name.len() > 63 { 72 return Err("Keep the name to 63 characters"); 73 } 74 if !name 75 .bytes() 76 .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') 77 { 78 return Err("Use letters, numbers and hyphens only"); 79 } 80 if name.starts_with('-') || name.ends_with('-') { 81 return Err("Start and end the name with a letter or number"); 82 } 83 Ok(()) 84 } 85 86 /// What is typed for the name, as it will be set: no spaces round it, lower case. 87 pub fn normalise_hostname(typed: &str) -> String { 88 typed.trim().to_lowercase() 89 } 90 91 /// The first-run form, in the order it is filled in. 92 #[derive(Default)] 93 pub struct SetupForm { 94 pub full_name: String, 95 pub username: String, 96 pub password: String, 97 pub confirm: String, 98 } 99 100 impl SetupForm { 101 /// The first thing wrong with it, or `None` when the account can be made. 102 pub fn problem(&self) -> Option<&'static str> { 103 if let Err(e) = check_full_name(&self.full_name) { 104 return Some(e); 105 } 106 if let Err(e) = check_username(&self.username) { 107 return Some(e); 108 } 109 if self.password.is_empty() { 110 return Some("Choose a password"); 111 } 112 if self.password != self.confirm { 113 return Some("The passwords don't match"); 114 } 115 None 116 } 117 118 pub fn wipe(&mut self) { 119 wipe(&mut self.password); 120 wipe(&mut self.confirm); 121 } 122 } 123 124 #[cfg(test)] 125 mod tests { 126 use super::*; 127 128 #[test] 129 fn user_names_follow_what_accountsservice_takes() { 130 for ok in ["alice", "a", "bob_2", "mary-jane", "x1"] { 131 assert_eq!(check_username(ok), Ok(()), "{ok}"); 132 } 133 for bad in [ 134 "", 135 "Alice", 136 "1abc", 137 "-x", 138 "a b", 139 "a:b", 140 "ålice", 141 "root.", 142 &"a".repeat(32), 143 ] { 144 assert!(check_username(bad).is_err(), "{bad:?}"); 145 } 146 assert_eq!(check_username(&"a".repeat(31)), Ok(())); 147 } 148 149 #[test] 150 fn host_names_are_single_dns_labels() { 151 for ok in [ 152 "sectorf", 153 "a", 154 "alices-laptop", 155 "x1", 156 "9lives", 157 &"a".repeat(63), 158 ] { 159 assert_eq!(check_hostname(ok), Ok(()), "{ok}"); 160 } 161 for bad in [ 162 "", 163 "-a", 164 "a-", 165 "my laptop", 166 "a_b", 167 "a.b", 168 "Å", 169 &"a".repeat(64), 170 ] { 171 assert!(check_hostname(bad).is_err(), "{bad:?}"); 172 } 173 assert_eq!(normalise_hostname(" Alices-Laptop "), "alices-laptop"); 174 } 175 176 #[test] 177 fn full_names_are_checked() { 178 assert_eq!(check_full_name("Alice Example"), Ok(())); 179 assert_eq!(check_full_name("Åsa Öberg"), Ok(())); 180 assert!(check_full_name(" ").is_err()); 181 assert!(check_full_name("A:B").is_err()); 182 assert!(check_full_name("tab\there").is_err()); 183 assert!(check_full_name(&"x".repeat(65)).is_err()); 184 } 185 186 #[test] 187 fn a_user_name_is_suggested_from_the_first_word() { 188 assert_eq!(suggest_username("Alice Example"), "alice"); 189 assert_eq!(suggest_username(" mary-jane smith"), "maryjane"); 190 assert_eq!(suggest_username("007 Bond"), ""); 191 assert_eq!(suggest_username("42nd Street"), "nd"); 192 assert_eq!(suggest_username(""), ""); 193 assert_eq!(suggest_username(&"x".repeat(50)).len(), 31); 194 for name in ["Alice Example", "bob", "Dr. Who"] { 195 let s = suggest_username(name); 196 assert!( 197 s.is_empty() || check_username(&s).is_ok(), 198 "{name} -> {s:?}" 199 ); 200 } 201 } 202 203 #[test] 204 fn wiping_empties_and_keeps_room() { 205 let mut password = String::from("hunter2"); 206 wipe(&mut password); 207 assert!(password.is_empty()); 208 assert!(password.capacity() >= MAX_PASSWORD); 209 } 210 211 #[test] 212 fn the_form_names_the_first_problem() { 213 let mut form = SetupForm::default(); 214 assert_eq!(form.problem(), Some("Enter your name")); 215 form.full_name = "Alice Example".into(); 216 assert_eq!(form.problem(), Some("Choose a user name")); 217 form.username = "alice".into(); 218 assert_eq!(form.problem(), Some("Choose a password")); 219 form.password = "secret".into(); 220 assert_eq!(form.problem(), Some("The passwords don't match")); 221 form.confirm = "secret".into(); 222 assert_eq!(form.problem(), None); 223 } 224 }