| 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 quasi_declare::declare; |
| 44 |
use quasi_router::screen::Choice; |
| 45 |
use quasi_router::{Method, Request, Response, RouteError}; |
| 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 = viewer.reader()?; |
| 107 |
let user_id = user.id; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
let keys = viewer |
| 112 |
.block_on(db::ssh_keys::list_keys_by_user(&viewer.app.db, user_id)) |
| 113 |
.map_err(|_| RouteError::internal("your keys could not be read"))?; |
| 114 |
let tokens = viewer |
| 115 |
.block_on(db::git_access_tokens::list_by_user(&viewer.app.db, user_id)) |
| 116 |
.map_err(|_| RouteError::internal("your tokens could not be read"))?; |
| 117 |
let profile = viewer |
| 118 |
.block_on(db::users::get_user_by_id(&viewer.app.db, user_id)) |
| 119 |
.map_err(|_| RouteError::internal("your account could not be read"))? |
| 120 |
.ok_or_else(|| RouteError::not_found("that account is gone"))?; |
| 121 |
|
| 122 |
let keys: Vec<KeyView> = keys |
| 123 |
.iter() |
| 124 |
.map(|k| KeyView { |
| 125 |
id: k.id.to_string(), |
| 126 |
fingerprint: k.fingerprint.clone(), |
| 127 |
label: k.label.clone(), |
| 128 |
added: k.created_at.format("%b %d, %Y").to_string(), |
| 129 |
}) |
| 130 |
.collect(); |
| 131 |
let tokens: Vec<TokenView> = tokens |
| 132 |
.iter() |
| 133 |
.map(|t| TokenView { |
| 134 |
id: t.id.to_string(), |
| 135 |
name: t.name.clone(), |
| 136 |
scope: if t.can_push { "Read + push" } else { "Read" }, |
| 137 |
expires: never_or(t.expires_at), |
| 138 |
last_used: never_or(t.last_used_at), |
| 139 |
}) |
| 140 |
.collect(); |
| 141 |
let themes = crate::theming::console_theme_options(profile.console_theme.as_deref()); |
| 142 |
|
| 143 |
Ok(Response::fragment( |
| 144 |
REGION, |
| 145 |
pane(user.username.as_ref(), &keys, &tokens, &themes), |
| 146 |
)) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
fn captured(captures: &quasi_router::Params) -> Result<uuid::Uuid, RouteError> { |
| 151 |
captures |
| 152 |
.get("id") |
| 153 |
.and_then(|id| id.parse().ok()) |
| 154 |
.ok_or_else(|| RouteError::not_found("no such thing")) |
| 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.reader()?.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.reader()?.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 |
declare! { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
shape pane( |
| 206 |
username: &str, |
| 207 |
keys: &[KeyView], |
| 208 |
tokens: &[TokenView], |
| 209 |
themes: &[ThemeOption], |
| 210 |
) -> Node; |
| 211 |
|
| 212 |
region REGION as Pane { |
| 213 |
section "SSH Keys"; |
| 214 |
text "Manage SSH keys for git clone and push access. \ |
| 215 |
Clone URL: git@makenot.work:{username}/{{repo}}.git"; |
| 216 |
include keys_list(keys); |
| 217 |
include add_key_form(); |
| 218 |
|
| 219 |
section "Console theme"; |
| 220 |
text "Color palette for your terminal dashboard over ssh makenot.work."; |
| 221 |
include theme_form(themes); |
| 222 |
|
| 223 |
section "Access Tokens (HTTPS)"; |
| 224 |
text "Personal access tokens for git over HTTPS. Use a token as the password. \ |
| 225 |
Clone URL: https://<token>@makenot.work/{username}/{{repo}}.git"; |
| 226 |
include tokens_list(tokens); |
| 227 |
include add_token_form(); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
declare! { |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
shape keys_list(keys: &[KeyView]) -> Node; |
| 244 |
|
| 245 |
given keys.is_empty() { |
| 246 |
true -> empty "No SSH keys registered."; |
| 247 |
otherwise -> table { |
| 248 |
column "Fingerprint" { |
| 249 |
width Fill; |
| 250 |
priority Essential; |
| 251 |
} |
| 252 |
column "Label" { |
| 253 |
width Content; |
| 254 |
} |
| 255 |
column "Added" { |
| 256 |
width Content; |
| 257 |
priority Optional; |
| 258 |
} |
| 259 |
column "" { |
| 260 |
width Content; |
| 261 |
priority Essential; |
| 262 |
} |
| 263 |
|
| 264 |
for key in keys.iter() { |
| 265 |
cells { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
cell "" { |
| 272 |
literal key.fingerprint.clone(); |
| 273 |
} |
| 274 |
cell key.label.clone(); |
| 275 |
cell "Added {key.added}"; |
| 276 |
cell "" { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
act "Remove" to delete "{PATH}/keys/{key.id}" awaiting { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
confirm "Remove this SSH key?"; |
| 290 |
tone Danger; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
declare! { |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
shape add_key_form() -> Node; |
| 308 |
|
| 309 |
form post "/api/users/me/ssh-keys" awaiting { |
| 310 |
submit "Add SSH Key"; |
| 311 |
field Textarea "public_key" "Public Key" { |
| 312 |
required; |
| 313 |
hint "Paste the contents of your ~/.ssh/id_ed25519.pub or similar public key file"; |
| 314 |
} |
| 315 |
field Text "label" "Label"; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
fn chosen_theme(themes: &[ThemeOption]) -> String { |
| 325 |
themes |
| 326 |
.iter() |
| 327 |
.find(|theme| theme.selected) |
| 328 |
.map(|theme| theme.id.clone()) |
| 329 |
.unwrap_or_default() |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
fn a_theme_is_chosen(themes: &[ThemeOption]) -> bool { |
| 334 |
themes.iter().any(|theme| theme.selected) |
| 335 |
} |
| 336 |
|
| 337 |
declare! { |
| 338 |
|
| 339 |
shape theme_form(themes: &[ThemeOption]) -> Node; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
form put "/api/users/me/console-theme" awaiting { |
| 345 |
submit "Save Theme"; |
| 346 |
field Select "theme_id" "Console theme" { |
| 347 |
hint "Following the terminal picks a light or dark palette from what your \ |
| 348 |
terminal reports. Separate from your profile theme, which is what \ |
| 349 |
visitors see."; |
| 350 |
for theme in themes.iter() { |
| 351 |
option Choice::new(theme.id.clone(), theme.name.clone()); |
| 352 |
} |
| 353 |
value chosen_theme(themes) when a_theme_is_chosen(themes); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
declare! { |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
shape tokens_list(tokens: &[TokenView]) -> Node; |
| 372 |
|
| 373 |
given tokens.is_empty() { |
| 374 |
true -> empty "No access tokens."; |
| 375 |
otherwise -> table { |
| 376 |
column "Name" { |
| 377 |
width Fill; |
| 378 |
priority Essential; |
| 379 |
} |
| 380 |
column "Scope" { |
| 381 |
width Content; |
| 382 |
} |
| 383 |
column "Expires" { |
| 384 |
width Content; |
| 385 |
priority Optional; |
| 386 |
} |
| 387 |
column "Last used" { |
| 388 |
width Content; |
| 389 |
priority Optional; |
| 390 |
} |
| 391 |
column "" { |
| 392 |
width Content; |
| 393 |
priority Essential; |
| 394 |
} |
| 395 |
|
| 396 |
for token in tokens.iter() { |
| 397 |
cells { |
| 398 |
cell token.name.clone(); |
| 399 |
cell token.scope; |
| 400 |
cell token.expires.clone(); |
| 401 |
cell token.last_used.clone(); |
| 402 |
cell "" { |
| 403 |
act "Revoke" to delete "{PATH}/tokens/{token.id}" awaiting { |
| 404 |
confirm "Revoke this token?"; |
| 405 |
tone Danger; |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
declare! { |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
shape add_token_form() -> Node; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
form post "/api/users/me/git-tokens" awaiting { |
| 430 |
submit "Create Token"; |
| 431 |
field Text "name" "Name" { |
| 432 |
required; |
| 433 |
} |
| 434 |
field Date "expires_on" "Expires (optional)" { |
| 435 |
hint "Leave blank for a token that does not expire."; |
| 436 |
} |
| 437 |
field Checkbox "can_push" "Allow push (write access)"; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
Webview::new().with_shell(viewer.shell()) |
| 451 |
} |
| 452 |
|
| 453 |
#[cfg(test)] |
| 454 |
mod tests { |
| 455 |
use super::*; |
| 456 |
use quasi_axum::Serves; |
| 457 |
use quasi_router::Node; |
| 458 |
|
| 459 |
fn key(id: &str, fingerprint: &str) -> KeyView { |
| 460 |
KeyView { |
| 461 |
id: id.into(), |
| 462 |
fingerprint: fingerprint.into(), |
| 463 |
label: "laptop".into(), |
| 464 |
added: "Aug 10, 2026".into(), |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
fn render(node: &Node) -> String { |
| 469 |
Webview::new().fragment(node) |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn the_region_matches_what_the_strip_draws() { |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
let nav = crate::quasi::settings_tabs::html( |
| 481 |
0, |
| 482 |
"", |
| 483 |
crate::quasi::settings_tabs::Gates { |
| 484 |
has_media: true, |
| 485 |
git_enabled: true, |
| 486 |
has_mt_memberships: true, |
| 487 |
has_sync_apps: true, |
| 488 |
}, |
| 489 |
); |
| 490 |
assert!( |
| 491 |
nav.contains(&format!("id=\"{REGION}\"")), |
| 492 |
"the settings strip draws #{REGION}:\n{nav}" |
| 493 |
); |
| 494 |
assert!(nav.contains(&format!("hx-get=\"{PATH}\"")), "{nav}"); |
| 495 |
} |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn an_empty_account_says_so_rather_than_showing_two_empty_lists() { |
| 499 |
let html = render(&pane("max", &[], &[], &[])); |
| 500 |
assert!(html.contains("No SSH keys registered.")); |
| 501 |
assert!(html.contains("No access tokens.")); |
| 502 |
} |
| 503 |
|
| 504 |
#[test] |
| 505 |
fn the_clone_urls_carry_the_viewers_own_name_and_cannot_smuggle_markup() { |
| 506 |
let html = render(&pane("max", &[], &[], &[])); |
| 507 |
assert!(html.contains("git@makenot.work:max/{repo}.git")); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
let hostile = render(&pane("<script>x()</script>", &[], &[], &[])); |
| 513 |
assert!(!hostile.contains("<script>x()")); |
| 514 |
} |
| 515 |
|
| 516 |
#[test] |
| 517 |
fn removing_a_key_asks_first_and_every_key_asks_about_itself() { |
| 518 |
let html = render(&keys_list(&[ |
| 519 |
key("k1", "SHA256:aaa"), |
| 520 |
key("k2", "SHA256:bbb"), |
| 521 |
])); |
| 522 |
|
| 523 |
assert!(html.contains("SHA256:aaa") && html.contains("SHA256:bbb")); |
| 524 |
assert_eq!(html.matches("hx-confirm").count(), 2, "both ask: {html}"); |
| 525 |
|
| 526 |
|
| 527 |
assert!(html.contains(&format!("hx-delete=\"{PATH}/keys/k1\""))); |
| 528 |
assert!(html.contains(&format!("hx-delete=\"{PATH}/keys/k2\""))); |
| 529 |
} |
| 530 |
|
| 531 |
#[test] |
| 532 |
fn what_destroys_something_is_marked_destructive_and_nothing_else_is() { |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
let html = render(&pane( |
| 538 |
"max", |
| 539 |
&[key("k1", "SHA256:aaa")], |
| 540 |
&[TokenView { |
| 541 |
id: "t1".into(), |
| 542 |
name: "laptop".into(), |
| 543 |
scope: "read", |
| 544 |
expires: "Never".into(), |
| 545 |
last_used: "Never".into(), |
| 546 |
}], |
| 547 |
&[], |
| 548 |
)); |
| 549 |
|
| 550 |
assert_eq!( |
| 551 |
html.matches(r#"data-tone="danger""#).count(), |
| 552 |
2, |
| 553 |
"removing a key and revoking a token, and nothing else: {html}" |
| 554 |
); |
| 555 |
|
| 556 |
|
| 557 |
assert_eq!( |
| 558 |
html.matches("hx-confirm").count(), |
| 559 |
2, |
| 560 |
"and both of them ask first: {html}" |
| 561 |
); |
| 562 |
assert!(html.contains("Remove this SSH key?"), "{html}"); |
| 563 |
assert!(html.contains("Revoke this token?"), "{html}"); |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn the_theme_picker_offers_every_theme_and_marks_the_chosen_one() { |
| 568 |
let themes = crate::theming::console_theme_options(Some("nord")); |
| 569 |
let html = render(&theme_form(&themes)); |
| 570 |
|
| 571 |
assert!(html.contains("nord")); |
| 572 |
assert_eq!( |
| 573 |
html.matches("selected").count(), |
| 574 |
1, |
| 575 |
"exactly one theme is current: {html}" |
| 576 |
); |
| 577 |
} |
| 578 |
|
| 579 |
#[test] |
| 580 |
fn every_write_on_this_screen_locks_itself_while_it_waits() { |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
let html = render(&pane("max", &[key("k1", "SHA256:aaa")], &[], &[])); |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
assert_eq!( |
| 590 |
html.matches(r#"hx-disable="find button[type='submit']""#) |
| 591 |
.count(), |
| 592 |
3, |
| 593 |
"every form guards its submit: {html}" |
| 594 |
); |
| 595 |
|
| 596 |
|
| 597 |
assert!( |
| 598 |
html.contains(r#"hx-disable="this""#), |
| 599 |
"the remove control locks itself: {html}" |
| 600 |
); |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
assert!(!html.contains("data-awaiting-amount"), "{html}"); |
| 605 |
assert_eq!( |
| 606 |
html.matches(r#"data-awaiting="indeterminate""#).count(), |
| 607 |
4, |
| 608 |
"three forms and one remove: {html}" |
| 609 |
); |
| 610 |
} |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn the_forms_post_to_the_addresses_the_api_actually_answers() { |
| 614 |
|
| 615 |
|
| 616 |
let html = render(&pane("max", &[], &[], &[])); |
| 617 |
for address in [ |
| 618 |
"/api/users/me/ssh-keys", |
| 619 |
"/api/users/me/console-theme", |
| 620 |
"/api/users/me/git-tokens", |
| 621 |
] { |
| 622 |
assert!(html.contains(address), "{address} is posted to: {html}"); |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
|