| 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 |
use makeover_layout as layout; |
| 44 |
use quasi_router::screen::{Act, Cell, Cells, Choice, Column, Field}; |
| 45 |
use quasi_router::{Action, Method, Node, RegionKind, Request, Response, RouteError, Slot}; |
| 46 |
use quasi_webview::Webview; |
| 47 |
|
| 48 |
use super::Viewer; |
| 49 |
use crate::db; |
| 50 |
use crate::theming::ThemeOption; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pub const SCREEN: &str = "user_ssh_keys"; |
| 58 |
|
| 59 |
|
| 60 |
pub const PATH: &str = "/dashboard/tabs/ssh-keys"; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub const REGION: &str = "settings-ssh-keys"; |
| 70 |
|
| 71 |
|
| 72 |
const REMOVE_KEY: &str = "/keys/{id}"; |
| 73 |
|
| 74 |
|
| 75 |
const REVOKE_TOKEN: &str = "/tokens/{id}"; |
| 76 |
|
| 77 |
|
| 78 |
pub const WRITES: &[(Method, &str, super::Screen)] = &[ |
| 79 |
(Method::Delete, REMOVE_KEY, remove_key), |
| 80 |
(Method::Delete, REVOKE_TOKEN, revoke_token), |
| 81 |
]; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub struct KeyView { |
| 89 |
id: String, |
| 90 |
fingerprint: String, |
| 91 |
label: String, |
| 92 |
added: String, |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
pub struct TokenView { |
| 97 |
id: String, |
| 98 |
name: String, |
| 99 |
scope: &'static str, |
| 100 |
expires: String, |
| 101 |
last_used: String, |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
pub fn screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 106 |
let user_id = viewer.user.id; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
let keys = viewer |
| 111 |
.block_on(db::ssh_keys::list_keys_by_user(&viewer.app.db, user_id)) |
| 112 |
.map_err(|_| RouteError::internal("your keys could not be read"))?; |
| 113 |
let tokens = viewer |
| 114 |
.block_on(db::git_access_tokens::list_by_user(&viewer.app.db, user_id)) |
| 115 |
.map_err(|_| RouteError::internal("your tokens could not be read"))?; |
| 116 |
let profile = viewer |
| 117 |
.block_on(db::users::get_user_by_id(&viewer.app.db, user_id)) |
| 118 |
.map_err(|_| RouteError::internal("your account could not be read"))? |
| 119 |
.ok_or_else(|| RouteError::not_found("that account is gone"))?; |
| 120 |
|
| 121 |
let keys: Vec<KeyView> = keys |
| 122 |
.iter() |
| 123 |
.map(|k| KeyView { |
| 124 |
id: k.id.to_string(), |
| 125 |
fingerprint: k.fingerprint.clone(), |
| 126 |
label: k.label.clone(), |
| 127 |
added: k.created_at.format("%b %d, %Y").to_string(), |
| 128 |
}) |
| 129 |
.collect(); |
| 130 |
let tokens: Vec<TokenView> = tokens |
| 131 |
.iter() |
| 132 |
.map(|t| TokenView { |
| 133 |
id: t.id.to_string(), |
| 134 |
name: t.name.clone(), |
| 135 |
scope: if t.can_push { "Read + push" } else { "Read" }, |
| 136 |
expires: never_or(t.expires_at), |
| 137 |
last_used: never_or(t.last_used_at), |
| 138 |
}) |
| 139 |
.collect(); |
| 140 |
let themes = crate::theming::console_theme_options(profile.console_theme.as_deref()); |
| 141 |
|
| 142 |
Ok(Response::fragment( |
| 143 |
REGION, |
| 144 |
pane(viewer.user.username.as_ref(), &keys, &tokens, &themes), |
| 145 |
)) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
fn captured(captures: &quasi_router::Params) -> Result<uuid::Uuid, RouteError> { |
| 150 |
captures |
| 151 |
.get("id") |
| 152 |
.and_then(|id| id.parse().ok()) |
| 153 |
.ok_or_else(|| RouteError::not_found("no such thing")) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
pub fn remove_key(viewer: &Viewer, request: Request) -> Result<Response, RouteError> { |
| 166 |
|
| 167 |
|
| 168 |
let captures = request.captures; |
| 169 |
let id = captured(&captures)?; |
| 170 |
viewer |
| 171 |
.block_on(db::ssh_keys::delete_key( |
| 172 |
&viewer.app.db, |
| 173 |
id.into(), |
| 174 |
viewer.user.id, |
| 175 |
)) |
| 176 |
.map_err(|_| RouteError::internal("that key could not be removed"))?; |
| 177 |
screen(viewer, Request::get(PATH)) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
pub fn revoke_token(viewer: &Viewer, request: Request) -> Result<Response, RouteError> { |
| 182 |
let captures = request.captures; |
| 183 |
let id = captured(&captures)?; |
| 184 |
viewer |
| 185 |
.block_on(db::git_access_tokens::revoke( |
| 186 |
&viewer.app.db, |
| 187 |
id.into(), |
| 188 |
viewer.user.id, |
| 189 |
)) |
| 190 |
.map_err(|_| RouteError::internal("that token could not be revoked"))?; |
| 191 |
screen(viewer, Request::get(PATH)) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
fn never_or(at: Option<chrono::DateTime<chrono::Utc>>) -> String { |
| 196 |
at.map_or_else(|| "Never".to_owned(), |d| d.format("%b %d, %Y").to_string()) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
fn pane(username: &str, keys: &[KeyView], tokens: &[TokenView], themes: &[ThemeOption]) -> Node { |
| 205 |
Node::Region( |
| 206 |
Slot::new(REGION, RegionKind::Pane) |
| 207 |
.with(Node::section("SSH Keys")) |
| 208 |
.with(Node::text(format!( |
| 209 |
"Manage SSH keys for git clone and push access. \ |
| 210 |
Clone URL: git@makenot.work:{username}/{{repo}}.git" |
| 211 |
))) |
| 212 |
.with(keys_list(keys)) |
| 213 |
.with(add_key_form()) |
| 214 |
.with(Node::section("Console theme")) |
| 215 |
.with(Node::text( |
| 216 |
"Color palette for your terminal dashboard over ssh makenot.work.", |
| 217 |
)) |
| 218 |
.with(theme_form(themes)) |
| 219 |
.with(Node::section("Access Tokens (HTTPS)")) |
| 220 |
.with(Node::text(format!( |
| 221 |
"Personal access tokens for git over HTTPS. Use a token as the password. \ |
| 222 |
Clone URL: https://<token>@makenot.work/{username}/{{repo}}.git" |
| 223 |
))) |
| 224 |
.with(tokens_list(tokens)) |
| 225 |
.with(add_token_form()), |
| 226 |
) |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
fn keys_list(keys: &[KeyView]) -> Node { |
| 231 |
if keys.is_empty() { |
| 232 |
return Node::empty("No SSH keys registered."); |
| 233 |
} |
| 234 |
Node::Table { |
| 235 |
|
| 236 |
|
| 237 |
columns: vec![ |
| 238 |
Column::new("Fingerprint") |
| 239 |
.width(layout::Width::Fill) |
| 240 |
.priority(layout::Priority::Essential), |
| 241 |
Column::new("Label").width(layout::Width::Content), |
| 242 |
Column::new("Added") |
| 243 |
.width(layout::Width::Content) |
| 244 |
.priority(layout::Priority::Optional), |
| 245 |
Column::new("") |
| 246 |
.width(layout::Width::Content) |
| 247 |
.priority(layout::Priority::Essential), |
| 248 |
], |
| 249 |
rows: keys |
| 250 |
.iter() |
| 251 |
.map(|key| { |
| 252 |
Cells::new([ |
| 253 |
Cell::new(key.fingerprint.clone()), |
| 254 |
Cell::new(key.label.clone()), |
| 255 |
Cell::new(format!("Added {}", key.added)), |
| 256 |
Cell::acts([Act::new( |
| 257 |
"Remove", |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
Action::delete(format!("{PATH}/keys/{}", key.id)).awaiting(), |
| 267 |
) |
| 268 |
|
| 269 |
|
| 270 |
.confirm("Remove this SSH key?") |
| 271 |
.tone(layout::Tone::Danger)]), |
| 272 |
]) |
| 273 |
}) |
| 274 |
.collect(), |
| 275 |
|
| 276 |
|
| 277 |
more: None, |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
fn add_key_form() -> Node { |
| 289 |
Node::Form { |
| 290 |
action: Action::post("/api/users/me/ssh-keys").awaiting(), |
| 291 |
submit: "Add SSH Key".into(), |
| 292 |
fields: vec![ |
| 293 |
Field::new(layout::FieldKind::Textarea, "public_key", "Public Key") |
| 294 |
.required() |
| 295 |
.hint( |
| 296 |
"Paste the contents of your ~/.ssh/id_ed25519.pub or similar public key file", |
| 297 |
), |
| 298 |
Field::new(layout::FieldKind::Text, "label", "Label"), |
| 299 |
], |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
fn theme_form(themes: &[ThemeOption]) -> Node { |
| 305 |
let options: Vec<Choice> = themes |
| 306 |
.iter() |
| 307 |
.map(|t| Choice::new(t.id.clone(), t.name.clone())) |
| 308 |
.collect(); |
| 309 |
let mut field = Field::select("theme_id", "Console theme", options).hint( |
| 310 |
"Following the terminal picks a light or dark palette from what your terminal reports. \ |
| 311 |
Separate from your profile theme, which is what visitors see.", |
| 312 |
); |
| 313 |
if let Some(chosen) = themes.iter().find(|t| t.selected) { |
| 314 |
field = field.value(chosen.id.clone()); |
| 315 |
} |
| 316 |
Node::Form { |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
action: Action::put("/api/users/me/console-theme").awaiting(), |
| 321 |
submit: "Save Theme".into(), |
| 322 |
fields: vec![field], |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
fn tokens_list(tokens: &[TokenView]) -> Node { |
| 328 |
if tokens.is_empty() { |
| 329 |
return Node::empty("No access tokens."); |
| 330 |
} |
| 331 |
Node::Table { |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
columns: vec![ |
| 337 |
Column::new("Name") |
| 338 |
.width(layout::Width::Fill) |
| 339 |
.priority(layout::Priority::Essential), |
| 340 |
Column::new("Scope").width(layout::Width::Content), |
| 341 |
Column::new("Expires") |
| 342 |
.width(layout::Width::Content) |
| 343 |
.priority(layout::Priority::Optional), |
| 344 |
Column::new("Last used") |
| 345 |
.width(layout::Width::Content) |
| 346 |
.priority(layout::Priority::Optional), |
| 347 |
Column::new("") |
| 348 |
.width(layout::Width::Content) |
| 349 |
.priority(layout::Priority::Essential), |
| 350 |
], |
| 351 |
rows: tokens |
| 352 |
.iter() |
| 353 |
.map(|token| { |
| 354 |
Cells::new([ |
| 355 |
Cell::new(token.name.clone()), |
| 356 |
Cell::new(token.scope), |
| 357 |
Cell::new(token.expires.clone()), |
| 358 |
Cell::new(token.last_used.clone()), |
| 359 |
Cell::acts([Act::new( |
| 360 |
"Revoke", |
| 361 |
Action::delete(format!("{PATH}/tokens/{}", token.id)).awaiting(), |
| 362 |
) |
| 363 |
.confirm("Revoke this token?") |
| 364 |
.tone(layout::Tone::Danger)]), |
| 365 |
]) |
| 366 |
}) |
| 367 |
.collect(), |
| 368 |
|
| 369 |
|
| 370 |
more: None, |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
fn add_token_form() -> Node { |
| 384 |
Node::Form { |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
action: Action::post("/api/users/me/git-tokens").awaiting(), |
| 389 |
submit: "Create Token".into(), |
| 390 |
fields: vec![ |
| 391 |
Field::new(layout::FieldKind::Text, "name", "Name").required(), |
| 392 |
Field::new(layout::FieldKind::Date, "expires_on", "Expires (optional)") |
| 393 |
.hint("Leave blank for a token that does not expire."), |
| 394 |
Field::new( |
| 395 |
layout::FieldKind::Checkbox, |
| 396 |
"can_push", |
| 397 |
"Allow push (write access)", |
| 398 |
), |
| 399 |
], |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
Webview::new().with_shell(viewer.shell()) |
| 413 |
} |
| 414 |
|
| 415 |
#[cfg(test)] |
| 416 |
mod tests { |
| 417 |
use super::*; |
| 418 |
use quasi_axum::Serves; |
| 419 |
|
| 420 |
fn key(id: &str, fingerprint: &str) -> KeyView { |
| 421 |
KeyView { |
| 422 |
id: id.into(), |
| 423 |
fingerprint: fingerprint.into(), |
| 424 |
label: "laptop".into(), |
| 425 |
added: "Aug 10, 2026".into(), |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
fn render(node: &Node) -> String { |
| 430 |
Webview::new().fragment(node) |
| 431 |
} |
| 432 |
|
| 433 |
#[test] |
| 434 |
fn the_region_matches_what_the_strip_draws() { |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
let nav = crate::quasi::settings_tabs::html( |
| 442 |
0, |
| 443 |
"", |
| 444 |
crate::quasi::settings_tabs::Gates { |
| 445 |
has_media: true, |
| 446 |
git_enabled: true, |
| 447 |
has_mt_memberships: true, |
| 448 |
has_sync_apps: true, |
| 449 |
}, |
| 450 |
); |
| 451 |
assert!( |
| 452 |
nav.contains(&format!("id=\"{REGION}\"")), |
| 453 |
"the settings strip draws #{REGION}:\n{nav}" |
| 454 |
); |
| 455 |
assert!(nav.contains(&format!("hx-get=\"{PATH}\"")), "{nav}"); |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
fn an_empty_account_says_so_rather_than_showing_two_empty_lists() { |
| 460 |
let html = render(&pane("max", &[], &[], &[])); |
| 461 |
assert!(html.contains("No SSH keys registered.")); |
| 462 |
assert!(html.contains("No access tokens.")); |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn the_clone_urls_carry_the_viewers_own_name_and_cannot_smuggle_markup() { |
| 467 |
let html = render(&pane("max", &[], &[], &[])); |
| 468 |
assert!(html.contains("git@makenot.work:max/{repo}.git")); |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
let hostile = render(&pane("<script>x()</script>", &[], &[], &[])); |
| 474 |
assert!(!hostile.contains("<script>x()")); |
| 475 |
} |
| 476 |
|
| 477 |
#[test] |
| 478 |
fn removing_a_key_asks_first_and_every_key_asks_about_itself() { |
| 479 |
let html = render(&keys_list(&[ |
| 480 |
key("k1", "SHA256:aaa"), |
| 481 |
key("k2", "SHA256:bbb"), |
| 482 |
])); |
| 483 |
|
| 484 |
assert!(html.contains("SHA256:aaa") && html.contains("SHA256:bbb")); |
| 485 |
assert_eq!(html.matches("hx-confirm").count(), 2, "both ask: {html}"); |
| 486 |
|
| 487 |
|
| 488 |
assert!(html.contains(&format!("hx-delete=\"{PATH}/keys/k1\""))); |
| 489 |
assert!(html.contains(&format!("hx-delete=\"{PATH}/keys/k2\""))); |
| 490 |
} |
| 491 |
|
| 492 |
#[test] |
| 493 |
fn what_destroys_something_is_marked_destructive_and_nothing_else_is() { |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
let html = render(&pane( |
| 499 |
"max", |
| 500 |
&[key("k1", "SHA256:aaa")], |
| 501 |
&[TokenView { |
| 502 |
id: "t1".into(), |
| 503 |
name: "laptop".into(), |
| 504 |
scope: "read", |
| 505 |
expires: "Never".into(), |
| 506 |
last_used: "Never".into(), |
| 507 |
}], |
| 508 |
&[], |
| 509 |
)); |
| 510 |
|
| 511 |
assert_eq!( |
| 512 |
html.matches(r#"data-tone="danger""#).count(), |
| 513 |
2, |
| 514 |
"removing a key and revoking a token, and nothing else: {html}" |
| 515 |
); |
| 516 |
|
| 517 |
|
| 518 |
assert_eq!( |
| 519 |
html.matches("hx-confirm").count(), |
| 520 |
2, |
| 521 |
"and both of them ask first: {html}" |
| 522 |
); |
| 523 |
assert!(html.contains("Remove this SSH key?"), "{html}"); |
| 524 |
assert!(html.contains("Revoke this token?"), "{html}"); |
| 525 |
} |
| 526 |
|
| 527 |
#[test] |
| 528 |
fn the_theme_picker_offers_every_theme_and_marks_the_chosen_one() { |
| 529 |
let themes = crate::theming::console_theme_options(Some("nord")); |
| 530 |
let html = render(&theme_form(&themes)); |
| 531 |
|
| 532 |
assert!(html.contains("nord")); |
| 533 |
assert_eq!( |
| 534 |
html.matches("selected").count(), |
| 535 |
1, |
| 536 |
"exactly one theme is current: {html}" |
| 537 |
); |
| 538 |
} |
| 539 |
|
| 540 |
#[test] |
| 541 |
fn every_write_on_this_screen_locks_itself_while_it_waits() { |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
let html = render(&pane("max", &[key("k1", "SHA256:aaa")], &[], &[])); |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
assert_eq!( |
| 551 |
html.matches(r#"hx-disable="find button[type='submit']""#) |
| 552 |
.count(), |
| 553 |
3, |
| 554 |
"every form guards its submit: {html}" |
| 555 |
); |
| 556 |
|
| 557 |
|
| 558 |
assert!( |
| 559 |
html.contains(r#"hx-disable="this""#), |
| 560 |
"the remove control locks itself: {html}" |
| 561 |
); |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
assert!(!html.contains("data-awaiting-amount"), "{html}"); |
| 566 |
assert_eq!( |
| 567 |
html.matches(r#"data-awaiting="indeterminate""#).count(), |
| 568 |
4, |
| 569 |
"three forms and one remove: {html}" |
| 570 |
); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn the_forms_post_to_the_addresses_the_api_actually_answers() { |
| 575 |
|
| 576 |
|
| 577 |
let html = render(&pane("max", &[], &[], &[])); |
| 578 |
for address in [ |
| 579 |
"/api/users/me/ssh-keys", |
| 580 |
"/api/users/me/console-theme", |
| 581 |
"/api/users/me/git-tokens", |
| 582 |
] { |
| 583 |
assert!(html.contains(address), "{address} is posted to: {html}"); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|