the switcher takes the family from im instead of the file: a background beat asks GET /family with the client pair every five minutes and keeps the answer in the setting store, the topbar renders that mirror — absent, unparseable or empty keeps the chrome bare — and the [[services]] block leaves the config for good; the sign-out's return address is resolved per walk now, the saved base_url setting first, the config's own second, the bound address last
Browse files at this commitcae1d0e0e8e368cd05dfb05aa441d864dabe7bddParent: dbbff0d7 changed files +217 -195
- config/in.toml +4 -22
- crates/in-client/src/lib.rs +80 -10
- crates/in-core/src/config.rs +0 -126
- crates/in-core/src/lib.rs +1 -1
- crates/in-web/src/layout.rs +21 -10
- crates/in-web/src/main.rs +43 -0
- crates/in-web/tests/http.rs +68 -26
config/in.tomlmodified+4 -22
| @@ -20,31 +20,13 @@ base_url = "" | ||
| 20 | 20 | # created on boot. Not written here: it defaults to beside the database file, |
| 21 | 21 | # and the boot completes this file with the value it resolved. |
| 22 | 22 | |
| 23 | -# The suite the signed-in chrome's switcher links to: one [[services]] | |
| 24 | -# table per app — `key` the word the switcher renders, `name` its hover | |
| 25 | -# label, `url` the address the link points at (scheme and host, no | |
| 26 | -# trailing slash). In names itself among them so the switcher can mark | |
| 27 | -# where the reader is. Production sets these on the deploy side as | |
| 28 | -# https://<key>.dizey.sh; this committed copy carries only the | |
| 29 | -# development addresses. | |
| 23 | +# The suite the switcher's wordmarks link to is im's to keep — im's admin | |
| 24 | +# panel owns the list, and In fetches it from im and keeps a mirror. This | |
| 25 | +# file no longer names the suite; a `[[services]]` block left behind by an | |
| 26 | +# older file is reported as ignored and then passed over. | |
| 30 | 27 | # Where drive files, thumbnails and staged upload chunks live as files, created on boot. |
| 31 | 28 | storage = "/home/tahinli/Documents/Code/Rust/in/storage" |
| 32 | 29 | |
| 33 | -[[services]] | |
| 34 | -key = "in" | |
| 35 | -name = "Files" | |
| 36 | -url = "http://127.0.0.1:7655" | |
| 37 | - | |
| 38 | -[[services]] | |
| 39 | -key = "im" | |
| 40 | -name = "Account" | |
| 41 | -url = "http://127.0.0.1:7650" | |
| 42 | - | |
| 43 | -[[services]] | |
| 44 | -key = "iz" | |
| 45 | -name = "Board" | |
| 46 | -url = "http://127.0.0.1:7654" | |
| 47 | - | |
| 48 | 30 | [oidc] |
| 49 | 31 | # The provider that signs people in. Required: no default is guessed. |
| 50 | 32 | issuer = "http://127.0.0.1:7650" |
crates/in-client/src/lib.rsmodified+80 -10
| @@ -11,6 +11,9 @@ | ||
| 11 | 11 | //! against im; when im has revoked the central session, the rotation is |
| 12 | 12 | //! refused and the app sees a signed-out user. |
| 13 | 13 | |
| 14 | +use std::pin::Pin; | |
| 15 | +use std::sync::Arc; | |
| 16 | + | |
| 14 | 17 | use base64::Engine; |
| 15 | 18 | use base64::engine::general_purpose::URL_SAFE_NO_PAD as b64url; |
| 16 | 19 | use chacha20poly1305::aead::Aead; |
| @@ -44,12 +47,16 @@ pub struct Config { | ||
| 44 | 47 | pub cookie_name: String, |
| 45 | 48 | /// 32 bytes, generated once per app and kept out of the repository. |
| 46 | 49 | pub cookie_key: [u8; 32], |
| 47 | - /// The app's public origin — `base_url` when the file sets one, else | |
| 48 | - /// the address bound, derived the way the app derives its public links. | |
| 49 | - /// After clearing the local cookie, `/auth/logout` walks the browser on | |
| 50 | - /// to im's `/logout?back={logout_back}/`, so the central session ends | |
| 51 | - /// and the person lands back at this app's front door. Empty is legal — | |
| 52 | - /// im catches a stray `back` on its own `/` — but a real app sets it. | |
| 50 | + /// The app's public origin as boot knew it — `base_url` when the file | |
| 51 | + /// sets one, else the address bound. After clearing the local cookie, | |
| 52 | + /// `/auth/logout` walks the browser on to im's `/logout?back={origin}/`, | |
| 53 | + /// so the central session ends and the person lands back at this app's | |
| 54 | + /// front door. This is the fallback: an app that keeps its public | |
| 55 | + /// address live mounts a [`LogoutBack`] beside [`mount`], and the | |
| 56 | + /// sign-out asks that first — per exit, so an address changed while the | |
| 57 | + /// process runs is honored without a restart. Empty is legal — im | |
| 58 | + /// catches a stray `back` on its own `/` — but a real app sets one of | |
| 59 | + /// the two. | |
| 53 | 60 | pub logout_back: String, |
| 54 | 61 | } |
| 55 | 62 | |
| @@ -88,6 +95,18 @@ impl InClient { | ||
| 88 | 95 | } |
| 89 | 96 | } |
| 90 | 97 | |
| 98 | +/// Where im's `/logout` should send the browser after a sign-out that | |
| 99 | +/// started here: the app's public origin as it is *now*, asked per | |
| 100 | +/// sign-out. Apps that keep the address live — a database setting an | |
| 101 | +/// admin edits while the process runs — mount one of these as an app | |
| 102 | +/// context beside [`mount`]; absent, or answering `None`, the sign-out | |
| 103 | +/// falls back to [`Config::logout_back`], the origin as boot knew it. | |
| 104 | +pub struct LogoutBack(pub Arc<dyn for<'a> Fn(&'a Cx) -> BackAnswer<'a> + Send + Sync>); | |
| 105 | + | |
| 106 | +/// The boxed answer a [`LogoutBack`] resolver returns: the origin, or | |
| 107 | +/// `None` to fall back to [`Config::logout_back`]. | |
| 108 | +pub type BackAnswer<'a> = Pin<Box<dyn Future<Output = Option<String>> + Send + 'a>>; | |
| 109 | + | |
| 91 | 110 | /// Registers the client state on the router. The routes themselves register |
| 92 | 111 | /// through `discover()` — call it as usual. |
| 93 | 112 | pub fn mount(builder: RouterBuilder, config: Config) -> RouterBuilder { |
| @@ -189,6 +208,49 @@ pub async fn directory(cx: &Cx) -> Option<Vec<DirectoryMember>> { | ||
| 189 | 208 | reply.json().await.ok() |
| 190 | 209 | } |
| 191 | 210 | |
| 211 | +/// One entry of im's family list: the word the switcher renders, the | |
| 212 | +/// human label for its title, and the service's base URL — the shape im's | |
| 213 | +/// `/family` answers with. | |
| 214 | +#[derive(Debug, Clone, Serialize, Deserialize)] | |
| 215 | +pub struct ServiceJson { | |
| 216 | + pub key: String, | |
| 217 | + pub name: String, | |
| 218 | + pub url: String, | |
| 219 | +} | |
| 220 | + | |
| 221 | +impl InClient { | |
| 222 | + /// The fetch behind [`family`], on the state directly — the shape the | |
| 223 | + /// background mirror takes, which holds its own `InClient` and has no | |
| 224 | + /// request context to borrow. | |
| 225 | + pub async fn family(&self) -> Option<Vec<ServiceJson>> { | |
| 226 | + let reply = self | |
| 227 | + .http | |
| 228 | + .get(format!("{}/family", self.config.issuer)) | |
| 229 | + .basic_auth(&self.config.client_id, Some(&self.config.client_secret)) | |
| 230 | + .send() | |
| 231 | + .await | |
| 232 | + .ok()?; | |
| 233 | + if !reply.status().is_success() { | |
| 234 | + return None; | |
| 235 | + } | |
| 236 | + reply.json().await.ok() | |
| 237 | + } | |
| 238 | +} | |
| 239 | + | |
| 240 | +/// im's family list — the suite the signed-in chrome's switcher renders — | |
| 241 | +/// fetched as the app (`Authorization: Basic base64(client_id ":" | |
| 242 | +/// client_secret)`, the same credentials the photo route and the | |
| 243 | +/// introspection round-trip take) from `{issuer}/family`. Only a | |
| 244 | +/// registered app gets an answer at all. | |
| 245 | +/// | |
| 246 | +/// `None` on anything that is not a readable list — a refused pair, a | |
| 247 | +/// dropped connection, a body that is not the array — because a missed | |
| 248 | +/// beat must never look like an empty suite: the caller keeps the list it | |
| 249 | +/// has and asks again next time. | |
| 250 | +pub async fn family(cx: &Cx) -> Option<Vec<ServiceJson>> { | |
| 251 | + client(cx).family().await | |
| 252 | +} | |
| 253 | + | |
| 192 | 254 | /// Path of im's RFC 7662 introspection endpoint, relative to the issuer. |
| 193 | 255 | const INTROSPECT_PATH: &str = "/introspect"; |
| 194 | 256 | |
| @@ -544,19 +606,27 @@ async fn in_callback(cx: &Cx) -> Result<Response, topcoat::Error> { | ||
| 544 | 606 | /// Signs out of the app, then out of im: the local cookie is cleared |
| 545 | 607 | /// first — this app forgets the person even if the central hop never |
| 546 | 608 | /// lands — and the browser is sent on to im's `/logout` with this app's |
| 547 | -/// public address as `back`. im revokes the central session and returns | |
| 548 | -/// them here. The hop is a top-level navigation (the user menu's sign-out | |
| 549 | -/// link carries `data-hard`), never a fetch. | |
| 609 | +/// public address as `back`. The address is resolved here, per sign-out: | |
| 610 | +/// a [`LogoutBack`] mounted beside `mount` answers first, so an origin | |
| 611 | +/// changed while the process runs is honored on the very next exit, and | |
| 612 | +/// `Config::logout_back` — the origin as boot knew it — stands in when | |
| 613 | +/// nothing live answers. im revokes the central session and returns the | |
| 614 | +/// browser to wherever `back` named. The hop is a top-level navigation | |
| 615 | +/// (the user menu's sign-out link carries `data-hard`), never a fetch. | |
| 550 | 616 | #[route(GET "/auth/logout")] |
| 551 | 617 | async fn in_logout(cx: &Cx) -> Result<Response, topcoat::Error> { |
| 552 | 618 | let state = client(cx); |
| 553 | 619 | clear_cookie(cx, &state.config.cookie_name); |
| 620 | + let back = match try_app_context::<LogoutBack>(cx) { | |
| 621 | + Some(ask) => (ask.0)(cx).await.unwrap_or_else(|| state.config.logout_back.clone()), | |
| 622 | + None => state.config.logout_back.clone(), | |
| 623 | + }; | |
| 554 | 624 | see( |
| 555 | 625 | cx, |
| 556 | 626 | &format!( |
| 557 | 627 | "{}/logout?back={}", |
| 558 | 628 | state.config.issuer, |
| 559 | - urlencoded(&format!("{}/", state.config.logout_back)), | |
| 629 | + urlencoded(&format!("{}/", back)), | |
| 560 | 630 | ), |
| 561 | 631 | ) |
| 562 | 632 | } |
crates/in-core/src/config.rsmodified+0 -126
| @@ -202,14 +202,6 @@ struct R2Toml { | ||
| 202 | 202 | other: std::collections::BTreeMap<String, toml::Value>, |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | -/// One `[[services]]` entry, before the values are checked. | |
| 206 | -#[derive(Deserialize)] | |
| 207 | -struct ServiceToml { | |
| 208 | - key: Option<String>, | |
| 209 | - name: Option<String>, | |
| 210 | - url: Option<String>, | |
| 211 | -} | |
| 212 | - | |
| 213 | 205 | /// The shape of `config/in.toml`, before the values are checked. Anything |
| 214 | 206 | /// else the file says lands in `other`, which is read for its key names only |
| 215 | 207 | /// — enough for the report to say a key was seen and not obeyed. |
| @@ -223,7 +215,6 @@ struct Toml { | ||
| 223 | 215 | default_quota_bytes: Option<u64>, |
| 224 | 216 | base_url: Option<String>, |
| 225 | 217 | oidc: Option<OidcToml>, |
| 226 | - services: Option<Vec<ServiceToml>>, | |
| 227 | 218 | #[serde(flatten)] |
| 228 | 219 | other: std::collections::BTreeMap<String, toml::Value>, |
| 229 | 220 | } |
| @@ -258,21 +249,6 @@ pub struct R2Config { | ||
| 258 | 249 | pub key_file: PathBuf, |
| 259 | 250 | } |
| 260 | 251 | |
| 261 | -/// One entry of the `[[services]]` list: an app of the suite the signed-in | |
| 262 | -/// chrome's switcher links to. | |
| 263 | -#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] | |
| 264 | -pub struct ServiceConfig { | |
| 265 | - /// The word the switcher renders for it — `in`, `im`, `iz`. The entry | |
| 266 | - /// whose key is this app's own (`"in"`) is the one marked as where the | |
| 267 | - /// reader already is. | |
| 268 | - pub key: String, | |
| 269 | - /// The human label — "Files", "Account", "Board" — the link's title. | |
| 270 | - pub name: String, | |
| 271 | - /// The service's base URL: scheme and host, no trailing slash — the | |
| 272 | - /// shape `base_url` has, refused on the same terms. | |
| 273 | - pub url: String, | |
| 274 | -} | |
| 275 | - | |
| 276 | 252 | /// What the process needs to know before it opens a socket. |
| 277 | 253 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 278 | 254 | pub struct Config { |
| @@ -302,10 +278,6 @@ pub struct Config { | ||
| 302 | 278 | /// banners that mint them — when the file sets one. `None` follows the |
| 303 | 279 | /// address bound. |
| 304 | 280 | pub base_url: Option<String>, |
| 305 | - /// The suite the signed-in chrome's switcher links to, from the | |
| 306 | - /// `[[services]]` tables. An empty list is a file silent about any | |
| 307 | - /// suite, and the chrome stays exactly as it has always been. | |
| 308 | - pub services: Vec<ServiceConfig>, | |
| 309 | 281 | /// The OIDC provider In trusts. |
| 310 | 282 | pub oidc: OidcConfig, |
| 311 | 283 | /// Keys the file sets that nothing here reads, in the order the file |
| @@ -482,22 +454,6 @@ impl Config { | ||
| 482 | 454 | })?; |
| 483 | 455 | } |
| 484 | 456 | |
| 485 | - // The suite the chrome's switcher links to. A silent list keeps the | |
| 486 | - // single-app chrome; an entry present must be whole, because a | |
| 487 | - // wordmark pointing nowhere is worse than none — and its url must | |
| 488 | - // be joinable to a link for the same reason `base_url` must. | |
| 489 | - let mut services = Vec::new(); | |
| 490 | - for (i, entry) in toml.services.unwrap_or_default().into_iter().enumerate() { | |
| 491 | - let key = value(entry.key) | |
| 492 | - .ok_or_else(|| service_error(i, "its key is missing or empty".into()))?; | |
| 493 | - let name = value(entry.name) | |
| 494 | - .ok_or_else(|| service_error(i, "its name is missing or empty".into()))?; | |
| 495 | - let url = value(entry.url) | |
| 496 | - .ok_or_else(|| service_error(i, "its url is missing or empty".into()))?; | |
| 497 | - Config::validate_base_url(&url).map_err(|why| service_error(i, why))?; | |
| 498 | - services.push(ServiceConfig { key, name, url }); | |
| 499 | - } | |
| 500 | - | |
| 501 | 457 | let oidc_toml = toml.oidc.ok_or(ConfigError::Missing("[oidc]"))?; |
| 502 | 458 | let issuer = value(oidc_toml.issuer).ok_or(ConfigError::Missing("oidc.issuer"))?; |
| 503 | 459 | let client_id = value(oidc_toml.client_id).ok_or(ConfigError::Missing("oidc.client_id"))?; |
| @@ -521,7 +477,6 @@ impl Config { | ||
| 521 | 477 | purge_after_days, |
| 522 | 478 | default_quota_bytes, |
| 523 | 479 | base_url, |
| 524 | - services, | |
| 525 | 480 | oidc: OidcConfig { |
| 526 | 481 | issuer, |
| 527 | 482 | client_id, |
| @@ -602,15 +557,6 @@ fn listen_url_of(listen: &SocketAddr) -> String { | ||
| 602 | 557 | } |
| 603 | 558 | } |
| 604 | 559 | |
| 605 | -/// The boot error for a `[[services]]` entry that is not whole. Entries are | |
| 606 | -/// numbered from one, so the message names the offender a human can find. | |
| 607 | -fn service_error(index: usize, why: String) -> ConfigError { | |
| 608 | - ConfigError::Invalid { | |
| 609 | - key: "services", | |
| 610 | - why: format!("entry {}: {why}", index + 1), | |
| 611 | - } | |
| 612 | -} | |
| 613 | - | |
| 614 | 560 | impl Config { |
| 615 | 561 | /// Whether a base URL is an origin a public link can be built on: an |
| 616 | 562 | /// `http` or `https` scheme and no trailing slash. Shared by the file's |
| @@ -947,78 +893,6 @@ redirect_uri = "https://files.example.com/auth/callback" | ||
| 947 | 893 | )); |
| 948 | 894 | } |
| 949 | 895 | |
| 950 | - /// The switcher is off until the file names a suite: a `[[services]]`-less | |
| 951 | - /// file is the chrome as it has always been, no half-rendered trio. | |
| 952 | - #[test] | |
| 953 | - fn a_file_without_services_leaves_the_switcher_off() { | |
| 954 | - let scratch = Scratch::new(); | |
| 955 | - scratch.write(FULL); | |
| 956 | - let config = scratch.load().unwrap(); | |
| 957 | - assert!(config.services.is_empty()); | |
| 958 | - } | |
| 959 | - | |
| 960 | - #[test] | |
| 961 | - fn services_parse_in_the_order_the_file_gives_them() { | |
| 962 | - let scratch = Scratch::new(); | |
| 963 | - scratch.write(&FULL.replace( | |
| 964 | - "[oidc]", | |
| 965 | - "[[services]]\n\ | |
| 966 | - key = \"in\"\n\ | |
| 967 | - name = \"Files\"\n\ | |
| 968 | - url = \"https://files.example.com\"\n\ | |
| 969 | - \n\ | |
| 970 | - [[services]]\n\ | |
| 971 | - key = \"im\"\n\ | |
| 972 | - name = \"Account\"\n\ | |
| 973 | - url = \"https://id.example.com\"\n\ | |
| 974 | - [oidc]", | |
| 975 | - )); | |
| 976 | - let config = scratch.load().unwrap(); | |
| 977 | - assert_eq!( | |
| 978 | - config.services, | |
| 979 | - vec![ | |
| 980 | - ServiceConfig { | |
| 981 | - key: "in".into(), | |
| 982 | - name: "Files".into(), | |
| 983 | - url: "https://files.example.com".into(), | |
| 984 | - }, | |
| 985 | - ServiceConfig { | |
| 986 | - key: "im".into(), | |
| 987 | - name: "Account".into(), | |
| 988 | - url: "https://id.example.com".into(), | |
| 989 | - }, | |
| 990 | - ] | |
| 991 | - ); | |
| 992 | - } | |
| 993 | - | |
| 994 | - #[test] | |
| 995 | - fn a_service_url_without_a_scheme_is_refused() { | |
| 996 | - let scratch = Scratch::new(); | |
| 997 | - scratch.write(&FULL.replace( | |
| 998 | - "[oidc]", | |
| 999 | - "[[services]]\nkey = \"im\"\nname = \"Account\"\nurl = \"id.example.com\"\n[oidc]", | |
| 1000 | - )); | |
| 1001 | - assert!(matches!( | |
| 1002 | - scratch.load().unwrap_err(), | |
| 1003 | - ConfigError::Invalid { key: "services", .. } | |
| 1004 | - )); | |
| 1005 | - } | |
| 1006 | - | |
| 1007 | - #[test] | |
| 1008 | - fn an_incomplete_service_entry_names_the_offender() { | |
| 1009 | - let scratch = Scratch::new(); | |
| 1010 | - scratch.write(&FULL.replace( | |
| 1011 | - "[oidc]", | |
| 1012 | - "[[services]]\nkey = \"im\"\nname = \"\"\nurl = \"https://id.example.com\"\n[oidc]", | |
| 1013 | - )); | |
| 1014 | - let err = scratch.load().unwrap_err(); | |
| 1015 | - assert!(matches!( | |
| 1016 | - err, | |
| 1017 | - ConfigError::Invalid { key: "services", .. } | |
| 1018 | - )); | |
| 1019 | - assert!(err.to_string().contains("entry 1"), "{err}"); | |
| 1020 | - } | |
| 1021 | - | |
| 1022 | 896 | #[test] |
| 1023 | 897 | fn zero_live_seconds_is_refused() { |
| 1024 | 898 | let scratch = Scratch::new(); |
crates/in-core/src/lib.rsmodified+1 -1
| @@ -20,7 +20,7 @@ pub use store::{ | ||
| 20 | 20 | }; |
| 21 | 21 | |
| 22 | 22 | #[cfg(feature = "server")] |
| 23 | -pub use config::{Config, ConfigError, OidcConfig, ServiceConfig}; | |
| 23 | +pub use config::{Config, ConfigError, OidcConfig}; | |
| 24 | 24 | #[cfg(feature = "server")] |
| 25 | 25 | pub use live::{Change, Topic, next_seq}; |
| 26 | 26 | #[cfg(feature = "server")] |
crates/in-web/src/layout.rsmodified+21 -10
| @@ -221,23 +221,34 @@ pub async fn topbar_nav(cx: &Cx, active: NavPage, lang: Lang) -> Result { | ||
| 221 | 221 | } |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | -/// This app's key in the `[[services]]` list: the entry matching it is the | |
| 225 | -/// one marked as where the reader already is. | |
| 224 | +/// This app's key in the family list: the entry matching it is the one | |
| 225 | +/// marked as where the reader already is. | |
| 226 | 226 | const SELF_KEY: &str = "in"; |
| 227 | 227 | |
| 228 | -/// The topbar's suite switcher: every `[[services]]` entry as its wordmark, | |
| 229 | -/// beside the page nav, the current one marked. Rendered only when the file | |
| 230 | -/// names a suite — a file silent about the others keeps the chrome exactly | |
| 231 | -/// as it has always been. The links point at the services' own addresses, | |
| 232 | -/// so the cross-origin ones navigate the browser natively; this app's entry | |
| 233 | -/// lands where a soft-nav swap would have gone anyway. | |
| 228 | +/// The suite list as im keeps it, mirrored into the `setting` store under | |
| 229 | +/// `family` by the background fetch that started at boot. A list that is | |
| 230 | +/// absent, or not parseable JSON, reads as empty — a suite nobody told | |
| 231 | +/// this app about keeps the chrome exactly as it has always been. | |
| 232 | +async fn family_of(cx: &Cx) -> Vec<in_client::ServiceJson> { | |
| 233 | + match app(cx).store.get_setting("family").await { | |
| 234 | + Ok(Some(raw)) => serde_json::from_str(&raw).unwrap_or_default(), | |
| 235 | + _ => Vec::new(), | |
| 236 | + } | |
| 237 | +} | |
| 238 | + | |
| 239 | +/// The topbar's suite switcher: every family entry as its wordmark, | |
| 240 | +/// beside the page nav, the current one marked. Rendered only when a | |
| 241 | +/// mirrored list is actually there. The links point at the services' own | |
| 242 | +/// addresses, so the cross-origin ones navigate the browser natively; | |
| 243 | +/// this app's entry lands where a soft-nav swap would have gone anyway. | |
| 234 | 244 | async fn app_switcher(cx: &Cx) -> Result { |
| 235 | - let empty = app(cx).config.services.is_empty(); | |
| 245 | + let family = family_of(cx).await; | |
| 246 | + let empty = family.is_empty(); | |
| 236 | 247 | view! { |
| 237 | 248 | cx => |
| 238 | 249 | if !empty { |
| 239 | 250 | <nav class="app-switcher"> |
| 240 | - for service in &app(cx).config.services { | |
| 251 | + for service in &family { | |
| 241 | 252 | <a |
| 242 | 253 | class=(if service.key == SELF_KEY { |
| 243 | 254 | "app-switcher-mark app-switcher-here" |
crates/in-web/src/main.rsmodified+43 -0
| @@ -5,6 +5,7 @@ use in_core::store::{Store, TursoStore}; | ||
| 5 | 5 | use in_core::store::r2::R2Blobs; |
| 6 | 6 | use topcoat::Result; |
| 7 | 7 | use topcoat::asset::{AssetBundle, RouterBuilderAssetExt}; |
| 8 | +use topcoat::context::Cx; | |
| 8 | 9 | use topcoat::cookie::RouterBuilderCookieExt; |
| 9 | 10 | use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, route}; |
| 10 | 11 | |
| @@ -212,6 +213,17 @@ async fn main() { | ||
| 212 | 213 | logout_back: config.public_origin(), |
| 213 | 214 | }; |
| 214 | 215 | |
| 216 | + // The suite the switcher's wordmarks link to is im's to keep: im's | |
| 217 | + // admin panel owns the list and `/family` serves it to registered | |
| 218 | + // apps. This mirror is refreshed on the beat, first pass right away so | |
| 219 | + // a fresh deploy shows the trio at boot; an im that does not answer — | |
| 220 | + // down, restarting, mid-deploy — costs one log line and nothing else: | |
| 221 | + // the last list filed keeps the switcher alive until the next beat. | |
| 222 | + tokio::spawn(family_sync( | |
| 223 | + store.clone(), | |
| 224 | + in_client::InClient::new(oidc.clone()), | |
| 225 | + )); | |
| 226 | + | |
| 215 | 227 | // Told when the process is stopping, so the live streams end instead of |
| 216 | 228 | // being waited out. See `in_web::live::Shutdown`. |
| 217 | 229 | let (stop, stopping) = tokio::sync::watch::channel(false); |
| @@ -238,6 +250,9 @@ async fn main() { | ||
| 238 | 250 | shutdown: in_web::live::Shutdown(stopping), |
| 239 | 251 | link_key: cookie_key, |
| 240 | 252 | }) |
| 253 | + .app_context(in_client::LogoutBack(Arc::new(|cx: &Cx| { | |
| 254 | + Box::pin(async move { Some(in_web::server::share_origin(cx).await) }) | |
| 255 | + }))) | |
| 241 | 256 | .app_context(in_web::live::LiveWindow(std::time::Duration::from_secs( |
| 242 | 257 | live_seconds, |
| 243 | 258 | ))) |
| @@ -310,3 +325,31 @@ async fn shutdown_signal() { | ||
| 310 | 325 | () = terminate => {} |
| 311 | 326 | } |
| 312 | 327 | } |
| 328 | + | |
| 329 | +/// How often the suite list is re-fetched from im. Short enough that a | |
| 330 | +/// service the admin adds over there shows up here before anyone goes | |
| 331 | +/// looking for its wordmark, long enough that the two are not talking | |
| 332 | +/// about it constantly. | |
| 333 | +const FAMILY_SECONDS: u64 = 300; | |
| 334 | + | |
| 335 | +/// Keeps the switcher's list a mirror of im's family: fetched as this app | |
| 336 | +/// (Basic client credentials) and filed as a JSON string under the | |
| 337 | +/// `family` setting — the one row the switcher reads, so a beat that | |
| 338 | +/// answers simply replaces what is filed. Runs the whole life of the | |
| 339 | +/// process; nothing here is worth keeping alive past shutdown. | |
| 340 | +async fn family_sync(store: Arc<dyn in_core::store::Store>, client: in_client::InClient) { | |
| 341 | + loop { | |
| 342 | + match client.family().await { | |
| 343 | + Some(family) => match serde_json::to_string(&family) { | |
| 344 | + Ok(json) => { | |
| 345 | + if let Err(problem) = store.set_setting("family", &json).await { | |
| 346 | + eprintln!("family sync: {problem}"); | |
| 347 | + } | |
| 348 | + } | |
| 349 | + Err(problem) => eprintln!("family sync: {problem}"), | |
| 350 | + }, | |
| 351 | + None => eprintln!("family sync: im did not answer; keeping the list there is"), | |
| 352 | + } | |
| 353 | + tokio::time::sleep(std::time::Duration::from_secs(FAMILY_SECONDS)).await; | |
| 354 | + } | |
| 355 | +} |
crates/in-web/tests/http.rsmodified+68 -26
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | //! |
| 10 | 10 | //! New HTTP tests belong in this file rather than a new `tests/*.rs`: one |
| 11 | 11 | //! test binary links and runs once. |
| 12 | -use in_core::{Config, OidcConfig, ServiceConfig}; | |
| 12 | +use in_core::{Config, OidcConfig}; | |
| 13 | 13 | use std::collections::HashMap; |
| 14 | 14 | use std::path::PathBuf; |
| 15 | 15 | use std::sync::{Arc, Mutex}; |
| @@ -18,6 +18,7 @@ use http::{HeaderValue, Request, StatusCode, header}; | ||
| 18 | 18 | use in_core::store::{ShareKind, Store, TursoStore}; |
| 19 | 19 | use in_web::server::App; |
| 20 | 20 | use topcoat::asset::{AssetBundle, RouterBuilderAssetExt}; |
| 21 | +use topcoat::context::Cx; | |
| 21 | 22 | use topcoat::cookie::RouterBuilderCookieExt; |
| 22 | 23 | use topcoat::router::{Body, BodyLimit, Router, RouterBuilderDiscoverExt, to_bytes}; |
| 23 | 24 | use ulid::Ulid; |
| @@ -315,13 +316,15 @@ struct TestApp { | ||
| 315 | 316 | |
| 316 | 317 | impl TestApp { |
| 317 | 318 | async fn build() -> Self { |
| 318 | - Self::build_with(None, Vec::new()).await | |
| 319 | + Self::build_with(None, &[]).await | |
| 319 | 320 | } |
| 320 | 321 | |
| 321 | 322 | /// The same workspace with a config `base_url` set — the key that |
| 322 | - /// decides where the public links are told to point — and optional | |
| 323 | - /// `[[services]]` entries, the suite the topbar's switcher links to. | |
| 324 | - async fn build_with(base_url: Option<&str>, services: Vec<ServiceConfig>) -> Self { | |
| 323 | + /// decides where the public links are told to point — and an optional | |
| 324 | + /// family list, the suite the topbar's switcher links to, seeded the | |
| 325 | + /// way the background fetch files it: one JSON row in the `setting` | |
| 326 | + /// store under `family`. | |
| 327 | + async fn build_with(base_url: Option<&str>, family: &[(&str, &str, &str)]) -> Self { | |
| 325 | 328 | let dir = std::env::temp_dir().join(format!("in-http-{}", Ulid::new())); |
| 326 | 329 | std::fs::create_dir_all(&dir).unwrap(); |
| 327 | 330 | let db = dir.join("in.db"); |
| @@ -331,6 +334,20 @@ impl TestApp { | ||
| 331 | 334 | .await |
| 332 | 335 | .unwrap(), |
| 333 | 336 | ); |
| 337 | + if !family.is_empty() { | |
| 338 | + let list: Vec<in_client::ServiceJson> = family | |
| 339 | + .iter() | |
| 340 | + .map(|(key, name, url)| in_client::ServiceJson { | |
| 341 | + key: key.to_string(), | |
| 342 | + name: name.to_string(), | |
| 343 | + url: url.to_string(), | |
| 344 | + }) | |
| 345 | + .collect(); | |
| 346 | + store | |
| 347 | + .set_setting("family", &serde_json::to_string(&list).unwrap()) | |
| 348 | + .await | |
| 349 | + .unwrap(); | |
| 350 | + } | |
| 334 | 351 | let fake = FakeIm::spawn().await; |
| 335 | 352 | let client = in_client::Config { |
| 336 | 353 | issuer: fake.url(), |
| @@ -349,7 +366,6 @@ impl TestApp { | ||
| 349 | 366 | purge_after_days: 30, |
| 350 | 367 | default_quota_bytes: 10 * 1024 * 1024 * 1024, |
| 351 | 368 | base_url: base_url.map(str::to_string), |
| 352 | - services, | |
| 353 | 369 | r2: None, |
| 354 | 370 | oidc: OidcConfig { |
| 355 | 371 | issuer: fake.url(), |
| @@ -381,6 +397,9 @@ impl TestApp { | ||
| 381 | 397 | // reach for `link_key`, not for whatever key sits nearest. |
| 382 | 398 | link_key: [11u8; 32], |
| 383 | 399 | }) |
| 400 | + .app_context(in_client::LogoutBack(Arc::new(|cx: &Cx| { | |
| 401 | + Box::pin(async move { Some(in_web::server::share_origin(cx).await) }) | |
| 402 | + }))) | |
| 384 | 403 | .build(); |
| 385 | 404 | Self { |
| 386 | 405 | dir, |
| @@ -2696,8 +2715,8 @@ async fn topbar_nav_has_no_search_link() { | ||
| 2696 | 2715 | } |
| 2697 | 2716 | |
| 2698 | 2717 | #[tokio::test] |
| 2699 | -async fn the_suite_switcher_renders_when_services_are_set() { | |
| 2700 | - // No [[services]]: no switcher — the chrome as it always was. | |
| 2718 | +async fn the_suite_switcher_renders_when_the_family_is_mirrored() { | |
| 2719 | + // No mirrored family: no switcher — the chrome as it always was. | |
| 2701 | 2720 | let bare = TestApp::build().await; |
| 2702 | 2721 | let cookie = bare |
| 2703 | 2722 | .sign_in("sub-switch0", "switch0@in.test", "Switch0") |
| @@ -2708,22 +2727,10 @@ async fn the_suite_switcher_renders_when_services_are_set() { | ||
| 2708 | 2727 | |
| 2709 | 2728 | let app = TestApp::build_with( |
| 2710 | 2729 | None, |
| 2711 | - vec![ | |
| 2712 | - ServiceConfig { | |
| 2713 | - key: "in".into(), | |
| 2714 | - name: "Files".into(), | |
| 2715 | - url: "https://files.example.com".into(), | |
| 2716 | - }, | |
| 2717 | - ServiceConfig { | |
| 2718 | - key: "im".into(), | |
| 2719 | - name: "Account".into(), | |
| 2720 | - url: "https://id.example.com".into(), | |
| 2721 | - }, | |
| 2722 | - ServiceConfig { | |
| 2723 | - key: "iz".into(), | |
| 2724 | - name: "Board".into(), | |
| 2725 | - url: "https://board.example.com".into(), | |
| 2726 | - }, | |
| 2730 | + &[ | |
| 2731 | + ("in", "Files", "https://files.example.com"), | |
| 2732 | + ("im", "Account", "https://id.example.com"), | |
| 2733 | + ("iz", "Board", "https://board.example.com"), | |
| 2727 | 2734 | ], |
| 2728 | 2735 | ) |
| 2729 | 2736 | .await; |
| @@ -2744,6 +2751,41 @@ async fn the_suite_switcher_renders_when_services_are_set() { | ||
| 2744 | 2751 | assert_eq!(nav.matches("aria-current").count(), 1, "{nav}"); |
| 2745 | 2752 | } |
| 2746 | 2753 | |
| 2754 | +/// The `back` the sign-out hands im is read per sign-out: an origin saved | |
| 2755 | +/// on the running install — the `base_url` setting the settings panel | |
| 2756 | +/// writes — takes effect on the very next exit, without a restart. Until | |
| 2757 | +/// one is saved, the chain falls back to the config origin, which here is | |
| 2758 | +/// the address bound. | |
| 2759 | +#[tokio::test] | |
| 2760 | +async fn logout_back_follows_the_origin_saved_at_runtime() { | |
| 2761 | + let app = TestApp::build().await; | |
| 2762 | + let cookie = app.sign_in("sub-logout", "logout@in.test", "Logout").await; | |
| 2763 | + | |
| 2764 | + // Nothing saved: the listen-derived origin carries the back address. | |
| 2765 | + let page = app.get("/auth/logout", Some(&cookie)).await; | |
| 2766 | + let expected = format!( | |
| 2767 | + "{}/logout?back=http%3A%2F%2F127.0.0.1%3A7655%2F", | |
| 2768 | + app.client.issuer | |
| 2769 | + ); | |
| 2770 | + assert_eq!(page.location.as_deref(), Some(expected.as_str())); | |
| 2771 | + | |
| 2772 | + // The admin saves a public origin; the next sign-out hands im that. | |
| 2773 | + let answer = app | |
| 2774 | + .post( | |
| 2775 | + "/api/settings/base_url", | |
| 2776 | + Some(&cookie), | |
| 2777 | + &[("base_url", "https://files.example.com")], | |
| 2778 | + ) | |
| 2779 | + .await; | |
| 2780 | + assert!(answer.accepted(), "base_url refused: {:?}", answer.location); | |
| 2781 | + let page = app.get("/auth/logout", Some(&cookie)).await; | |
| 2782 | + let expected = format!( | |
| 2783 | + "{}/logout?back=https%3A%2F%2Ffiles.example.com%2F", | |
| 2784 | + app.client.issuer | |
| 2785 | + ); | |
| 2786 | + assert_eq!(page.location.as_deref(), Some(expected.as_str())); | |
| 2787 | +} | |
| 2788 | + | |
| 2747 | 2789 | // -- settings --------------------------------------------------------------- |
| 2748 | 2790 | |
| 2749 | 2791 | #[tokio::test] |
| @@ -2842,7 +2884,7 @@ async fn settings_quota_and_disable_guards() { | ||
| 2842 | 2884 | /// `base_url`. |
| 2843 | 2885 | #[tokio::test] |
| 2844 | 2886 | async fn share_links_carry_the_configured_base_url() { |
| 2845 | - let app = TestApp::build_with(Some("https://files.example.com"), Vec::new()).await; | |
| 2887 | + let app = TestApp::build_with(Some("https://files.example.com"), &[]).await; | |
| 2846 | 2888 | let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; |
| 2847 | 2889 | let owner = owner_of(&app, "sub-admin").await; |
| 2848 | 2890 | let file = file_id(&app, &owner, "notes.txt", b"hello in").await; |
| @@ -3169,7 +3211,7 @@ async fn server_address_setting_drives_share_links() { | ||
| 3169 | 3211 | /// file sets one: the setting wins only while it is non-empty. |
| 3170 | 3212 | #[tokio::test] |
| 3171 | 3213 | async fn cleared_setting_falls_back_to_the_configured_base_url() { |
| 3172 | - let app = TestApp::build_with(Some("https://cfg.example.com"), Vec::new()).await; | |
| 3214 | + let app = TestApp::build_with(Some("https://cfg.example.com"), &[]).await; | |
| 3173 | 3215 | let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; |
| 3174 | 3216 | let owner = owner_of(&app, "sub-admin").await; |
| 3175 | 3217 | let file = file_id(&app, &owner, "notes.txt", b"hello in").await; |