| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::{filters, Error, Page, Shell}; |
| 5 | | use crate::auth::{self, totp, Viewer, ViewerAccount}; |
| 6 | | use crate::db::{self, accounts}; |
| 7 | | use crate::Shared; |
| 8 | | use askama::Template; |
| 9 | | use axum::extract::{Form, Multipart, Path, Query, State}; |
| 10 | | use axum::http::{header, HeaderMap, StatusCode}; |
| 11 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 12 | | use oxy_core::identity::{Identity, Signature}; |
| 13 | | use serde::Deserialize; |
| 14 | |
|
| 15 | |
|
| 16 | 401× | pub fn require(viewer: &Viewer, shell: &Shell) -> Result<ViewerAccount, Error> { |
| 17 | 401× | viewer.0.clone().ok_or_else(|| Error::redirect(format!("/login?next={}", urlencode(&shell.path)))) |
| 18 | 401× | } |
| 19 | |
|
| 20 | 368× | pub fn urlencode(s: &str) -> String { |
| 21 | 368× | let mut out = String::new(); |
| 22 | 8229× | for b in s.bytes() { |
| 23 | 8229× | match b { |
| 24 | 7374× | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => out.push(b as char), |
| 25 | 855× | _ => out.push_str(&format!("%{b:02X}")), |
| 26 | | } |
| 27 | | } |
| 28 | 368× | out |
| 29 | 368× | } |
| 30 | |
|
| 31 | 440× | pub fn csrf(viewer: &Viewer, token: &str, shell: &Shell) -> Result<(), Error> { |
| 32 | 440× | if viewer.csrf_ok(token) { |
| 33 | 437× | Ok(()) |
| 34 | | } else { |
| 35 | 3× | Err(Error::bad(shell.clone(), "This form has expired; reload the page and try again.")) |
| 36 | | } |
| 37 | 440× | } |
| 38 | |
|
| 39 | | #[derive(Template)] |
| 40 | | #[template(path = "settings.html")] |
| 41 | | pub struct SettingsPage { |
| 42 | | shell: Shell, |
| 43 | | me: accounts::Account, |
| 44 | | notice: Option<String>, |
| 45 | | error: Option<String>, |
| 46 | | |
| 47 | | theme: &'static str, |
| 48 | | themes: &'static [(&'static str, &'static str)], |
| 49 | | |
| 50 | | backdrop: &'static str, |
| 51 | | backdrops: &'static [(&'static str, &'static str)], |
| 52 | | |
| 53 | | mail: bool, |
| 54 | | } |
| 55 | |
|
| 56 | |
|
| 57 | | const THEMES: &[(&str, &str)] = &[("light", "Light"), ("dark", "Dark"), ("auto", "Match the system")]; |
| 58 | |
|
| 59 | |
|
| 60 | | const BACKDROPS: &[(&str, &str)] = &[("moving", "Moving"), ("still", "Still"), ("off", "Off")]; |
| 61 | |
|
| 62 | |
|
| 63 | 25× | fn backdrop_of(headers: &HeaderMap) -> &'static str { |
| 64 | 25× | let v = auth::viewer::cookie(headers, "backdrop").unwrap_or_default(); |
| 65 | 75× | BACKDROPS.iter().map(|(t, _)| *t).find(|t| *t == v).unwrap_or("moving") |
| 66 | 25× | } |
| 67 | |
|
| 68 | |
|
| 69 | 25× | fn theme_of(headers: &HeaderMap) -> &'static str { |
| 70 | 25× | let v = auth::viewer::cookie(headers, "theme").unwrap_or_default(); |
| 71 | 74× | THEMES.iter().map(|(t, _)| *t).find(|t| *t == v).unwrap_or("light") |
| 72 | 25× | } |
| 73 | |
|
| 74 | | impl SettingsPage { |
| 75 | 25× | fn new(shell: Shell, me: accounts::Account, headers: &HeaderMap) -> SettingsPage { |
| 76 | 25× | SettingsPage { |
| 77 | 25× | shell, |
| 78 | 25× | me, |
| 79 | 25× | notice: None, |
| 80 | 25× | error: None, |
| 81 | 25× | theme: theme_of(headers), |
| 82 | 25× | themes: THEMES, |
| 83 | 25× | backdrop: backdrop_of(headers), |
| 84 | 25× | backdrops: BACKDROPS, |
| 85 | 25× | mail: false, |
| 86 | 25× | } |
| 87 | 25× | } |
| 88 | |
|
| 89 | 25× | fn with_mail(mut self, app: &Shared) -> Self { |
| 90 | 25× | self.mail = crate::mail::enabled(app); |
| 91 | 25× | self |
| 92 | 25× | } |
| 93 | |
|
| 94 | 8× | fn notice(mut self, n: impl Into<String>) -> Self { |
| 95 | 8× | self.notice = Some(n.into()); |
| 96 | 8× | self |
| 97 | 8× | } |
| 98 | |
|
| 99 | 4× | fn error(mut self, e: impl Into<String>) -> Self { |
| 100 | 4× | self.error = Some(e.into()); |
| 101 | 4× | self |
| 102 | 4× | } |
| 103 | | } |
| 104 | |
|
| 105 | 18× | pub async fn profile(State(app): State<Shared>, viewer: Viewer, headers: HeaderMap) -> Result<Page<SettingsPage>, Error> { |
| 106 | 18× | let shell = Shell::new(&app, "/settings", &viewer); |
| 107 | 18× | let me = require(&viewer, &shell)?; |
| 108 | 13× | Ok(Page(SettingsPage::new(shell, me.account, &headers).with_mail(&app))) |
| 109 | 18× | } |
| 110 | |
|
| 111 | |
|
| 112 | 4× | pub async fn avatar( |
| 113 | 4× | State(app): State<Shared>, |
| 114 | 4× | viewer: Viewer, |
| 115 | 4× | headers: HeaderMap, |
| 116 | 4× | multipart: Multipart, |
| 117 | 4× | ) -> Result<Page<SettingsPage>, Error> { |
| 118 | 4× | let shell = Shell::new(&app, "/settings", &viewer); |
| 119 | 4× | let me = require(&viewer, &shell)?; |
| 120 | 4× | let upload = super::avatar::read_upload(multipart).await; |
| 121 | 4× | csrf(&viewer, &upload.csrf, &shell)?; |
| 122 | 3× | let result = super::avatar::apply(&app, "account", me.account.id, upload).await; |
| 123 | 3× | if result.is_ok() { |
| 124 | 2× | accounts::audit(&app.db, Some(&me.account), "profile.picture", &me.account.username, "").await.ok(); |
| 125 | 1× | } |
| 126 | 3× | let fresh = accounts::account_by_id(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(me.account); |
| 127 | 3× | let page = SettingsPage::new(shell, fresh, &headers).with_mail(&app); |
| 128 | 3× | Ok(Page(match result { |
| 129 | 2× | Ok(m) => page.notice(m), |
| 130 | 1× | Err(e) => page.error(e), |
| 131 | | })) |
| 132 | 4× | } |
| 133 | |
|
| 134 | |
|
| 135 | 1× | pub async fn resend_verification( |
| 136 | 1× | State(app): State<Shared>, |
| 137 | 1× | viewer: Viewer, |
| 138 | 1× | headers: HeaderMap, |
| 139 | 1× | Form(f): Form<super::session::CsrfForm>, |
| 140 | 1× | ) -> Result<Page<SettingsPage>, Error> { |
| 141 | 1× | let shell = Shell::new(&app, "/settings", &viewer); |
| 142 | 1× | let me = require(&viewer, &shell)?; |
| 143 | 1× | csrf(&viewer, &f.csrf, &shell)?; |
| 144 | 1× | let page = SettingsPage::new(shell, me.account.clone(), &headers).with_mail(&app); |
| 145 | 1× | Ok(Page(match super::recovery::send_verification(&app, &me.account).await { |
| 146 | 0× | Ok(()) if me.account.email_verified => page.notice("Your email is already verified."), |
| 147 | 0× | Ok(()) => page.notice(format!("A verification link is on its way to {}.", me.account.email)), |
| 148 | 1× | Err(e) => page.error(e), |
| 149 | | })) |
| 150 | 1× | } |
| 151 | |
|
| 152 | | #[derive(Deserialize)] |
| 153 | | pub struct AppearanceForm { |
| 154 | | #[serde(rename = "_csrf")] |
| 155 | | csrf_token: String, |
| 156 | | theme: String, |
| 157 | | #[serde(default)] |
| 158 | | backdrop: String, |
| 159 | | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | 5× | pub async fn appearance(State(app): State<Shared>, viewer: Viewer, Form(f): Form<AppearanceForm>) -> Result<Response, Error> { |
| 164 | 5× | let shell = Shell::new(&app, "/settings", &viewer); |
| 165 | 5× | require(&viewer, &shell)?; |
| 166 | 5× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 167 | 7× | let Some((theme, _)) = THEMES.iter().find(|(t, _)| *t == f.theme) else { |
| 168 | 1× | return Err(Error::bad(shell, "Pick dark, light or match the system.")); |
| 169 | | }; |
| 170 | 3× | let secure = if app.config.base_url().starts_with("https://") { "; Secure" } else { "" }; |
| 171 | 3× | let backdrop = if f.backdrop.is_empty() { "moving" } else { f.backdrop.as_str() }; |
| 172 | 6× | let Some((backdrop, _)) = BACKDROPS.iter().find(|(b, _)| *b == backdrop) else { |
| 173 | 1× | return Err(Error::bad(shell, "Pick a moving, still or no background.")); |
| 174 | | }; |
| 175 | 2× | Ok(( |
| 176 | 2× | StatusCode::SEE_OTHER, |
| 177 | 2× | axum::response::AppendHeaders([ |
| 178 | 2× | (header::SET_COOKIE, format!("theme={theme}; Path=/; Max-Age=31536000; SameSite=Lax{secure}")), |
| 179 | 2× | (header::SET_COOKIE, format!("backdrop={backdrop}; Path=/; Max-Age=31536000; SameSite=Lax{secure}")), |
| 180 | 2× | (header::LOCATION, "/settings".to_string()), |
| 181 | 2× | ]), |
| 182 | 2× | ) |
| 183 | 2× | .into_response()) |
| 184 | 5× | } |
| 185 | |
|
| 186 | | #[derive(Deserialize)] |
| 187 | | pub struct ProfileForm { |
| 188 | | #[serde(rename = "_csrf")] |
| 189 | | csrf_token: String, |
| 190 | | display_name: String, |
| 191 | | email: String, |
| 192 | | #[serde(default)] |
| 193 | | bio: String, |
| 194 | | #[serde(default)] |
| 195 | | website: String, |
| 196 | | #[serde(default)] |
| 197 | | location: String, |
| 198 | | #[serde(default)] |
| 199 | | bluesky: String, |
| 200 | | #[serde(default)] |
| 201 | | mastodon: String, |
| 202 | | #[serde(default)] |
| 203 | | github: String, |
| 204 | | } |
| 205 | |
|
| 206 | 9× | pub async fn update_profile( |
| 207 | 9× | State(app): State<Shared>, |
| 208 | 9× | viewer: Viewer, |
| 209 | 9× | headers: HeaderMap, |
| 210 | 9× | Form(f): Form<ProfileForm>, |
| 211 | 9× | ) -> Result<Page<SettingsPage>, Error> { |
| 212 | 9× | let shell = Shell::new(&app, "/settings", &viewer); |
| 213 | 9× | let me = require(&viewer, &shell)?; |
| 214 | 9× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 215 | 8× | let website = f.website.trim(); |
| 216 | 8× | if !(website.is_empty() || website.starts_with("https://") || website.starts_with("http://")) || website.len() > 200 { |
| 217 | 1× | return Ok(Page( |
| 218 | 1× | SettingsPage::new(shell, me.account, &headers).with_mail(&app).error("The website must be an http:// or https:// address."), |
| 219 | 1× | )); |
| 220 | 7× | } |
| 221 | | |
| 222 | 19× | let link = |given: &str, what: &str, parse: fn(&str) -> Option<String>| -> Result<String, String> { |
| 223 | 19× | if given.trim().is_empty() { |
| 224 | 15× | Ok(String::new()) |
| 225 | | } else { |
| 226 | 4× | parse(given).ok_or_else(|| format!("That is not a {what}.")) |
| 227 | | } |
| 228 | 19× | }; |
| 229 | 7× | let links = (|| { |
| 230 | | Ok::<_, String>(( |
| 231 | 7× | link(&f.bluesky, "Bluesky handle (such as andy.bsky.social)", crate::social::bluesky)?, |
| 232 | 6× | link(&f.mastodon, "Mastodon address (such as @[email protected])", crate::social::mastodon)?, |
| 233 | 6× | link(&f.github, "GitHub username", crate::social::github)?, |
| 234 | | )) |
| 235 | | })(); |
| 236 | 7× | let (bluesky, mastodon, github) = match links { |
| 237 | 6× | Ok(l) => l, |
| 238 | 1× | Err(msg) => return Ok(Page(SettingsPage::new(shell, me.account, &headers).with_mail(&app).error(&msg))), |
| 239 | | }; |
| 240 | 6× | let bio: String = f.bio.trim().chars().take(280).collect(); |
| 241 | 6× | let location: String = f.location.trim().chars().take(80).collect(); |
| 242 | 6× | accounts::set_profile(&app.db, me.account.id, f.display_name.trim(), f.email.trim()) |
| 243 | 6× | .await |
| 244 | 6× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 245 | 6× | accounts::set_bio(&app.db, me.account.id, &bio, website, &location).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 246 | 6× | accounts::set_links(&app.db, me.account.id, &bluesky, &mastodon, &github).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 247 | 6× | let changed = me.account.email != f.email.trim(); |
| 248 | 6× | let updated = |
| 249 | 6× | accounts::account_by_id(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(me.account); |
| 250 | 6× | let mut notice = "Profile saved.".to_string(); |
| 251 | 6× | if changed && !updated.email.is_empty() { |
| 252 | 4× | accounts::audit(&app.db, Some(&updated), "email.changed", &updated.username, "").await.ok(); |
| 253 | 4× | match super::recovery::send_verification(&app, &updated).await { |
| 254 | 3× | Ok(()) => notice = format!("Profile saved. A verification link is on its way to {}.", updated.email), |
| 255 | 1× | Err(e) => notice = format!("Profile saved. {e}"), |
| 256 | | } |
| 257 | 2× | } |
| 258 | 6× | Ok(Page(SettingsPage::new(shell, updated, &headers).with_mail(&app).notice(notice))) |
| 259 | 9× | } |
| 260 | |
|
| 261 | | #[derive(Deserialize)] |
| 262 | | pub struct PasswordForm { |
| 263 | | #[serde(rename = "_csrf")] |
| 264 | | csrf_token: String, |
| 265 | | current: String, |
| 266 | | password: String, |
| 267 | | } |
| 268 | |
|
| 269 | 0× | pub async fn change_password( |
| 270 | 0× | State(app): State<Shared>, |
| 271 | 0× | viewer: Viewer, |
| 272 | 0× | headers: HeaderMap, |
| 273 | 0× | Form(f): Form<PasswordForm>, |
| 274 | 0× | ) -> Result<Response, Error> { |
| 275 | 0× | let shell = Shell::new(&app, "/settings", &viewer); |
| 276 | 0× | let me = require(&viewer, &shell)?; |
| 277 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 278 | 0× | if !auth::verify_password(&me.account.password_hash, &f.current) { |
| 279 | 0× | return Ok( |
| 280 | 0× | Page(SettingsPage::new(shell, me.account, &headers).with_mail(&app).error("Your current password is wrong.")).into_response() |
| 281 | 0× | ); |
| 282 | 0× | } |
| 283 | 0× | if let Err(e) = auth::password_ok(&f.password) { |
| 284 | 0× | return Ok(Page(SettingsPage::new(shell, me.account, &headers).with_mail(&app).error(e)).into_response()); |
| 285 | 0× | } |
| 286 | 0× | let hash = auth::hash_password(&f.password).map_err(|e| Error::bad(shell.clone(), e.to_string()))?; |
| 287 | | |
| 288 | 0× | accounts::set_password(&app.db, me.account.id, &hash, &auth::random_hex(16)).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 289 | 0× | accounts::audit(&app.db, Some(&me.account), "password.changed", &me.account.username, "").await.ok(); |
| 290 | 0× | let fresh = accounts::account_by_id(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(me.account); |
| 291 | 0× | Ok(super::session::signed_in(&app, &fresh, "/settings/security").await) |
| 292 | 0× | } |
| 293 | |
|
| 294 | | #[derive(Template)] |
| 295 | | #[template(path = "settings_security.html")] |
| 296 | | pub struct SecurityPage { |
| 297 | | shell: Shell, |
| 298 | | me: accounts::Account, |
| 299 | | notice: Option<String>, |
| 300 | | error: Option<String>, |
| 301 | | |
| 302 | | enrol: Option<(String, String, String)>, |
| 303 | | recovery: Vec<String>, |
| 304 | | |
| 305 | | connected: Vec<(&'static str, &'static str, Option<String>)>, |
| 306 | | |
| 307 | | sessions: Vec<SessionView>, |
| 308 | | } |
| 309 | |
|
| 310 | |
|
| 311 | | pub struct SessionView { |
| 312 | | pub id: i64, |
| 313 | | pub current: bool, |
| 314 | | pub browser: String, |
| 315 | | pub address: String, |
| 316 | | pub seen_ms: i64, |
| 317 | | pub created_ms: i64, |
| 318 | | } |
| 319 | |
|
| 320 | |
|
| 321 | 15× | pub fn browser_of(agent: &str) -> String { |
| 322 | 198× | let has = |s: &str| agent.contains(s); |
| 323 | 15× | let browser = if has("Edg/") { |
| 324 | 1× | "Edge" |
| 325 | 14× | } else if has("OPR/") { |
| 326 | 0× | "Opera" |
| 327 | 14× | } else if has("Firefox/") { |
| 328 | 1× | "Firefox" |
| 329 | 13× | } else if has("Chrome/") || has("Chromium/") { |
| 330 | 1× | "Chrome" |
| 331 | 12× | } else if has("Safari/") { |
| 332 | 1× | "Safari" |
| 333 | 11× | } else if has("curl/") { |
| 334 | 0× | "curl" |
| 335 | | } else { |
| 336 | 11× | "" |
| 337 | | }; |
| 338 | 15× | let os = if has("Android") { |
| 339 | 0× | "Android" |
| 340 | 15× | } else if has("iPhone") || has("iPad") { |
| 341 | 1× | "iOS" |
| 342 | 14× | } else if has("Windows") { |
| 343 | 1× | "Windows" |
| 344 | 13× | } else if has("Mac OS X") || has("Macintosh") { |
| 345 | 1× | "macOS" |
| 346 | 12× | } else if has("CrOS") { |
| 347 | 0× | "ChromeOS" |
| 348 | 12× | } else if has("Linux") { |
| 349 | 1× | "Linux" |
| 350 | | } else { |
| 351 | 11× | "" |
| 352 | | }; |
| 353 | 15× | match (browser, os) { |
| 354 | 15× | ("", "") => "Unknown browser".into(), |
| 355 | 4× | (b, "") => b.into(), |
| 356 | 4× | ("", o) => format!("A browser on {o}"), |
| 357 | 4× | (b, o) => format!("{b} on {o}"), |
| 358 | | } |
| 359 | 15× | } |
| 360 | |
|
| 361 | 7× | async fn sessions_of(app: &Shared, me: &crate::auth::ViewerAccount) -> sqlx::Result<Vec<SessionView>> { |
| 362 | 7× | Ok(crate::db::sessions::of_account(&app.db, me.account.id, &me.account.session_salt) |
| 363 | 7× | .await? |
| 364 | 7× | .into_iter() |
| 365 | 7× | .map(|s| SessionView { |
| 366 | 10× | id: s.id, |
| 367 | 10× | current: s.id == me.session_id, |
| 368 | 10× | browser: browser_of(&s.agent), |
| 369 | 10× | address: s.address, |
| 370 | 10× | seen_ms: s.seen_ms, |
| 371 | 10× | created_ms: s.created_ms, |
| 372 | 10× | }) |
| 373 | 7× | .collect()) |
| 374 | 7× | } |
| 375 | |
|
| 376 | | #[derive(Deserialize)] |
| 377 | | pub struct SecurityQuery { |
| 378 | | notice: Option<String>, |
| 379 | | error: Option<String>, |
| 380 | | } |
| 381 | |
|
| 382 | 11× | pub async fn security(State(app): State<Shared>, viewer: Viewer, Query(q): Query<SecurityQuery>) -> Result<Page<SecurityPage>, Error> { |
| 383 | 11× | let shell = Shell::new(&app, "/settings/security", &viewer); |
| 384 | 11× | let me = require(&viewer, &shell)?; |
| 385 | 7× | let connected = connected_of(&app, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 386 | 7× | let sessions = sessions_of(&app, &me).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 387 | 7× | Ok(Page(SecurityPage { shell, me: me.account, notice: q.notice, error: q.error, enrol: None, recovery: vec![], connected, sessions })) |
| 388 | 11× | } |
| 389 | |
|
| 390 | |
|
| 391 | 7× | pub async fn connected_of(app: &Shared, account_id: i64) -> sqlx::Result<Vec<(&'static str, &'static str, Option<String>)>> { |
| 392 | 7× | let links = crate::signin::links_of(&app.db, account_id).await?; |
| 393 | 7× | let mut out = vec![]; |
| 394 | 7× | for p in crate::signin::enabled(app).await { |
| 395 | 6× | out.push((p.id, p.name, links.iter().find(|l| l.provider == p.id).map(|l| l.login.clone()))); |
| 396 | | } |
| 397 | 7× | Ok(out) |
| 398 | 7× | } |
| 399 | |
|
| 400 | | #[derive(Deserialize)] |
| 401 | | pub struct TotpForm { |
| 402 | | #[serde(rename = "_csrf")] |
| 403 | | csrf_token: String, |
| 404 | | #[serde(default)] |
| 405 | | secret: String, |
| 406 | | #[serde(default)] |
| 407 | | code: String, |
| 408 | | #[serde(default)] |
| 409 | | password: String, |
| 410 | | } |
| 411 | |
|
| 412 | |
|
| 413 | 1× | pub async fn totp_begin(State(app): State<Shared>, viewer: Viewer, Form(f): Form<TotpForm>) -> Result<Page<SecurityPage>, Error> { |
| 414 | 1× | let shell = Shell::new(&app, "/settings/security", &viewer); |
| 415 | 1× | let me = require(&viewer, &shell)?; |
| 416 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 417 | 1× | let secret = totp::new_secret(); |
| 418 | 1× | let uri = totp::uri(&app.config.site.name, &me.account.username, &secret); |
| 419 | 1× | let qr = totp::qr_svg(&uri).unwrap_or_default(); |
| 420 | 1× | Ok(Page(SecurityPage { |
| 421 | 1× | shell, |
| 422 | 1× | me: me.account, |
| 423 | 1× | notice: None, |
| 424 | 1× | error: None, |
| 425 | 1× | enrol: Some((secret, uri, qr)), |
| 426 | 1× | recovery: vec![], |
| 427 | 1× | connected: vec![], |
| 428 | 1× | sessions: vec![], |
| 429 | 1× | })) |
| 430 | 1× | } |
| 431 | |
|
| 432 | |
|
| 433 | 1× | pub async fn totp_enable(State(app): State<Shared>, viewer: Viewer, Form(f): Form<TotpForm>) -> Result<Page<SecurityPage>, Error> { |
| 434 | 1× | let shell = Shell::new(&app, "/settings/security", &viewer); |
| 435 | 1× | let me = require(&viewer, &shell)?; |
| 436 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 437 | 1× | let now = (db::now_ms() / 1000) as u64; |
| 438 | 1× | if f.secret.is_empty() || !totp::verify(&f.secret, &f.code, now) { |
| 439 | 0× | let uri = totp::uri(&app.config.site.name, &me.account.username, &f.secret); |
| 440 | 0× | let qr = totp::qr_svg(&uri).unwrap_or_default(); |
| 441 | 0× | return Ok(Page(SecurityPage { |
| 442 | 0× | shell, |
| 443 | 0× | me: me.account, |
| 444 | 0× | notice: None, |
| 445 | 0× | error: Some("That code did not match; scan again and enter the current code.".into()), |
| 446 | 0× | enrol: Some((f.secret, uri, qr)), |
| 447 | 0× | recovery: vec![], |
| 448 | 0× | connected: vec![], |
| 449 | 0× | sessions: vec![], |
| 450 | 0× | })); |
| 451 | 1× | } |
| 452 | 1× | let (codes, stored) = totp::new_recovery_codes(); |
| 453 | 1× | accounts::set_totp(&app.db, me.account.id, Some(&f.secret), &stored).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 454 | 1× | accounts::audit(&app.db, Some(&me.account), "totp.enabled", &me.account.username, "").await.ok(); |
| 455 | 1× | let fresh = accounts::account_by_id(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(me.account); |
| 456 | 1× | Ok(Page(SecurityPage { |
| 457 | 1× | shell, |
| 458 | 1× | me: fresh, |
| 459 | 1× | notice: Some("Two-factor authentication is on. Keep these recovery codes somewhere safe; each works once.".into()), |
| 460 | 1× | error: None, |
| 461 | 1× | enrol: None, |
| 462 | 1× | recovery: codes, |
| 463 | 1× | connected: vec![], |
| 464 | 1× | sessions: vec![], |
| 465 | 1× | })) |
| 466 | 1× | } |
| 467 | |
|
| 468 | 0× | pub async fn totp_disable(State(app): State<Shared>, viewer: Viewer, Form(f): Form<TotpForm>) -> Result<Page<SecurityPage>, Error> { |
| 469 | 0× | let shell = Shell::new(&app, "/settings/security", &viewer); |
| 470 | 0× | let me = require(&viewer, &shell)?; |
| 471 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 472 | 0× | if !auth::verify_password(&me.account.password_hash, &f.password) { |
| 473 | 0× | return Ok(Page(SecurityPage { |
| 474 | 0× | shell, |
| 475 | 0× | me: me.account, |
| 476 | 0× | notice: None, |
| 477 | 0× | error: Some("Your password is wrong.".into()), |
| 478 | 0× | enrol: None, |
| 479 | 0× | recovery: vec![], |
| 480 | 0× | connected: vec![], |
| 481 | 0× | sessions: vec![], |
| 482 | 0× | })); |
| 483 | 0× | } |
| 484 | 0× | accounts::set_totp(&app.db, me.account.id, None, "").await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 485 | 0× | accounts::audit(&app.db, Some(&me.account), "totp.disabled", &me.account.username, "").await.ok(); |
| 486 | 0× | let fresh = accounts::account_by_id(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(me.account); |
| 487 | 0× | Ok(Page(SecurityPage { |
| 488 | 0× | shell, |
| 489 | 0× | me: fresh, |
| 490 | 0× | notice: Some("Two-factor authentication is off.".into()), |
| 491 | 0× | error: None, |
| 492 | 0× | enrol: None, |
| 493 | 0× | recovery: vec![], |
| 494 | 0× | connected: vec![], |
| 495 | 0× | sessions: vec![], |
| 496 | 0× | })) |
| 497 | 0× | } |
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | 1× | pub async fn sessions_reset(State(app): State<Shared>, viewer: Viewer, Form(f): Form<super::session::CsrfForm>) -> Result<Response, Error> { |
| 502 | 1× | let shell = Shell::new(&app, "/settings/security", &viewer); |
| 503 | 1× | let me = require(&viewer, &shell)?; |
| 504 | 1× | csrf(&viewer, &f.csrf, &shell)?; |
| 505 | 1× | accounts::set_session_salt(&app.db, me.account.id, &auth::random_hex(16)).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 506 | 1× | accounts::audit(&app.db, Some(&me.account), "sessions.reset", &me.account.username, "").await.ok(); |
| 507 | 1× | let fresh = accounts::account_by_id(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(me.account); |
| 508 | 1× | Ok(super::session::signed_in(&app, &fresh, "/settings/security").await) |
| 509 | 1× | } |
| 510 | |
|
| 511 | |
|
| 512 | 1× | pub async fn session_end( |
| 513 | 1× | State(app): State<Shared>, |
| 514 | 1× | viewer: Viewer, |
| 515 | 1× | Path(id): Path<i64>, |
| 516 | 1× | Form(f): Form<super::session::CsrfForm>, |
| 517 | 1× | ) -> Result<Response, Error> { |
| 518 | 1× | let shell = Shell::new(&app, "/settings/security", &viewer); |
| 519 | 1× | let me = require(&viewer, &shell)?; |
| 520 | 1× | csrf(&viewer, &f.csrf, &shell)?; |
| 521 | 1× | let ended = crate::db::sessions::end(&app.db, me.account.id, id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 522 | 1× | if ended { |
| 523 | 1× | accounts::audit(&app.db, Some(&me.account), "session.ended", &me.account.username, &id.to_string()).await.ok(); |
| 524 | 0× | } |
| 525 | 1× | let to = if id == me.session_id { "/" } else { "/settings/security?notice=That+browser+is+signed+out." }; |
| 526 | 1× | Ok(Redirect::to(to).into_response()) |
| 527 | 1× | } |
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | | #[derive(Template)] |
| 532 | | #[template(path = "settings_keys.html")] |
| 533 | | pub struct KeysPage { |
| 534 | | shell: Shell, |
| 535 | | me: accounts::Account, |
| 536 | | keys: Vec<accounts::LinkedIdentity>, |
| 537 | | notice: Option<String>, |
| 538 | | error: Option<String>, |
| 539 | | |
| 540 | | pending: Option<(i64, String, String)>, |
| 541 | | } |
| 542 | |
|
| 543 | 9× | async fn keys_page( |
| 544 | 9× | app: &Shared, |
| 545 | 9× | shell: Shell, |
| 546 | 9× | me: accounts::Account, |
| 547 | 9× | notice: Option<String>, |
| 548 | 9× | error: Option<String>, |
| 549 | 9× | pending: Option<(i64, String, String)>, |
| 550 | 9× | ) -> Result<Page<KeysPage>, Error> { |
| 551 | 9× | let keys = accounts::identities_of(&app.db, me.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 552 | 9× | Ok(Page(KeysPage { shell, me, keys, notice, error, pending })) |
| 553 | 9× | } |
| 554 | |
|
| 555 | 3× | pub async fn keys(State(app): State<Shared>, viewer: Viewer) -> Result<Page<KeysPage>, Error> { |
| 556 | 3× | let shell = Shell::new(&app, "/settings/keys", &viewer); |
| 557 | 3× | let me = require(&viewer, &shell)?; |
| 558 | 3× | keys_page(&app, shell, me.account, None, None, None).await |
| 559 | 3× | } |
| 560 | |
|
| 561 | |
|
| 562 | |
|
| 563 | 3× | pub async fn keys_upload(State(app): State<Shared>, viewer: Viewer, mut multipart: Multipart) -> Result<Page<KeysPage>, Error> { |
| 564 | 3× | let shell = Shell::new(&app, "/settings/keys", &viewer); |
| 565 | 3× | let me = require(&viewer, &shell)?; |
| 566 | 3× | let mut token = String::new(); |
| 567 | 3× | let mut object: Vec<u8> = Vec::new(); |
| 568 | 9× | while let Ok(Some(field)) = multipart.next_field().await { |
| 569 | 6× | match field.name() { |
| 570 | 6× | Some("_csrf") => token = field.text().await.unwrap_or_default(), |
| 571 | 3× | Some("identity") => object = field.bytes().await.map(|b| b.to_vec()).unwrap_or_default(), |
| 572 | 0× | _ => {} |
| 573 | | } |
| 574 | | } |
| 575 | 3× | csrf(&viewer, &token, &shell)?; |
| 576 | 3× | let identity = match Identity::decode(&object) { |
| 577 | 3× | Ok(i) => i, |
| 578 | 0× | Err(e) => return keys_page(&app, shell, me.account, None, Some(format!("That is not an identity file: {e}")), None).await, |
| 579 | | }; |
| 580 | 3× | if identity.hash() == app.hub_key.key { |
| 581 | 1× | let msg = "That is this hub's own key, which only the hub holds. Upload your identity: after `oxy identity delegate add` with the hub's key, run `oxy identity export` in that checkout and upload the file it writes."; |
| 582 | 1× | return keys_page(&app, shell, me.account, None, Some(msg.into()), None).await; |
| 583 | 2× | } |
| 584 | 2× | let key = identity.hash().to_hex(); |
| 585 | 2× | if let Some(existing) = accounts::identity_by_key(&app.db, &key).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 586 | 0× | let msg = if existing.account_id == me.account.id { |
| 587 | 0× | "That key is already linked to you. If you have delegated since (`oxy identity delegate add`), export again: a delegation makes a new version of your identity." |
| 588 | | } else { |
| 589 | 0× | "That key is linked to another account." |
| 590 | | }; |
| 591 | 0× | return keys_page(&app, shell, me.account, None, Some(msg.into()), None).await; |
| 592 | 2× | } |
| 593 | 2× | let bytes = auth::random_hex(32); |
| 594 | 2× | let id = accounts::create_challenge(&app.db, me.account.id, &bytes).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 595 | 2× | sqlx::query("UPDATE challenges SET object = $1 WHERE id = $2") |
| 596 | 2× | .bind(&object) |
| 597 | 2× | .bind(id) |
| 598 | 2× | .execute(&app.db) |
| 599 | 2× | .await |
| 600 | 2× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 601 | 2× | keys_page(&app, shell, me.account, None, None, Some((id, bytes, identity.name))).await |
| 602 | 3× | } |
| 603 | |
|
| 604 | | #[derive(Deserialize)] |
| 605 | | pub struct KeyVerifyForm { |
| 606 | | #[serde(rename = "_csrf")] |
| 607 | | csrf_token: String, |
| 608 | | challenge: i64, |
| 609 | | signature: String, |
| 610 | | } |
| 611 | |
|
| 612 | 3× | pub async fn keys_verify(State(app): State<Shared>, viewer: Viewer, Form(f): Form<KeyVerifyForm>) -> Result<Page<KeysPage>, Error> { |
| 613 | 3× | let shell = Shell::new(&app, "/settings/keys", &viewer); |
| 614 | 3× | let me = require(&viewer, &shell)?; |
| 615 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 616 | 3× | let object: Option<Vec<u8>> = sqlx::query_scalar("SELECT object FROM challenges WHERE id = $1 AND account_id = $2") |
| 617 | 3× | .bind(f.challenge) |
| 618 | 3× | .bind(me.account.id) |
| 619 | 3× | .fetch_optional(&app.db) |
| 620 | 3× | .await |
| 621 | 3× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 622 | 3× | let bytes = accounts::take_challenge(&app.db, me.account.id, f.challenge).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 623 | 3× | let (Some(object), Some(bytes)) = (object, bytes) else { |
| 624 | 1× | return keys_page(&app, shell, me.account, None, Some("That challenge has expired; upload the identity again.".into()), None).await; |
| 625 | | }; |
| 626 | 2× | let Ok(identity) = Identity::decode(&object) else { |
| 627 | 0× | return keys_page(&app, shell, me.account, None, Some("The stored identity is unreadable.".into()), None).await; |
| 628 | | }; |
| 629 | 2× | let sig_bytes = hex::decode(f.signature.trim()).unwrap_or_default(); |
| 630 | 2× | let sigs = Signature::decode_list(&sig_bytes).unwrap_or_default(); |
| 631 | 2× | let challenge = hex::decode(&bytes).unwrap_or_default(); |
| 632 | 2× | let ok = sigs.first().is_some_and(|sig| oxy_core::identity::verify_challenge(&identity, sig, &challenge).is_ok()); |
| 633 | 2× | if !ok { |
| 634 | | |
| 635 | | |
| 636 | 1× | let msg = match sigs.first() { |
| 637 | 1× | Some(sig) if sig.key != identity.hash() => format!( |
| 638 | | "That signature is by key {}, but the identity uploaded is {} ({}). Sign in a checkout whose `oxy identity export` gives that identity, without an `author.signer` set, or upload the identity that checkout exports.", |
| 639 | 1× | sig.key.short(12), |
| 640 | 1× | identity.hash().short(12), |
| 641 | | identity.name |
| 642 | | ), |
| 643 | 0× | _ => "The signature does not verify against that identity and challenge.".into(), |
| 644 | | }; |
| 645 | 1× | return keys_page(&app, shell, me.account, None, Some(msg), None).await; |
| 646 | 1× | } |
| 647 | 1× | let key = identity.hash().to_hex(); |
| 648 | 1× | accounts::link_identity(&app.db, me.account.id, &key, &identity.name, &identity.email, &object) |
| 649 | 1× | .await |
| 650 | 1× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 651 | 1× | accounts::audit(&app.db, Some(&me.account), "identity.linked", &key, &identity.name).await.ok(); |
| 652 | 1× | keys_page(&app, shell, me.account, Some("Key linked. Changes signed with it are now yours on every page.".into()), None, None).await |
| 653 | 3× | } |
| 654 | |
|
| 655 | | #[derive(Deserialize)] |
| 656 | | pub struct KeyForm { |
| 657 | | #[serde(rename = "_csrf")] |
| 658 | | csrf_token: String, |
| 659 | | key: String, |
| 660 | | } |
| 661 | |
|
| 662 | 0× | pub async fn keys_unlink(State(app): State<Shared>, viewer: Viewer, Form(f): Form<KeyForm>) -> Result<Page<KeysPage>, Error> { |
| 663 | 0× | let shell = Shell::new(&app, "/settings/keys", &viewer); |
| 664 | 0× | let me = require(&viewer, &shell)?; |
| 665 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 666 | 0× | accounts::unlink_identity(&app.db, me.account.id, &f.key).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 667 | 0× | accounts::audit(&app.db, Some(&me.account), "identity.unlinked", &f.key, "").await.ok(); |
| 668 | 0× | keys_page(&app, shell, me.account, Some("Key unlinked.".into()), None, None).await |
| 669 | 0× | } |
| 670 | |
|
| 671 | |
|
| 672 | |
|
| 673 | | #[derive(Template)] |
| 674 | | #[template(path = "settings_tokens.html")] |
| 675 | | pub struct TokensPage { |
| 676 | | shell: Shell, |
| 677 | | scopes: &'static [(&'static str, &'static str)], |
| 678 | | me: accounts::Account, |
| 679 | | tokens: Vec<accounts::Token>, |
| 680 | | |
| 681 | | fresh: Option<String>, |
| 682 | | notice: Option<String>, |
| 683 | | error: Option<String>, |
| 684 | | } |
| 685 | |
|
| 686 | 37× | async fn tokens_page( |
| 687 | 37× | app: &Shared, |
| 688 | 37× | shell: Shell, |
| 689 | 37× | me: accounts::Account, |
| 690 | 37× | fresh: Option<String>, |
| 691 | 37× | notice: Option<String>, |
| 692 | 37× | error: Option<String>, |
| 693 | 37× | ) -> Result<Page<TokensPage>, Error> { |
| 694 | 37× | let tokens = accounts::tokens_of(&app.db, me.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 695 | 37× | Ok(Page(TokensPage { shell, scopes: SCOPES, me, tokens, fresh, notice, error })) |
| 696 | 37× | } |
| 697 | |
|
| 698 | 18× | pub async fn tokens(State(app): State<Shared>, viewer: Viewer) -> Result<Page<TokensPage>, Error> { |
| 699 | 18× | let shell = Shell::new(&app, "/settings/tokens", &viewer); |
| 700 | 18× | let me = require(&viewer, &shell)?; |
| 701 | 18× | tokens_page(&app, shell, me.account, None, None, None).await |
| 702 | 18× | } |
| 703 | |
|
| 704 | | #[derive(Deserialize)] |
| 705 | | pub struct TokenForm { |
| 706 | | #[serde(rename = "_csrf")] |
| 707 | | csrf_token: String, |
| 708 | | name: String, |
| 709 | | #[serde(default)] |
| 710 | | scopes: Vec<String>, |
| 711 | | #[serde(default)] |
| 712 | | repos: String, |
| 713 | | #[serde(default)] |
| 714 | | expires_days: String, |
| 715 | | |
| 716 | | #[serde(default)] |
| 717 | | agent: String, |
| 718 | | } |
| 719 | |
|
| 720 | |
|
| 721 | |
|
| 722 | | pub const SCOPES: &[(&str, &str)] = &[ |
| 723 | | ("repo:read", "read repositories you can see: clone, fetch, pull, and the read API"), |
| 724 | | ("repo:write", "push to repositories you can write to; also covers proposals, issues and checks"), |
| 725 | | ("repo:admin", "create repositories and change settings through the API"), |
| 726 | | ("proposals:write", "open, comment on and review proposals, without push access"), |
| 727 | | ("issues:write", "open, comment on and close issues, without push access"), |
| 728 | | ("checks:write", "report checks on states, for CI systems, without push access"), |
| 729 | | ("hooks:admin", "manage webhooks through the API"), |
| 730 | | ("user:read", "read your own profile through the API"), |
| 731 | | ]; |
| 732 | |
|
| 733 | 19× | pub async fn tokens_create( |
| 734 | 19× | State(app): State<Shared>, |
| 735 | 19× | viewer: Viewer, |
| 736 | 19× | axum_extra::extract::Form(f): axum_extra::extract::Form<TokenForm>, |
| 737 | 19× | ) -> Result<Page<TokensPage>, Error> { |
| 738 | 19× | let shell = Shell::new(&app, "/settings/tokens", &viewer); |
| 739 | 19× | let me = require(&viewer, &shell)?; |
| 740 | 19× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 741 | 19× | let name = f.name.trim(); |
| 742 | 19× | if name.is_empty() { |
| 743 | 0× | return tokens_page(&app, shell, me.account, None, None, Some("Give the token a name.".into())).await; |
| 744 | 19× | } |
| 745 | 69× | let scopes: Vec<&str> = f.scopes.iter().map(String::as_str).filter(|s| SCOPES.iter().any(|(k, _)| k == s)).collect(); |
| 746 | 19× | if scopes.is_empty() { |
| 747 | 0× | return tokens_page(&app, shell, me.account, None, None, Some("Pick at least one scope.".into())).await; |
| 748 | 19× | } |
| 749 | 19× | let expires_ms = f.expires_days.trim().parse::<i64>().ok().filter(|d| *d > 0).map(|d| db::now_ms() + d * 86_400_000); |
| 750 | 19× | let repos = f.repos.split(',').map(str::trim).filter(|r| !r.is_empty()).collect::<Vec<_>>().join(","); |
| 751 | 19× | let (secret, hash) = auth::new_token(); |
| 752 | 19× | accounts::create_token(&app.db, me.account.id, name, &hash, &scopes.join(","), &repos, expires_ms) |
| 753 | 19× | .await |
| 754 | 19× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 755 | 19× | if !f.agent.is_empty() { |
| 756 | 0× | if let Ok(Some(t)) = accounts::token_by_hash(&app.db, &hash).await { |
| 757 | 0× | accounts::set_token_agent(&app.db, me.account.id, t.id, true).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 758 | 0× | } |
| 759 | 19× | } |
| 760 | 19× | accounts::audit(&app.db, Some(&me.account), "token.created", name, &scopes.join(",")).await.ok(); |
| 761 | 19× | tokens_page(&app, shell, me.account, Some(secret), Some("Copy the token now; it is not shown again.".into()), None).await |
| 762 | 19× | } |
| 763 | |
|
| 764 | |
|
| 765 | |
|
| 766 | 0× | pub async fn tokens_agent( |
| 767 | 0× | State(app): State<Shared>, |
| 768 | 0× | viewer: Viewer, |
| 769 | 0× | Path(id): Path<i64>, |
| 770 | 0× | Form(f): Form<super::session::CsrfForm>, |
| 771 | 0× | ) -> Result<Page<TokensPage>, Error> { |
| 772 | 0× | let shell = Shell::new(&app, "/settings/tokens", &viewer); |
| 773 | 0× | let me = require(&viewer, &shell)?; |
| 774 | 0× | csrf(&viewer, &f.csrf, &shell)?; |
| 775 | 0× | let tokens = accounts::tokens_of(&app.db, me.account.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 776 | 0× | let Some(t) = tokens.iter().find(|t| t.id == id) else { |
| 777 | 0× | return tokens_page(&app, shell, me.account, None, None, Some("No such token.".into())).await; |
| 778 | | }; |
| 779 | 0× | accounts::set_token_agent(&app.db, me.account.id, id, !t.agent).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 780 | 0× | accounts::audit(&app.db, Some(&me.account), "token.agent", &t.name, if t.agent { "off" } else { "on" }).await.ok(); |
| 781 | 0× | let notice = if t.agent { |
| 782 | 0× | format!("{} is no longer marked as an agent's.", t.name) |
| 783 | | } else { |
| 784 | 0× | format!("{} is an agent's: you will hear what it does.", t.name) |
| 785 | | }; |
| 786 | 0× | tokens_page(&app, shell, me.account, None, Some(notice), None).await |
| 787 | 0× | } |
| 788 | |
|
| 789 | 0× | pub async fn tokens_delete( |
| 790 | 0× | State(app): State<Shared>, |
| 791 | 0× | viewer: Viewer, |
| 792 | 0× | Path(id): Path<i64>, |
| 793 | 0× | Form(f): Form<super::session::CsrfForm>, |
| 794 | 0× | ) -> Result<Page<TokensPage>, Error> { |
| 795 | 0× | let shell = Shell::new(&app, "/settings/tokens", &viewer); |
| 796 | 0× | let me = require(&viewer, &shell)?; |
| 797 | 0× | csrf(&viewer, &f.csrf, &shell)?; |
| 798 | 0× | accounts::delete_token(&app.db, me.account.id, id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 799 | 0× | accounts::audit(&app.db, Some(&me.account), "token.deleted", &id.to_string(), "").await.ok(); |
| 800 | 0× | tokens_page(&app, shell, me.account, None, Some("Token deleted.".into()), None).await |
| 801 | 0× | } |
| 802 | |
|
| 803 | | #[cfg(test)] |
| 804 | | mod tests { |
| 805 | | use super::browser_of; |
| 806 | |
|
| 807 | | #[test] |
| 808 | 1× | fn browsers_are_named_from_their_agent() { |
| 809 | 1× | let ff = "Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0"; |
| 810 | 1× | let chrome_mac = |
| 811 | 1× | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0 Safari/537.36"; |
| 812 | 1× | let edge = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0 Safari/537.36 Edg/129.0"; |
| 813 | 1× | let iphone = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Mobile/15E148 Safari/604.1"; |
| 814 | 1× | assert_eq!(browser_of(ff), "Firefox on Linux"); |
| 815 | 1× | assert_eq!(browser_of(chrome_mac), "Chrome on macOS"); |
| 816 | 1× | assert_eq!(browser_of(edge), "Edge on Windows"); |
| 817 | 1× | assert_eq!(browser_of(iphone), "Safari on iOS"); |
| 818 | 1× | assert_eq!(browser_of(""), "Unknown browser"); |
| 819 | 1× | } |
| 820 | | } |