Services and clients merge into one admin section
Browse files at this commitb163bda9de2cab7fb5e8cd5c366d79802717a10cParent: 1abf2418 changed files +324 -138
- crates/im-core/src/services.rs +37 -13
- crates/im-core/src/store.rs +11 -8
- crates/im-web/assets/main.css +13 -3
- crates/im-web/src/admin.rs +210 -107
- crates/im-web/src/directory.rs +49 -1
- crates/im-web/src/i18n.rs +0 -6
- crates/im-web/src/main.rs +3 -0
- style/main.scss +1 -0
crates/im-core/src/services.rsmodified+37 -13
| @@ -29,6 +29,14 @@ pub struct Service { | ||
| 29 | 29 | /// three public fields the siblings mirror, and nothing more. |
| 30 | 30 | #[serde(skip)] |
| 31 | 31 | pub owner: Option<String>, |
| 32 | + /// The relying-party credential this row answers for — the client id | |
| 33 | + /// `POST /family/register` authenticated, stamped on every register, | |
| 34 | + /// insert and refresh alike. im's own row stays `None`: the IdP is not | |
| 35 | + /// its own client. This is the panel's join key between the wordmark | |
| 36 | + /// and its Rotate/Revoke controls; a row the seed or the panel made | |
| 37 | + /// carries `None` until its app registers. | |
| 38 | + #[serde(skip)] | |
| 39 | + pub client_id: Option<String>, | |
| 32 | 40 | } |
| 33 | 41 | |
| 34 | 42 | /// The owner im writes on its own row, registered from `issuer` on every |
| @@ -84,7 +92,7 @@ pub async fn list(store: &Store) -> Result<Vec<Service>> { | ||
| 84 | 92 | let conn = store.conn.lock().await; |
| 85 | 93 | let mut rows = conn |
| 86 | 94 | .query( |
| 87 | - "SELECT key, name, url, owner FROM services ORDER BY position, key", | |
| 95 | + "SELECT key, name, url, owner, client_id FROM services ORDER BY position, key", | |
| 88 | 96 | (), |
| 89 | 97 | ) |
| 90 | 98 | .await |
| @@ -96,6 +104,7 @@ pub async fn list(store: &Store) -> Result<Vec<Service>> { | ||
| 96 | 104 | name: store::text(&row, 1)?, |
| 97 | 105 | url: store::text(&row, 2)?, |
| 98 | 106 | owner: store::opt_text(&row, 3)?, |
| 107 | + client_id: store::opt_text(&row, 4)?, | |
| 99 | 108 | }); |
| 100 | 109 | } |
| 101 | 110 | Ok(services) |
| @@ -281,6 +290,7 @@ pub async fn register( | ||
| 281 | 290 | name: &str, |
| 282 | 291 | url: &str, |
| 283 | 292 | owner: &str, |
| 293 | + client_id: Option<&str>, | |
| 284 | 294 | ) -> Result<Service> { |
| 285 | 295 | validate(key, name, url).map_err(StoreError::Invalid)?; |
| 286 | 296 | let url = url.trim_end_matches('/').to_string(); |
| @@ -292,11 +302,12 @@ pub async fn register( | ||
| 292 | 302 | } |
| 293 | 303 | Some(_) => { |
| 294 | 304 | // Claiming an unowned row or refreshing our own: the address is |
| 295 | - // the app's to say, the name stays whatever is stored. | |
| 305 | + // the app's to say, the name stays whatever is stored, and the | |
| 306 | + // credential stamp follows the caller on every register. | |
| 296 | 307 | let conn = store.conn.lock().await; |
| 297 | 308 | conn.execute( |
| 298 | - "UPDATE services SET url = ?2, owner = ?3 WHERE key = ?1", | |
| 299 | - turso::params![key, url.as_str(), owner], | |
| 309 | + "UPDATE services SET url = ?2, owner = ?3, client_id = ?4 WHERE key = ?1", | |
| 310 | + turso::params![key, url.as_str(), owner, client_id], | |
| 300 | 311 | ) |
| 301 | 312 | .await |
| 302 | 313 | .map_err(backend)?; |
| @@ -304,9 +315,9 @@ pub async fn register( | ||
| 304 | 315 | None => { |
| 305 | 316 | let conn = store.conn.lock().await; |
| 306 | 317 | conn.execute( |
| 307 | - "INSERT INTO services (key, name, url, position, owner) \ | |
| 308 | - VALUES (?1, ?2, ?3, (SELECT COALESCE(MAX(position), -1) + 1 FROM services), ?4)", | |
| 309 | - turso::params![key, name.trim(), url.as_str(), owner], | |
| 318 | + "INSERT INTO services (key, name, url, position, owner, client_id) \ | |
| 319 | + VALUES (?1, ?2, ?3, (SELECT COALESCE(MAX(position), -1) + 1 FROM services), ?4, ?5)", | |
| 320 | + turso::params![key, name.trim(), url.as_str(), owner, client_id], | |
| 310 | 321 | ) |
| 311 | 322 | .await |
| 312 | 323 | .map_err(backend)?; |
| @@ -364,6 +375,7 @@ mod tests { | ||
| 364 | 375 | name: name.to_string(), |
| 365 | 376 | url: url.to_string(), |
| 366 | 377 | owner: None, |
| 378 | + client_id: None, | |
| 367 | 379 | } |
| 368 | 380 | } |
| 369 | 381 | |
| @@ -519,6 +531,7 @@ mod tests { | ||
| 519 | 531 | "Account", |
| 520 | 532 | "http://127.0.0.1:7650/", |
| 521 | 533 | SELF_OWNER, |
| 534 | + None, | |
| 522 | 535 | ) |
| 523 | 536 | .await |
| 524 | 537 | .unwrap(); |
| @@ -527,25 +540,36 @@ mod tests { | ||
| 527 | 540 | "the slash is normalized off" |
| 528 | 541 | ); |
| 529 | 542 | assert_eq!(row.owner.as_deref(), Some(SELF_OWNER)); |
| 543 | + assert_eq!(row.client_id, None, "im is not its own client"); | |
| 530 | 544 | |
| 531 | 545 | // A seeded row belongs to nobody; the app that names it claims it, |
| 532 | 546 | // keeping the name the seed (or a later rename) gave it. |
| 533 | 547 | add(&store, &service("in", "Dosyalar", "http://127.0.0.1:7655")) |
| 534 | 548 | .await |
| 535 | 549 | .unwrap(); |
| 536 | - let row = register(&store, "in", "Files", "https://in.example", "in-client") | |
| 550 | + let row = register(&store, "in", "Files", "https://in.example", "in-client", Some("in-client")) | |
| 537 | 551 | .await |
| 538 | 552 | .unwrap(); |
| 539 | 553 | assert_eq!(row.name, "Dosyalar", "the name stays the admin's"); |
| 540 | 554 | assert_eq!(row.url, "https://in.example"); |
| 541 | 555 | assert_eq!(row.owner.as_deref(), Some("in-client")); |
| 556 | + assert_eq!( | |
| 557 | + row.client_id.as_deref(), | |
| 558 | + Some("in-client"), | |
| 559 | + "the claim stamps the credential" | |
| 560 | + ); | |
| 542 | 561 | |
| 543 | 562 | // The same app's next boot moves the address only. |
| 544 | - let row = register(&store, "in", "Whatever", "https://in.dizey.sh", "in-client") | |
| 563 | + let row = register(&store, "in", "Whatever", "https://in.dizey.sh", "in-client", Some("in-client")) | |
| 545 | 564 | .await |
| 546 | 565 | .unwrap(); |
| 547 | 566 | assert_eq!(row.name, "Dosyalar"); |
| 548 | 567 | assert_eq!(row.url, "https://in.dizey.sh"); |
| 568 | + assert_eq!( | |
| 569 | + row.client_id.as_deref(), | |
| 570 | + Some("in-client"), | |
| 571 | + "the refresh keeps the stamp current" | |
| 572 | + ); | |
| 549 | 573 | // And no row was duplicated by any of it. |
| 550 | 574 | assert_eq!(keys(&store).await, vec!["im".to_string(), "in".to_string()]); |
| 551 | 575 | } |
| @@ -553,11 +577,11 @@ mod tests { | ||
| 553 | 577 | #[tokio::test] |
| 554 | 578 | async fn a_key_another_app_keeps_is_a_conflict_and_the_row_is_untouched() { |
| 555 | 579 | let store = store().await; |
| 556 | - register(&store, "in", "Files", "https://in.example", "in-client") | |
| 580 | + register(&store, "in", "Files", "https://in.example", "in-client", Some("in-client")) | |
| 557 | 581 | .await |
| 558 | 582 | .unwrap(); |
| 559 | 583 | assert!(matches!( |
| 560 | - register(&store, "in", "Files", "https://evil.example", "iz-client").await, | |
| 584 | + register(&store, "in", "Files", "https://evil.example", "iz-client", None).await, | |
| 561 | 585 | Err(StoreError::Conflict(_)) |
| 562 | 586 | )); |
| 563 | 587 | assert_eq!(list(&store).await.unwrap()[0].url, "https://in.example"); |
| @@ -566,7 +590,7 @@ mod tests { | ||
| 566 | 590 | #[tokio::test] |
| 567 | 591 | async fn the_panel_cannot_remove_an_owned_row_or_move_its_address() { |
| 568 | 592 | let store = store().await; |
| 569 | - register(&store, "in", "Files", "https://in.example", "in-client") | |
| 593 | + register(&store, "in", "Files", "https://in.example", "in-client", Some("in-client")) | |
| 570 | 594 | .await |
| 571 | 595 | .unwrap(); |
| 572 | 596 | assert!(matches!( |
| @@ -595,7 +619,7 @@ mod tests { | ||
| 595 | 619 | #[tokio::test] |
| 596 | 620 | async fn the_family_json_never_carries_the_owner() { |
| 597 | 621 | let store = store().await; |
| 598 | - register(&store, "im", "Account", "http://127.0.0.1:7650", SELF_OWNER) | |
| 622 | + register(&store, "im", "Account", "http://127.0.0.1:7650", SELF_OWNER, None) | |
| 599 | 623 | .await |
| 600 | 624 | .unwrap(); |
| 601 | 625 | let json = serde_json::to_value(list(&store).await.unwrap()).unwrap(); |
crates/im-core/src/store.rsmodified+11 -8
| @@ -153,7 +153,8 @@ CREATE TABLE IF NOT EXISTS services ( | ||
| 153 | 153 | name TEXT NOT NULL, |
| 154 | 154 | url TEXT NOT NULL, |
| 155 | 155 | position INTEGER NOT NULL, |
| 156 | - owner TEXT | |
| 156 | + owner TEXT, | |
| 157 | + client_id TEXT | |
| 157 | 158 | ); |
| 158 | 159 | "; |
| 159 | 160 | |
| @@ -261,13 +262,15 @@ impl Store { | ||
| 261 | 262 | .await |
| 262 | 263 | .map_err(backend)?; |
| 263 | 264 | } |
| 264 | - // What a session remembers about its browser: the listing shows | |
| 265 | - // these, so old databases grow them the same guarded way. | |
| 266 | - // The app that keeps a service row: `im` for this app's own | |
| 267 | - // entry, a client id for a sibling that registers itself, NULL | |
| 268 | - // for the config seed's and the panel's own rows. | |
| 269 | - if !has_column(&conn, "services", "owner").await? { | |
| 270 | - conn.execute("ALTER TABLE services ADD COLUMN owner TEXT", ()) | |
| 265 | + // The family rows' client linkage (the merge of the panel's | |
| 266 | + // services and clients sections): a `POST /family/register` | |
| 267 | + // stamps the authenticated client's id beside its row, so the | |
| 268 | + // panel can show the wordmark and its credential as one thing. | |
| 269 | + // ALTER over the owner precedent: databases born before the | |
| 270 | + // column get it here, and every existing row reads NULL until | |
| 271 | + // its app re-registers. | |
| 272 | + if !has_column(&conn, "services", "client_id").await? { | |
| 273 | + conn.execute("ALTER TABLE services ADD COLUMN client_id TEXT", ()) | |
| 271 | 274 | .await |
| 272 | 275 | .map_err(backend)?; |
| 273 | 276 | } |
crates/im-web/assets/main.cssmodified+13 -3
| @@ -984,6 +984,13 @@ body:has(.log-list) .admin-card:has(.log-list) { | ||
| 984 | 984 | border-top: 1px solid var(--border-soft); |
| 985 | 985 | vertical-align: middle; |
| 986 | 986 | } |
| 987 | +.admin-table td.mono { | |
| 988 | + overflow-wrap: anywhere; | |
| 989 | +} | |
| 990 | + | |
| 991 | +.admin-clients td.muted { | |
| 992 | + white-space: nowrap; | |
| 993 | +} | |
| 987 | 994 | |
| 988 | 995 | .mono { |
| 989 | 996 | font-family: var(--font-mono); |
| @@ -995,6 +1002,7 @@ body:has(.log-list) .admin-card:has(.log-list) { | ||
| 995 | 1002 | |
| 996 | 1003 | .actions { |
| 997 | 1004 | display: flex; |
| 1005 | + flex-wrap: wrap; | |
| 998 | 1006 | gap: 8px; |
| 999 | 1007 | justify-content: flex-end; |
| 1000 | 1008 | } |
| @@ -1129,15 +1137,17 @@ body:has(.log-list) .admin-card:has(.log-list) { | ||
| 1129 | 1137 | } |
| 1130 | 1138 | |
| 1131 | 1139 | .admin-confirm-pop { |
| 1132 | - position: absolute; | |
| 1133 | - right: 0; | |
| 1134 | - top: calc(100% + 6px); | |
| 1140 | + position: fixed; | |
| 1141 | + top: 50%; | |
| 1142 | + left: 50%; | |
| 1143 | + transform: translate(-50%, -50%); | |
| 1135 | 1144 | z-index: 30; |
| 1136 | 1145 | width: 240px; |
| 1137 | 1146 | padding: 12px; |
| 1138 | 1147 | background: var(--surface); |
| 1139 | 1148 | border: 1px solid var(--border-strong); |
| 1140 | 1149 | border-radius: var(--radius); |
| 1150 | + box-shadow: var(--shadow-2); | |
| 1141 | 1151 | display: flex; |
| 1142 | 1152 | flex-direction: column; |
| 1143 | 1153 | gap: 8px; |
crates/im-web/src/admin.rsmodified+210 -107
| @@ -132,7 +132,6 @@ async fn admin_page(cx: &Cx) -> Result<Response> { | ||
| 132 | 132 | [ |
| 133 | 133 | ("users", t(lang, Key::NavUsers)), |
| 134 | 134 | ("services", t(lang, Key::NavServices)), |
| 135 | - ("clients", t(lang, Key::NavClients)), | |
| 136 | 135 | ("message", t(lang, Key::NavMessage)), |
| 137 | 136 | ("settings", t(lang, Key::NavSettings)), |
| 138 | 137 | ("logs", t(lang, Key::NavLogs)), |
| @@ -152,8 +151,9 @@ async fn admin_page(cx: &Cx) -> Result<Response> { | ||
| 152 | 151 | "message" => message_section(cx, lang).await?, |
| 153 | 152 | "settings" => settings_section(cx, lang).await?, |
| 154 | 153 | "logs" => logs_section(cx, lang).await?, |
| 155 | - "services" => services_section(cx, lang).await?, | |
| 156 | - "clients" => clients_section(cx, shown, lang).await?, | |
| 154 | + // `section=clients` lands here too — the sections are one table | |
| 155 | + // now, and the old address keeps working. | |
| 156 | + "services" | "clients" => services_section(cx, shown, lang).await?, | |
| 157 | 157 | _ => users_section(cx, &me, invited.as_deref(), lang).await?, |
| 158 | 158 | }; |
| 159 | 159 | |
| @@ -438,13 +438,26 @@ async fn users_section( | ||
| 438 | 438 | )) |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | -/// The family registry: the wordmarks every topbar's trio and the landing's | |
| 442 | -/// services home render, and `/family` serves. One table in the panel's own | |
| 443 | -/// skins — edit and remove as the two-step disclosures, the up/down pair as | |
| 444 | -/// plain row buttons — and the add line under it wearing the invite form's | |
| 445 | -/// skin. | |
| 446 | -async fn services_section(cx: &Cx, lang: i18n::Lang) -> Result<String, topcoat::Error> { | |
| 441 | +/// The family registry, wordmarks and credentials as one table. Every | |
| 442 | +/// service row carries its client half beside it — client id, redirect | |
| 443 | +/// URIs, registered date, Rotate/Revoke — joined by the client id its | |
| 444 | +/// `POST /family/register` authenticated; a client with no service row (a | |
| 445 | +/// test or stray credential) renders as a credential-only row, and a row | |
| 446 | +/// with no client yet shows the dash. The wordmark controls stay the | |
| 447 | +/// panel's on rows no app keeps, and the two add lines — a service, a | |
| 448 | +/// client — sit under the table. | |
| 449 | +async fn services_section( | |
| 450 | + cx: &Cx, | |
| 451 | + shown: Option<(String, String)>, | |
| 452 | + lang: i18n::Lang, | |
| 453 | +) -> Result<String, topcoat::Error> { | |
| 447 | 454 | let services = im_core::services::list(&app(cx).store).await?; |
| 455 | + let clients = im_core::oidc::list_clients(&app(cx).store).await?; | |
| 456 | + // The join: a service row's client id names the client whose wordmark | |
| 457 | + // half and credential half render as one. | |
| 458 | + let linked = |id: Option<&String>| { | |
| 459 | + id.and_then(|cid| clients.iter().find(|client| &client.client_id.to_string() == cid)) | |
| 460 | + }; | |
| 448 | 461 | let mut rows = String::new(); |
| 449 | 462 | for service in &services { |
| 450 | 463 | let key = escape(&service.key); |
| @@ -508,34 +521,141 @@ async fn services_section(cx: &Cx, lang: i18n::Lang) -> Result<String, topcoat:: | ||
| 508 | 521 | t(lang, Key::ConfirmRemove), |
| 509 | 522 | ) |
| 510 | 523 | }; |
| 524 | + let (client_cell, uris_cell, registered_cell, client_actions) = | |
| 525 | + match linked(service.client_id.as_ref()) { | |
| 526 | + Some(client) => { | |
| 527 | + let id = escape(&client.client_id.to_string()); | |
| 528 | + let uris = client | |
| 529 | + .redirect_uris | |
| 530 | + .iter() | |
| 531 | + .map(|uri| escape(uri)) | |
| 532 | + .collect::<Vec<_>>() | |
| 533 | + .join("<br>"); | |
| 534 | + let registered = client | |
| 535 | + .created_at | |
| 536 | + .format(&time::macros::format_description!( | |
| 537 | + "[year]-[month]-[day]" | |
| 538 | + )) | |
| 539 | + .unwrap_or_default(); | |
| 540 | + let controls = client_controls( | |
| 541 | + &id, | |
| 542 | + &escape(&client.name), | |
| 543 | + lang, | |
| 544 | + ); | |
| 545 | + (id, uris, registered, controls) | |
| 546 | + } | |
| 547 | + None => ("—".to_string(), "—".to_string(), "—".to_string(), String::new()), | |
| 548 | + }; | |
| 511 | 549 | rows.push_str(&format!( |
| 512 | - r#"<tr><td class="mono">{key}</td><td>{name}</td><td class="mono">{url}</td><td class="actions">{edit}{up}{down}{remove}</td></tr>"#, | |
| 550 | + r#"<tr><td class="mono">{key}</td><td>{name}</td><td class="mono">{url}</td><td class="mono">{client_cell}</td><td class="mono">{uris_cell}</td><td class="muted">{registered_cell}</td><td class="actions">{edit}{up}{down}{remove}{client_actions}</td></tr>"#, | |
| 513 | 551 | )); |
| 514 | 552 | } |
| 553 | + // Clients no service row answers for — the CLI's or the panel's test | |
| 554 | + // credentials — still sit in the table, their credential half alone. | |
| 555 | + for client in &clients { | |
| 556 | + let id = client.client_id.to_string(); | |
| 557 | + if services | |
| 558 | + .iter() | |
| 559 | + .any(|service| service.client_id.as_deref() == Some(id.as_str())) | |
| 560 | + { | |
| 561 | + continue; | |
| 562 | + } | |
| 563 | + let name = escape(&client.name); | |
| 564 | + let uris = client | |
| 565 | + .redirect_uris | |
| 566 | + .iter() | |
| 567 | + .map(|uri| escape(uri)) | |
| 568 | + .collect::<Vec<_>>() | |
| 569 | + .join("<br>"); | |
| 570 | + let registered = client | |
| 571 | + .created_at | |
| 572 | + .format(&time::macros::format_description!("[year]-[month]-[day]")) | |
| 573 | + .unwrap_or_default(); | |
| 574 | + let controls = client_controls(&id, &name, lang); | |
| 575 | + rows.push_str(&format!( | |
| 576 | + r#"<tr><td class="muted">—</td><td>{name}</td><td class="muted">—</td><td class="mono">{}</td><td class="mono">{uris}</td><td class="muted">{registered}</td><td class="actions">{controls}</td></tr>"#, | |
| 577 | + escape(&id), | |
| 578 | + )); | |
| 579 | + } | |
| 580 | + // The show-once banner: the note says what to do with it, the value is | |
| 581 | + // click-to-copy, and the shelf behind it has already handed it over. | |
| 582 | + let shown_html = shown | |
| 583 | + .map(|(client_id, secret)| { | |
| 584 | + format!( | |
| 585 | + r#"<div class="auth-note">{note}<div class="admin-copy-row"><div class="muted">{id_label}: <span class="mono">{client_id}</span></div><div class="auth-secret admin-copy-value">{secret}</div><button class="admin-action admin-copy" type="button" data-copied-label="{copied}">{copy}</button></div></div>"#, | |
| 586 | + note = t(lang, Key::SecretShownNote), | |
| 587 | + id_label = t(lang, Key::ClientIdLabel), | |
| 588 | + client_id = escape(&client_id), | |
| 589 | + secret = escape(&secret), | |
| 590 | + copied = t(lang, Key::CopiedWord), | |
| 591 | + copy = t(lang, Key::CopyWord), | |
| 592 | + ) | |
| 593 | + }) | |
| 594 | + .unwrap_or_default(); | |
| 515 | 595 | Ok(format!( |
| 516 | 596 | r#"<div class="admin-card"> |
| 517 | 597 | <div class="auth-title">{title}</div> |
| 598 | + {shown_html} | |
| 518 | 599 | <div class="admin-table-wrap"> |
| 519 | - <table class="admin-table"> | |
| 520 | - <thead><tr><th>{key_label}</th><th>{name_label}</th><th>{url_label}</th><th></th></tr></thead> | |
| 600 | + <table class="admin-table admin-clients"> | |
| 601 | + <thead><tr><th>{key_label}</th><th>{name_label}</th><th>{url_label}</th><th>{id_label}</th><th>{uris_label}</th><th>{registered_label}</th><th></th></tr></thead> | |
| 521 | 602 | <tbody>{rows}</tbody> |
| 522 | 603 | </table> |
| 523 | 604 | </div> |
| 605 | + <div class="muted">{service_add_label}</div> | |
| 524 | 606 | <form method="post" action="/admin/services_add" class="admin-invite"> |
| 525 | 607 | <input class="auth-input auth-input-mono" type="text" name="key" placeholder="in" aria-label="{key_label}" required> |
| 526 | 608 | <input class="auth-input" type="text" name="name" placeholder="{name_label}" aria-label="{name_label}" required> |
| 527 | 609 | <input class="auth-input auth-input-mono" type="text" name="url" placeholder="https://in.dizey.sh" aria-label="{url_label}" required> |
| 528 | 610 | <button class="auth-submit admin-invite-go" type="submit"><span class="auth-submit-text">{add}</span></button> |
| 529 | 611 | </form> |
| 612 | + <div class="muted">{client_add_label}</div> | |
| 613 | + <form method="post" action="/admin/clients_add" class="admin-invite"> | |
| 614 | + <input class="auth-input" type="text" name="name" placeholder="drive" aria-label="{name_label}" required> | |
| 615 | + <input class="auth-input auth-input-mono" type="text" name="redirect_uris" placeholder="http://127.0.0.1:9000/callback https://drive.dizey.sh/callback" aria-label="{uris_label}" required> | |
| 616 | + <button class="auth-submit admin-invite-go" type="submit"><span class="auth-submit-text">{client_add}</span></button> | |
| 617 | + </form> | |
| 530 | 618 | </div>"#, |
| 531 | 619 | title = t(lang, Key::ServicesTitle), |
| 532 | 620 | key_label = t(lang, Key::ServiceKeyLabel), |
| 533 | 621 | name_label = t(lang, Key::NameCol), |
| 534 | 622 | url_label = t(lang, Key::AddressLabel), |
| 623 | + id_label = t(lang, Key::ClientIdLabel), | |
| 624 | + uris_label = t(lang, Key::RedirectUrisLabel), | |
| 625 | + registered_label = t(lang, Key::RegisteredCol), | |
| 626 | + service_add_label = t(lang, Key::ServiceAdd), | |
| 535 | 627 | add = t(lang, Key::ServiceAdd), |
| 628 | + client_add_label = t(lang, Key::ClientAdd), | |
| 629 | + client_add = t(lang, Key::ClientAdd), | |
| 536 | 630 | )) |
| 537 | 631 | } |
| 538 | 632 | |
| 633 | +/// A client's Rotate/Revoke pair, as the two-step disclosures — the same | |
| 634 | +/// controls on a linked service row and on a credential-only row. | |
| 635 | +fn client_controls(client_id: &str, name_html: &str, lang: i18n::Lang) -> String { | |
| 636 | + let rotate = confirm_action( | |
| 637 | + "client", | |
| 638 | + client_id, | |
| 639 | + "/admin/clients_rotate", | |
| 640 | + t(lang, Key::RotateWord), | |
| 641 | + "", | |
| 642 | + &i18n::rotate_client_title(lang, name_html), | |
| 643 | + t(lang, Key::RotateCost), | |
| 644 | + t(lang, Key::ConfirmRotate), | |
| 645 | + ); | |
| 646 | + let revoke_action = confirm_action( | |
| 647 | + "client", | |
| 648 | + client_id, | |
| 649 | + "/admin/clients_revoke", | |
| 650 | + t(lang, Key::RevokeWord), | |
| 651 | + " admin-danger", | |
| 652 | + &i18n::revoke_client_title(lang, name_html), | |
| 653 | + t(lang, Key::RevokeCost), | |
| 654 | + t(lang, Key::ConfirmRevoke), | |
| 655 | + ); | |
| 656 | + format!("{rotate}{revoke_action}") | |
| 657 | +} | |
| 658 | + | |
| 539 | 659 | /// One user of the services forms: key, name, address. The add form fills |
| 540 | 660 | /// all three; the edit form carries the key in its hidden field. |
| 541 | 661 | #[derive(Deserialize)] |
| @@ -558,6 +678,7 @@ async fn services_add(cx: &Cx, Form(input): Form<ServiceForm>) -> Result<Respons | ||
| 558 | 678 | name: input.name, |
| 559 | 679 | url: input.url, |
| 560 | 680 | owner: None, |
| 681 | + client_id: None, | |
| 561 | 682 | }, |
| 562 | 683 | ) |
| 563 | 684 | .await; |
| @@ -633,99 +754,6 @@ async fn service_outcome( | ||
| 633 | 754 | } |
| 634 | 755 | } |
| 635 | 756 | |
| 636 | -/// The relying parties: every client the family's apps sign in with. One | |
| 637 | -/// table in the panel's own skins — rotate and revoke as the two-step | |
| 638 | -/// disclosures — and the add line under it wearing the invite form's skin. | |
| 639 | -/// A create or rotate answers a 303 whose `shown` ticket picks the fresh | |
| 640 | -/// secret off the shelf for exactly one render; the page never learns a | |
| 641 | -/// secret twice. | |
| 642 | -async fn clients_section( | |
| 643 | - cx: &Cx, | |
| 644 | - shown: Option<(String, String)>, | |
| 645 | - lang: i18n::Lang, | |
| 646 | -) -> Result<String, topcoat::Error> { | |
| 647 | - let clients = im_core::oidc::list_clients(&app(cx).store).await?; | |
| 648 | - let mut rows = String::new(); | |
| 649 | - for client in &clients { | |
| 650 | - let id = escape(&client.client_id.to_string()); | |
| 651 | - let name = escape(&client.name); | |
| 652 | - let uris = client | |
| 653 | - .redirect_uris | |
| 654 | - .iter() | |
| 655 | - .map(|uri| escape(uri)) | |
| 656 | - .collect::<Vec<_>>() | |
| 657 | - .join("<br>"); | |
| 658 | - let registered = client | |
| 659 | - .created_at | |
| 660 | - .format(&time::macros::format_description!("[year]-[month]-[day]")) | |
| 661 | - .unwrap_or_default(); | |
| 662 | - // Rotation kills the old pair the moment the new one exists, so it | |
| 663 | - // says what it costs before it does it — like every destructive row | |
| 664 | - // action here. | |
| 665 | - let rotate = confirm_action( | |
| 666 | - "client", | |
| 667 | - &id, | |
| 668 | - "/admin/clients_rotate", | |
| 669 | - t(lang, Key::RotateWord), | |
| 670 | - "", | |
| 671 | - &i18n::rotate_client_title(lang, &name), | |
| 672 | - t(lang, Key::RotateCost), | |
| 673 | - t(lang, Key::ConfirmRotate), | |
| 674 | - ); | |
| 675 | - let revoke_action = confirm_action( | |
| 676 | - "client", | |
| 677 | - &id, | |
| 678 | - "/admin/clients_revoke", | |
| 679 | - t(lang, Key::RevokeWord), | |
| 680 | - " admin-danger", | |
| 681 | - &i18n::revoke_client_title(lang, &name), | |
| 682 | - t(lang, Key::RevokeCost), | |
| 683 | - t(lang, Key::ConfirmRevoke), | |
| 684 | - ); | |
| 685 | - rows.push_str(&format!( | |
| 686 | - r#"<tr><td>{name}</td><td class="mono">{id}</td><td class="mono">{uris}</td><td class="muted">{registered}</td><td class="actions">{rotate}{revoke_action}</td></tr>"#, | |
| 687 | - )); | |
| 688 | - } | |
| 689 | - // The show-once banner: the note says what to do with it, the value is | |
| 690 | - // click-to-copy, and the shelf behind it has already handed it over. | |
| 691 | - let shown_html = shown | |
| 692 | - .map(|(client_id, secret)| { | |
| 693 | - format!( | |
| 694 | - r#"<div class="auth-note">{note}<div class="admin-copy-row"><div class="muted">{id_label}: <span class="mono">{client_id}</span></div><div class="auth-secret admin-copy-value">{secret}</div><button class="admin-action admin-copy" type="button" data-copied-label="{copied}">{copy}</button></div></div>"#, | |
| 695 | - note = t(lang, Key::SecretShownNote), | |
| 696 | - id_label = t(lang, Key::ClientIdLabel), | |
| 697 | - client_id = escape(&client_id), | |
| 698 | - secret = escape(&secret), | |
| 699 | - copied = t(lang, Key::CopiedWord), | |
| 700 | - copy = t(lang, Key::CopyWord), | |
| 701 | - ) | |
| 702 | - }) | |
| 703 | - .unwrap_or_default(); | |
| 704 | - Ok(format!( | |
| 705 | - r#"<div class="admin-card"> | |
| 706 | - <div class="auth-title">{title}</div> | |
| 707 | - {shown_html} | |
| 708 | - <div class="admin-table-wrap"> | |
| 709 | - <table class="admin-table admin-clients"> | |
| 710 | - <thead><tr><th>{name_label}</th><th>{id_label}</th><th>{uris_label}</th><th>{registered_label}</th><th></th></tr></thead> | |
| 711 | - <tbody>{rows}</tbody> | |
| 712 | - </table> | |
| 713 | - </div> | |
| 714 | - <form method="post" action="/admin/clients_add" class="admin-invite"> | |
| 715 | - <input class="auth-input" type="text" name="name" placeholder="drive" aria-label="{name_label}" required> | |
| 716 | - <input class="auth-input auth-input-mono" type="text" name="redirect_uris" placeholder="http://127.0.0.1:9000/callback https://drive.dizey.sh/callback" aria-label="{uris_label}" required> | |
| 717 | - <button class="auth-submit admin-invite-go" type="submit"><span class="auth-submit-text">{add}</span></button> | |
| 718 | - </form> | |
| 719 | -</div>"#, | |
| 720 | - title = t(lang, Key::ClientsTitle), | |
| 721 | - name_label = t(lang, Key::NameCol), | |
| 722 | - id_label = t(lang, Key::ClientIdLabel), | |
| 723 | - uris_label = t(lang, Key::RedirectUrisLabel), | |
| 724 | - registered_label = t(lang, Key::RegisteredCol), | |
| 725 | - add = t(lang, Key::ClientAdd), | |
| 726 | - )) | |
| 727 | -} | |
| 728 | - | |
| 729 | 757 | /// One user of the clients forms: the name, and one or more redirect URIs |
| 730 | 758 | /// — whitespace- or comma-separated, as loose as the CLI's argument list, |
| 731 | 759 | /// because a family dev's `http://127.0.0.1` redirect is a legitimate row. |
| @@ -1916,8 +1944,12 @@ mod tests { | ||
| 1916 | 1944 | assert!(location.starts_with("/admin?section=clients&shown=")); |
| 1917 | 1945 | let ticket = location.trim_start_matches("/admin?section=clients&shown="); |
| 1918 | 1946 | let (id, secret) = server::take_shown_secret(ticket).expect("the one showing"); |
| 1919 | - let row = list_clients(&setup.store).await.unwrap(); | |
| 1920 | - assert_eq!(id, row[0].client_id.to_string()); | |
| 1947 | + let stored = list_clients(&setup.store).await.unwrap(); | |
| 1948 | + let row = stored | |
| 1949 | + .iter() | |
| 1950 | + .find(|client| client.name == name) | |
| 1951 | + .expect("the fresh row"); | |
| 1952 | + assert_eq!(id, row.client_id.to_string()); | |
| 1921 | 1953 | (id, secret) |
| 1922 | 1954 | } |
| 1923 | 1955 | |
| @@ -2182,4 +2214,75 @@ mod tests { | ||
| 2182 | 2214 | .await; |
| 2183 | 2215 | assert!(!forged.contains("<div class=\"auth-secret")); |
| 2184 | 2216 | } |
| 2217 | + | |
| 2218 | + #[tokio::test] | |
| 2219 | + async fn merged_section_renders_linked_unlinked_and_bare_rows() { | |
| 2220 | + let setup = setup().await; | |
| 2221 | + | |
| 2222 | + // A client registered through the panel, then the service row its | |
| 2223 | + // app writes with that pair: the two halves link. | |
| 2224 | + let (id, _secret) = create_via_panel(&setup, "drive").await; | |
| 2225 | + im_core::services::register( | |
| 2226 | + &setup.store, | |
| 2227 | + "drive", | |
| 2228 | + "Drive", | |
| 2229 | + "https://drive.dizey.sh", | |
| 2230 | + &id, | |
| 2231 | + Some(&id), | |
| 2232 | + ) | |
| 2233 | + .await | |
| 2234 | + .unwrap(); | |
| 2235 | + // A second client no service row answers for, and a service row | |
| 2236 | + // with no client yet. | |
| 2237 | + create_via_panel(&setup, "stray").await; | |
| 2238 | + im_core::services::add( | |
| 2239 | + &setup.store, | |
| 2240 | + &im_core::services::Service { | |
| 2241 | + key: "wiki".into(), | |
| 2242 | + name: "Wiki".into(), | |
| 2243 | + url: "https://wiki.dizey.sh".into(), | |
| 2244 | + owner: None, | |
| 2245 | + client_id: None, | |
| 2246 | + }, | |
| 2247 | + ) | |
| 2248 | + .await | |
| 2249 | + .unwrap(); | |
| 2250 | + | |
| 2251 | + // Rendering needs the built asset bundle; without it the render | |
| 2252 | + // assertions stay skipped (the routes themselves are covered). | |
| 2253 | + if !setup.assets { | |
| 2254 | + return; | |
| 2255 | + } | |
| 2256 | + let (status, _, body) = get_full( | |
| 2257 | + &setup.router, | |
| 2258 | + "/admin?section=services", | |
| 2259 | + Some(&setup.admin_cookie), | |
| 2260 | + ) | |
| 2261 | + .await; | |
| 2262 | + assert_eq!(status, StatusCode::OK); | |
| 2263 | + | |
| 2264 | + // The linked row: wordmark half and credential half, one row. | |
| 2265 | + assert!(body.contains(">Drive</td>"), "the service name renders"); | |
| 2266 | + assert!(body.contains(&id), "the linked client id renders"); | |
| 2267 | + assert!(body.contains("https://drive.dizey.sh"), "the url renders"); | |
| 2268 | + assert!(body.contains("http://127.0.0.1:9000/callback"), "uris render"); | |
| 2269 | + // The credential-only row: the stray client's name, no key or url. | |
| 2270 | + assert!(body.contains(">stray</td>"), "the stray client renders"); | |
| 2271 | + // The bare service row and the unlinked client carry the dash. | |
| 2272 | + assert!(body.contains(">Wiki</td>"), "the bare service renders"); | |
| 2273 | + assert!( | |
| 2274 | + body.contains("<td class=\"muted\">—</td>"), | |
| 2275 | + "client-less halves read as the dash" | |
| 2276 | + ); | |
| 2277 | + | |
| 2278 | + // The old address lands on the merged section too. | |
| 2279 | + let (status, _, alias) = get_full( | |
| 2280 | + &setup.router, | |
| 2281 | + "/admin?section=clients", | |
| 2282 | + Some(&setup.admin_cookie), | |
| 2283 | + ) | |
| 2284 | + .await; | |
| 2285 | + assert_eq!(status, StatusCode::OK); | |
| 2286 | + assert_eq!(alias.contains(">Drive</td>"), body.contains(">Drive</td>")); | |
| 2287 | + } | |
| 2185 | 2288 | } |
crates/im-web/src/directory.rsmodified+49 -1
| @@ -201,7 +201,15 @@ async fn family_register( | ||
| 201 | 201 | .into_response(cx); |
| 202 | 202 | }; |
| 203 | 203 | let store = server::app(cx).store.clone(); |
| 204 | - match im_core::services::register(&store, &input.key, &input.name, &input.url, &client_id).await | |
| 204 | + match im_core::services::register( | |
| 205 | + &store, | |
| 206 | + &input.key, | |
| 207 | + &input.name, | |
| 208 | + &input.url, | |
| 209 | + &client_id, | |
| 210 | + Some(&client_id), | |
| 211 | + ) | |
| 212 | + .await | |
| 205 | 213 | { |
| 206 | 214 | Ok(service) => Json(serde_json::to_value(service).unwrap()).into_response(cx), |
| 207 | 215 | Err(im_core::store::StoreError::Conflict(_)) => ( |
| @@ -396,6 +404,7 @@ mod tests { | ||
| 396 | 404 | name: service.name.clone(), |
| 397 | 405 | url: service.url.clone(), |
| 398 | 406 | owner: None, |
| 407 | + client_id: None, | |
| 399 | 408 | }) |
| 400 | 409 | .collect() |
| 401 | 410 | } |
| @@ -853,6 +862,7 @@ mod tests { | ||
| 853 | 862 | name: "Files".into(), |
| 854 | 863 | url: "http://127.0.0.1:7655".into(), |
| 855 | 864 | owner: None, |
| 865 | + client_id: None, | |
| 856 | 866 | }]; |
| 857 | 867 | assert!(!im_core::services::seed_from(&store, &reseed).await.unwrap()); |
| 858 | 868 | let (_, body) = get_family(&router, Some(basic(&client_id, &secret))).await; |
| @@ -1051,6 +1061,44 @@ mod tests { | ||
| 1051 | 1061 | ); |
| 1052 | 1062 | } |
| 1053 | 1063 | |
| 1064 | + #[tokio::test] | |
| 1065 | + async fn family_register_stamps_the_authenticated_client_on_its_row() { | |
| 1066 | + let Setup { | |
| 1067 | + router, | |
| 1068 | + client_id, | |
| 1069 | + secret, | |
| 1070 | + store, | |
| 1071 | + .. | |
| 1072 | + } = setup().await; | |
| 1073 | + | |
| 1074 | + // First register: the row is appended with the caller's stamp. | |
| 1075 | + let (status, _) = post_register( | |
| 1076 | + &router, | |
| 1077 | + Some(basic(&client_id, &secret)), | |
| 1078 | + serde_json::json!({"key": "drive", "name": "Drive", "url": "https://drive.dizey.sh"}), | |
| 1079 | + ) | |
| 1080 | + .await; | |
| 1081 | + assert_eq!(status, StatusCode::OK); | |
| 1082 | + let rows = im_core::services::list(&store).await.unwrap(); | |
| 1083 | + let row = rows.iter().find(|s| s.key == "drive").unwrap(); | |
| 1084 | + assert_eq!(row.client_id.as_deref(), Some(client_id.as_str())); | |
| 1085 | + | |
| 1086 | + // The next boot's register refreshes the row and keeps the stamp | |
| 1087 | + // current — one row, still the caller's. | |
| 1088 | + let (status, _) = post_register( | |
| 1089 | + &router, | |
| 1090 | + Some(basic(&client_id, &secret)), | |
| 1091 | + serde_json::json!({"key": "drive", "name": "Drive", "url": "https://drive2.dizey.sh"}), | |
| 1092 | + ) | |
| 1093 | + .await; | |
| 1094 | + assert_eq!(status, StatusCode::OK); | |
| 1095 | + let rows = im_core::services::list(&store).await.unwrap(); | |
| 1096 | + assert_eq!(rows.iter().filter(|s| s.key == "drive").count(), 1); | |
| 1097 | + let row = rows.iter().find(|s| s.key == "drive").unwrap(); | |
| 1098 | + assert_eq!(row.url, "https://drive2.dizey.sh"); | |
| 1099 | + assert_eq!(row.client_id.as_deref(), Some(client_id.as_str())); | |
| 1100 | + } | |
| 1101 | + | |
| 1054 | 1102 | #[tokio::test] |
| 1055 | 1103 | async fn the_landing_lists_connected_apps_and_an_empty_note_without_them() { |
| 1056 | 1104 | let Setup { |
crates/im-web/src/i18n.rsmodified+0 -6
| @@ -243,8 +243,6 @@ pub enum Key { | ||
| 243 | 243 | InviteButton, |
| 244 | 244 | |
| 245 | 245 | // Admin clients section: the family's relying parties. |
| 246 | - NavClients, | |
| 247 | - ClientsTitle, | |
| 248 | 246 | RedirectUrisLabel, |
| 249 | 247 | RegisteredCol, |
| 250 | 248 | ClientAdd, |
| @@ -726,10 +724,6 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 726 | 724 | (InviteButton, En) => "Invite", |
| 727 | 725 | (InviteButton, Tr) => "Davet et", |
| 728 | 726 | |
| 729 | - (NavClients, En) => "Clients", | |
| 730 | - (NavClients, Tr) => "İstemciler", | |
| 731 | - (ClientsTitle, En) => "Clients", | |
| 732 | - (ClientsTitle, Tr) => "İstemciler", | |
| 733 | 727 | (RedirectUrisLabel, En) => "Redirect URIs", |
| 734 | 728 | (RedirectUrisLabel, Tr) => "Yönlendirme adresleri", |
| 735 | 729 | (RegisteredCol, En) => "Registered", |
crates/im-web/src/main.rsmodified+3 -0
| @@ -173,6 +173,7 @@ async fn seed_services(store: &Store, config: &Config) { | ||
| 173 | 173 | name: service.name.clone(), |
| 174 | 174 | url: service.url.clone(), |
| 175 | 175 | owner: None, |
| 176 | + client_id: None, | |
| 176 | 177 | }) |
| 177 | 178 | .collect(); |
| 178 | 179 | match im_core::services::seed_from(store, &seed).await { |
| @@ -198,6 +199,8 @@ async fn register_self(store: &Store, config: &Config) { | ||
| 198 | 199 | "Account", |
| 199 | 200 | &config.issuer, |
| 200 | 201 | im_core::services::SELF_OWNER, |
| 202 | + // im is nobody's client: its own row carries no credential stamp. | |
| 203 | + None, | |
| 201 | 204 | ) |
| 202 | 205 | .await |
| 203 | 206 | { |
style/main.scssmodified+1 -0
| @@ -1041,6 +1041,7 @@ body:has(.log-list) { | ||
| 1041 | 1041 | |
| 1042 | 1042 | .actions { |
| 1043 | 1043 | display: flex; |
| 1044 | + flex-wrap: wrap; | |
| 1044 | 1045 | gap: 8px; |
| 1045 | 1046 | justify-content: flex-end; |
| 1046 | 1047 |