| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use std::time::SystemTime; |
| 14 | |
| 15 | use serde::Deserialize; |
| 16 | use topcoat::{ |
| 17 | Result, |
| 18 | context::Cx, |
| 19 | router::{ |
| 20 | IntoResponse, Response, StatusCode, |
| 21 | content::Form, |
| 22 | error::{redirect, see_other}, |
| 23 | page, path_param, query_params, route, |
| 24 | }, |
| 25 | view::{attributes, component, view}, |
| 26 | }; |
| 27 | |
| 28 | use crate::{ |
| 29 | application::{ |
| 30 | Error, FORGOT_ACKNOWLEDGEMENT, find_live_reset, port::Mailer, request_password_reset, |
| 31 | reset_password, |
| 32 | }, |
| 33 | components::{ |
| 34 | button::button, |
| 35 | flash::{FlashKind, flash}, |
| 36 | input::input, |
| 37 | label::label, |
| 38 | }, |
| 39 | domain::{DomainError, Password}, |
| 40 | }; |
| 41 | |
| 42 | use super::{ |
| 43 | context::{claimed, mailer, public_origin, resets, server_error, sessions, users}, |
| 44 | layout::narrow, |
| 45 | rate_limit::{reset_throttled, throttled}, |
| 46 | urls::encode, |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | #[path_param] |
| 51 | struct Token(str); |
| 52 | |
| 53 | #[derive(Debug, Deserialize)] |
| 54 | struct ForgotForm { |
| 55 | email: String, |
| 56 | } |
| 57 | |
| 58 | #[derive(Debug, Deserialize)] |
| 59 | struct ResetForm { |
| 60 | password: String, |
| 61 | confirmation: String, |
| 62 | } |
| 63 | |
| 64 | |
| 65 | #[query_params(error = bad_request)] |
| 66 | struct Sent { |
| 67 | sent: Option<String>, |
| 68 | } |
| 69 | |
| 70 | |
| 71 | #[query_params(error = bad_request)] |
| 72 | struct Failed { |
| 73 | error: Option<String>, |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | fn refusal(code: Option<&str>) -> &'static str { |
| 81 | match code { |
| 82 | Some("mismatch") => "Those two passwords are different.", |
| 83 | |
| 84 | Some("short") => Password::RULE, |
| 85 | Some("long") => "That password is too long to hash.", |
| 86 | Some(_) => "That did not work. Try again.", |
| 87 | None => "", |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | pub(super) fn refusal_code(reason: &str) -> &'static str { |
| 97 | if reason.contains("match") { |
| 98 | "mismatch" |
| 99 | } else if reason.contains("bytes") { |
| 100 | "long" |
| 101 | } else { |
| 102 | "short" |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | #[page("/auth/forgot")] |
| 111 | async fn forgot_page(cx: &Cx) -> Result { |
| 112 | if !claimed(cx).await? { |
| 113 | return Err(redirect("/auth/setup").into()); |
| 114 | } |
| 115 | |
| 116 | let sent = query_params::<Sent>(cx)?.sent.is_some(); |
| 117 | |
| 118 | view! { |
| 119 | forgot_view(can_mail: mailer(cx).configured(), sent: sent) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | #[route(POST "/auth/forgot")] |
| 132 | async fn forgot(cx: &Cx, Form(form): Form<ForgotForm>) -> Result<Response> { |
| 133 | if let Some(response) = throttled(cx) { |
| 134 | return Ok(response); |
| 135 | } |
| 136 | |
| 137 | request_password_reset( |
| 138 | &form.email, |
| 139 | &public_origin(cx), |
| 140 | SystemTime::now(), |
| 141 | &users(cx), |
| 142 | &resets(cx), |
| 143 | &crate::infrastructure::password::Argon2Hasher::new(), |
| 144 | &mailer(cx), |
| 145 | ) |
| 146 | .await |
| 147 | |
| 148 | .map_err(|error| server_error(std::io::Error::other(error.to_string())))?; |
| 149 | |
| 150 | see_other("/auth/forgot?sent").into_response(cx) |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | #[page("/auth/reset/{token}")] |
| 159 | async fn reset_page(cx: &Cx) -> Result { |
| 160 | if reset_throttled(cx).is_some() { |
| 161 | return view! { |
| 162 | (StatusCode::TOO_MANY_REQUESTS) |
| 163 | narrow(<p class="mt-10 text-sm">"Too many attempts. Try again shortly."</p>) |
| 164 | }; |
| 165 | } |
| 166 | |
| 167 | let token = path_param::<Token>(cx).to_owned(); |
| 168 | |
| 169 | match find_live_reset(&token, SystemTime::now(), &resets(cx)).await { |
| 170 | |
| 171 | |
| 172 | |
| 173 | Ok(live) => view! { |
| 174 | reset_view( |
| 175 | token: token.as_str(), |
| 176 | expires_at: live.expires_at, |
| 177 | error: refusal(query_params::<Failed>(cx)?.error.as_deref()), |
| 178 | ) |
| 179 | }, |
| 180 | |
| 181 | |
| 182 | |
| 183 | Err(Error::Domain(DomainError::NotFound { .. })) => view! { |
| 184 | (StatusCode::NOT_FOUND) |
| 185 | dead_link_view() |
| 186 | }, |
| 187 | Err(other) => Err(server_error(std::io::Error::other(other.to_string()))), |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | #[route(POST "/auth/reset/{token}")] |
| 197 | async fn reset(cx: &Cx, Form(form): Form<ResetForm>) -> Result<Response> { |
| 198 | if let Some(response) = reset_throttled(cx) { |
| 199 | return Ok(response); |
| 200 | } |
| 201 | |
| 202 | let token = path_param::<Token>(cx).to_owned(); |
| 203 | |
| 204 | let outcome = reset_password( |
| 205 | &token, |
| 206 | &form.password, |
| 207 | &form.confirmation, |
| 208 | SystemTime::now(), |
| 209 | &users(cx), |
| 210 | &resets(cx), |
| 211 | &sessions(cx), |
| 212 | &crate::infrastructure::password::Argon2Hasher::new(), |
| 213 | ) |
| 214 | .await; |
| 215 | |
| 216 | let code = match outcome { |
| 217 | Ok(()) => return see_other("/auth/login?reset").into_response(cx), |
| 218 | |
| 219 | |
| 220 | Err(Error::Domain(DomainError::NotFound { .. })) => { |
| 221 | return see_other(&reset_url(&token, None)).into_response(cx); |
| 222 | } |
| 223 | Err(Error::Domain(DomainError::Validation { reason, .. })) => refusal_code(&reason), |
| 224 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 225 | }; |
| 226 | |
| 227 | see_other(&reset_url(&token, Some(code))).into_response(cx) |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | fn reset_url(token: &str, error: Option<&str>) -> String { |
| 244 | let path = format!("/auth/reset/{}", encode(token, false)); |
| 245 | |
| 246 | match error { |
| 247 | Some(code) => format!("{path}?error={code}"), |
| 248 | None => path, |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | #[component] |
| 255 | async fn forgot_view(can_mail: bool, sent: bool) -> Result { |
| 256 | view! { |
| 257 | narrow( |
| 258 | <div class="mx-auto mt-16 max-w-sm"> |
| 259 | <h1 class="text-xl font-semibold tracking-tight">"Forgotten password"</h1> |
| 260 | |
| 261 | if !can_mail { |
| 262 | <p class="mt-2 text-sm text-muted-foreground"> |
| 263 | "This instance is not configured to send email, so there is no " |
| 264 | "link to ask for." |
| 265 | </p> |
| 266 | <div class="mt-6"> |
| 267 | flash( |
| 268 | kind: FlashKind::Info, |
| 269 | "Anyone with a shell on the server can print a one-time link." |
| 270 | ) |
| 271 | </div> |
| 272 | <pre class="mt-4 overflow-x-auto rounded-lg bg-surface px-3 py-2 \ |
| 273 | font-mono text-xs" |
| 274 | >"steid reset-password <your handle>"</pre> |
| 275 | <p class="mt-3 text-xs text-muted-foreground"> |
| 276 | "The operator can set STEID_SMTP_URL and STEID_MAIL_FROM to turn " |
| 277 | "this form on." |
| 278 | </p> |
| 279 | } else if sent { |
| 280 | <div class="mt-6"> |
| 281 | flash(kind: FlashKind::Success, (FORGOT_ACKNOWLEDGEMENT)) |
| 282 | </div> |
| 283 | <p class="mt-4 text-xs text-muted-foreground"> |
| 284 | "Nothing arrived? Check the address and " |
| 285 | <a |
| 286 | href="/auth/forgot" |
| 287 | class="text-foreground underline underline-offset-2" |
| 288 | >"try again"</a> |
| 289 | "." |
| 290 | </p> |
| 291 | } else { |
| 292 | <p class="mt-2 text-sm text-muted-foreground"> |
| 293 | "Give the address on the account and a one-time link comes back " |
| 294 | "by email." |
| 295 | </p> |
| 296 | <form method="post" action="/auth/forgot" class="mt-6 space-y-5"> |
| 297 | <div class="space-y-2"> |
| 298 | label(attrs: attributes! { for="email" }, "Email") |
| 299 | input(attrs: attributes! { |
| 300 | id="email" |
| 301 | name="email" |
| 302 | type="email" |
| 303 | autocomplete="email" |
| 304 | required=(true) |
| 305 | placeholder="you@example.com" |
| 306 | }) |
| 307 | </div> |
| 308 | button(attrs: attributes! { type="submit" }, "Send a link") |
| 309 | </form> |
| 310 | } |
| 311 | |
| 312 | <p class="mt-8 border-t border-border pt-4 text-xs text-muted-foreground"> |
| 313 | <a href="/auth/login" class="hover:text-foreground">"Back to sign in"</a> |
| 314 | </p> |
| 315 | </div> |
| 316 | ) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | #[component] |
| 322 | async fn reset_view(token: &str, expires_at: SystemTime, error: &str) -> Result { |
| 323 | view! { |
| 324 | narrow( |
| 325 | <div class="mx-auto mt-16 max-w-sm"> |
| 326 | <h1 class="text-xl font-semibold tracking-tight">"Choose a new password"</h1> |
| 327 | <p class="mt-2 text-sm text-muted-foreground"> |
| 328 | "This link works once, and expires in " (remaining(expires_at)) "." |
| 329 | </p> |
| 330 | |
| 331 | if !error.is_empty() { |
| 332 | <div class="mt-6">flash(kind: FlashKind::Error, (error))</div> |
| 333 | } |
| 334 | |
| 335 | <form |
| 336 | method="post" |
| 337 | action=(format!("/auth/reset/{token}")) |
| 338 | class="mt-6 space-y-5" |
| 339 | > |
| 340 | <div class="space-y-2"> |
| 341 | label(attrs: attributes! { for="password" }, "New password") |
| 342 | input(attrs: attributes! { |
| 343 | id="password" |
| 344 | name="password" |
| 345 | type="password" |
| 346 | autocomplete="new-password" |
| 347 | required=(true) |
| 348 | }) |
| 349 | </div> |
| 350 | <div class="space-y-2"> |
| 351 | label(attrs: attributes! { for="confirmation" }, "New password again") |
| 352 | input(attrs: attributes! { |
| 353 | id="confirmation" |
| 354 | name="confirmation" |
| 355 | type="password" |
| 356 | autocomplete="new-password" |
| 357 | required=(true) |
| 358 | }) |
| 359 | </div> |
| 360 | <p class="text-xs text-muted-foreground"> |
| 361 | (Password::RULE) |
| 362 | </p> |
| 363 | <p class="text-xs text-muted-foreground"> |
| 364 | "Every device signed in to this account is signed out." |
| 365 | </p> |
| 366 | button(attrs: attributes! { type="submit" }, "Set password") |
| 367 | </form> |
| 368 | </div> |
| 369 | ) |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | |
| 374 | #[component] |
| 375 | async fn dead_link_view() -> Result { |
| 376 | view! { |
| 377 | narrow( |
| 378 | <div class="mx-auto mt-16 max-w-sm"> |
| 379 | <h1 class="text-xl font-semibold tracking-tight">"This link is no longer good"</h1> |
| 380 | <p class="mt-2 text-sm text-muted-foreground"> |
| 381 | "A reset link works once and lasts thirty minutes. This one has been " |
| 382 | "used, has expired, or was never issued." |
| 383 | </p> |
| 384 | <p class="mt-6"> |
| 385 | <a |
| 386 | href="/auth/forgot" |
| 387 | class="text-sm font-medium text-primary hover:underline" |
| 388 | >"Ask for another →"</a> |
| 389 | </p> |
| 390 | </div> |
| 391 | ) |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | fn remaining(expires_at: SystemTime) -> String { |
| 401 | let left = expires_at |
| 402 | .duration_since(SystemTime::now()) |
| 403 | .unwrap_or_default(); |
| 404 | |
| 405 | |
| 406 | |
| 407 | let seconds = left.as_secs() + u64::from(left.subsec_nanos() > 0); |
| 408 | |
| 409 | match seconds.div_ceil(60) { |
| 410 | 0 | 1 => "1 minute".to_owned(), |
| 411 | minutes => format!("{minutes} minutes"), |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | #[cfg(test)] |
| 416 | mod tests { |
| 417 | #[test] |
| 418 | fn a_real_token_survives_the_round_trip_unchanged() { |
| 419 | let token = "b2a9c17e4d5f80316a7c9e2b4d8f0135b2a9c17e4d5f80316a7c9e2b4d8f0135"; |
| 420 | |
| 421 | assert_eq!(reset_url(token, None), format!("/auth/reset/{token}")); |
| 422 | assert_eq!( |
| 423 | reset_url(token, Some("mismatch")), |
| 424 | format!("/auth/reset/{token}?error=mismatch") |
| 425 | ); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | #[test] |
| 432 | fn a_token_carrying_a_newline_cannot_reach_a_header() { |
| 433 | let url = reset_url("a\r\nb", None); |
| 434 | |
| 435 | assert_eq!(url, "/auth/reset/a%0D%0Ab"); |
| 436 | assert!(topcoat::router::HeaderValue::try_from(url).is_ok()); |
| 437 | } |
| 438 | |
| 439 | use std::time::Duration; |
| 440 | |
| 441 | use super::*; |
| 442 | |
| 443 | #[test] |
| 444 | fn a_refusal_code_becomes_words_this_module_owns() { |
| 445 | assert_eq!( |
| 446 | refusal(Some("mismatch")), |
| 447 | "Those two passwords are different." |
| 448 | ); |
| 449 | assert_eq!(refusal(Some("short")), Password::RULE); |
| 450 | assert_eq!(refusal(None), ""); |
| 451 | } |
| 452 | |
| 453 | #[test] |
| 454 | fn every_refusal_a_use_case_can_give_has_a_code() { |
| 455 | |
| 456 | |
| 457 | |
| 458 | assert_eq!(refusal_code("the two did not match"), "mismatch"); |
| 459 | assert_eq!(refusal_code(Password::RULE), "short"); |
| 460 | assert_eq!(refusal_code("must be at most 512 bytes"), "long"); |
| 461 | } |
| 462 | |
| 463 | #[test] |
| 464 | fn an_invented_refusal_code_cannot_put_words_on_the_page() { |
| 465 | |
| 466 | assert_eq!( |
| 467 | refusal(Some("<script>alert(1)</script>")), |
| 468 | "That did not work. Try again." |
| 469 | ); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn a_deadline_is_rounded_up_so_a_live_link_never_reads_zero() { |
| 474 | let now = SystemTime::now(); |
| 475 | |
| 476 | assert_eq!(remaining(now + Duration::from_secs(40)), "1 minute"); |
| 477 | assert_eq!(remaining(now + Duration::from_secs(61)), "2 minutes"); |
| 478 | assert_eq!(remaining(now + Duration::from_secs(29 * 60)), "29 minutes"); |
| 479 | assert_eq!(remaining(now - Duration::from_secs(10)), "1 minute"); |
| 480 | } |
| 481 | } |