The modal offers im's directory as a dropdown
Browse files at this commit83f47c57661878e48d43ae3649560caa50945dcfParent: 76a4897- feat
- the modal offers im's directory as a dropdown
4 changed files +241 -14
- crates/in-client/src/lib.rs +35 -0
- crates/in-web/src/i18n.rs +3 -0
- crates/in-web/src/share.rs +33 -1
- crates/in-web/tests/http.rs +170 -13
crates/in-client/src/lib.rsmodified+35 -0
| @@ -147,6 +147,41 @@ pub async fn photo_for(cx: &Cx, user_id: &str) -> Option<(Vec<u8>, String)> { | ||
| 147 | 147 | Some((bytes, mime)) |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | +/// One entry of im's directory: the stable subject, the address, the | |
| 151 | +/// display name, and whether im calls the person an admin — exactly what | |
| 152 | +/// mirroring the directory into local user rows needs. | |
| 153 | +#[derive(Debug, Clone, Deserialize)] | |
| 154 | +pub struct DirectoryMember { | |
| 155 | + pub sub: String, | |
| 156 | + pub email: String, | |
| 157 | + pub name: String, | |
| 158 | + pub admin: bool, | |
| 159 | +} | |
| 160 | + | |
| 161 | +/// im's family phonebook: every person im knows, fetched as the app | |
| 162 | +/// (`Authorization: Basic base64(client_id ":" client_secret)`, the same | |
| 163 | +/// credentials the photo route and the introspection round-trip take) from | |
| 164 | +/// `{issuer}/directory`. Only a registered app gets an answer at all. | |
| 165 | +/// | |
| 166 | +/// `None` on anything that is not a readable list — a refused pair, a | |
| 167 | +/// dropped connection, a body that is not the array — because a missed | |
| 168 | +/// beat must never look like an empty directory: the caller keeps the | |
| 169 | +/// rows it has and asks again next time. | |
| 170 | +pub async fn directory(cx: &Cx) -> Option<Vec<DirectoryMember>> { | |
| 171 | + let state = client(cx); | |
| 172 | + let reply = state | |
| 173 | + .http | |
| 174 | + .get(format!("{}/directory", state.config.issuer)) | |
| 175 | + .basic_auth(&state.config.client_id, Some(&state.config.client_secret)) | |
| 176 | + .send() | |
| 177 | + .await | |
| 178 | + .ok()?; | |
| 179 | + if !reply.status().is_success() { | |
| 180 | + return None; | |
| 181 | + } | |
| 182 | + reply.json().await.ok() | |
| 183 | +} | |
| 184 | + | |
| 150 | 185 | /// Path of im's RFC 7662 introspection endpoint, relative to the issuer. |
| 151 | 186 | const INTROSPECT_PATH: &str = "/introspect"; |
| 152 | 187 |
crates/in-web/src/i18n.rsmodified+3 -0
| @@ -144,6 +144,7 @@ pub enum Key { | ||
| 144 | 144 | SharedWith, |
| 145 | 145 | ShareWithUser, |
| 146 | 146 | SharePlaceholder, |
| 147 | + SharePickPerson, | |
| 147 | 148 | CanDownload, |
| 148 | 149 | RemoveAccess, |
| 149 | 150 | NoSharedItems, |
| @@ -296,6 +297,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 296 | 297 | SharedWith => "Shared with", |
| 297 | 298 | ShareWithUser => "Share with someone", |
| 298 | 299 | SharePlaceholder => "Name or address", |
| 300 | + SharePickPerson => "Pick a person", | |
| 299 | 301 | CanDownload => "May download", |
| 300 | 302 | RemoveAccess => "Remove access", |
| 301 | 303 | NoSharedItems => "Nothing is shared with you yet.", |
| @@ -437,6 +439,7 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 437 | 439 | SharedWith => "Paylaşılanlar", |
| 438 | 440 | ShareWithUser => "Biriyle paylaş", |
| 439 | 441 | SharePlaceholder => "Ad ya da adres", |
| 442 | + SharePickPerson => "Birini seç", | |
| 440 | 443 | CanDownload => "İndirebilir", |
| 441 | 444 | RemoveAccess => "Erişimi kaldır", |
| 442 | 445 | NoSharedItems => "Seninle henüz bir şey paylaşılmadı.", |
crates/in-web/src/share.rsmodified+33 -1
| @@ -1137,6 +1137,29 @@ pub(crate) async fn share_modal( | ||
| 1137 | 1137 | people.push((grantee.display_name, grantee.email, grant.can_download)); |
| 1138 | 1138 | } |
| 1139 | 1139 | } |
| 1140 | + // The shareable people come from im's directory: each entry mirrored | |
| 1141 | + // into a local user row here, so the address the form posts resolves | |
| 1142 | + // the way a typed one would — then the owner, anyone already on the | |
| 1143 | + // list above, and any locally-disabled account are taken back out | |
| 1144 | + // (im's directory not naming someone disabled here must not offer | |
| 1145 | + // them). A directory that does not answer, or answers nobody new, | |
| 1146 | + // leaves the typed-address field in place below. | |
| 1147 | + let default_quota = app(cx).config.default_quota_bytes; | |
| 1148 | + let mut candidates: Vec<User> = Vec::new(); | |
| 1149 | + if let Some(members) = in_client::directory(cx).await { | |
| 1150 | + for member in members { | |
| 1151 | + let row = store | |
| 1152 | + .provision_user(&member.sub, &member.email, &member.name, default_quota) | |
| 1153 | + .await?; | |
| 1154 | + if !row.disabled | |
| 1155 | + && row.id != user.id | |
| 1156 | + && !people.iter().any(|(_, email, _)| *email == row.email) | |
| 1157 | + { | |
| 1158 | + candidates.push(row); | |
| 1159 | + } | |
| 1160 | + } | |
| 1161 | + } | |
| 1162 | + candidates.sort_by(|a, b| a.display_name.cmp(&b.display_name)); | |
| 1140 | 1163 | let refusal = query_value(uri(cx).query().unwrap_or(""), "refusal") |
| 1141 | 1164 | .and_then(|code| Refusal::from_code(&code)); |
| 1142 | 1165 | Ok(Some(view! { |
| @@ -1169,7 +1192,16 @@ pub(crate) async fn share_modal( | ||
| 1169 | 1192 | <form class="pop-row-form share-add" method="post" action="/api/share/user/add"> |
| 1170 | 1193 | <input type="hidden" name="kind" value=(kind.as_str())> |
| 1171 | 1194 | <input type="hidden" name="target_id" value=(target_id.to_string())> |
| 1172 | - <input class="field-input share-add-email" type="email" name="email" required="" placeholder=(t(language, Key::SharePlaceholder)) aria-label=(t(language, Key::EmailAddress))> | |
| 1195 | + if candidates.is_empty() { | |
| 1196 | + <input class="field-input share-add-email" type="email" name="email" required="" placeholder=(t(language, Key::SharePlaceholder)) aria-label=(t(language, Key::EmailAddress))> | |
| 1197 | + } else { | |
| 1198 | + <select class="field-input share-add-email" name="email" required="" aria-label=(t(language, Key::SharePickPerson))> | |
| 1199 | + <option value="" disabled="" selected="">(t(language, Key::SharePickPerson))</option> | |
| 1200 | + for person in &candidates { | |
| 1201 | + <option value=(person.email.clone())>(format!("{} — {}", person.display_name.clone(), person.email.clone()))</option> | |
| 1202 | + } | |
| 1203 | + </select> | |
| 1204 | + } | |
| 1173 | 1205 | <select class="field-input" name="can_download" aria-label=(t(language, Key::CanDownload))> |
| 1174 | 1206 | <option value="1">(t(language, Key::CanDownload))</option> |
| 1175 | 1207 | <option value="0">(t(language, Key::ViewOnly))</option> |
crates/in-web/tests/http.rsmodified+170 -13
| @@ -42,6 +42,7 @@ struct FakeIm { | ||
| 42 | 42 | addr: std::net::SocketAddr, |
| 43 | 43 | tokens: Arc<Mutex<HashMap<String, serde_json::Value>>>, |
| 44 | 44 | photos: Arc<Mutex<HashMap<String, (Vec<u8>, String)>>>, |
| 45 | + directory: Arc<Mutex<Vec<serde_json::Value>>>, | |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | 48 | impl FakeIm { |
| @@ -52,8 +53,10 @@ impl FakeIm { | ||
| 52 | 53 | Arc::new(Mutex::new(HashMap::new())); |
| 53 | 54 | let photos: Arc<Mutex<HashMap<String, (Vec<u8>, String)>>> = |
| 54 | 55 | Arc::new(Mutex::new(HashMap::new())); |
| 56 | + let directory: Arc<Mutex<Vec<serde_json::Value>>> = Arc::new(Mutex::new(Vec::new())); | |
| 55 | 57 | let map = tokens.clone(); |
| 56 | 58 | let pmap = photos.clone(); |
| 59 | + let dmap = directory.clone(); | |
| 57 | 60 | tokio::spawn(async move { |
| 58 | 61 | loop { |
| 59 | 62 | let Ok((socket, _)) = listener.accept().await else { |
| @@ -61,6 +64,7 @@ impl FakeIm { | ||
| 61 | 64 | }; |
| 62 | 65 | let map = map.clone(); |
| 63 | 66 | let pmap = pmap.clone(); |
| 67 | + let dmap = dmap.clone(); | |
| 64 | 68 | tokio::spawn(async move { |
| 65 | 69 | use tokio::io::{AsyncReadExt, AsyncWriteExt}; |
| 66 | 70 | let mut socket = socket; |
| @@ -100,16 +104,22 @@ impl FakeIm { | ||
| 100 | 104 | let (status, content_type, payload) = |
| 101 | 105 | match photo_answer(&pmap, &head, &first) { |
| 102 | 106 | Some(photo) => photo, |
| 103 | - None => match answer_for(&map, &first, &body) { | |
| 104 | - Some(answer) => ( | |
| 105 | - "200 OK", | |
| 106 | - "application/json".to_string(), | |
| 107 | - serde_json::to_vec(&answer).unwrap(), | |
| 108 | - ), | |
| 109 | - // Anything but the introspection route is | |
| 110 | - // nothing at all, the way the real im 404s | |
| 111 | - // unknown paths rather than answering them. | |
| 112 | - None => ("404 Not Found", "text/plain".to_string(), Vec::new()), | |
| 107 | + None => match directory_answer(&dmap, &head, &first) { | |
| 108 | + Some(members) => members, | |
| 109 | + None => match answer_for(&map, &first, &body) { | |
| 110 | + Some(answer) => ( | |
| 111 | + "200 OK", | |
| 112 | + "application/json".to_string(), | |
| 113 | + serde_json::to_vec(&answer).unwrap(), | |
| 114 | + ), | |
| 115 | + // Anything but the photo and directory | |
| 116 | + // routes is nothing at all, the way the | |
| 117 | + // real im 404s unknown paths rather than | |
| 118 | + // answering them. | |
| 119 | + None => { | |
| 120 | + ("404 Not Found", "text/plain".to_string(), Vec::new()) | |
| 121 | + } | |
| 122 | + }, | |
| 113 | 123 | }, |
| 114 | 124 | }; |
| 115 | 125 | let response = format!( |
| @@ -130,6 +140,7 @@ impl FakeIm { | ||
| 130 | 140 | addr, |
| 131 | 141 | tokens, |
| 132 | 142 | photos, |
| 143 | + directory, | |
| 133 | 144 | } |
| 134 | 145 | } |
| 135 | 146 | |
| @@ -145,10 +156,18 @@ impl FakeIm { | ||
| 145 | 156 | .unwrap() |
| 146 | 157 | .insert(user_id.to_string(), (bytes, mime.to_string())); |
| 147 | 158 | } |
| 159 | + | |
| 160 | + /// Stages the members `GET /directory` answers with — the people im | |
| 161 | + /// knows. Unset, the route is simply not there, as for an app im has | |
| 162 | + /// not registered. | |
| 163 | + fn set_directory(&self, members: &[serde_json::Value]) { | |
| 164 | + *self.directory.lock().unwrap() = members.to_vec(); | |
| 165 | + } | |
| 148 | 166 | } |
| 149 | 167 | |
| 150 | -/// The Basic credential the app presents for `GET /photo/*`: `in-test:s3cr3t`. | |
| 151 | -const PHOTO_BASIC: &str = "Basic aW4tdGVzdDpzM2NyM3Q="; | |
| 168 | +/// The Basic credential the app presents for `GET /photo/*` and | |
| 169 | +/// `GET /directory`: `in-test:s3cr3t`. | |
| 170 | +const APP_BASIC: &str = "Basic aW4tdGVzdDpzM2NyM3Q="; | |
| 152 | 171 | |
| 153 | 172 | /// Answers `GET /photo/{id}` from the photo map — the app's Basic credential |
| 154 | 173 | /// or nothing, a missing photo exactly like a missing person. `None` for |
| @@ -175,7 +194,7 @@ fn photo_answer( | ||
| 175 | 194 | .lines() |
| 176 | 195 | .filter_map(|line| line.split_once(':')) |
| 177 | 196 | .any(|(name, value)| { |
| 178 | - name.trim().eq_ignore_ascii_case("authorization") && value.trim() == PHOTO_BASIC | |
| 197 | + name.trim().eq_ignore_ascii_case("authorization") && value.trim() == APP_BASIC | |
| 179 | 198 | }); |
| 180 | 199 | if !authed { |
| 181 | 200 | return Some(("404 Not Found", "text/plain".to_string(), Vec::new())); |
| @@ -186,6 +205,46 @@ fn photo_answer( | ||
| 186 | 205 | } |
| 187 | 206 | } |
| 188 | 207 | |
| 208 | +/// Answers `GET /directory` with the staged members — the app's Basic | |
| 209 | +/// credential or nothing, and with nothing staged no route at all. `None` | |
| 210 | +/// for anything else, which stays the introspection JSON. | |
| 211 | +fn directory_answer( | |
| 212 | + directory: &Arc<Mutex<Vec<serde_json::Value>>>, | |
| 213 | + head: &str, | |
| 214 | + request_line: &str, | |
| 215 | +) -> Option<(&'static str, String, Vec<u8>)> { | |
| 216 | + let mut parts = request_line.split_whitespace(); | |
| 217 | + if parts.next() != Some("GET") { | |
| 218 | + return None; | |
| 219 | + } | |
| 220 | + let target = parts.next().unwrap_or(""); | |
| 221 | + let path = target | |
| 222 | + .split_once('?') | |
| 223 | + .map(|(path, _)| path) | |
| 224 | + .unwrap_or(target); | |
| 225 | + if path != "/directory" { | |
| 226 | + return None; | |
| 227 | + } | |
| 228 | + let authed = head | |
| 229 | + .lines() | |
| 230 | + .filter_map(|line| line.split_once(':')) | |
| 231 | + .any(|(name, value)| { | |
| 232 | + name.trim().eq_ignore_ascii_case("authorization") && value.trim() == APP_BASIC | |
| 233 | + }); | |
| 234 | + if !authed { | |
| 235 | + return Some(("404 Not Found", "text/plain".to_string(), Vec::new())); | |
| 236 | + } | |
| 237 | + let members = directory.lock().unwrap(); | |
| 238 | + if members.is_empty() { | |
| 239 | + return Some(("404 Not Found", "text/plain".to_string(), Vec::new())); | |
| 240 | + } | |
| 241 | + Some(( | |
| 242 | + "200 OK", | |
| 243 | + "application/json".to_string(), | |
| 244 | + serde_json::to_vec(&*members).unwrap(), | |
| 245 | + )) | |
| 246 | +} | |
| 247 | + | |
| 189 | 248 | fn headers_end(req: &[u8]) -> Option<usize> { |
| 190 | 249 | req.windows(4) |
| 191 | 250 | .position(|w| w == b"\r\n\r\n") |
| @@ -1710,6 +1769,104 @@ async fn per_user_share_lands_in_shared_with_me() { | ||
| 1710 | 1769 | assert!(!bobs.text().contains("joint.txt"), "{}", bobs.text()); |
| 1711 | 1770 | } |
| 1712 | 1771 | |
| 1772 | +/// The share modal picks people from im's directory: a member who has | |
| 1773 | +/// never signed in here is offered — the render itself mirrors her into a | |
| 1774 | +/// local row — the owner is not, and granting the picked address lands her | |
| 1775 | +/// under Who has access. | |
| 1776 | +#[tokio::test] | |
| 1777 | +async fn share_modal_offers_the_im_directory() { | |
| 1778 | + let app = TestApp::build().await; | |
| 1779 | + app.fake.set_directory(&[ | |
| 1780 | + serde_json::json!({"sub": "sub-ember", "email": "ember@in.test", "name": "Ember", "admin": false}), | |
| 1781 | + serde_json::json!({"sub": "sub-bora", "email": "bora@in.test", "name": "Bora", "admin": false}), | |
| 1782 | + // The owner as im knows her: never offered to share with herself. | |
| 1783 | + serde_json::json!({"sub": "sub-admin", "email": "ada@in.test", "name": "Ada", "admin": true}), | |
| 1784 | + // Valid in im but disabled here: sharing with her is sharing with | |
| 1785 | + // nobody, so the dropdown never offers her. | |
| 1786 | + serde_json::json!({"sub": "sub-gonca", "email": "gonca@in.test", "name": "Gonca", "admin": false}), | |
| 1787 | + ]); | |
| 1788 | + let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; | |
| 1789 | + app.sign_in("sub-gonca", "gonca@in.test", "Gonca").await; | |
| 1790 | + let gonca = app | |
| 1791 | + .store | |
| 1792 | + .user_by_email("gonca@in.test") | |
| 1793 | + .await | |
| 1794 | + .unwrap() | |
| 1795 | + .unwrap(); | |
| 1796 | + app.store.set_user_disabled(&gonca.id, true).await.unwrap(); | |
| 1797 | + let owner = owner_of(&app, "sub-admin").await; | |
| 1798 | + let file = file_id(&app, &owner, "pick.txt", b"pick").await; | |
| 1799 | + | |
| 1800 | + let page = app | |
| 1801 | + .get(&format!("/drive?share=file:{file}"), Some(&admin)) | |
| 1802 | + .await; | |
| 1803 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | |
| 1804 | + let text = page.text(); | |
| 1805 | + assert!( | |
| 1806 | + text.contains("<select class=\"field-input share-add-email\" name=\"email\""), | |
| 1807 | + "modal kept the typed address: {text}" | |
| 1808 | + ); | |
| 1809 | + assert!( | |
| 1810 | + text.contains("Pick a person"), | |
| 1811 | + "no placeholder option: {text}" | |
| 1812 | + ); | |
| 1813 | + assert!(text.contains("Ember — ember@in.test"), "{}", text); | |
| 1814 | + assert!(text.contains("Bora — bora@in.test"), "{}", text); | |
| 1815 | + assert!( | |
| 1816 | + text.find("Bora").unwrap() < text.find("Ember").unwrap(), | |
| 1817 | + "directory not offered by name: {text}" | |
| 1818 | + ); | |
| 1819 | + assert!(!text.contains("Ada — ada@in.test"), "owner offered: {text}"); | |
| 1820 | + assert!( | |
| 1821 | + !text.contains("Gonca — gonca@in.test"), | |
| 1822 | + "disabled account offered: {text}" | |
| 1823 | + ); | |
| 1824 | + | |
| 1825 | + // The grant posts the picked address; Ember never signed in here. | |
| 1826 | + let answer = app | |
| 1827 | + .post( | |
| 1828 | + "/api/share/user/add", | |
| 1829 | + Some(&admin), | |
| 1830 | + &[ | |
| 1831 | + ("kind", "file"), | |
| 1832 | + ("target_id", &file), | |
| 1833 | + ("email", "ember@in.test"), | |
| 1834 | + ("can_download", "1"), | |
| 1835 | + ], | |
| 1836 | + ) | |
| 1837 | + .await; | |
| 1838 | + assert!(answer.accepted(), "add refused: {:?}", answer.location); | |
| 1839 | + let page = app | |
| 1840 | + .get(&format!("/drive?share=file:{file}"), Some(&admin)) | |
| 1841 | + .await; | |
| 1842 | + assert!(page.text().contains("Ember"), "{}", page.text()); | |
| 1843 | +} | |
| 1844 | + | |
| 1845 | +/// A directory that does not answer — im without the route, the way an | |
| 1846 | +/// unregistered or unreachable im behaves — leaves the modal exactly as | |
| 1847 | +/// it was: the typed address. | |
| 1848 | +#[tokio::test] | |
| 1849 | +async fn share_modal_without_a_directory_keeps_the_typed_address() { | |
| 1850 | + let app = TestApp::build().await; | |
| 1851 | + let admin = app.sign_in("sub-admin", "ada@in.test", "Ada").await; | |
| 1852 | + let owner = owner_of(&app, "sub-admin").await; | |
| 1853 | + let file = file_id(&app, &owner, "typed.txt", b"typed").await; | |
| 1854 | + | |
| 1855 | + let page = app | |
| 1856 | + .get(&format!("/drive?share=file:{file}"), Some(&admin)) | |
| 1857 | + .await; | |
| 1858 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | |
| 1859 | + let text = page.text(); | |
| 1860 | + assert!( | |
| 1861 | + text.contains("type=\"email\" name=\"email\""), | |
| 1862 | + "typed address lost: {text}" | |
| 1863 | + ); | |
| 1864 | + assert!( | |
| 1865 | + !text.contains("<select class=\"field-input share-add-email\""), | |
| 1866 | + "dropdown rendered without a directory: {text}" | |
| 1867 | + ); | |
| 1868 | +} | |
| 1869 | + | |
| 1713 | 1870 | /// View only means preview, no download: the reader sees the media inline |
| 1714 | 1871 | /// on the viewer page and reads the inline bytes, while `?dl=1` stays dead |
| 1715 | 1872 | /// and the page never offers the Download link. |