the family switcher rides the topbar: a services config names the siblings and puts the in·im·iz wordmarks beside the page nav with the current app inked, base_url is honored again as this app's public address, and iz_logout now clears the cookie and hands the browser to the provider's logout pointed back home — so a sign-out anywhere ends the central session
Browse files at this commit2117c6022c4b6c61138283cf4d5382435c6c87deParent: 5295ebe7 changed files +406 -20
- crates/iz-client/examples/mint.rs +1 -0
- crates/iz-client/src/lib.rs +20 -4
- crates/iz-core/src/config.rs +238 -16
- crates/iz-web/src/layout.rs +35 -0
- crates/iz-web/src/main.rs +4 -0
- crates/iz-web/tests/http.rs +82 -0
- style/main.scss +26 -0
crates/iz-client/examples/mint.rsmodified+1 -0
| @@ -27,6 +27,7 @@ fn main() { | ||
| 27 | 27 | client_id: String::new(), |
| 28 | 28 | client_secret: String::new(), |
| 29 | 29 | redirect_uri: String::new(), |
| 30 | + logout_back: String::new(), | |
| 30 | 31 | cookie_name, |
| 31 | 32 | cookie_key: key, |
| 32 | 33 | }; |
crates/iz-client/src/lib.rsmodified+20 -4
| @@ -42,6 +42,10 @@ pub struct Config { | ||
| 42 | 42 | pub client_secret: String, |
| 43 | 43 | /// Must exactly match one of the URIs registered with im. |
| 44 | 44 | pub redirect_uri: String, |
| 45 | + /// Where im sends the browser when a sign-out that started here | |
| 46 | + /// finishes: this app's own public address, slash-terminated. Offered | |
| 47 | + /// to im's `/logout` as its `back`, so the browser comes home. | |
| 48 | + pub logout_back: String, | |
| 45 | 49 | /// The app's session cookie name, e.g. `iz_session`. |
| 46 | 50 | pub cookie_name: String, |
| 47 | 51 | /// 32 bytes, generated once per app and kept out of the repository. |
| @@ -538,12 +542,22 @@ async fn iz_callback(cx: &Cx) -> Result<Response, topcoat::Error> { | ||
| 538 | 542 | } |
| 539 | 543 | } |
| 540 | 544 | |
| 541 | -/// Signs out of the app only. To end the central session too, post to im's | |
| 542 | -/// `/logout` — a link there is a silent re-login, by design. | |
| 545 | +/// Signs out of the app and then of the family: the local cookie is | |
| 546 | +/// cleared, and the browser is sent on to im's `/logout` with `back` | |
| 547 | +/// pointed back here. Ending the central session is im's half of the | |
| 548 | +/// handoff — which is why a sign-out anywhere is a sign-out everywhere. | |
| 543 | 549 | #[route(GET "/auth/logout")] |
| 544 | 550 | async fn iz_logout(cx: &Cx) -> Result<Response, topcoat::Error> { |
| 545 | - clear_cookie(cx, &client(cx).config.cookie_name); | |
| 546 | - see(cx, "/") | |
| 551 | + let config = &client(cx).config; | |
| 552 | + clear_cookie(cx, &config.cookie_name); | |
| 553 | + see( | |
| 554 | + cx, | |
| 555 | + &format!( | |
| 556 | + "{}/logout?back={}", | |
| 557 | + config.issuer, | |
| 558 | + urlencoded(&config.logout_back) | |
| 559 | + ), | |
| 560 | + ) | |
| 547 | 561 | } |
| 548 | 562 | |
| 549 | 563 | fn urlencoded(raw: &str) -> String { |
| @@ -863,6 +877,7 @@ mod tests { | ||
| 863 | 877 | client_id: "client-1".into(), |
| 864 | 878 | client_secret: "secret".into(), |
| 865 | 879 | redirect_uri: "http://app.test/auth/callback".into(), |
| 880 | + logout_back: "http://app.test/".into(), | |
| 866 | 881 | cookie_name: "iz_session".into(), |
| 867 | 882 | cookie_key: [7u8; 32], |
| 868 | 883 | }; |
| @@ -942,6 +957,7 @@ mod tests { | ||
| 942 | 957 | client_id: "client-1".into(), |
| 943 | 958 | client_secret: "s3cr3t".into(), |
| 944 | 959 | redirect_uri: "http://app.test/auth/callback".into(), |
| 960 | + logout_back: "http://app.test/".into(), | |
| 945 | 961 | cookie_name: "iz_session".into(), |
| 946 | 962 | cookie_key: [7u8; 32], |
| 947 | 963 | } |
crates/iz-core/src/config.rsmodified+238 -16
| @@ -53,10 +53,37 @@ storage = "storage" | ||
| 53 | 53 | # this is the only thing that decides where İz binds. It is also the |
| 54 | 54 | # address mail links fall back to, until an admin sets one in Settings. |
| 55 | 55 | listen = "127.0.0.1:7654" |
| 56 | +# This app's public address, when it differs from `listen` — a proxy in | |
| 57 | +# front, for instance. It is where a sign-out sends the browser back to, | |
| 58 | +# so a deployment the family reaches by name sets it. Empty: derived | |
| 59 | +# from `listen`. | |
| 60 | +base_url = "" | |
| 56 | 61 | # How long a live-update connection is held before the browser is asked to |
| 57 | 62 | # reconnect, in seconds. The reconnect is what re-checks the session, so a |
| 58 | 63 | # revoked sign-in stops receiving within this long. |
| 59 | 64 | live_seconds = 300 |
| 65 | + | |
| 66 | +# The dizey family, as the topbar's wordmark switcher shows it: one | |
| 67 | +# [[services]] table per app, with key ("in"|"im"|"iz"), a human name, and | |
| 68 | +# the url, absolute, no trailing slash. An empty list keeps the switcher | |
| 69 | +# off. Production serves the family at https://<key>.dizey.sh, written in | |
| 70 | +# the deploy's own config; this file carries only the development | |
| 71 | +# addresses. | |
| 72 | +[[services]] | |
| 73 | +key = "in" | |
| 74 | +name = "Files" | |
| 75 | +url = "http://127.0.0.1:7655" | |
| 76 | + | |
| 77 | +[[services]] | |
| 78 | +key = "im" | |
| 79 | +name = "Account" | |
| 80 | +url = "http://127.0.0.1:7650" | |
| 81 | + | |
| 82 | +[[services]] | |
| 83 | +key = "iz" | |
| 84 | +name = "Board" | |
| 85 | +url = "http://127.0.0.1:7654" | |
| 86 | + | |
| 60 | 87 | [oidc] |
| 61 | 88 | # The provider that signs people in. Required: no default is guessed. |
| 62 | 89 | issuer = "" |
| @@ -113,6 +140,27 @@ const OPTIONAL_KEYS: &[(&str, &str)] = &[ | ||
| 113 | 140 | "live_seconds = 300\n" |
| 114 | 141 | ), |
| 115 | 142 | ), |
| 143 | + ( | |
| 144 | + "base_url", | |
| 145 | + concat!( | |
| 146 | + "# This app's public address, when it differs from `listen` — a proxy\n", | |
| 147 | + "# in front, for instance. It is where a sign-out sends the browser\n", | |
| 148 | + "# back to, so a deployment the family reaches by name sets it.\n", | |
| 149 | + "# Empty: derived from `listen`.\n", | |
| 150 | + "base_url = \"\"\n" | |
| 151 | + ), | |
| 152 | + ), | |
| 153 | + ( | |
| 154 | + "services", | |
| 155 | + concat!( | |
| 156 | + "# The dizey family, as the topbar's wordmark switcher shows it: one\n", | |
| 157 | + "# [[services]] table per app, with key (\"in\"|\"im\"|\"iz\"), a human\n", | |
| 158 | + "# name, and the url, absolute, no trailing slash. An empty list keeps\n", | |
| 159 | + "# the switcher off. Production serves the family at\n", | |
| 160 | + "# https://<key>.dizey.sh, written in the deploy's own config.\n", | |
| 161 | + "services = []\n" | |
| 162 | + ), | |
| 163 | + ), | |
| 116 | 164 | ]; |
| 117 | 165 | |
| 118 | 166 | /// The `[oidc]` table of `config/iz.toml`, before the values are checked. |
| @@ -128,6 +176,15 @@ struct OidcToml { | ||
| 128 | 176 | other: std::collections::BTreeMap<String, toml::Value>, |
| 129 | 177 | } |
| 130 | 178 | |
| 179 | +/// One `[[services]]` entry of `config/iz.toml`, before the values are | |
| 180 | +/// checked. | |
| 181 | +#[derive(Deserialize)] | |
| 182 | +struct ServiceToml { | |
| 183 | + key: String, | |
| 184 | + name: String, | |
| 185 | + url: String, | |
| 186 | +} | |
| 187 | + | |
| 131 | 188 | /// The shape of `config/iz.toml`, before the values are checked. Anything |
| 132 | 189 | /// else the file says lands in `other`, which is read for its key names only |
| 133 | 190 | /// — enough for the report to say a key was seen and not obeyed. |
| @@ -137,6 +194,8 @@ struct Toml { | ||
| 137 | 194 | storage: Option<String>, |
| 138 | 195 | listen: Option<String>, |
| 139 | 196 | live_seconds: Option<u64>, |
| 197 | + base_url: Option<String>, | |
| 198 | + services: Option<Vec<ServiceToml>>, | |
| 140 | 199 | oidc: Option<OidcToml>, |
| 141 | 200 | #[serde(flatten)] |
| 142 | 201 | other: std::collections::BTreeMap<String, toml::Value>, |
| @@ -156,6 +215,18 @@ pub struct OidcConfig { | ||
| 156 | 215 | pub redirect_uri: String, |
| 157 | 216 | } |
| 158 | 217 | |
| 218 | +/// One of the apps the topbar's wordmark switcher links to. | |
| 219 | +#[derive(Clone, Debug, PartialEq, Eq)] | |
| 220 | +pub struct ServiceConfig { | |
| 221 | + /// The wordmark, lowercase — and how the app recognises itself in the | |
| 222 | + /// list (`"iz"` here). | |
| 223 | + pub key: String, | |
| 224 | + /// The human name, offered as the link's title: "Files", "Account". | |
| 225 | + pub name: String, | |
| 226 | + /// Where the app lives: absolute, no trailing slash. | |
| 227 | + pub url: String, | |
| 228 | +} | |
| 229 | + | |
| 159 | 230 | /// What the process needs to know before it opens a socket. |
| 160 | 231 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 161 | 232 | pub struct Config { |
| @@ -175,6 +246,14 @@ pub struct Config { | ||
| 175 | 246 | /// asked to reconnect. The reconnect re-authenticates, which is how a |
| 176 | 247 | /// session revoked mid-stream stops being fed. |
| 177 | 248 | pub live_seconds: u64, |
| 249 | + /// This app's own public address as a URL, when the file says one. | |
| 250 | + /// Empty means "derive it from `listen`" — the same arrangement | |
| 251 | + /// `redirect_uri` has. It is where the family sends the browser back | |
| 252 | + /// to after a sign-out that started here. | |
| 253 | + pub base_url: String, | |
| 254 | + /// The apps of the family, as the topbar's wordmark switcher links | |
| 255 | + /// them. Empty: no switcher, and everything else stays as it was. | |
| 256 | + pub services: Vec<ServiceConfig>, | |
| 178 | 257 | /// The OIDC provider İz trusts. |
| 179 | 258 | pub oidc: OidcConfig, |
| 180 | 259 | /// Whether `config/iz.toml` did not exist and was just written with the |
| @@ -286,6 +365,30 @@ impl Config { | ||
| 286 | 365 | why: format!("{listen:?} is not a host:port address — {err}"), |
| 287 | 366 | })?; |
| 288 | 367 | |
| 368 | + // Empty means "derive it": the same arrangement `redirect_uri` has, | |
| 369 | + // so a deployment behind a proxy says its public name once and | |
| 370 | + // everyone else follows `listen` for free. | |
| 371 | + let base_url = value(toml.base_url).unwrap_or_default(); | |
| 372 | + | |
| 373 | + // An entry that cannot be shown — no wordmark, no name, no | |
| 374 | + // destination — is refused rather than rendered half of itself. | |
| 375 | + let mut services = Vec::new(); | |
| 376 | + for service in toml.services.unwrap_or_default() { | |
| 377 | + if service.key.trim().is_empty() | |
| 378 | + || service.name.trim().is_empty() | |
| 379 | + || service.url.trim().is_empty() | |
| 380 | + { | |
| 381 | + return Err(ConfigError::Invalid { | |
| 382 | + key: "services", | |
| 383 | + why: "every [[services]] needs a non-empty key, name and url".to_string(), | |
| 384 | + }); | |
| 385 | + } | |
| 386 | + services.push(ServiceConfig { | |
| 387 | + key: service.key, | |
| 388 | + name: service.name, | |
| 389 | + url: service.url, | |
| 390 | + }); | |
| 391 | + } | |
| 289 | 392 | // Zero would mean a stream that ends the moment it opens, which is a |
| 290 | 393 | // reconnect loop rather than a live feed; the key is refused rather |
| 291 | 394 | // than quietly corrected, because a deployment that meant to say |
| @@ -315,6 +418,8 @@ impl Config { | ||
| 315 | 418 | database, |
| 316 | 419 | storage, |
| 317 | 420 | listen, |
| 421 | + base_url, | |
| 422 | + services, | |
| 318 | 423 | live_seconds, |
| 319 | 424 | oidc: OidcConfig { |
| 320 | 425 | issuer, |
| @@ -339,6 +444,21 @@ impl Config { | ||
| 339 | 444 | listen_url_of(&self.listen) |
| 340 | 445 | } |
| 341 | 446 | |
| 447 | + /// This app's address as a URL, for when another service sends the | |
| 448 | + /// browser back here after a sign-out that started here: `base_url` | |
| 449 | + /// when the file sets one, else the address bound. | |
| 450 | + /// | |
| 451 | + /// The derivation mirrors `redirect_uri`'s, and for the same reason: a | |
| 452 | + /// deployment behind a proxy says its public name once, and everyone | |
| 453 | + /// else follows `listen` for free. | |
| 454 | + pub fn public_url(&self) -> String { | |
| 455 | + if self.base_url.is_empty() { | |
| 456 | + self.listen_url() | |
| 457 | + } else { | |
| 458 | + self.base_url.clone() | |
| 459 | + } | |
| 460 | + } | |
| 461 | + | |
| 342 | 462 | /// The lines to print once at startup. Nothing secret is among them. |
| 343 | 463 | pub fn report(&self) -> Vec<String> { |
| 344 | 464 | let mut lines = vec![ |
| @@ -463,13 +583,17 @@ fn complete(path: &Path, text: &str, base: &Path) { | ||
| 463 | 583 | } |
| 464 | 584 | } |
| 465 | 585 | |
| 466 | -/// Whether the file sets this key — a line whose first word it is. A key | |
| 467 | -/// named inside a comment or a value does not count. | |
| 586 | +/// Whether the file sets this key — a line whose first word it is, or an | |
| 587 | +/// array-of-tables header naming it (`[[services]]`), which is how a list | |
| 588 | +/// of tables says the same thing. A key named inside a comment or a value | |
| 589 | +/// does not count. | |
| 468 | 590 | fn mentions(text: &str, key: &str) -> bool { |
| 591 | + let header = format!("[[{key}]]"); | |
| 469 | 592 | text.lines().any(|line| { |
| 470 | - line.trim_start() | |
| 471 | - .strip_prefix(key) | |
| 593 | + let line = line.trim_start(); | |
| 594 | + line.strip_prefix(key) | |
| 472 | 595 | .is_some_and(|rest| rest.trim_start().starts_with('=')) |
| 596 | + || line.starts_with(&header) | |
| 473 | 597 | }) |
| 474 | 598 | } |
| 475 | 599 | |
| @@ -593,7 +717,8 @@ mod tests { | ||
| 593 | 717 | let dir = scratch(); |
| 594 | 718 | std::fs::create_dir_all(dir.join("config")).unwrap(); |
| 595 | 719 | let mine = &format!( |
| 596 | - "database = \"iz.db\"\nstorage = \"files\"\nlisten = \"0.0.0.0:8080\"\nlive_seconds = 30\n{OIDC}" | |
| 720 | + "database = \"iz.db\"\nstorage = \"files\"\nlisten = \"0.0.0.0:8080\"\n\ | |
| 721 | + live_seconds = 30\nbase_url = \"\"\nservices = []\n{OIDC}" | |
| 597 | 722 | ); |
| 598 | 723 | std::fs::write(dir.join(FILE_NAME), mine).unwrap(); |
| 599 | 724 | let config = Config::load_from(&dir).unwrap(); |
| @@ -758,30 +883,28 @@ mod tests { | ||
| 758 | 883 | } |
| 759 | 884 | |
| 760 | 885 | /// The sender used to be environment variables read directly, and the |
| 761 | - /// base URL used to be a key of its own. Both are gone, so a file that | |
| 762 | - /// still carries them must do nothing at all — not half-configure a | |
| 763 | - /// sender, not stop the boot, and above all not quietly send through an | |
| 764 | - /// account the Settings screen does not show. The report says they were | |
| 765 | - /// seen, so a typo is visible too. | |
| 886 | + /// mail base URL used to be a key of this file too before the Settings | |
| 887 | + /// screen took it over. A file that still carries the sender's secrets | |
| 888 | + /// must do nothing at all — not half-configure a sender, not stop the | |
| 889 | + /// boot, and above all not quietly send through an account the Settings | |
| 890 | + /// screen does not show. The report says they were seen, so a typo is | |
| 891 | + /// visible too. (`base_url` is a key here again, with a different job: | |
| 892 | + /// the address the family returns the browser to after a sign-out.) | |
| 766 | 893 | #[test] |
| 767 | 894 | fn keys_nothing_reads_are_named_rather_than_obeyed() { |
| 768 | 895 | let config = Config::parse( |
| 769 | - &format!( | |
| 770 | - "database = \"/srv/iz.db\"\nbase_url = \"https://iz.sh\"\n\ | |
| 771 | - smtp_password = \"hunter2-and-then-some\"\n{OIDC}" | |
| 772 | - ), | |
| 896 | + &format!("database = \"/srv/iz.db\"\nsmtp_password = \"hunter2-and-then-some\"\n{OIDC}"), | |
| 773 | 897 | Path::new("."), |
| 774 | 898 | false, |
| 775 | 899 | ) |
| 776 | 900 | .expect("a key the file no longer honours is not an error"); |
| 777 | 901 | |
| 778 | - assert_eq!(config.ignored, vec!["base_url", "smtp_password"]); | |
| 902 | + assert_eq!(config.ignored, vec!["smtp_password"]); | |
| 779 | 903 | let report = config.report().join("\n"); |
| 780 | 904 | assert!( |
| 781 | 905 | !report.contains("hunter2-and-then-some"), |
| 782 | 906 | "the report read a key it no longer honours: {report}" |
| 783 | 907 | ); |
| 784 | - assert!(report.contains("base_url"), "{report}"); | |
| 785 | 908 | assert!( |
| 786 | 909 | report.contains("the sender is in Settings"), |
| 787 | 910 | "the report should say where the sender lives: {report}" |
| @@ -837,6 +960,80 @@ mod tests { | ||
| 837 | 960 | ); |
| 838 | 961 | } |
| 839 | 962 | |
| 963 | + /// The family list parses into the switcher's entries, in the order the | |
| 964 | + /// file gives them — the order the wordmarks show. | |
| 965 | + #[test] | |
| 966 | + fn services_parse_into_the_switcher_list() { | |
| 967 | + let config = Config::parse( | |
| 968 | + &format!( | |
| 969 | + "database = \"iz.db\"\n\ | |
| 970 | + [[services]]\nkey = \"in\"\nname = \"Files\"\nurl = \"http://127.0.0.1:7655\"\n\ | |
| 971 | + [[services]]\nkey = \"iz\"\nname = \"Board\"\nurl = \"http://127.0.0.1:7654\"\n\ | |
| 972 | + [[services]]\nkey = \"im\"\nname = \"Account\"\nurl = \"http://127.0.0.1:7650\"\n\ | |
| 973 | + {OIDC}" | |
| 974 | + ), | |
| 975 | + Path::new("."), | |
| 976 | + false, | |
| 977 | + ) | |
| 978 | + .unwrap(); | |
| 979 | + let keys: Vec<&str> = config.services.iter().map(|s| s.key.as_str()).collect(); | |
| 980 | + assert_eq!(keys, vec!["in", "iz", "im"]); | |
| 981 | + assert_eq!(config.services[1].name, "Board"); | |
| 982 | + assert_eq!(config.services[0].url, "http://127.0.0.1:7655"); | |
| 983 | + } | |
| 984 | + | |
| 985 | + /// A file silent about the family keeps the switcher off, and the | |
| 986 | + /// address the family returns the browser to falls back to the address | |
| 987 | + /// bound — the same fallback `redirect_uri` has. | |
| 988 | + #[test] | |
| 989 | + fn without_services_or_base_url_the_switcher_is_off_and_the_address_is_derived() { | |
| 990 | + let config = Config::parse( | |
| 991 | + &format!("database = \"iz.db\"\nlisten = \"0.0.0.0:9000\"\n{OIDC}"), | |
| 992 | + Path::new("."), | |
| 993 | + false, | |
| 994 | + ) | |
| 995 | + .unwrap(); | |
| 996 | + assert!(config.services.is_empty()); | |
| 997 | + assert_eq!(config.public_url(), "http://127.0.0.1:9000"); | |
| 998 | + } | |
| 999 | + | |
| 1000 | + /// An explicit public address wins over the derived one: a deployment | |
| 1001 | + /// behind a proxy says its name once, and the sign-out `back` uses it. | |
| 1002 | + #[test] | |
| 1003 | + fn an_explicit_base_url_wins_for_the_address_the_family_returns_to() { | |
| 1004 | + let config = Config::parse( | |
| 1005 | + &format!( | |
| 1006 | + "database = \"iz.db\"\nlisten = \"127.0.0.1:7654\"\n\ | |
| 1007 | + base_url = \"https://iz.dizey.sh\"\n{OIDC}" | |
| 1008 | + ), | |
| 1009 | + Path::new("."), | |
| 1010 | + false, | |
| 1011 | + ) | |
| 1012 | + .unwrap(); | |
| 1013 | + assert_eq!(config.public_url(), "https://iz.dizey.sh"); | |
| 1014 | + } | |
| 1015 | + | |
| 1016 | + /// An entry that could not be shown — a wordmark, a name or a | |
| 1017 | + /// destination missing — stops the boot naming the key, rather than | |
| 1018 | + /// rendering half a link. | |
| 1019 | + #[test] | |
| 1020 | + fn a_service_missing_a_field_stops_the_boot_naming_the_key() { | |
| 1021 | + let problem = Config::parse( | |
| 1022 | + &format!( | |
| 1023 | + "database = \"iz.db\"\n\ | |
| 1024 | + [[services]]\nkey = \"in\"\nname = \"\"\nurl = \"http://127.0.0.1:7655\"\n\ | |
| 1025 | + {OIDC}" | |
| 1026 | + ), | |
| 1027 | + Path::new("."), | |
| 1028 | + false, | |
| 1029 | + ) | |
| 1030 | + .unwrap_err(); | |
| 1031 | + assert!( | |
| 1032 | + matches!(problem, ConfigError::Invalid { key: "services", .. }), | |
| 1033 | + "{problem:?}" | |
| 1034 | + ); | |
| 1035 | + } | |
| 1036 | + | |
| 840 | 1037 | #[test] |
| 841 | 1038 | fn a_key_inside_oidc_nothing_reads_is_named_with_its_table() { |
| 842 | 1039 | let config = Config::parse( |
| @@ -869,4 +1066,29 @@ mod tests { | ||
| 869 | 1066 | assert!(reparsed.get("listen").is_some()); |
| 870 | 1067 | let _ = std::fs::remove_dir_all(&dir); |
| 871 | 1068 | } |
| 1069 | + | |
| 1070 | + /// A file that already carries `[[services]]` tables is not completed | |
| 1071 | + /// with the empty list: the header is how a list of tables says the | |
| 1072 | + /// key is set, and an appended `services = []` beside it would read as | |
| 1073 | + /// a phantom, empty extra entry. | |
| 1074 | + #[test] | |
| 1075 | + fn completion_does_not_add_services_beside_tables_already_there() { | |
| 1076 | + let dir = scratch(); | |
| 1077 | + std::fs::create_dir_all(dir.join("config")).unwrap(); | |
| 1078 | + let mine = &format!( | |
| 1079 | + "database = \"iz.db\"\n\ | |
| 1080 | + [[services]]\nkey = \"in\"\nname = \"Files\"\nurl = \"http://127.0.0.1:7655\"\n{OIDC}" | |
| 1081 | + ); | |
| 1082 | + std::fs::write(dir.join(FILE_NAME), mine).unwrap(); | |
| 1083 | + Config::load_from(&dir).unwrap(); | |
| 1084 | + let written = std::fs::read_to_string(dir.join(FILE_NAME)).unwrap(); | |
| 1085 | + assert_eq!(written.matches("[[services]]").count(), 1, "{written}"); | |
| 1086 | + assert!(!written.contains("services = []"), "{written}"); | |
| 1087 | + // What was there is untouched: the sole family entry survives, and | |
| 1088 | + // a re-parse reads it back as exactly one service. | |
| 1089 | + assert!(written.contains("key = \"in\""), "{written}"); | |
| 1090 | + let reparsed: toml::Value = toml::from_str(&written).unwrap(); | |
| 1091 | + assert_eq!(reparsed["services"].as_array().map(Vec::len), Some(1)); | |
| 1092 | + let _ = std::fs::remove_dir_all(&dir); | |
| 1093 | + } | |
| 872 | 1094 | } |
crates/iz-web/src/layout.rsmodified+35 -0
| @@ -212,6 +212,7 @@ impl NavPage { | ||
| 212 | 212 | pub async fn topbar_nav(cx: &Cx, active: NavPage, role: iz_core::Role, lang: Lang) -> Result { |
| 213 | 213 | view! { |
| 214 | 214 | cx => |
| 215 | + (service_switcher(cx).await?) | |
| 215 | 216 | <nav class="topbar-nav-links"> |
| 216 | 217 | for page in NavPage::ALL { |
| 217 | 218 | if role.can_administer() || !matches!(page, NavPage::Rules | NavPage::Logs | NavPage::Tags) { |
| @@ -227,6 +228,40 @@ pub async fn topbar_nav(cx: &Cx, active: NavPage, role: iz_core::Role, lang: Lan | ||
| 227 | 228 | } |
| 228 | 229 | } |
| 229 | 230 | |
| 231 | +/// The family's wordmark trio — `in im iz` — as the topbar's switcher | |
| 232 | +/// between the apps of dizey.sh. The current app is held in ink and marked | |
| 233 | +/// `aria-current`; the others are plain doors out, titled with the human | |
| 234 | +/// name the config gives them. Rendered only when config/iz.toml lists | |
| 235 | +/// services, so a deployment standing alone shows a chrome without the | |
| 236 | +/// trio. The links leave this origin, so they carry `data-hard`, like the | |
| 237 | +/// issuer link in the user menu. | |
| 238 | +async fn service_switcher(cx: &Cx) -> Result { | |
| 239 | + const SELF: &str = "iz"; | |
| 240 | + let services = crate::server::config(cx).services; | |
| 241 | + view! { | |
| 242 | + cx => | |
| 243 | + if !services.is_empty() { | |
| 244 | + <nav class="service-switcher"> | |
| 245 | + for service in services { | |
| 246 | + if service.key == SELF { | |
| 247 | + <span | |
| 248 | + class="service-mark service-mark-on" | |
| 249 | + aria-current="page" | |
| 250 | + title=(service.name) | |
| 251 | + > | |
| 252 | + (service.key) | |
| 253 | + </span> | |
| 254 | + } else { | |
| 255 | + <a class="service-mark" href=(service.url) title=(service.name) data-hard=""> | |
| 256 | + (service.key) | |
| 257 | + </a> | |
| 258 | + } | |
| 259 | + } | |
| 260 | + </nav> | |
| 261 | + } | |
| 262 | + } | |
| 263 | +} | |
| 264 | + | |
| 230 | 265 | /// The page-swap machinery that keeps every mutation on the same document. |
| 231 | 266 | /// |
| 232 | 267 | /// Every `/api/*` mutation is a plain form post answered with a 303 — sound |
crates/iz-web/src/main.rsmodified+4 -0
| @@ -123,6 +123,10 @@ async fn main() { | ||
| 123 | 123 | client_id: config.oidc.client_id.clone(), |
| 124 | 124 | client_secret: config.oidc.client_secret.clone(), |
| 125 | 125 | redirect_uri: config.oidc.redirect_uri.clone(), |
| 126 | + // Where im's /logout sends the browser when a sign-out that | |
| 127 | + // started here finishes: this app's public address, derived from | |
| 128 | + // `listen` unless the file says a `base_url`. | |
| 129 | + logout_back: format!("{}/", config.public_url().trim_end_matches('/')), | |
| 126 | 130 | cookie_name: "iz_session".to_string(), |
| 127 | 131 | cookie_key, |
| 128 | 132 | }; |
crates/iz-web/tests/http.rsmodified+82 -0
| @@ -280,6 +280,12 @@ struct App { | ||
| 280 | 280 | |
| 281 | 281 | impl App { |
| 282 | 282 | async fn build(mail: Mail) -> Self { |
| 283 | + Self::build_with(mail, Vec::new()).await | |
| 284 | + } | |
| 285 | + | |
| 286 | + /// Like `build`, but with a `[[services]]` list, so a test can see the | |
| 287 | + /// family switcher a configured deployment shows. | |
| 288 | + async fn build_with(mail: Mail, services: Vec<iz_core::config::ServiceConfig>) -> Self { | |
| 283 | 289 | let dir = std::env::temp_dir().join(format!("iz-http-{}", Ulid::new())); |
| 284 | 290 | std::fs::create_dir_all(&dir).unwrap(); |
| 285 | 291 | let db = dir.join("iz.db"); |
| @@ -295,6 +301,7 @@ impl App { | ||
| 295 | 301 | client_id: "iz-test".to_string(), |
| 296 | 302 | client_secret: "s3cr3t".to_string(), |
| 297 | 303 | redirect_uri: "http://127.0.0.1:7655/auth/callback".to_string(), |
| 304 | + logout_back: "http://127.0.0.1:7655/".to_string(), | |
| 298 | 305 | cookie_name: "iz_session".to_string(), |
| 299 | 306 | cookie_key: [7u8; 32], |
| 300 | 307 | }; |
| @@ -303,6 +310,8 @@ impl App { | ||
| 303 | 310 | storage, |
| 304 | 311 | listen: "127.0.0.1:7655".parse().unwrap(), |
| 305 | 312 | live_seconds: 300, |
| 313 | + base_url: String::new(), | |
| 314 | + services, | |
| 306 | 315 | oidc: iz_core::config::OidcConfig { |
| 307 | 316 | issuer: fake.url(), |
| 308 | 317 | client_id: "iz-test".to_string(), |
| @@ -371,6 +380,7 @@ impl App { | ||
| 371 | 380 | client_id: "iz-test".to_string(), |
| 372 | 381 | client_secret: "s3cr3t".to_string(), |
| 373 | 382 | redirect_uri: "http://127.0.0.1:7655/auth/callback".to_string(), |
| 383 | + logout_back: "http://127.0.0.1:7655/".to_string(), | |
| 374 | 384 | cookie_name: "iz_session".to_string(), |
| 375 | 385 | cookie_key: [7u8; 32], |
| 376 | 386 | }; |
| @@ -378,6 +388,8 @@ impl App { | ||
| 378 | 388 | database: db, |
| 379 | 389 | storage, |
| 380 | 390 | listen: "127.0.0.1:7655".parse().unwrap(), |
| 391 | + base_url: String::new(), | |
| 392 | + services: Vec::new(), | |
| 381 | 393 | live_seconds: 300, |
| 382 | 394 | oidc: iz_core::config::OidcConfig { |
| 383 | 395 | issuer: fake.url(), |
| @@ -677,6 +689,11 @@ impl App { | ||
| 677 | 689 | .get(header::ETAG) |
| 678 | 690 | .and_then(|value| value.to_str().ok()) |
| 679 | 691 | .map(str::to_string); |
| 692 | + let location = response | |
| 693 | + .headers() | |
| 694 | + .get(header::LOCATION) | |
| 695 | + .and_then(|value| value.to_str().ok()) | |
| 696 | + .map(str::to_string); | |
| 680 | 697 | let bytes = to_bytes(response.into_body(), usize::MAX) |
| 681 | 698 | .await |
| 682 | 699 | .unwrap() |
| @@ -688,6 +705,7 @@ impl App { | ||
| 688 | 705 | content_range, |
| 689 | 706 | accept_ranges, |
| 690 | 707 | etag, |
| 708 | + location, | |
| 691 | 709 | bytes, |
| 692 | 710 | } |
| 693 | 711 | } |
| @@ -708,6 +726,8 @@ struct Raw { | ||
| 708 | 726 | content_range: Option<String>, |
| 709 | 727 | accept_ranges: Option<String>, |
| 710 | 728 | etag: Option<String>, |
| 729 | + /// Where a 303 sends the browser — the sign-out handoff's whole point. | |
| 730 | + location: Option<String>, | |
| 711 | 731 | bytes: Vec<u8>, |
| 712 | 732 | } |
| 713 | 733 | |
| @@ -9557,3 +9577,65 @@ fn the_stylesheet_guard_refuses_a_foreign_bundle() { | ||
| 9557 | 9577 | assert!(problem.contains("another build"), "{problem}"); |
| 9558 | 9578 | std::fs::remove_dir_all(&foreign).unwrap(); |
| 9559 | 9579 | } |
| 9580 | + | |
| 9581 | +/// Sign-out leaves through the family's front door: the local cookie is | |
| 9582 | +/// cleared, and the browser is handed to im's `/logout` with `back` pointed | |
| 9583 | +/// at this app, so the central session ends with the local one. | |
| 9584 | +#[tokio::test] | |
| 9585 | +async fn a_sign_out_hands_the_browser_to_the_providers_logout_pointed_home() { | |
| 9586 | + let app = App::open().await; | |
| 9587 | + let admin = admin(&app).await; | |
| 9588 | + let raw = app.get("/auth/logout", Some(&admin)).await; | |
| 9589 | + assert_eq!(raw.status, StatusCode::SEE_OTHER); | |
| 9590 | + let expected = format!( | |
| 9591 | + "{}/logout?back={}", | |
| 9592 | + app.client.issuer, | |
| 9593 | + encode("http://127.0.0.1:7655/") | |
| 9594 | + ); | |
| 9595 | + assert_eq!(raw.location.as_deref(), Some(expected.as_str())); | |
| 9596 | +} | |
| 9597 | + | |
| 9598 | +/// The signed-in chrome carries the family's wordmark trio when the config | |
| 9599 | +/// lists services: one mark per `[[services]]` row in the file's order, the | |
| 9600 | +/// current app held in ink with `aria-current`, the others plain links out. | |
| 9601 | +/// A deployment that lists none shows no switcher at all. | |
| 9602 | +#[tokio::test] | |
| 9603 | +async fn the_topbar_shows_the_family_switcher_and_marks_this_app_only_when_configured() { | |
| 9604 | + let services = vec![ | |
| 9605 | + iz_core::config::ServiceConfig { | |
| 9606 | + key: "in".to_string(), | |
| 9607 | + name: "Files".to_string(), | |
| 9608 | + url: "http://127.0.0.1:7655".to_string(), | |
| 9609 | + }, | |
| 9610 | + iz_core::config::ServiceConfig { | |
| 9611 | + key: "iz".to_string(), | |
| 9612 | + name: "Board".to_string(), | |
| 9613 | + url: "http://127.0.0.1:7654".to_string(), | |
| 9614 | + }, | |
| 9615 | + iz_core::config::ServiceConfig { | |
| 9616 | + key: "im".to_string(), | |
| 9617 | + name: "Account".to_string(), | |
| 9618 | + url: "http://127.0.0.1:7650".to_string(), | |
| 9619 | + }, | |
| 9620 | + ]; | |
| 9621 | + let app = App::build_with(Mail::silent(), services).await; | |
| 9622 | + let board_admin = admin(&app).await; | |
| 9623 | + let html = String::from_utf8_lossy(&app.get("/", Some(&board_admin)).await.bytes).to_string(); | |
| 9624 | + assert!(html.contains("service-switcher"), "{html}"); | |
| 9625 | + assert!(html.contains("service-mark-on"), "{html}"); | |
| 9626 | + assert!(html.contains("aria-current=\"page\""), "{html}"); | |
| 9627 | + // The trio reads in the order the config gave, with the current app | |
| 9628 | + // between its neighbours, not pushed to an end. | |
| 9629 | + let in_at = html.find("href=\"http://127.0.0.1:7655\"").unwrap(); | |
| 9630 | + let iz_at = html.find("service-mark-on").unwrap(); | |
| 9631 | + let im_at = html.find("href=\"http://127.0.0.1:7650\"").unwrap(); | |
| 9632 | + assert!(in_at < iz_at && iz_at < im_at, "marks out of order: {html}"); | |
| 9633 | + | |
| 9634 | + let alone = App::open().await; | |
| 9635 | + let alone_admin = admin(&alone).await; | |
| 9636 | + let plain = String::from_utf8_lossy(&alone.get("/", Some(&alone_admin)).await.bytes).to_string(); | |
| 9637 | + assert!( | |
| 9638 | + !plain.contains("service-switcher"), | |
| 9639 | + "no services, no switcher: {plain}" | |
| 9640 | + ); | |
| 9641 | +} |
style/main.scssmodified+26 -0
| @@ -449,6 +449,32 @@ a { | ||
| 449 | 449 | background: var(--border); |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | +// The family switcher: the dizey wordmarks, lowercase like the brand's own | |
| 453 | +// name, standing with the mark while the page nav keeps its auto-margin | |
| 454 | +// place on the right. The current app is ink; the others are muted until | |
| 455 | +// the pointer finds them. | |
| 456 | +.service-switcher { | |
| 457 | + display: flex; | |
| 458 | + align-items: center; | |
| 459 | + gap: 12px; | |
| 460 | +} | |
| 461 | + | |
| 462 | +.service-mark { | |
| 463 | + font-family: var(--font-mono); | |
| 464 | + font-size: 13px; | |
| 465 | + font-weight: 500; | |
| 466 | + letter-spacing: 0.02em; | |
| 467 | + color: var(--muted); | |
| 468 | + text-decoration: none; | |
| 469 | + white-space: nowrap; | |
| 470 | + | |
| 471 | + &:hover { color: var(--ink); } | |
| 472 | + | |
| 473 | + &-on { | |
| 474 | + color: var(--ink); | |
| 475 | + } | |
| 476 | +} | |
| 477 | + | |
| 452 | 478 | .topbar-nav-links { |
| 453 | 479 | display: flex; |
| 454 | 480 | gap: 16px; |