| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
use makeover_layout as layout; |
| 62 |
use quasi_router::screen::Field; |
| 63 |
use quasi_router::{Action, Document, Node, RegionKind, Screen as Described, Slot}; |
| 64 |
use quasi_webview::Webview; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
pub const PAGE_REGION: &str = "login-container"; |
| 71 |
|
| 72 |
|
| 73 |
pub const LOGIN_FEEDBACK: &str = "login-errors"; |
| 74 |
|
| 75 |
|
| 76 |
pub const FEEDBACK: &str = "form-feedback"; |
| 77 |
|
| 78 |
|
| 79 |
const MEASURE: layout::Measure = layout::Measure::Contained; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#[must_use] |
| 89 |
pub fn renderer(csrf: &str, tail: &str) -> Webview { |
| 90 |
Webview::new().with_shell( |
| 91 |
crate::shell::described() |
| 92 |
.sending("X-CSRF-Token", csrf) |
| 93 |
.with_body_last(format!("{}{tail}", crate::shell::body_last())) |
| 94 |
.with_body_first(format!( |
| 95 |
"{}{}", |
| 96 |
crate::shell::skip_link(PAGE_REGION), |
| 97 |
crate::shell::wordmark() |
| 98 |
)) |
| 99 |
.with_head(format!( |
| 100 |
"<meta name=\"csrf-token\" content=\"{}\">", |
| 101 |
crate::helpers::escape_html(csrf) |
| 102 |
)), |
| 103 |
) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
const PASSKEY: &str = concat!( |
| 118 |
"<div class=\"login-divider\">or</div>", |
| 119 |
"<button class=\"btn-secondary login-passkey-btn\" data-action=\"loginWithPasskey\">", |
| 120 |
"Sign in with Passkey (fingerprint, face, or security key)</button>", |
| 121 |
"<div id=\"passkey-login-error\" class=\"login-passkey-error\"></div>", |
| 122 |
); |
| 123 |
|
| 124 |
|
| 125 |
pub const PASSKEY_REGION: &str = "passkey-login"; |
| 126 |
|
| 127 |
|
| 128 |
const LOGIN_SCRIPTS: &str = concat!( |
| 129 |
"<script src=\"/static/passkey.js\"></script>", |
| 130 |
"<script src=\"/static/page-login-2.js?v=0623\" defer></script>", |
| 131 |
); |
| 132 |
|
| 133 |
|
| 134 |
#[must_use] |
| 135 |
pub fn document(csrf: Option<&str>, screen: &Described) -> String { |
| 136 |
use quasi_axum::Serves as _; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
let offers_passkey = screen.slot(PASSKEY_REGION).is_some(); |
| 143 |
let mut webview = renderer( |
| 144 |
csrf.unwrap_or_default(), |
| 145 |
if offers_passkey { LOGIN_SCRIPTS } else { "" }, |
| 146 |
); |
| 147 |
if offers_passkey { |
| 148 |
webview = webview.with_fill(PASSKEY_REGION, PASSKEY); |
| 149 |
} |
| 150 |
webview.screen(screen) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#[must_use] |
| 166 |
pub fn answered( |
| 167 |
id: &str, |
| 168 |
tone: layout::Tone, |
| 169 |
message: &str, |
| 170 |
onward: Option<(&str, &str)>, |
| 171 |
) -> String { |
| 172 |
use quasi_axum::Serves as _; |
| 173 |
|
| 174 |
let mut slot = Slot::new(id, RegionKind::Pane).with(Node::banner(tone, message)); |
| 175 |
if let Some((route, label)) = onward { |
| 176 |
slot = slot.with(Node::act(label, Action::get(route).navigating())); |
| 177 |
} |
| 178 |
Webview::new().fragment(&Node::Region(slot)) |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
fn page(title: &str, body: Slot) -> Described { |
| 184 |
Described::single(title) |
| 185 |
.measured(MEASURE) |
| 186 |
.documented(Document::default().classed(crate::shell::body_class(MEASURE, &[]))) |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
.indexed(false) |
| 191 |
.with(body) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
fn feedback(id: &str, error: Option<&str>) -> Node { |
| 200 |
let mut slot = Slot::new(id, RegionKind::Pane); |
| 201 |
if let Some(message) = error { |
| 202 |
slot = slot.with(Node::banner(layout::Tone::Danger, message)); |
| 203 |
} |
| 204 |
Node::Region(slot) |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
fn back_to_login() -> Node { |
| 209 |
Node::Link { |
| 210 |
text: "Back to login".to_owned(), |
| 211 |
action: Action::get("/login").navigating(), |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#[must_use] |
| 222 |
pub fn login( |
| 223 |
prefill: &str, |
| 224 |
error: Option<&str>, |
| 225 |
notice: Option<&str>, |
| 226 |
sso_enabled: bool, |
| 227 |
) -> Described { |
| 228 |
let mut body = Slot::new(PAGE_REGION, RegionKind::Pane); |
| 229 |
|
| 230 |
if let Some(note) = notice { |
| 231 |
body = body.with(Node::banner(layout::Tone::Info, note)); |
| 232 |
} |
| 233 |
|
| 234 |
if sso_enabled { |
| 235 |
return page( |
| 236 |
"Log In - Makenotwork", |
| 237 |
body.with(feedback(LOGIN_FEEDBACK, error)) |
| 238 |
.with(Node::section("Log in")) |
| 239 |
.with(Node::text( |
| 240 |
"testnot.work is a preview of makenot.work. Sign in with your \ |
| 241 |
makenot.work account to continue. Your password is only ever \ |
| 242 |
entered on makenot.work.", |
| 243 |
)) |
| 244 |
.with(Node::act( |
| 245 |
"Sign in with Makenot.work", |
| 246 |
Action::get("/sso/login").navigating(), |
| 247 |
)), |
| 248 |
); |
| 249 |
} |
| 250 |
|
| 251 |
let mut login_field = Field::new(layout::FieldKind::Text, "login", "Username or Email") |
| 252 |
.required() |
| 253 |
|
| 254 |
|
| 255 |
.value(prefill); |
| 256 |
login_field.placeholder = Some("username or you@example.com".to_owned()); |
| 257 |
|
| 258 |
let mut password = Field::new(layout::FieldKind::Secret, "password", "Password").required(); |
| 259 |
password.placeholder = Some("--------".to_owned()); |
| 260 |
|
| 261 |
page( |
| 262 |
"Log In - Makenotwork", |
| 263 |
body.with(feedback(LOGIN_FEEDBACK, error)) |
| 264 |
.with(Node::section("Log in")) |
| 265 |
.with(Node::Form { |
| 266 |
action: Action::post("/login").replacing(LOGIN_FEEDBACK), |
| 267 |
submit: "Log In".to_owned(), |
| 268 |
fields: vec![ |
| 269 |
login_field, |
| 270 |
password, |
| 271 |
Field::new(layout::FieldKind::Checkbox, "remember_me", "Remember me"), |
| 272 |
], |
| 273 |
}) |
| 274 |
.with(Node::Link { |
| 275 |
text: "Reset Password".to_owned(), |
| 276 |
action: Action::get("/forgot-password").navigating(), |
| 277 |
}) |
| 278 |
.with(Node::Link { |
| 279 |
text: "Join now".to_owned(), |
| 280 |
action: Action::get("/join").navigating(), |
| 281 |
}) |
| 282 |
|
| 283 |
|
| 284 |
.with(Node::Region(Slot::handover( |
| 285 |
PASSKEY_REGION, |
| 286 |
"the passkey offer", |
| 287 |
))), |
| 288 |
) |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
.opening_at("login") |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
#[must_use] |
| 297 |
pub fn forgot_password() -> Described { |
| 298 |
let mut email = Field::new(layout::FieldKind::Email, "email", "Email").required(); |
| 299 |
email.placeholder = Some("you@example.com".to_owned()); |
| 300 |
|
| 301 |
page( |
| 302 |
"Reset Password - Makenotwork", |
| 303 |
Slot::new(PAGE_REGION, RegionKind::Pane) |
| 304 |
.with(feedback(FEEDBACK, None)) |
| 305 |
.with(Node::section("Reset Password")) |
| 306 |
.with(Node::text( |
| 307 |
"Enter your email address and we'll send you a link to reset your password.", |
| 308 |
)) |
| 309 |
.with(Node::Form { |
| 310 |
action: Action::post("/forgot-password").replacing(FEEDBACK), |
| 311 |
submit: "Send Reset Link".to_owned(), |
| 312 |
fields: vec![email], |
| 313 |
}) |
| 314 |
.with(back_to_login()), |
| 315 |
) |
| 316 |
.opening_at("email") |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
#[must_use] |
| 329 |
pub fn reset_password(valid: bool, token: &str, error: Option<&str>) -> Described { |
| 330 |
let mut body = Slot::new(PAGE_REGION, RegionKind::Pane).with(feedback(FEEDBACK, error)); |
| 331 |
|
| 332 |
if valid { |
| 333 |
let mut password = Field::new(layout::FieldKind::Secret, "password", "New Password") |
| 334 |
.required() |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
.hint("At least 8 characters"); |
| 339 |
password.placeholder = Some("--------".to_owned()); |
| 340 |
|
| 341 |
let mut confirm = Field::new( |
| 342 |
layout::FieldKind::Secret, |
| 343 |
"password_confirm", |
| 344 |
"Confirm Password", |
| 345 |
) |
| 346 |
.required(); |
| 347 |
confirm.placeholder = Some("--------".to_owned()); |
| 348 |
|
| 349 |
body = body |
| 350 |
.with(Node::section("Set New Password")) |
| 351 |
.with(Node::text("Enter your new password below.")) |
| 352 |
.with(Node::Form { |
| 353 |
action: Action::post("/reset-password") |
| 354 |
.with("token", token) |
| 355 |
.replacing(FEEDBACK), |
| 356 |
submit: "Set Password".to_owned(), |
| 357 |
fields: vec![password, confirm], |
| 358 |
}); |
| 359 |
} else { |
| 360 |
body = body |
| 361 |
.with(Node::section("Link Expired")) |
| 362 |
.with(Node::text( |
| 363 |
"This password reset link has expired or is invalid. Please request a new one.", |
| 364 |
)) |
| 365 |
.with(Node::act( |
| 366 |
"Request New Link", |
| 367 |
Action::get("/forgot-password").navigating(), |
| 368 |
)); |
| 369 |
} |
| 370 |
|
| 371 |
let screen = page("Set New Password - Makenotwork", body.with(back_to_login())); |
| 372 |
if valid { |
| 373 |
screen.opening_at("password") |
| 374 |
} else { |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
screen |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
#[must_use] |
| 384 |
pub fn two_factor(error: Option<&str>) -> Described { |
| 385 |
let mut code = Field::new(layout::FieldKind::Text, "code", "Verification Code").required(); |
| 386 |
code.placeholder = Some("000000".to_owned()); |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
code.max_length = Some(8); |
| 391 |
|
| 392 |
page( |
| 393 |
"Two-Factor Authentication - Makenotwork", |
| 394 |
Slot::new(PAGE_REGION, RegionKind::Pane) |
| 395 |
.with(feedback(FEEDBACK, error)) |
| 396 |
.with(Node::section("Two-Factor Authentication")) |
| 397 |
.with(Node::text( |
| 398 |
"Enter the 6-digit code from your authenticator app, or use a backup code.", |
| 399 |
)) |
| 400 |
.with(Node::Form { |
| 401 |
action: Action::post("/auth/verify-2fa").replacing(FEEDBACK), |
| 402 |
submit: "Verify".to_owned(), |
| 403 |
fields: vec![code], |
| 404 |
}) |
| 405 |
.with(back_to_login()), |
| 406 |
) |
| 407 |
.opening_at("code") |
| 408 |
} |
| 409 |
|
| 410 |
#[cfg(test)] |
| 411 |
mod tests { |
| 412 |
use super::*; |
| 413 |
|
| 414 |
fn html(screen: &Described) -> String { |
| 415 |
document(Some("tok&en"), screen) |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
#[test] |
| 421 |
fn every_page_opens_where_its_template_put_the_caret() { |
| 422 |
for (screen, name) in [ |
| 423 |
(login("", None, None, false), "login"), |
| 424 |
(forgot_password(), "email"), |
| 425 |
(reset_password(true, "t", None), "password"), |
| 426 |
(two_factor(None), "code"), |
| 427 |
] { |
| 428 |
assert_eq!( |
| 429 |
screen.opens_at.as_deref(), |
| 430 |
Some(name), |
| 431 |
"{} opened somewhere else", |
| 432 |
screen.title |
| 433 |
); |
| 434 |
let rendered = html(&screen); |
| 435 |
assert_eq!( |
| 436 |
rendered.matches("autofocus").count(), |
| 437 |
1, |
| 438 |
"{} emitted more or less than one caret: {rendered}", |
| 439 |
screen.title |
| 440 |
); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
#[test] |
| 448 |
fn an_expired_link_has_no_caret_to_place() { |
| 449 |
let screen = reset_password(false, "", None); |
| 450 |
assert!(screen.opens_at.is_none()); |
| 451 |
assert!(!html(&screen).contains("autofocus")); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
#[test] |
| 457 |
fn the_reset_token_travels_with_the_call() { |
| 458 |
let rendered = html(&reset_password(true, "a-real-token", None)); |
| 459 |
assert!(rendered.contains("a-real-token"), "{rendered}"); |
| 460 |
assert!( |
| 461 |
!rendered.contains("type=\"hidden\""), |
| 462 |
"the token is markup again: {rendered}" |
| 463 |
); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
#[test] |
| 469 |
fn every_form_aims_at_the_region_its_handler_answers_into() { |
| 470 |
assert!(html(&login("", None, None, false)).contains(LOGIN_FEEDBACK)); |
| 471 |
for screen in [ |
| 472 |
forgot_password(), |
| 473 |
reset_password(true, "t", None), |
| 474 |
two_factor(None), |
| 475 |
] { |
| 476 |
assert!(html(&screen).contains(FEEDBACK), "{}", screen.title); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
#[test] |
| 483 |
fn a_refused_login_keeps_what_was_typed_and_says_why() { |
| 484 |
let rendered = html(&login( |
| 485 |
"areader", |
| 486 |
Some("Invalid username or password"), |
| 487 |
None, |
| 488 |
false, |
| 489 |
)); |
| 490 |
assert!(rendered.contains("areader"), "{rendered}"); |
| 491 |
assert!( |
| 492 |
rendered.contains("Invalid username or password"), |
| 493 |
"{rendered}" |
| 494 |
); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
#[test] |
| 500 |
fn the_sso_shape_offers_one_way_in_and_no_form() { |
| 501 |
let screen = login("", None, None, true); |
| 502 |
assert!(screen.opens_at.is_none()); |
| 503 |
let rendered = html(&screen); |
| 504 |
assert!(rendered.contains("/sso/login"), "{rendered}"); |
| 505 |
assert!(!rendered.contains("name=\"password\""), "{rendered}"); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
#[test] |
| 511 |
fn the_login_page_hands_over_the_passkey_offer_and_carries_its_scripts() { |
| 512 |
let rendered = html(&login("", None, None, false)); |
| 513 |
assert!(rendered.contains("loginWithPasskey"), "{rendered}"); |
| 514 |
assert!(rendered.contains("page-login-2.js"), "{rendered}"); |
| 515 |
assert!(rendered.contains("passkey.js"), "{rendered}"); |
| 516 |
|
| 517 |
|
| 518 |
let other = html(&forgot_password()); |
| 519 |
assert!(!other.contains("passkey"), "{other}"); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
#[test] |
| 525 |
fn none_of_these_pages_is_indexable() { |
| 526 |
for screen in [ |
| 527 |
login("", None, None, false), |
| 528 |
forgot_password(), |
| 529 |
reset_password(true, "t", None), |
| 530 |
two_factor(None), |
| 531 |
] { |
| 532 |
assert!(!screen.discovery.indexable, "{}", screen.title); |
| 533 |
assert!(html(&screen).contains("noindex"), "{}", screen.title); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
#[test] |
| 540 |
fn these_pages_show_the_wordmark_and_no_site_header() { |
| 541 |
let rendered = html(&login("", None, None, false)); |
| 542 |
assert!(rendered.contains("brand-h1"), "{rendered}"); |
| 543 |
assert!(!rendered.contains("chrome-band"), "{rendered}"); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
#[test] |
| 549 |
fn these_pages_spell_no_spinner() { |
| 550 |
for screen in [ |
| 551 |
login("", None, None, false), |
| 552 |
forgot_password(), |
| 553 |
reset_password(true, "t", None), |
| 554 |
two_factor(None), |
| 555 |
] { |
| 556 |
let rendered = html(&screen); |
| 557 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 558 |
assert!( |
| 559 |
!rendered.contains(spelling), |
| 560 |
"{spelling} in {}", |
| 561 |
screen.title |
| 562 |
); |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
|