diff --git a/apps/rustgis-web/src/arcgis.rs b/apps/rustgis-web/src/arcgis.rs index c84e6d4cb1..1df611bd1b 100644 --- a/apps/rustgis-web/src/arcgis.rs +++ b/apps/rustgis-web/src/arcgis.rs @@ -8,7 +8,8 @@ use std::cell::Cell; use rustgis_ui_egui::Slot; -use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{arcgis_auth, cache}; +use rustgis_ui_egui::rustgis_engine::data::secrets::Secret; +use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{arcgis_auth, arcgis_token, cache}; use wasm_bindgen::JsCast as _; /// How long a request to the portal may take before it is given up. @@ -208,3 +209,107 @@ pub async fn renew_if_due(url: &str, still: Option) { } arcgis_auth::renewal_done(&r, answer.as_deref().map_err(String::clone)); } + +/// How often a token request looks at its cancellation predicate and its deadline. +const TOKEN_WATCH_MS: i32 = 50; + +/// Asks an ArcGIS Server for a token (see `rustgis_tiles::arcgis_token`): the form is POSTed to +/// `endpoint` once. The request is never redirected and carries no cookies, referrer or other +/// login; its answer is read in chunks up to the limit; every failure is a fixed sentence, never +/// the browser's own text. It is given up, and the fetch aborted, when `still` turns false +/// (checked every 50 ms) or at the deadline. `done` is called exactly once, whatever happens. +pub fn generate_token(endpoint: String, body: Secret, still: cache::Still, done: arcgis_token::Done) { + wasm_bindgen_futures::spawn_local(async move { + let result = token_request(&endpoint, &body, &still).await; + done(result); + }); +} + +async fn token_request(endpoint: &str, body: &Secret, still: &cache::Still) -> Result { + use std::{cell::Cell, rc::Rc}; + let held = Some(still.clone()); + let stopped = |_: String| arcgis_token::CANCELLED.to_string(); + cache::check_still(&held).map_err(stopped)?; + let window = web_sys::window().ok_or(arcgis_token::FAILED)?; + let failed = |_: wasm_bindgen::JsValue| arcgis_token::FAILED.to_string(); + let abort = web_sys::AbortController::new().map_err(failed)?; + let init = web_sys::RequestInit::new(); + init.set_method("POST"); + init.set_redirect(web_sys::RequestRedirect::Error); + init.set_credentials(web_sys::RequestCredentials::Omit); + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + let headers = web_sys::Headers::new().map_err(failed)?; + headers.set("Content-Type", "application/x-www-form-urlencoded").map_err(failed)?; + headers.set("Accept", "application/json").map_err(failed)?; + init.set_headers(&headers); + init.set_body(&wasm_bindgen::JsValue::from_str(body.expose())); + init.set_signal(Some(&abort.signal())); + // The watchdog aborts the fetch the moment the request is cancelled or out of time, saying why. + let why: Rc>> = Rc::default(); + let began = web_time::Instant::now(); + let watch = { + let (abort, still, why) = (abort.clone(), still.clone(), why.clone()); + wasm_bindgen::closure::Closure::::new(move || { + if !still() { + why.set(Some(arcgis_token::CANCELLED)); + abort.abort(); + } else if began.elapsed() >= std::time::Duration::from_secs(arcgis_token::DEADLINE_SECS) { + why.set(Some(arcgis_token::TIMED_OUT)); + abort.abort(); + } + }) + }; + let timer = + window.set_interval_with_callback_and_timeout_and_arguments_0(watch.as_ref().unchecked_ref(), TOKEN_WATCH_MS).map_err(failed)?; + let read = async { + // The last look before the request leaves. + cache::check_still(&held).map_err(stopped)?; + let response: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(endpoint, &init)) + .await + .map_err(failed)? + .dyn_into() + .map_err(failed)?; + let status = response.status(); + if response + .headers() + .get("content-length") + .ok() + .flatten() + .and_then(|l| l.parse::().ok()) + .is_some_and(|n| n > arcgis_token::MAX_ANSWER) + { + return Err(arcgis_token::TOO_LARGE.to_string()); + } + let mut bytes: Vec = Vec::new(); + if let Some(stream) = response.body() { + let reader: web_sys::ReadableStreamDefaultReader = + stream.get_reader().dyn_into().map_err(|_| arcgis_token::FAILED.to_string())?; + loop { + let chunk = wasm_bindgen_futures::JsFuture::from(reader.read()).await.map_err(failed)?; + if js_sys::Reflect::get(&chunk, &"done".into()).ok().and_then(|d| d.as_bool()).unwrap_or(true) { + break; + } + let value = js_sys::Uint8Array::new(&js_sys::Reflect::get(&chunk, &"value".into()).map_err(failed)?); + if bytes.len() + value.length() as usize > arcgis_token::MAX_ANSWER { + let _ = reader.cancel(); + return Err(arcgis_token::TOO_LARGE.to_string()); + } + let at = bytes.len(); + bytes.resize(at + value.length() as usize, 0); + value.copy_to(bytes.get_mut(at..).unwrap_or_default()); + } + } + // An answer that arrives after a cancel, or past the deadline a throttled timer missed, is discarded. + cache::check_still(&held).map_err(stopped)?; + if began.elapsed() >= std::time::Duration::from_secs(arcgis_token::DEADLINE_SECS) { + return Err(arcgis_token::TIMED_OUT.to_string()); + } + arcgis_token::from_answer(status, &bytes, arcgis_token::now_millis()) + }; + let result = read.await; + window.clear_interval_with_handle(timer); + abort.abort(); + drop(watch); + // An abort reads as the browser's failure: what the watchdog saw is why. + result.map_err(|e| why.get().map(str::to_string).unwrap_or(e)) +} diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index a2c810a42e..834c099661 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -686,18 +686,26 @@ async fn fetch_reached( let prepared = cache::prepare_get(url)?; let url = prepared.url.as_str(); let destination = check_destination(url, ceiling)?; + // A connection's own ArcGIS token (already in the login's address) outranks the portal + // sign-in: with one applied, the sign-in's token is neither renewed nor added. + let target = logins::inherit(bound, url); + let mut login = if prepared.signed { None } else { logins::apply(&target).map_err(str::to_string)? }; + let explicit = login.as_ref().is_some_and(|applied| applied.arcgis); // An ArcGIS sign-in's token, for the portal and the servers it covers, renewed first when it // is about to expire. It goes as the request's `token` parameter, not a header: ArcGIS's // query endpoints answer the header's CORS preflight with an error. - if !prepared.signed { + if !prepared.signed && !explicit { crate::arcgis::renew_if_due(url, still.clone()).await; cache::check_still(&still)?; + // The renewal suspended this request: its login is looked at once more. + login = logins::apply(&target).map_err(str::to_string)?; } - let target = logins::inherit(bound, url); - let login = if prepared.signed { None } else { logins::apply(&target).map_err(str::to_string)? }; let login_url = login.as_ref().map_or(url, |applied| applied.url.as_str()); - let arcgis = - if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) }; + let arcgis = if prepared.signed || explicit { + None + } else { + rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) + }; // Only the request carries credentials: messages and cache keys keep the original address. let request = arcgis.as_deref().unwrap_or(login_url); // URL parsing in a browser can normalize a numeric or escaped hostname. Judge @@ -1163,6 +1171,8 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e done, )); }); + // So are the token requests of ArcGIS connections made from a user and password. + rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_token::set_generator(crate::arcgis::generate_token); let open_inbox = inbox.clone(); let font_ctx = ctx.clone(); let (gl_list_ctx, gl_run_ctx) = (ctx.clone(), ctx.clone()); diff --git a/crates/engine/src/cmd/connection_token.rs b/crates/engine/src/cmd/connection_token.rs new file mode 100644 index 0000000000..458a46c4c9 --- /dev/null +++ b/crates/engine/src/cmd/connection_token.rs @@ -0,0 +1,59 @@ +//! `connection.generateToken`, `connection.tokenStatus` and `connection.cancelToken`: making an +//! ArcGIS token for a saved connection from its user and a password, in the background. +//! +//! All three are session operations: they change no project, make no undo step, and take no +//! response, token or completion from their caller. The work, its ownership and its landing are +//! [`crate::service_tokens`]'s. + +use serde_json::{Value, json}; + +use super::{Reopened, activate_token, finished_login, found, remember_flag}; +use crate::service_logins as web; +use crate::service_tokens::{self as tokens, Polled, Ready}; +use crate::{CmdResult, Session}; + +/// A required text parameter, as it was given. +fn text<'a>(p: &'a Value, key: &str) -> Result<&'a str, String> { + match p.get(key) { + Some(Value::String(t)) if !t.is_empty() => Ok(t), + None | Some(Value::Null | Value::String(_)) => Err(format!("{key} is required")), + Some(_) => Err(format!("{key} is text")), + } +} + +pub(super) fn generate(s: &mut Session, p: &Value) -> CmdResult { + let c = found(s, p)?.clone(); + if !web::is_service(&c) { + return Err(format!("{} connections don't log in with a token: only web-service connections do", c.kind)); + } + if c.auth != "arcgis" { + return Err("this connection doesn't log in with an ArcGIS token: set auth to arcgis (connection.edit) first".into()); + } + let endpoint = text(p, "tokenEndpoint")?.trim(); + let password = text(p, "password")?; + let remember = remember_flag(p)?; + tokens::start(s, &c, endpoint, password, remember)?; + Ok(json!({"id": c.id, "pending": true})) +} + +pub(super) fn status(s: &mut Session, p: &Value) -> CmdResult { + let c = found(s, p)?.clone(); + match tokens::poll(s, &c)? { + Polled::Idle => Ok(json!({"id": c.id, "pending": false, "loggedIn": web::logged_in(s, &c)})), + Polled::Pending => Ok(json!({"id": c.id, "pending": true})), + Polled::Ready(Ready { credential, remember, owner }) => { + // Logging in is the user's answer to a request waiting at this origin (the login ends it). + let answers = web::answers(s, &c); + // Landed under the authority that started it, never the poller's own. + let landed: Result = s.resume(&owner, |s| activate_token(s, &c, &credential, false, remember))?; + let mut out = finished_login(s, c, answers, landed?)?; + out["pending"] = json!(false); + Ok(out) + } + } +} + +pub(super) fn cancel(s: &mut Session, p: &Value) -> CmdResult { + let c = found(s, p)?.clone(); + Ok(json!({"id": c.id, "cancelled": tokens::cancel(s, &c.id)?})) +} diff --git a/crates/engine/src/cmd/connections.rs b/crates/engine/src/cmd/connections.rs index 668b0a593f..ef1c031b70 100644 --- a/crates/engine/src/cmd/connections.rs +++ b/crates/engine/src/cmd/connections.rs @@ -7,7 +7,7 @@ use serde_json::{Value, json}; use super::{b, ps}; use crate::connections::{Connection, DATABASE_KINDS, DESKTOP_ONLY, KINDS}; -use crate::render::rustgis_tiles::{s3, stac}; +use crate::render::rustgis_tiles::{arcgis_token, s3, stac}; use crate::s3_keys as keys; use crate::service_logins as web; use crate::{CmdResult, CommandSpec as C, Session}; @@ -17,6 +17,9 @@ mod browse; #[path = "connection_logins.rs"] mod logins; + +#[path = "connection_token.rs"] +mod token; #[cfg(not(target_arch = "wasm32"))] use logins::request_password; pub(crate) use logins::{clear_active, resolve_keys, resolve_project, restore_active}; @@ -29,7 +32,7 @@ pub fn commands() -> Vec { r#"{"kind"?: "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "group"?: text}"#, ), C::new("connection.new", "New Connection", "Home › Add › Data Sources", new).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( - r#"{"kind": "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "address"?: "as the kind's add command takes it, without its password or token (postgres:host=… dbname=… user=…, mysql://user@host/db, mssql://user@host/db, a service's or STAC API's https:// address, an S3 endpoint: AWS when absent)", "name"?: "unique (default from the address)", "group"?: text, "options"?: {"region"?: text, "buckets"?: "a,b", "credentials"?: "keys"|"profile"|"environment"|"none", "profile"?: text} (S3 only), "auth"?: "none"|"basic"|"header"|"query": how a web-service connection logs in (basic authentication, an API key in a header, or an API key in a query parameter; the password or key is given to connection.login, never kept here), "user"?: "the basic login's user name", "keyName"?: "the header (auth header, e.g. X-API-Key) or the query parameter (auth query, e.g. api_key) the API key goes in; a query parameter of that name is taken out of the address"} (the result's stripped is true when a password, token or key was left out of the address; strippedParameters names the key parameters left out, to give as keyName with auth query)"#, + r#"{"kind": "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "address"?: "as the kind's add command takes it, without its password or token (postgres:host=… dbname=… user=…, mysql://user@host/db, mssql://user@host/db, a service's or STAC API's https:// address, an S3 endpoint: AWS when absent)", "name"?: "unique (default from the address)", "group"?: text, "options"?: {"region"?: text, "buckets"?: "a,b", "credentials"?: "keys"|"profile"|"environment"|"none", "profile"?: text} (S3 only), "auth"?: "none"|"basic"|"header"|"query"|"arcgis": how a web-service connection logs in (basic authentication, an API key in a header, an API key in a query parameter, or an ArcGIS token sent as the request's token parameter, pasted or made by connection.generateToken; the password, key or token is given to connection.login, never kept here), "user"?: "the basic login's user name, or the user connection.generateToken makes an ArcGIS token for", "keyName"?: "the header (auth header, e.g. X-API-Key) or the query parameter (auth query, e.g. api_key) the API key goes in; a query parameter of that name is taken out of the address"} (the result's stripped is true when a password, token or key was left out of the address; strippedParameters names the key parameters left out, to give as keyName with auth query)"#, ), C::new("connection.edit", "Edit Connection", "Home › Add › Data Sources", edit).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": "a connection's id or name", "name"?: text, "group"?: text, "address"?: text, "options"?: "as connection.new (replaces them)", "auth"?: "as connection.new", "user"?: text, "keyName"?: text, "kind"?: "its kind (unchanged)"} (another origin, login method, user or key name ends the web connection's login and deletes its saved one; for a connection this session didn't remember, a store that refuses the cleanup only warns that a saved login may remain)"#, @@ -37,7 +40,16 @@ pub fn commands() -> Vec { C::new("connection.duplicate", "Duplicate Connection", "Home › Add › Data Sources", duplicate).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": "a connection's id or name", "name"?: text}"#), C::new("connection.remove", "Remove Connection", "Home › Add › Data Sources", remove).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params(r#"{"id": "a connection's id or name"} (deletes its saved password, keys or login, and ends its web-service session login; for a web-service connection this session didn't remember, a store that refuses the cleanup only warns that a saved login may remain)"#), C::new("connection.login", "Log In to Connection", "Home › Add › Data Sources", login_command).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( - r#"{"id": "a connection's id or name; blank credentials explicitly use this ID's session or saved credentials", "password"?: "a database password, or a web service's basic-authentication password; session-only by default", "apiKey"?: "a web service's API key (auth header or query); session-only by default", "allowInsecure"?: "boolean, default false: confirms sending a web service's login to its plain http:// address, unencrypted", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "temporary S3 credentials, never persisted", "remember"?: "boolean, default false: opt into this computer's credential store for a newly typed database password, web-service password or key, or complete S3 key pair (saved keys mode only). No credentials are stored in connections or projects; a failed save preserves the session login"} (a web service's login is sent only with this session's requests bound to that connection and only to its address's origin: the same scheme, host and port; an API key in the query replaces a parameter of the same name in the address; logging in to one of several connections chooses it for unassigned requests to that address)"#, + r#"{"id": "a connection's id or name; blank credentials explicitly use this ID's session or saved credentials", "password"?: "a database password, or a web service's basic-authentication password; session-only by default", "apiKey"?: "a web service's API key (auth header or query); session-only by default", "token"?: "an ArcGIS-token connection's (auth arcgis) pasted token, replacing its login; session-only by default (connection.generateToken makes one from the connection's user and a password)", "expiresAt"?: "integer: the pasted token's expiry, Unix milliseconds in the future; none means it isn't known, so it is never renewed here, and the server's refusal ends its use", "allowInsecure"?: "boolean, default false: confirms sending a web service's login to its plain http:// address, unencrypted", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "temporary S3 credentials, never persisted", "remember"?: "boolean, default false: opt into this computer's credential store for a newly typed database password, web-service password, key or ArcGIS token (with its expiry), or complete S3 key pair (saved keys mode only). No credentials are stored in connections or projects; a failed save preserves the session login; an expired saved token is not used and stays until forgotten or replaced"} (a web service's login is sent only with this session's requests bound to that connection and only to its address's origin: the same scheme, host and port; an API key in the query replaces a parameter of the same name in the address, and an ArcGIS token replaces the address's own token parameter; logging in to one of several connections chooses it for unassigned requests to that address)"#, + ), + C::new("connection.generateToken", "Generate ArcGIS Token", "Home › Add › Data Sources", token::generate).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Network)).view().params( + r#"{"id": "a saved ArcGIS-token connection's id or name; its user is who the token is made for", "tokenEndpoint": "the https:// ArcGIS Server token address at the connection's own origin (https://host/arcgis/tokens/generateToken): no user information, query or fragment, and never taken from a service's answer", "password": "the user's password, sent in this one request's form and never kept", "remember"?: "boolean, default false: keep the resulting token and its expiry (never the password) on this computer"} → {id, pending: true}: the request runs in the background (30 seconds at most); poll connection.tokenStatus, stop it with connection.cancelToken. A token is never renewed automatically: generate another once it has expired"#, + ), + C::new("connection.tokenStatus", "ArcGIS Token Status", "Home › Add › Data Sources", token::status).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( + r#"{"id": "the connection's id or name"} → {id, pending: true} while its token is being generated; once done, the token is its login and the result is connection.login's, with pending: false; with nothing under way, {id, pending: false, loggedIn}. A failure says why in a fixed sentence and ends the operation, leaving any earlier login as it was. Only the caller that started the generation can ask"#, + ), + C::new("connection.cancelToken", "Cancel ArcGIS Token", "Home › Add › Data Sources", token::cancel).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( + r#"{"id": "the connection's id or name"} → {id, cancelled}: stops its token being generated (nothing is kept from the answer); an earlier login and saved login stay. Only the caller that started the generation can cancel it"#, ), C::new("connection.logout", "Log Out of Connection", "Home › Add › Data Sources", logout).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)).view().params( r#"{"id": "a web-service connection's id or name"} (clears this session's login and its password or key, which is no longer sent; a login saved on this computer stays, and is reused only after an explicit login)"#, @@ -881,12 +893,19 @@ fn login_command(s: &mut Session, p: &Value) -> CmdResult { if web::is_service(&c) { let remember = remember_flag(p)?; insecure_flag(p)?; - let typed = ["password", "apiKey"].iter().any(|key| ps(p, key).is_some_and(|v| !v.is_empty())); - if !remember && !typed { + let typed = ["password", "apiKey", "token"].iter().any(|key| ps(p, key).is_some_and(|v| !v.is_empty())); + if c.auth != "arcgis" && !remember && !typed { web::retry(s, &c.id); } } let result = login(s, &c, p)?; + finished_login(s, c, answers, result) +} + +/// What a login that worked answers: the connection as `shown` gives it, the sources it reopened +/// and what it couldn't keep. The tail of `connection.login`, which a generated token's landing +/// (`connection.tokenStatus`) shares. +fn finished_login(s: &mut Session, c: Connection, answers: bool, result: Reopened) -> CmdResult { if web::is_service(&c) { if answers { web::adopt(s, &c); @@ -983,6 +1002,12 @@ fn service_login(s: &mut Session, c: &Connection, p: &Value, remember: bool) -> if c.id.is_empty() { return Err(web::UNSAVED.into()); } + if c.auth == "arcgis" { + return arcgis_login(s, c, p, remember, insecure); + } + if ["token", "expiresAt"].iter().any(|key| p.get(*key).is_some_and(|v| !v.is_null())) { + return Err("token and expiresAt are for a connection that logs in with an ArcGIS token: set auth to arcgis first".into()); + } let password = ps(p, "password").filter(|t| !t.is_empty()); let key = ps(p, "apiKey").filter(|t| !t.is_empty()); let typed = match (c.auth.as_str(), password, key) { @@ -993,7 +1018,9 @@ fn service_login(s: &mut Session, c: &Connection, p: &Value, remember: bool) -> web::activate(s, c, None, false)?; return Ok(Reopened::default()); } - ("", ..) => return Err("this connection has no login method: set auth (basic, header or query) with connection.edit first".into()), + ("", ..) => { + return Err("this connection has no login method: set auth (basic, header, query or arcgis) with connection.edit first".into()); + } ("basic", typed, None) => typed, ("basic", _, Some(_)) => return Err("a basic login takes a password, not an apiKey".into()), (_, None, typed) => typed, @@ -1031,6 +1058,81 @@ fn service_login(s: &mut Session, c: &Connection, p: &Value, remember: bool) -> Ok(done) } +/// Logs in to a saved ArcGIS-token connection with the token typed (and its expiry, when known), +/// or else the one given this session or saved on this computer. A password isn't a token: +/// `connection.generateToken` makes one from it. An expiry that isn't known stays unknown: the +/// token is used until the server refuses it, never renewed or given a guessed lifetime. +fn arcgis_login(s: &mut Session, c: &Connection, p: &Value, remember: bool, insecure: bool) -> Result { + if ["password", "apiKey"].iter().any(|key| p.get(*key).is_some_and(|v| !v.is_null() && v.as_str() != Some(""))) { + return Err( + "an ArcGIS token login takes a token, not a password or apiKey (connection.generateToken makes a token from a password)".into(), + ); + } + let token = match p.get("token") { + None | Some(Value::Null) => None, + Some(Value::String(t)) => Some(t.trim()).filter(|t| !t.is_empty()), + Some(_) => return Err("token is text".into()), + }; + let expires_at = match p.get("expiresAt") { + None | Some(Value::Null) => None, + Some(Value::Number(n)) => Some(n.as_i64().filter(|at| *at > 0).ok_or(arcgis_token::BAD_EXPIRY)?), + Some(_) => return Err("expiresAt is a whole number of Unix milliseconds".into()), + }; + if expires_at.is_some() && token.is_none() { + return Err("expiresAt goes with a token: give the token it is the expiry of".into()); + } + let origin = crate::connections::login_origin(c).ok_or(web::NO_ORIGIN)?; + let Some(token) = token else { + if remember { + return Err("remember keeps the token typed in this login: give it".into()); + } + // Only a fully validated request to reuse this login may clear a deliberate Logout. + web::retry(s, &c.id); + // Already logged in: choose it again, keeping its binding and its cached tiles. + if web::logged_in(s, c) { + web::reselect(s, c, &origin); + return Ok(Reopened::default()); + } + // Given this session or saved on this computer: confirmed for this origin when typed. An + // expired one is refused (known_secret says so), never swapped for another. + let secret = web::known_secret(s, c)?; + web::activate(s, c, Some(&secret), true)?; + return Ok(Reopened::default()); + }; + if origin.starts_with("http://") && !insecure { + return Err(web::INSECURE.into()); + } + let credential = arcgis_token::Credential::new(token, expires_at)?; + let done = activate_token(s, c, &credential, insecure, remember)?; + // A token given on purpose replaces a generation still under way for this connection. + crate::service_tokens::cancel_for(s, &c.id); + Ok(done) +} + +/// Makes `credential` the login of the saved connection `c`, and keeps it on this computer when +/// asked. Its private payload is what a session or a saved login holds: the token and its expiry, +/// never a password. A failed save leaves the login as it is and says so. +fn activate_token( + s: &mut Session, + c: &Connection, + credential: &arcgis_token::Credential, + insecure: bool, + remember: bool, +) -> Result { + let payload = credential.payload(); + web::check_login_secret(c, payload.expose())?; + web::activate(s, c, Some(&payload), insecure)?; + let mut done = Reopened::default(); + if remember { + if web::save(s.database_logins.secret_store(), c, payload.expose()) { + s.connections.set_remembered(&c.id, true); + } else { + done.warning = Some(web::NOT_REMEMBERED); + } + } + Ok(done) +} + fn tested(s: &Session, p: &Value) -> Result { if ps(p, "id").is_some() { let c = found(s, p)?; @@ -1127,8 +1229,9 @@ fn test_connection(s: &mut Session, p: &Value, c: Connection) -> CmdResult { if DATABASE_KINDS.contains(&c.kind.as_str()) && b(p, "probe").unwrap_or(false) { return Ok(json!({"connection":c.id, "fetch":null})); } - let secrets = - ["password", "apiKey", "accessKeyId", "secretAccessKey", "sessionToken"].iter().any(|k| ps(p, k).is_some_and(|v| !v.is_empty())); + let secrets = ["password", "apiKey", "token", "accessKeyId", "secretAccessKey", "sessionToken"] + .iter() + .any(|k| ps(p, k).is_some_and(|v| !v.is_empty())); if !DATABASE_KINDS.contains(&c.kind.as_str()) && secrets { let mut input = p.clone(); if let Some(o) = input.as_object_mut() { diff --git a/crates/engine/src/connections.rs b/crates/engine/src/connections.rs index df94623501..a2a6de1676 100644 --- a/crates/engine/src/connections.rs +++ b/crates/engine/src/connections.rs @@ -51,8 +51,9 @@ pub const DATABASE_KINDS: &[&str] = &["postgres", "mysql", "sqlserver"]; /// The web-service kinds, which can log in with a [`SERVICE_AUTH`] method. pub const SERVICE_KINDS: &[&str] = &["service", "wms", "wmts", "wfs", "wcs", "ogcapi", "arcgis", "csw", "stac"]; /// How a web-service connection logs in: not at all, HTTP basic authentication (a user name and a -/// password), an API key in a header, or an API key in a query parameter. -pub const SERVICE_AUTH: &[&str] = &["none", "basic", "header", "query"]; +/// password), an API key in a header, an API key in a query parameter, or an ArcGIS token (pasted, +/// or generated from a user name and password, and sent as the request's `token` parameter). +pub const SERVICE_AUTH: &[&str] = &["none", "basic", "header", "query", "arcgis"]; /// The kind of a [`LoginRequest`] for a web service's origin (its `key`). pub const WEB: &str = "web"; pub const S3_CREDENTIALS: &[&str] = &["keys", "profile", "environment", "none"]; @@ -301,6 +302,15 @@ fn clean_login(c: &mut Connection) -> Result { } Ok(false) } + "arcgis" => { + // The user names who a token is generated for; it is optional (a pasted token has no + // use for one). The token endpoint is given per generation, never kept here. + c.key_name.clear(); + if c.user.chars().count() > MAX_NAME || c.user.chars().any(char::is_control) { + return Err(format!("an ArcGIS token login's user must be at most {MAX_NAME} characters, without control characters")); + } + Ok(false) + } "header" => { c.user.clear(); if !logins::valid_header_name(&c.key_name) { @@ -1213,6 +1223,11 @@ impl DatabaseLogins { self.session.insert(id.to_string(), Secret::new(password)); } + /// Drops the password or token given this session for `id`, and nothing else of it. + pub(crate) fn clear_session_password(&mut self, id: &str) { + self.session.remove(id); + } + /// Drops a refused session credential and suppresses an equivalent saved value for this run. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn reject_session_password(&mut self, c: &Connection) { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 62b2e8c6ee..455166c106 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -39,6 +39,7 @@ pub mod sample; #[cfg(feature = "schema")] pub mod schema; pub mod service_logins; +pub mod service_tokens; pub mod sources; #[cfg(test)] mod tests; diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index 52538155df..85ea36a6e9 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -1008,7 +1008,7 @@ impl Session { opened: self.opened, map: self.map().instance_id(), epoch: self.policy.epoch.clone(), - accounts: if reads { self.policy_accounts() } else { Value::Null }, + accounts: if reads { self.continuation_accounts(&node) } else { Value::Null }, sources, time_sources: node_time_sources(&node), tables, @@ -1075,6 +1075,17 @@ impl Session { let node = self.policy.nodes.last().cloned().ok_or("late work needs an authorized initiating operation")?; self.continuation_for(node) } + /// Retains the admitted token-generation authority without its password. Canonicalizing the + /// already-resolved connection selector keeps name-based calls bound to the same login identity. + pub(crate) fn token_continuation(&self, id: &str) -> Result { + let node = self.policy.nodes.last().ok_or("late work needs an authorized initiating operation")?; + let mut clean = (**node).clone(); + if let Some(params) = clean.params.as_object_mut() { + params.remove("password"); + params.insert("id".into(), json!(id)); + } + self.continuation_for(Arc::new(clean)) + } /// Checks generation, map, revocation, current load and private source/account identity. pub(crate) fn check_continuation(&self, c: &Continuation) -> Result<(), String> { self.check_continuation_origin(c)?; @@ -1108,7 +1119,7 @@ impl Session { if c.opened != self.opened { return Err("this run belongs to a replaced project".into()); } - if !c.accounts.is_null() && c.accounts != self.policy_accounts() { + if !c.accounts.is_null() && c.accounts != self.continuation_accounts(&c.node) { return Err("the live source or account changed since this request started".into()); } Ok(()) @@ -1194,6 +1205,15 @@ impl Session { } Ok(()) } + /// Exactly the caller that started the late work, root and scoped alike: another caller (even + /// the trusted native root, which `check_delivery_owner` lets deliver) is refused before it + /// can read, consume or cancel the owner's result. + pub(crate) fn same_owner(&self, c: &Continuation) -> Result<(), String> { + if !self.scope().same_caller(&c.scope) { + return Err("this run belongs to another caller".into()); + } + Ok(()) + } /// Native workers may deliver at root; an existing ingress must own the stored continuation. pub(crate) fn check_delivery(&self, c: &Continuation) -> Result<(), String> { self.check_delivery_owner(c)?; diff --git a/crates/engine/src/policy/plan.rs b/crates/engine/src/policy/plan.rs index 748f220561..651932d473 100644 --- a/crates/engine/src/policy/plan.rs +++ b/crates/engine/src/policy/plan.rs @@ -1439,6 +1439,34 @@ impl Session { "web":{"selected":self.database_logins.web_selections(),"active":self.web_logins.active_ids()} }) } + /// What a continuation compares of the accounts. A token generation belongs to a login + /// identity, not to its display name or service path, and it holds identity only: no + /// connection's session secret (a live token included) stays in it, since `poll` checks the + /// target's login identity itself. Everything else, including which logins are active, keeps + /// the full check, so logging in or out of another connection while one is pending ends it. + pub(super) fn continuation_accounts(&self, node: &Node) -> Value { + let mut accounts = self.policy_accounts(); + if node.id() != "connection.generateToken" { + return accounts; + } + let id = node.params.get("id").and_then(Value::as_str); + if let Some(rows) = accounts.get_mut("connections").and_then(Value::as_array_mut) { + for row in rows { + let Some(row) = row.as_object_mut() else { continue }; + row.insert("password".into(), Value::Null); + let target = row.get("definition").and_then(|d| d.get("id")).and_then(Value::as_str) == id; + let keeps_origin = id + .and_then(|id| self.connections.find(id)) + .filter(|c| c.auth == "arcgis" && crate::connections::SERVICE_KINDS.contains(&c.kind.as_str())); + if let (true, Some(c), Some(definition)) = (target, keeps_origin, row.get_mut("definition").and_then(Value::as_object_mut)) + { + definition.insert("name".into(), Value::Null); + definition.insert("address".into(), json!(crate::connections::login_origin(c))); + } + } + } + accounts + } } /// What a batch's tool reaches beyond a built-in's, without its per-item parameters. diff --git a/crates/engine/src/policy/scope.rs b/crates/engine/src/policy/scope.rs index d2b6ab2b0c..697f0a3608 100644 --- a/crates/engine/src/policy/scope.rs +++ b/crates/engine/src/policy/scope.rs @@ -209,6 +209,19 @@ impl Continuation { pub(crate) fn native_tool_owner(&self) -> bool { matches!(self.scope.kind, Kind::User) } + /// Whether `text` appears anywhere this continuation retains of its invocation (tests only). + #[cfg(test)] + pub(crate) fn mentions(&self, text: &str) -> bool { + let node = &self.node; + [ + node.params.to_string(), + serde_json::Value::Array(node.places.clone()).to_string(), + serde_json::to_string(&node.display).unwrap_or_default(), + self.accounts.to_string(), + ] + .iter() + .any(|held| held.contains(text)) + } /// Distinguishes two invocations of the same command without exposing their raw nodes. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn same_run(&self, other: &Self) -> bool { diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index 209d442469..d7d9408503 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -1,5 +1,6 @@ //! Logins of saved web-service connections (WMS, WMTS, WFS, WCS, OGC API, ArcGIS REST, CSW, -//! STAC): HTTP basic authentication, an API key in a header, or an API key in a query parameter. +//! STAC): HTTP basic authentication, an API key in a header, an API key in a query parameter, or an +//! ArcGIS token (pasted, or generated from a user name and password: see [`crate::service_tokens`]). //! //! The connection keeps the method, user name and header or parameter name, never the password //! or key. A secret given to `connection.login` lasts for this session, held by connection id in @@ -26,7 +27,7 @@ use std::sync::Arc; use crate::Session; use crate::connections::{Connection, LoginReason, LoginRequest, SERVICE_KINDS, WEB, account, login_origin}; -use crate::render::rustgis_tiles::{cache, logins}; +use crate::render::rustgis_tiles::{arcgis_token, cache, logins}; /// A transient secret whose connection identity changed. pub const UNUSABLE: &str = "The session login could not be used. Enter it again."; @@ -47,6 +48,8 @@ pub const OTHERS_LEFT: &str = "The login saved under this connection's id belongs to another connection or a newer RustGIS, so it was left unchanged."; /// Automatic reads respect a deliberate logout until an explicit login. pub const LOGGED_OUT: &str = "This connection is logged out. Log in explicitly to use it again."; +/// An ArcGIS token past its expiry: a new one is generated or pasted. +pub const TOKEN_EXPIRED: &str = arcgis_token::EXPIRED; /// A login typed for a plain http:// address without the confirmation. pub const INSECURE: &str = "This connection's address uses plain http://, so its login would be sent unencrypted. Confirm with allowInsecure: true to send it anyway."; /// A password or key that can't be sent. @@ -54,7 +57,7 @@ pub const BAD_SECRET: &str = "The password or key must be at most 4096 bytes, wi /// An API key a header can't carry the same way everywhere (the browser sends Latin-1 only). pub const HEADER_ASCII: &str = "An API key sent in a header must use plain ASCII characters."; /// A login asked of a connection with nothing to log in with this session. -pub const REQUIRED: &str = "This connection's login is required: give its password (basic) or apiKey (header, query)."; +pub const REQUIRED: &str = "This connection's login is required: give its password (basic), apiKey (header, query) or token (arcgis)."; /// A login for a definition that isn't saved. pub const UNSAVED: &str = "Save the connection first: a login belongs to a saved connection."; /// An address a login can't go to. @@ -267,6 +270,10 @@ fn method(c: &Connection, secret: &str) -> Option<(logins::Method, String)> { "basic" => Some((logins::Method::Header("Authorization".into()), logins::basic(&c.user, secret))), "header" => Some((logins::Method::Header(c.key_name.clone()), secret.to_string())), "query" => Some((logins::Method::Query(c.key_name.clone()), secret.to_string())), + "arcgis" => { + let credential = arcgis_token::Credential::parse(secret).ok()?; + Some((logins::Method::Arcgis { expires_at: credential.expires_at }, credential.token.expose().to_string())) + } _ => None, } } @@ -278,6 +285,10 @@ pub fn check_secret(secret: &str) -> Result<(), &'static str> { /// Checks the complete wire credential before a form edit can invalidate an earlier login. pub fn check_login_secret(c: &Connection, secret: &str) -> Result<(), String> { + // The private payload of a token (its JSON wrapper) isn't a password or key: it has its own checks. + if c.auth == "arcgis" { + return arcgis_token::Credential::parse(secret).map(drop).map_err(str::to_string); + } check_secret(secret).map_err(str::to_string)?; if c.auth == "basic" && !logins::valid_basic(&c.user, secret) { return Err("The encoded Basic login exceeds the supported header size or contains invalid characters.".into()); @@ -375,6 +386,8 @@ pub struct SessionLogins { failed: BTreeMap, /// Connections whose saved-login failure resolution has said already. said: BTreeSet, + /// Token generations under way, by connection id (see [`crate::service_tokens`]). + pub(crate) pending: BTreeMap, } impl SessionLogins { @@ -387,6 +400,7 @@ impl SessionLogins { logged_out: BTreeSet::new(), failed: BTreeMap::new(), said: BTreeSet::new(), + pending: BTreeMap::new(), } } @@ -437,6 +451,9 @@ impl Drop for SessionLogins { for active in self.active.values() { ended |= logins::deactivate(&active.binding); } + for op in self.pending.values() { + op.cancel(); + } if ended { clear_cache(); } @@ -467,8 +484,9 @@ fn clear_cache() { /// Returns the number of addresses that could not be rebound; resolution reports it once. pub(crate) fn activate(s: &mut Session, c: &Connection, secret: Option<&Secret>, insecure: bool) -> Result { let origin = login_origin(c).ok_or(NO_ORIGIN)?; - let next = match secret.and_then(|secret| method(c, secret.expose())) { - Some((method, value)) => { + let next = match secret { + Some(secret) => { + let (method, value) = method(c, secret.expose()).ok_or(UNUSABLE)?; let binding = logins::activate(&origin, &s.web_logins.owner(&c.id), method, &value, insecure).map_err(str::to_string)?; Some(ActiveLogin { origin: origin.clone(), binding, auth: c.auth.clone(), user: c.user.clone(), key_name: c.key_name.clone() }) } @@ -538,6 +556,7 @@ pub(crate) fn forget_failure(s: &mut Session, id: &str) { /// removed). The login saved on this computer is the caller's to delete. pub(crate) fn forget(s: &mut Session, c: &Connection) { deactivate(s, c, None); + crate::service_tokens::cancel_for(s, &c.id); retry(s, &c.id); s.database_logins.forget_connection(&c.id); } @@ -572,6 +591,16 @@ pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> Result return Err(format!("plugins can't set a legend at the address of a saved web-service connection ('{}')", c.name)); } if !logged_in(s, c) { + // An expired token is refused, never read anonymously (or with a portal sign-in's token). + if c.auth == "arcgis" + && let Some(active) = s.web_logins.active.get(&c.id).filter(|a| a.matches(c)) + { + logins::bind(&active.binding, url).map_err(str::to_string)?; + } + // The expiry stands until a new login replaces the token: nothing falls back to anonymous. + if c.auth == "arcgis" && s.web_logins.failed.get(&c.id).copied() == Some(TOKEN_EXPIRED) { + return Err(TOKEN_EXPIRED.into()); + } return Ok(url.to_string()); } let Some(active) = s.web_logins.active.get(&c.id) else { return Ok(url.to_string()) }; @@ -583,18 +612,43 @@ pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> Result }) } +/// Whether `secret` is an ArcGIS token of `c` that is past its expiry (or expires on a clock that +/// can't be read). What can't be read as a token isn't judged here: activating it refuses it. +fn lapsed(c: &Connection, secret: &Secret) -> bool { + c.auth == "arcgis" && arcgis_token::Credential::parse(secret.expose()).is_ok_and(|token| token.is_expired(arcgis_token::now_millis())) +} + +/// Ends this session's use of an expired ArcGIS token of `c`: its login generation is revoked +/// (cached authenticated responses go with it), its session credential is cleared, and the +/// expiry stands in for the saved login's automatic reads until an explicit login, so the store +/// isn't asked for the same expired payload again. The saved login itself is left as it is. +/// The connection stays chosen for its origin, so asking for a new login finds it. +fn expire(s: &mut Session, c: &Connection) -> String { + if let Some(old) = s.web_logins.active.remove(&c.id) { + logins::deactivate(&old.binding); + clear_cache(); + } + s.database_logins.clear_session_password(&c.id); + s.web_logins.failed.insert(c.id.clone(), TOKEN_EXPIRED); + TOKEN_EXPIRED.into() +} + /// The secret `c` logs in with without one typed: the one given this session, else the one /// saved on this computer (read once: a failure is kept, and said, until an explicit login). -/// Nothing is read for a connection logged out this session. +/// Nothing is read for a connection logged out this session. An expired ArcGIS token is neither +/// used nor replaced by another: it needs a new one. pub fn known_secret(s: &mut Session, c: &Connection) -> Result { if s.web_logins.logged_out.contains(&c.id) { return Err(LOGGED_OUT.into()); } - if let Some(secret) = s.database_logins.session_password(&c.id) { + if let Some(secret) = s.database_logins.session_password(&c.id).cloned() { if !s.web_logins.active.get(&c.id).is_some_and(|a| a.matches(c)) { return Err(UNUSABLE.into()); } - return Ok(secret.clone()); + if lapsed(c, &secret) { + return Err(expire(s, c)); + } + return Ok(secret); } if let Some(error) = s.web_logins.failed.get(&c.id) { return Err((*error).into()); @@ -603,6 +657,7 @@ pub fn known_secret(s: &mut Session, c: &Connection) -> Result { return Err(REQUIRED.into()); } let error = match load(s.database_logins.secret_store(), c) { + Loading::Found(secret) if lapsed(c, &secret) => TOKEN_EXPIRED, Loading::Found(secret) => return Ok(secret), Loading::Missing => { s.connections.set_remembered(&c.id, false); @@ -1035,7 +1090,7 @@ pub(crate) fn resolve(s: &mut Session, only: Option<&BTreeSet>) { .any(|r| r.kind == WEB && r.key == origin && r.connections == [c.id.clone()] && r.reason == LoginReason::Password); // A saved login that can't be read is said once per session (the failure is kept // until an explicit login); a missing login or a deliberate logout only asks. - let saved_failure = [NONE_SAVED, UNAVAILABLE, SAVED_UNUSABLE].contains(&e.as_str()); + let saved_failure = [NONE_SAVED, UNAVAILABLE, SAVED_UNUSABLE, TOKEN_EXPIRED].contains(&e.as_str()); let say = if saved_failure { s.web_logins.said.insert(c.id.clone()) } else { e != REQUIRED && e != LOGGED_OUT && !already_asked }; if say { diff --git a/crates/engine/src/service_tokens.rs b/crates/engine/src/service_tokens.rs new file mode 100644 index 0000000000..75ef902264 --- /dev/null +++ b/crates/engine/src/service_tokens.rs @@ -0,0 +1,155 @@ +//! Token generation for saved ArcGIS connections: the pending operations of a session. +//! +//! `connection.generateToken` sends the connection's user name and a password to the ArcGIS +//! Server token endpoint of the connection's own origin (see +//! [`rustgis_tiles::arcgis_token`](crate::render::rustgis_tiles::arcgis_token)) off the caller's +//! thread, and `connection.tokenStatus` lands the answer: nothing is activated or saved from the +//! network callback. Each operation owns +//! +//! - an immutable snapshot of the connection definition it was started for, +//! - the continuation of the invocation that started it: the scope, account and project it was +//! admitted under, and its admitted node *without its `password`* (see +//! [`Session::token_continuation`]). Exactly that caller, root or scoped, can poll or cancel +//! it; any other is refused before it can touch the result, +//! - a private result slot only its own callback fills, holding a validated token or a fixed +//! sentence (never the password, never server text), and +//! - a cancellation flag. +//! +//! The password lives in the one POST's form and nowhere else past the invocation that gave it: +//! nothing the operation retains holds it. Cancelling, a failure, a changed definition, a +//! replaced project or revoked authority discards the answer without touching the login the +//! connection had. + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; + +use crate::Session; +use crate::connections::{Connection, login_origin, same_login}; +use crate::policy::Continuation; +use crate::render::rustgis_tiles::{arcgis_token, cache, logins}; +use crate::service_logins as web; + +/// A generation already under way for this connection. +pub const ALREADY: &str = "A token is already being generated for this connection. Cancel it first."; +/// A connection with no user to generate a token for. +pub const NO_USER: &str = "This connection has no user: set user (connection.edit) to generate a token."; +/// A password that can't be sent. +pub const BAD_PASSWORD: &str = "The password must be at most 4096 bytes, without control characters."; +/// A generation whose connection changed while it was under way. +pub const CHANGED: &str = "The connection changed while its token was being generated. Generate it again."; + +type Slot = Arc>>>; + +/// One generation under way. Deliberately neither `Debug` nor `Clone`. +pub(crate) struct Pending { + connection: Connection, + remember: bool, + owner: Continuation, + slot: Slot, + cancelled: Arc, +} + +impl Pending { + /// Stops the request at its next look and drops whatever answer is or will be in its slot. + pub(crate) fn cancel(&self) { + self.cancelled.store(true, Ordering::SeqCst); + *self.slot.lock().unwrap_or_else(|e| e.into_inner()) = None; + } + + /// What the operation retains of its starting invocation (tests only). + #[cfg(test)] + pub(crate) fn owner(&self) -> &Continuation { + &self.owner + } +} + +/// What a landed generation hands the command that polled it. +pub(crate) struct Ready { + pub credential: arcgis_token::Credential, + pub remember: bool, + /// The invocation that started it, to land the token under. + pub owner: Continuation, +} + +#[expect(clippy::large_enum_variant, reason = "Keep the owned continuation inline instead of allocating again on completion.")] +pub(crate) enum Polled { + /// Nothing is under way for this connection. + Idle, + Pending, + Ready(Ready), +} + +/// Starts generating a token for the saved connection `c`. Nothing is sent when anything about +/// the request is wrong. The endpoint must be an https:// address at the connection's own origin. +pub(crate) fn start(s: &mut Session, c: &Connection, endpoint: &str, password: &str, remember: bool) -> Result<(), String> { + // A generation left from a replaced project (its connection ids are reused) is not this one's. + if s.web_logins.pending.get(&c.id).is_some_and(|op| s.check_continuation(&op.owner).is_err()) { + cancel_for(s, &c.id); + } + if s.web_logins.pending.contains_key(&c.id) { + return Err(ALREADY.into()); + } + let origin = login_origin(c).ok_or(web::NO_ORIGIN)?; + arcgis_token::check_endpoint(endpoint, &origin)?; + if c.user.trim().is_empty() { + return Err(NO_USER.into()); + } + if !logins::valid_value(password) { + return Err(BAD_PASSWORD.into()); + } + let owner = s.token_continuation(&c.id)?; + owner.check()?; + let slot: Slot = Slot::default(); + let cancelled = Arc::new(AtomicBool::new(false)); + let still: cache::Still = { + let (owner, flag) = (owner.clone(), cancelled.clone()); + Arc::new(move || !flag.load(Ordering::SeqCst) && owner.check().is_ok()) + }; + let done: arcgis_token::Done = { + let (fill, flag) = (slot.clone(), cancelled.clone()); + Box::new(move |result| { + if !flag.load(Ordering::SeqCst) { + *fill.lock().unwrap_or_else(|e| e.into_inner()) = Some(result); + } + }) + }; + s.web_logins.pending.insert(c.id.clone(), Pending { connection: c.clone(), remember, owner, slot, cancelled }); + arcgis_token::generate(endpoint.to_string(), arcgis_token::form(&c.user, password), still, done); + Ok(()) +} + +/// Looks at the generation under way for `id`. Another caller's is refused and left as it is; one +/// whose project was replaced or whose authority was revoked is dropped; a finished one is +/// consumed here, whether it gave a token or failed. A token comes out only for the connection +/// definition it was started for. +pub(crate) fn poll(s: &mut Session, current: &Connection) -> Result { + let Some(op) = s.web_logins.pending.get(¤t.id) else { return Ok(Polled::Idle) }; + s.same_owner(&op.owner)?; + let owner = op.owner.clone(); + if let Err(why) = s.check_continuation(&owner) { + cancel_for(s, ¤t.id); + return Err(why); + } + let landed = op.slot.lock().unwrap_or_else(|e| e.into_inner()).take(); + let Some(result) = landed else { return Ok(Polled::Pending) }; + let Some(op) = s.web_logins.pending.remove(¤t.id) else { return Ok(Polled::Idle) }; + let credential = result?; + if !same_login(&op.connection, current) { + return Err(CHANGED.into()); + } + Ok(Polled::Ready(Ready { credential, remember: op.remember, owner })) +} + +/// Cancels the generation of `id` for its owner: whether there was one. Another caller's is +/// refused and left as it is. +pub(crate) fn cancel(s: &mut Session, id: &str) -> Result { + let Some(op) = s.web_logins.pending.get(id) else { return Ok(false) }; + s.same_owner(&op.owner)?; + Ok(cancel_for(s, id)) +} + +/// Cancels the generation of `id` whoever started it: the connection is going away, changing its +/// login or being logged in again. The login it has stays as it is. +pub(crate) fn cancel_for(s: &mut Session, id: &str) -> bool { + s.web_logins.pending.remove(id).map(|op| op.cancel()).is_some() +} diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 2fc18ad1d6..4ec31af69e 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -3096,3 +3096,635 @@ fn a_remembered_query_login_reconnects_on_the_wire_without_leaking_into_history_ assert!(!public.contains(private), "{private} reached History, recording or saved text: {public}"); } } + +/// ArcGIS token connections: a pasted token, or one generated from a user name and password. +mod arcgis_tokens { + use super::*; + use crate::policy::{AgentAccess, Effects, Revocation, Scope}; + use crate::render::rustgis_tiles::arcgis_token::{self, Credential, Done}; + use crate::render::rustgis_tiles::cache::Still; + use rustgis_data::diagnostics::{Filter, History}; + use std::collections::BTreeMap; + use std::sync::{LazyLock, Mutex, Once}; + + const FUTURE: i64 = 4_102_444_800_000; // 2100-01-01 + + type Handler = Arc; + + static HANDLERS: LazyLock>> = LazyLock::new(Mutex::default); + + /// Answers the token requests to `endpoint` with `f` (the form the server got, the request's + /// predicate, and what to call). The fake transport is installed once for the process; each + /// test names an endpoint of its own, so none shares another's answers. + fn serve(endpoint: &str, f: impl Fn(String, Still, Done) + Send + Sync + 'static) { + static ONCE: Once = Once::new(); + ONCE.call_once(|| { + arcgis_token::set_generator(|endpoint, body, still, done| { + let handler = HANDLERS.lock().unwrap().get(&endpoint).cloned(); + match handler { + Some(handler) => handler(body.expose().to_string(), still, done), + None => done(Err("no fake answers this endpoint".into())), + } + }); + }); + HANDLERS.lock().unwrap().insert(endpoint.to_string(), Arc::new(f)); + } + + /// One request held by a fake server until the test lands its answer. + #[derive(Clone, Default)] + struct Held { + forms: Arc>>, + held: Arc>>, + } + + impl Held { + fn at(endpoint: &str) -> Held { + let held = Held::default(); + let state = held.clone(); + serve(endpoint, move |form, still, done| { + state.forms.lock().unwrap().push(form); + state.held.lock().unwrap().push((done, still)); + }); + held + } + + fn forms(&self) -> Vec { + self.forms.lock().unwrap().clone() + } + + /// The oldest request not yet answered: its predicate, and what answers it. + fn take(&self) -> (Done, Still) { + self.held.lock().unwrap().remove(0) + } + } + + fn token(text: &str) -> Credential { + Credential::new(text, Some(FUTURE)).ok().unwrap() + } + + fn arcgis(s: &mut Session, origin: &str) -> String { + new(s, json!({"kind":"arcgis","address":format!("{origin}/arcgis/rest/services"),"auth":"arcgis","user":"alice"})) + } + + fn visible(s: &mut Session, history: &History) -> String { + let (project, _) = s.project.saved_text(None).unwrap(); + let mut recording = Vec::new(); + s.log.write(&mut recording, false).unwrap(); + format!( + "{}\n{project}\n{}\n{:?}\n{}\n{:?}", + s.connections.to_json(), + String::from_utf8(recording).unwrap(), + history.records(), + history.report(Filter::All), + s.notices + ) + } + + struct Allow; + impl Revocation for Allow { + fn allows(&self, _: Effects) -> bool { + true + } + } + + #[test] + fn arcgis_token_definitions_keep_a_user_and_never_a_key_name() { + let mut s = session(None); + let id = new( + &mut s, + json!({"kind":"wms","address":"https://definition-arcgis757.example.test/wms","auth":" ArcGIS ","user":" alice ","keyName":"x"}), + ); + let c = s.connections.find(&id).unwrap(); + assert_eq!((c.auth.as_str(), c.user.as_str(), c.key_name.as_str()), ("arcgis", "alice", "")); + // The user is optional (a pasted token has no use for one), bounded and free of controls. + let bare = new(&mut s, json!({"kind":"arcgis","address":"https://definition-arcgis757.example.test/rest","auth":"arcgis"})); + assert_eq!(s.connections.find(&bare).unwrap().user, ""); + for bad in ["a\nb".to_string(), "a".repeat(201)] { + let error = s + .run( + "connection.new", + &json!({"kind":"arcgis","address":"https://definition-arcgis757.example.test/x","auth":"arcgis","user":bad}), + ) + .unwrap_err(); + assert!(error.contains("user"), "{error}"); + } + // Editing the user changes the login's identity: the saved token isn't for another user. + assert!(s.run("connection.edit", &json!({"id":bare,"user":"bob"})).is_ok()); + } + + #[test] + fn arcgis_token_pasted_login_sends_one_request_only_token_and_leaks_nowhere() { + let origin = "https://pasted-arcgis757.example.test"; + let mut s = session(None); + let id = arcgis(&mut s, origin); + let history = History::default(); + let _scope = history.enter(); + s.run("project.openText", &json!({"text":project(origin,&id)})).unwrap(); + let result = s.run("connection.login", &json!({"id":id,"token":" sentinel-token-757 "})).unwrap(); + assert_eq!(result["loggedIn"], true); + let address = bound(&mut s, &id, &format!("{origin}/arcgis/rest/services/A/MapServer?ToKeN=old&f=json")); + let applied = logins::apply(&address).unwrap().unwrap(); + assert!(applied.arcgis && applied.header.is_none()); + assert_eq!(applied.url, format!("{origin}/arcgis/rest/services/A/MapServer?f=json&token=sentinel-token-757")); + // The address keeps what it was given (its stale token is the user's, not the login's); the login's is not in it. + assert!(!address.contains("sentinel-token-757")); + // Layers of the project are rebound, request-only too; another origin stays anonymous. + let tile = s.map().tiles[0].url.clone(); + assert!(logins::apply(&tile).unwrap().unwrap().url.ends_with("?token=sentinel-token-757")); + for foreign in [ + "https://elsewhere757.example.test/x", + "https://pasted-arcgis757.example.test:8443/x", + "http://pasted-arcgis757.example.test/x", + ] { + assert!(logins::apply(&bound(&mut s, &id, foreign)).unwrap().is_none(), "{foreign}"); + } + let text = format!("{result}\n{}", visible(&mut s, &history)); + for private in ["sentinel-token-757", "rustgis-login"] { + assert!(!text.contains(private), "{private} reached a record: {text}"); + } + // Logging in again with another token replaces the generation; the old one is dead. + s.run("connection.login", &json!({"id":id,"token":"replacement-token-757"})).unwrap(); + assert!(logins::apply(&address).is_err()); + assert!(logins::apply(&bound(&mut s, &id, &format!("{origin}/a"))).unwrap().unwrap().url.ends_with("?token=replacement-token-757")); + s.run("connection.logout", &json!({"id":id})).unwrap(); + assert!(logins::apply(&tile).is_err()); + assert_eq!(s.run("connection.login", &json!({"id":id})).unwrap_err(), web::REQUIRED); + } + + #[test] + fn arcgis_token_login_refuses_what_is_not_a_token_before_changing_anything() { + let origin = "https://refusals-arcgis757.example.test"; + let mut s = session(None); + let id = arcgis(&mut s, origin); + s.run("connection.login", &json!({"id":id,"token":"earlier-token-757"})).unwrap(); + let earlier = bound(&mut s, &id, &format!("{origin}/a")); + for (params, expected) in [ + (json!({"id":id,"token":5}), "token is text"), + (json!({"id":id,"token":"has space"}), arcgis_token::BAD_TOKEN), + (json!({"id":id,"token":"é"}), arcgis_token::BAD_TOKEN), + (json!({"id":id,"token":"refusal-token-757","expiresAt":"5"}), "expiresAt is a whole number of Unix milliseconds"), + (json!({"id":id,"token":"refusal-token-757","expiresAt":1.5}), arcgis_token::BAD_EXPIRY), + (json!({"id":id,"token":"refusal-token-757","expiresAt":-1}), arcgis_token::BAD_EXPIRY), + (json!({"id":id,"token":"refusal-token-757","expiresAt":0}), arcgis_token::BAD_EXPIRY), + (json!({"id":id,"token":"refusal-token-757","expiresAt":u64::MAX}), arcgis_token::BAD_EXPIRY), + // Already over: no token is made of it. + (json!({"id":id,"token":"refusal-token-757","expiresAt":1}), arcgis_token::EXPIRED), + (json!({"id":id,"expiresAt":FUTURE}), "expiresAt goes with a token: give the token it is the expiry of"), + ( + json!({"id":id,"password":"password-757"}), + "an ArcGIS token login takes a token, not a password or apiKey (connection.generateToken makes a token from a password)", + ), + ( + json!({"id":id,"apiKey":"api-key-757"}), + "an ArcGIS token login takes a token, not a password or apiKey (connection.generateToken makes a token from a password)", + ), + (json!({"id":id,"token":"refusal-token-757","remember":true}), web::NOT_REMEMBERED), + ] { + let outcome = s.run("connection.login", ¶ms); + // No store: remembering is a warning on a login that worked, not a refusal. + if expected == web::NOT_REMEMBERED { + assert_eq!(outcome.unwrap()["warning"], expected); + s.run("connection.login", &json!({"id":id,"token":"earlier-token-757"})).unwrap(); + continue; + } + assert_eq!(outcome.unwrap_err(), expected, "{params}"); + assert!(logins::apply(&earlier).unwrap().unwrap().url.ends_with("?token=earlier-token-757"), "{params} replaced the login"); + } + // Another method's connection takes no token. + let basic = new(&mut s, json!({"kind":"wmts","address":format!("{origin}/wmts"),"auth":"basic","user":"ana"})); + let error = s.run("connection.login", &json!({"id":basic,"token":"refusal-token-757"})).unwrap_err(); + assert!(error.contains("set auth to arcgis"), "{error}"); + // Plain http:// is sent only on explicit confirmation. + let http = new(&mut s, json!({"kind":"arcgis","address":"http://refusals-arcgis757.example.test/rest","auth":"arcgis"})); + assert_eq!(s.run("connection.login", &json!({"id":http,"token":"http-token-757"})).unwrap_err(), web::INSECURE); + assert!(s.run("connection.login", &json!({"id":http,"token":"http-token-757","allowInsecure":true})).is_ok()); + } + + #[test] + fn arcgis_token_rejected_login_preserves_sticky_logout() { + let origin = "https://sticky-refusal-arcgis757.example.test"; + let store = Arc::new(CountingStore::default()); + let mut s = session(Some(store.clone())); + let id = arcgis(&mut s, origin); + s.run("connection.login", &json!({"id":id,"token":"remembered-sticky-token-757","remember":true})).unwrap(); + s.run("connection.logout", &json!({"id":id})).unwrap(); + let reads = store.reads.load(Ordering::SeqCst); + for params in [ + json!({"id":id,"token":5}), + json!({"id":id,"expiresAt":"invalid"}), + json!({"id":id,"expiresAt":FUTURE}), + json!({"id":id,"token":"has space"}), + json!({"id":id,"token":"expired-sticky-token-757","expiresAt":1}), + json!({"id":id,"password":5}), + json!({"id":id,"apiKey":{}}), + ] { + assert!(s.run("connection.login", ¶ms).is_err()); + web::resolve(&mut s, None); + assert!(web::request(&mut s, Some(&id), &format!("{origin}/a")).is_err(), "rejected input restored a saved login"); + assert_eq!(store.reads.load(Ordering::SeqCst), reads, "rejected input cleared sticky Logout"); + } + // A validated explicit reuse still ends Logout and restores the saved token. + s.run("connection.login", &json!({"id":id})).unwrap(); + let address = bound(&mut s, &id, &format!("{origin}/a")); + assert!(logins::apply(&address).unwrap().unwrap().url.ends_with("?token=remembered-sticky-token-757")); + } + + #[test] + fn arcgis_token_generation_retains_no_session_token_of_any_connection() { + let origin = "https://retention-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + let _server = Held::at(&endpoint); + let mut s = session(None); + let id = arcgis(&mut s, origin); + let other = new( + &mut s, + json!({"kind":"arcgis","address":"https://retention-other-arcgis757.example.test/rest","auth":"arcgis","user":"bob"}), + ); + s.run("connection.login", &json!({"id":id,"token":"target-session-token-757"})).unwrap(); + s.run("connection.login", &json!({"id":other,"token":"other-session-token-757"})).unwrap(); + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"retention-password-757"})).unwrap(); + let owner = s.web_logins.pending.get(&id).unwrap().owner(); + for private in ["retention-password-757", "target-session-token-757", "other-session-token-757"] { + assert!(!owner.mentions(private), "{private} is retained by the pending generation"); + } + // Logging out of the other connection still ends it: the login set is part of its authority. + s.run("connection.logout", &json!({"id":other})).unwrap(); + assert!(s.run("connection.tokenStatus", &json!({"id":id})).is_err()); + } + + #[test] + fn arcgis_token_generation_by_name_survives_rename_and_same_origin_path_edit() { + let origin = "https://named-generation-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + let server = Held::at(&endpoint); + let mut s = session(None); + let name = "Named ArcGIS token connection"; + let id = new( + &mut s, + json!({ + "kind":"arcgis", "name":name, "address":format!("{origin}/arcgis/rest/services"), "auth":"arcgis", "user":"alice" + }), + ); + s.run("connection.generateToken", &json!({"id":name,"tokenEndpoint":endpoint,"password":"named-password-757"})).unwrap(); + s.run("connection.edit", &json!({"id":id,"name":"Renamed token connection","address":format!("{origin}/another/rest/services")})) + .unwrap(); + let (done, still) = server.take(); + assert!(still()); + done(Ok(token("named-generated-token-757"))); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["loggedIn"], true); + let address = bound(&mut s, &id, &format!("{origin}/a")); + assert!(logins::apply(&address).unwrap().unwrap().url.ends_with("?token=named-generated-token-757")); + } + + #[test] + fn arcgis_token_session_login_expires_and_is_never_replaced_by_anonymous_access() { + let origin = "https://expiring-arcgis757.example.test"; + let mut s = session(None); + let id = arcgis(&mut s, origin); + s.run("project.openText", &json!({"text":project(origin,&id)})).unwrap(); + let soon = arcgis_token::now_millis().unwrap() + 150; + s.run("connection.login", &json!({"id":id,"token":"expiring-token-757","expiresAt":soon})).unwrap(); + let address = bound(&mut s, &id, &format!("{origin}/a")); + assert!(logins::apply(&address).is_ok()); + std::thread::sleep(std::time::Duration::from_millis(300)); // test-wait: a token's expiry on the real clock is what is under test + // Nothing is sent, read from the cache or bound with it; no request goes without it either. + assert_eq!(logins::apply(&address).err(), Some(arcgis_token::EXPIRED)); + assert_eq!(logins::check(&address), Err(arcgis_token::EXPIRED)); + assert_eq!(web::bind_logged_in(&s, &id, &format!("{origin}/legend")).unwrap_err(), arcgis_token::EXPIRED); + let listed = s.run("connection.list", &Value::Null).unwrap(); + assert_eq!(listed["connections"][0]["loggedIn"], false); + // Asking for it ends the generation and its session credential, and asks for a login again. + assert_eq!(web::request(&mut s, Some(&id), &format!("{origin}/a")).unwrap_err(), web::TOKEN_EXPIRED); + assert!(!s.database_logins.has_session_password(&id)); + assert!(logins::apply(&address).is_err()); + assert_eq!(web::request(&mut s, Some(&id), &format!("{origin}/a")).unwrap_err(), web::TOKEN_EXPIRED); + // Once ended, a legend at its origin is still refused, until a login replaces it. + assert_eq!(web::bind_logged_in(&s, &id, &format!("{origin}/legend")).unwrap_err(), web::TOKEN_EXPIRED); + web::resolve(&mut s, None); + assert_eq!(requests(&mut s).as_array().unwrap().len(), 1); + // Nothing is saved, so a login with nothing typed asks for a token: the expired one isn't offered again. + assert_eq!(s.run("connection.login", &json!({"id":id})).unwrap_err(), web::REQUIRED); + // A new token logs in again. + s.run("connection.login", &json!({"id":id,"token":"renewed-token-757","expiresAt":FUTURE})).unwrap(); + assert!(logins::apply(&bound(&mut s, &id, &format!("{origin}/a"))).unwrap().unwrap().url.ends_with("?token=renewed-token-757")); + assert!(web::bind_logged_in(&s, &id, &format!("{origin}/legend")).is_ok()); + } + + #[test] + fn arcgis_token_remembered_login_restores_unexpired_and_keeps_expired_untouched() { + let origin = "https://remembered-arcgis757.example.test"; + let store = Arc::new(CountingStore::default()); + let mut s = session(Some(store.clone())); + let id = arcgis(&mut s, origin); + let account = format!("connection/{id}"); + s.run("connection.login", &json!({"id":id,"token":"remembered-token-757","expiresAt":FUTURE,"remember":true})).unwrap(); + assert!(s.connections.find(&id).unwrap().remembered); + let saved: Value = serde_json::from_str(store.inner.load(&account).unwrap().unwrap().expose()).unwrap(); + // The existing envelope; its secret is the private payload. + assert_eq!( + (saved["version"].clone(), saved["auth"].clone(), saved["user"].clone(), saved["keyName"].clone()), + (json!(1), json!("arcgis"), json!("alice"), json!("")) + ); + assert_eq!(saved["origin"], origin); + assert_eq!( + serde_json::from_str::(saved["secret"].as_str().unwrap()).unwrap(), + json!({"version":1,"token":"remembered-token-757","expiresAt":FUTURE}) + ); + // A later session restores it through the opt-in path. + let prefs = serde_json::Map::from_iter([(crate::connections::PREF.to_string(), Value::String(s.connections.to_json()))]); + let mut later = session(None); + crate::prefs::use_saved_secrets(&mut later, &prefs, store.clone()); + let url = bound(&mut later, &id, &format!("{origin}/a")); + assert!(logins::apply(&url).unwrap().unwrap().url.ends_with("?token=remembered-token-757")); + drop(later); + + // A saved token that has expired is not used, nor deleted, nor rewritten, nor read twice. + let past = Credential::new("expired-token-757", Some(1)).ok().unwrap().payload(); + let c = s.connections.find(&id).unwrap().clone(); + assert!(web::save(s.database_logins.secret_store(), &c, past.expose())); + let before = store.inner.load(&account).unwrap(); + let mut expired = session(Some(store.clone())); + expired.load_connections(&s.connections.to_json()); + let reads = store.reads.load(Ordering::SeqCst); + expired.run("project.openText", &json!({"text":project(origin,&id)})).unwrap(); + assert_eq!(store.reads.load(Ordering::SeqCst), reads + 1); + assert_eq!(requests(&mut expired).as_array().unwrap().len(), 1, "the login is asked for"); + assert_eq!(format!("{:?}", expired.notices).matches(web::TOKEN_EXPIRED).count(), 1, "said once"); + for _ in 0..3 { + assert_eq!(web::request(&mut expired, Some(&id), &format!("{origin}/a")).unwrap_err(), web::TOKEN_EXPIRED); + } + assert_eq!(store.reads.load(Ordering::SeqCst), reads + 1, "the expired payload is not read again"); + assert_eq!(store.inner.load(&account).unwrap(), before); + assert_eq!(store.saves.load(Ordering::SeqCst), 2); + assert!(expired.connections.find(&id).unwrap().remembered); + // A user other than the one it was saved for, or another method, can't use it. + expired.run("connection.edit", &json!({"id":id,"user":"bob"})).unwrap(); + assert_eq!(store.inner.load(&account).unwrap(), None, "a changed login identity deletes its own saved token"); + } + + #[test] + fn arcgis_token_generation_lands_through_status_and_never_keeps_the_password() { + let origin = "https://generated-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + let store = Arc::new(MemorySecrets::new()); + let mut s = session(Some(store.clone())); + let id = arcgis(&mut s, origin); + let account = format!("connection/{id}"); + let server = Held::at(&endpoint); + let history = History::default(); + let _scope = history.enter(); + s.run("project.openText", &json!({"text":project(origin,&id)})).unwrap(); + + let started = s + .run( + "connection.generateToken", + &json!({"id":id,"tokenEndpoint":endpoint,"password":"p&= secret-password-757","remember":true}), + ) + .unwrap(); + assert_eq!(started, json!({"id":id,"pending":true})); + // Only the transient form carries the user and the encoded password. + assert_eq!(server.forms(), ["f=json&username=alice&password=p%26%3D%20secret-password-757&client=requestip&expiration=60"]); + // What the pending operation keeps of its invocation holds no password. + let pending = s.web_logins.pending.get(&id).unwrap(); + assert!(!pending.owner().mentions("secret-password-757")); + assert!(pending.owner().mentions(&id), "it keeps the authority it was admitted under"); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap(), json!({"id":id,"pending":true})); + // Nothing logged in or saved yet; a second generation is refused before anything is sent. + assert_eq!(s.run("connection.list", &Value::Null).unwrap()["connections"][0]["loggedIn"], false); + assert_eq!(store.load(&account).unwrap(), None); + let again = + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"other-password-757"})).unwrap_err(); + assert_eq!(again, crate::service_tokens::ALREADY); + assert_eq!(server.forms().len(), 1); + + let (done, still) = server.take(); + assert!(still()); + done(Ok(token("generated-token-757"))); + let landed = s.run("connection.tokenStatus", &json!({"id":id})).unwrap(); + assert_eq!( + (landed["pending"].clone(), landed["loggedIn"].clone(), landed["remembered"].clone()), + (json!(false), json!(true), json!(true)) + ); + assert!(landed.get("warning").is_none()); + let tile = s.map().tiles[0].url.clone(); + assert!(logins::apply(&tile).unwrap().unwrap().url.ends_with("?token=generated-token-757")); + // Consumed: asking again says nothing is under way. + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap(), json!({"id":id,"pending":false,"loggedIn":true})); + assert!(s.web_logins.pending.is_empty()); + // The saved login is the private payload in the existing envelope: the token, its expiry, + // never the password. + let kept = store.load(&account).unwrap().unwrap(); + let kept: Value = serde_json::from_str(kept.expose()).unwrap(); + assert_eq!( + serde_json::from_str::(kept["secret"].as_str().unwrap()).unwrap(), + json!({"version":1,"token":"generated-token-757","expiresAt":FUTURE}) + ); + let text = format!("{landed}\n{started}\n{kept}\n{}", visible(&mut s, &history)); + assert!(!text.contains("secret-password-757") && !text.contains("p%26%3D"), "the password reached a record: {text}"); + let text = format!("{landed}\n{started}\n{}", visible(&mut s, &history)); + for private in ["generated-token-757", "rustgis-login"] { + assert!(!text.contains(private), "{private} reached a record: {text}"); + } + // A later session restores the token (and only an unexpired one). + let prefs = serde_json::Map::from_iter([(crate::connections::PREF.to_string(), Value::String(s.connections.to_json()))]); + let mut later = session(None); + crate::prefs::use_saved_secrets(&mut later, &prefs, store.clone()); + assert!( + logins::apply(&bound(&mut later, &id, &format!("{origin}/a"))).unwrap().unwrap().url.ends_with("?token=generated-token-757") + ); + } + + #[test] + fn arcgis_token_generation_is_refused_before_anything_is_sent() { + let origin = "https://refused-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + let sent = Arc::new(AtomicUsize::new(0)); + let counted = sent.clone(); + serve(&endpoint, move |_, _, _| { + counted.fetch_add(1, Ordering::SeqCst); + }); + let mut s = session(None); + let id = arcgis(&mut s, origin); + let nameless = new(&mut s, json!({"kind":"arcgis","address":format!("{origin}/other"),"auth":"arcgis"})); + let basic = new(&mut s, json!({"kind":"wmts","address":format!("{origin}/wmts"),"auth":"basic","user":"ana"})); + let generate = |s: &mut Session, id: &str, endpoint: &str, password: Value| { + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":password})) + }; + for bad in [ + "http://refused-arcgis757.example.test/arcgis/tokens/generateToken", + "https://other-arcgis757.example.test/arcgis/tokens/generateToken", + "https://refused-arcgis757.example.test:8443/arcgis/tokens/generateToken", + "https://user:pass@refused-arcgis757.example.test/arcgis/tokens/generateToken", + "https://refused-arcgis757.example.test/arcgis/tokens/generateToken?f=json", + "https://refused-arcgis757.example.test/arcgis/tokens/generateToken#fragment", + "/arcgis/tokens/generateToken", + ] { + assert_eq!(generate(&mut s, &id, bad, json!("password-757")).unwrap_err(), arcgis_token::BAD_ENDPOINT, "{bad}"); + } + assert_eq!(generate(&mut s, &nameless, &endpoint, json!("password-757")).unwrap_err(), crate::service_tokens::NO_USER); + assert_eq!(generate(&mut s, &id, &endpoint, json!("")).unwrap_err(), "password is required"); + assert_eq!(generate(&mut s, &id, &endpoint, json!("a\nb")).unwrap_err(), crate::service_tokens::BAD_PASSWORD); + assert_eq!(generate(&mut s, &id, &endpoint, json!(5)).unwrap_err(), "password is text"); + assert!(generate(&mut s, &basic, &endpoint, json!("password-757")).unwrap_err().contains("set auth to arcgis")); + assert!(generate(&mut s, "no-such-connection", &endpoint, json!("password-757")).is_err()); + assert!(s.run("connection.generateToken", &json!({"id":id,"password":"password-757"})).unwrap_err().contains("tokenEndpoint")); + assert!( + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757","remember":"yes"})) + .is_err() + ); + assert_eq!(sent.load(Ordering::SeqCst), 0, "a refused generation sends nothing"); + assert!(s.web_logins.pending.is_empty()); + // Status and cancel take nothing from their caller and do nothing without an operation. + assert_eq!( + s.run("connection.tokenStatus", &json!({"id":id,"token":"injected-token-757"})).unwrap(), + json!({"id":id,"pending":false,"loggedIn":false}) + ); + assert_eq!(s.run("connection.cancelToken", &json!({"id":id})).unwrap(), json!({"id":id,"cancelled":false})); + } + + #[test] + fn arcgis_token_generation_cancel_failure_and_changes_leave_the_earlier_login() { + let origin = "https://races-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + let server = Held::at(&endpoint); + let mut s = session(None); + let id = arcgis(&mut s, origin); + s.run("project.openText", &json!({"text":project(origin,&id)})).unwrap(); + s.run("connection.login", &json!({"id":id,"token":"earlier-token-757"})).unwrap(); + let earlier = || format!("{origin}/arcgis/rest/services"); + let start = |s: &mut Session| { + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + }; + let current = |s: &mut Session| logins::apply(&bound(s, &id, &earlier())).unwrap().unwrap().url; + let earlier_url = format!("{}?token=earlier-token-757", earlier()); + + // Cancelled before an answer: the request is told to stop, the late answer is dropped. + start(&mut s); + assert_eq!(s.run("connection.cancelToken", &json!({"id":id})).unwrap(), json!({"id":id,"cancelled":true})); + let (done, still) = server.take(); + assert!(!still(), "a cancelled request is told to stop"); + done(Ok(token("late-token-757"))); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap(), json!({"id":id,"pending":false,"loggedIn":true})); + assert_eq!(current(&mut s), earlier_url); + + // Cancelled after the answer arrived: nothing is landed. + start(&mut s); + let (done, _) = server.take(); + done(Ok(token("landed-then-cancelled-757"))); + assert_eq!(s.run("connection.cancelToken", &json!({"id":id})).unwrap()["cancelled"], true); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["pending"], false); + assert_eq!(current(&mut s), earlier_url); + + // A failure is a fixed sentence, consumes the operation and keeps the earlier login. + start(&mut s); + let (done, _) = server.take(); + done(Err(arcgis_token::REFUSED.into())); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap_err(), arcgis_token::REFUSED); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["pending"], false); + assert_eq!(current(&mut s), earlier_url); + + // A rename or another path at the same origin keeps the generation's identity. + start(&mut s); + s.run("connection.edit", &json!({"id":id,"name":"Renamed ArcGIS","address":format!("{origin}/elsewhere/rest/services")})).unwrap(); + let (done, _) = server.take(); + done(Ok(token("renamed-token-757"))); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["loggedIn"], true); + assert!(current(&mut s).ends_with("?token=renamed-token-757")); + s.run("connection.login", &json!({"id":id,"token":"earlier-token-757"})).unwrap(); + + // A pasted token replaces a generation under way. + start(&mut s); + s.run("connection.login", &json!({"id":id,"token":"pasted-over-757"})).unwrap(); + let (done, still) = server.take(); + assert!(!still()); + done(Ok(token("overtaken-token-757"))); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["pending"], false); + assert!(current(&mut s).ends_with("?token=pasted-over-757")); + + // Another user, logout and removal end it; their late answers land nowhere. + for action in ["user", "logout", "remove"] { + start(&mut s); + match action { + "user" => drop(s.run("connection.edit", &json!({"id":id,"user":"bob"})).unwrap()), + "logout" => drop(s.run("connection.logout", &json!({"id":id})).unwrap()), + _ => drop(s.run("connection.remove", &json!({"id":id})).unwrap()), + } + let (done, still) = server.take(); + assert!(!still(), "{action}"); + done(Ok(token("never-landed-757"))); + if action == "remove" { + assert!(s.run("connection.tokenStatus", &json!({"id":id})).is_err()); + } else { + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap(), json!({"id":id,"pending":false,"loggedIn":false})); + assert!(s.run("connection.edit", &json!({"id":id,"user":"alice"})).is_ok()); + } + } + assert!(s.web_logins.pending.is_empty()); + } + + #[test] + fn arcgis_token_generation_belongs_to_its_caller_project_and_session() { + let origin = "https://owners-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + let server = Held::at(&endpoint); + let mut s = session(None); + let id = arcgis(&mut s, origin); + let current = s.connections.find(&id).unwrap().clone(); + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + + // Another caller can neither poll nor cancel it, and doesn't consume or change it. + let foreign = Scope::agent(AgentAccess::Full, false, "foreign-arcgis757", None, Arc::new(Allow)); + let polled = s.with_scope(foreign.clone(), |s| crate::service_tokens::poll(s, ¤t).err()).unwrap(); + assert_eq!(polled.as_deref(), Some("this run belongs to another caller")); + let cancelled = s.with_scope(foreign, |s| crate::service_tokens::cancel(s, &id)).unwrap(); + assert_eq!(cancelled.unwrap_err(), "this run belongs to another caller"); + let (done, still) = server.take(); + assert!(still() && s.web_logins.pending.contains_key(&id)); + done(Ok(token("owned-token-757"))); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["loggedIn"], true); + + // A replaced project drops the generation: its answer is refused, whatever it holds. + s.run("connection.logout", &json!({"id":id})).unwrap(); + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + s.run("project.new", &json!({})).unwrap(); + let (done, _) = server.take(); + done(Ok(token("replaced-project-token-757"))); + let error = s.run("connection.tokenStatus", &json!({"id":id})).unwrap_err(); + assert!(error.contains("replaced project"), "{error}"); + assert!(s.web_logins.pending.is_empty()); + let address = web::request(&mut s, Some(&id), &format!("{origin}/a")); + assert!(address.is_err(), "no login was made from it: {address:?}"); + + // A generation left behind by a replaced project never blocks a new one for the same id. + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + let (_, stale) = server.take(); + s.run("project.new", &json!({})).unwrap(); + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + assert!(!stale(), "the replaced project's request was stopped"); + let (done, fresh) = server.take(); + assert!(fresh()); + done(Ok(token("new-project-token-757"))); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap()["loggedIn"], true); + + // Dropping the session cancels what is under way. + let mut dropped = session(None); + let id = arcgis(&mut dropped, origin); + dropped.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + let (_, still) = server.take(); + assert!(still()); + drop(dropped); + assert!(!still(), "a dropped session stops its token request"); + } + + #[test] + fn arcgis_token_generation_that_fails_to_start_says_so() { + let origin = "https://no-transport-arcgis757.example.test"; + let endpoint = format!("{origin}/arcgis/tokens/generateToken"); + serve(&endpoint, |_, _, done| done(Err(arcgis_token::NO_WORKER.into()))); + let mut s = session(None); + let id = arcgis(&mut s, origin); + s.run("connection.generateToken", &json!({"id":id,"tokenEndpoint":endpoint,"password":"password-757"})).unwrap(); + assert_eq!(s.run("connection.tokenStatus", &json!({"id":id})).unwrap_err(), arcgis_token::NO_WORKER); + assert!(s.web_logins.pending.is_empty()); + } +} diff --git a/crates/tiles/src/arcgis_auth.rs b/crates/tiles/src/arcgis_auth.rs index 558c389d73..85451f5722 100644 --- a/crates/tiles/src/arcgis_auth.rs +++ b/crates/tiles/src/arcgis_auth.rs @@ -140,7 +140,7 @@ fn encode(s: &str) -> String { out } -fn form(params: &[(&str, &str)]) -> String { +pub(crate) fn form(params: &[(&str, &str)]) -> String { params.iter().map(|(k, v)| format!("{k}={}", encode(v))).collect::>().join("&") } diff --git a/crates/tiles/src/arcgis_token.rs b/crates/tiles/src/arcgis_token.rs new file mode 100644 index 0000000000..61a8607b74 --- /dev/null +++ b/crates/tiles/src/arcgis_token.rs @@ -0,0 +1,293 @@ +//! ArcGIS Server connection tokens: the credential a saved connection logs in with, and the +//! `generateToken` request that makes one from a user name and password. +//! +//! A token is sent as the request's one `token` query parameter ([`crate::logins::Method::Arcgis`]), +//! which works from a web page without a CORS preflight. Only the token and its expiry are ever +//! kept (the private payload below); the password lives in one request's form and nowhere else. +//! +//! Token generation is its own transport, separate from the portal's OAuth exchange +//! ([`crate::arcgis_auth`]): HTTPS only, at the connection's own origin, no redirects, no cookies, +//! no other login, 30 seconds, at most 1 MiB of answer. A failure is a fixed sentence: neither the +//! server's description nor a transport's error text (which may quote a request) is passed on. +//! The sans-I/O parts (the form, the answer, the payload) are here; the transport is native +//! (`ureq`) or installed by the browser ([`set_generator`]). + +use std::sync::{Arc, LazyLock, Mutex}; + +use rustgis_data::secrets::Secret; +use serde_json::{Value, json}; + +use crate::cache::Still; + +/// The longest token, in bytes. +pub const MAX_TOKEN: usize = crate::logins::MAX_VALUE; +/// The longest token endpoint, in bytes. +pub const MAX_ENDPOINT: usize = 2048; +/// How long a generation may take before it is given up, in seconds. +pub const DEADLINE_SECS: u64 = 30; +/// The most bytes of answer read. +pub const MAX_ANSWER: usize = 1 << 20; +/// The lifetime asked of a generated token, in minutes. The server's `expires` is what counts. +const EXPIRATION_MINUTES: &str = "60"; + +/// A token that can't be sent in a request. +pub const BAD_TOKEN: &str = "The ArcGIS token must be at most 4096 ASCII letters, digits or - _ . ~ + / = characters."; +/// An expiry that isn't a future time. +pub const BAD_EXPIRY: &str = "expiresAt must be a Unix time in milliseconds, in the future."; +/// A token already past its expiry. +pub const EXPIRED: &str = "The ArcGIS token has expired. Log in to this connection again."; +/// A token that expires, on a computer whose clock can't be read. +pub const NO_CLOCK: &str = "This computer's clock can't be read, so an expiring ArcGIS token can't be used."; +/// A token endpoint the connection can't use. +pub const BAD_ENDPOINT: &str = + "The token endpoint must be an https:// address at this connection's own origin, without user information, a query or a fragment."; +/// The server answered, with no token. +pub const REFUSED: &str = "The server didn't give a token. Check the user name, password and token endpoint."; +/// An answer that isn't an ArcGIS token. +pub const BAD_ANSWER: &str = "The server's answer wasn't an ArcGIS token."; +/// The request failed before an answer. +pub const FAILED: &str = "The token request failed."; +/// Nothing answered in time. +pub const TIMED_OUT: &str = "The server didn't answer the token request in time."; +/// An answer past its limit. +pub const TOO_LARGE: &str = "The server's answer to the token request was too large."; +/// A generation given up. +pub const CANCELLED: &str = "The token request was cancelled."; +/// A worker that couldn't be started. +pub const NO_WORKER: &str = "The token request couldn't be started."; +/// A build with no way to send the request. +pub const NO_TRANSPORT: &str = "This app can't request ArcGIS tokens."; + +/// A nonempty ASCII token of at most [`MAX_TOKEN`] bytes, of the characters ArcGIS tokens use +/// (letters, digits and `-_.~+/=`), so it is the same text in an address, a form and a log, and +/// redaction can recognize it. +pub fn valid_token(token: &str) -> bool { + !token.is_empty() && token.len() <= MAX_TOKEN && token.bytes().all(|b| b.is_ascii_alphanumeric() || b"-_.~+/=".contains(&b)) +} + +/// Unix milliseconds now, or `None` when the clock can't be read. +pub fn now_millis() -> Option { + let since = web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).ok()?; + i64::try_from(since.as_millis()).ok() +} + +/// Whether a token expiring at `expires_at` (none: it isn't known to) is past it at `now`. A +/// clock that can't be read refuses an expiring token rather than treating it as unexpired. +pub fn expired(expires_at: Option, now: Option) -> bool { + expires_at.is_some_and(|at| now.is_none_or(|now| now >= at)) +} + +/// A token and when it stops working. Deliberately neither `Debug` nor `Clone`: nothing prints +/// or copies it by accident. +pub struct Credential { + pub token: Secret, + /// Unix milliseconds, when known. + pub expires_at: Option, +} + +impl Credential { + /// A token as it can be sent, with its optional expiry (a positive time). + pub fn new(token: &str, expires_at: Option) -> Result { + if !valid_token(token) { + return Err(BAD_TOKEN); + } + if expires_at.is_some_and(|at| at <= 0) { + return Err(BAD_EXPIRY); + } + Ok(Self { token: Secret::new(token), expires_at }) + } + + /// The private payload kept in a session or a saved login: + /// `{"version":1,"token":"…","expiresAt":null|}`. + pub fn payload(&self) -> Secret { + Secret::new(json!({"version": 1, "token": self.token.expose(), "expiresAt": self.expires_at}).to_string()) + } + + /// Reads [`Credential::payload`]: exactly its three fields. What it can't read (another + /// version, another shape) is refused without quoting it. + pub fn parse(payload: &str) -> Result { + let Ok(Value::Object(o)) = serde_json::from_str::(payload) else { return Err(BAD_TOKEN) }; + if o.len() != 3 || o.get("version").and_then(Value::as_u64) != Some(1) { + return Err(BAD_TOKEN); + } + let token = o.get("token").and_then(Value::as_str).ok_or(BAD_TOKEN)?; + let expires_at = match o.get("expiresAt").ok_or(BAD_TOKEN)? { + Value::Null => None, + value => Some(value.as_i64().ok_or(BAD_EXPIRY)?), + }; + Self::new(token, expires_at) + } + + /// Whether it is past its expiry at `now`. + pub fn is_expired(&self, now: Option) -> bool { + expired(self.expires_at, now) + } +} + +/// Checks a token endpoint against the connection's `origin`: an absolute https:// address on +/// exactly that origin, with no user information, query or fragment. It is never found in a +/// service's answer or guessed from a path. +pub fn check_endpoint(endpoint: &str, origin: &str) -> Result<(), &'static str> { + if endpoint.len() > MAX_ENDPOINT + || !crate::address::is_https(endpoint) + || endpoint.contains(['?', '#']) + || crate::logins::origin(endpoint).as_deref() != Some(origin) + { + return Err(BAD_ENDPOINT); + } + Ok(()) +} + +/// The `generateToken` form: `f=json`, the user, the password, `client=requestip` and a lifetime. +pub fn form(username: &str, password: &str) -> Secret { + Secret::new(crate::arcgis_auth::form(&[ + ("f", "json"), + ("username", username), + ("password", password), + ("client", "requestip"), + ("expiration", EXPIRATION_MINUTES), + ])) +} + +/// What a `generateToken` answer gives: a valid token and an integer `expires` (Unix +/// milliseconds) after `now`. An expiry is never assumed, an OAuth `access_token` answer isn't +/// this protocol, and no server text is passed on. +pub fn from_answer(status: u16, body: &[u8], now: Option) -> Result { + if !(200..300).contains(&status) { + return Err(format!("The server refused the token request (HTTP {status}).")); + } + let Ok(Value::Object(o)) = serde_json::from_slice::(body) else { return Err(BAD_ANSWER.into()) }; + if o.contains_key("error") { + return Err(REFUSED.into()); + } + let (Some(token), Some(expires)) = (o.get("token").and_then(Value::as_str), o.get("expires").and_then(Value::as_i64)) else { + return Err(BAD_ANSWER.into()); + }; + if expired(Some(expires), Some(now.ok_or(NO_CLOCK)?)) { + return Err(BAD_ANSWER.into()); + } + Credential::new(token, Some(expires)).map_err(|_| BAD_ANSWER.into()) +} + +/// How a generation's answer is delivered, once. +pub type Done = Box) + Send>; + +type Generator = dyn Fn(String, Secret, Still, Done) + Send + Sync; + +static GENERATOR: LazyLock>>> = LazyLock::new(Mutex::default); + +/// Replaces how a token is requested (the browser's `fetch`): the endpoint, the form, the +/// request's predicate (false once it is cancelled or its authority revoked) and what to call +/// once, with a credential made by [`from_answer`]. The transport sends the form to the endpoint +/// only: no redirects, cookies, referrer or other login. +pub fn set_generator(f: impl Fn(String, Secret, Still, Done) + Send + Sync + 'static) { + *GENERATOR.lock().unwrap_or_else(|e| e.into_inner()) = Some(Arc::new(f)); +} + +/// Requests a token, off the caller's thread, and calls `done` exactly once (even when nothing +/// could be started). `endpoint` has been checked by [`check_endpoint`]; this refuses anything +/// but https:// again. `still` turns false when the request is cancelled: the answer is then +/// discarded, and a transport stops as soon as it next looks. +pub fn generate(endpoint: String, body: Secret, still: Still, done: Done) { + if !crate::address::is_https(&endpoint) { + done(Err(BAD_ENDPOINT.into())); + return; + } + let installed = GENERATOR.lock().unwrap_or_else(|e| e.into_inner()).clone(); + if let Some(send) = installed { + send(endpoint, body, still, done); + return; + } + #[cfg(not(target_arch = "wasm32"))] + native::start(endpoint, body, still, done); + #[cfg(target_arch = "wasm32")] + { + let _ = (body, still); + done(Err(NO_TRANSPORT.into())); + } +} + +#[cfg(not(target_arch = "wasm32"))] +mod native { + use std::io::Read; + use std::sync::{Arc, LazyLock, Mutex}; + use std::time::Duration; + + use super::{ + BAD_ANSWER, CANCELLED, Credential, DEADLINE_SECS, Done, FAILED, MAX_ANSWER, NO_WORKER, Secret, TIMED_OUT, TOO_LARGE, from_answer, + now_millis, + }; + use crate::cache::{Still, check_still}; + + /// One agent, made once (its TLS setup reads the system's certificates). It never follows a + /// redirect, which would replay the password; keeps no connection; has no cookie jar. + static AGENT: LazyLock = LazyLock::new(|| { + ureq::Agent::config_builder() + .tls_config(rustgis_data::http::tls_config()) + .http_status_as_error(false) + .timeout_global(Some(Duration::from_secs(DEADLINE_SECS))) + .user_agent(concat!("RustGIS/", env!("CARGO_PKG_VERSION"), " (+https://rustgis.app)")) + .max_redirects(0) + .max_idle_connections(0) + .build() + .into() + }); + + /// Calls `done` once, whichever of the worker and a failed start gets here first. + pub(super) fn finish(slot: &Mutex>, result: Result) { + let done = slot.lock().unwrap_or_else(|e| e.into_inner()).take(); + if let Some(done) = done { + done(result); + } + } + + pub(super) fn start(endpoint: String, body: Secret, still: Still, done: Done) { + let slot = Arc::new(Mutex::new(Some(done))); + let worker = slot.clone(); + let spawned = std::thread::Builder::new().name("arcgis-token".into()).spawn(move || { + let result = run(&AGENT, &endpoint, &body, &still); + finish(&worker, result); + }); + if spawned.is_err() { + finish(&slot, Err(NO_WORKER.into())); + } + } + + /// Sends the form and reads the answer. The cancellation predicate is looked at before the + /// request is prepared, immediately before it is sent, and between chunks of the answer; one + /// blocking network call may run on to the deadline, and its answer is then discarded. + pub(super) fn run(agent: &ureq::Agent, endpoint: &str, body: &Secret, still: &Still) -> Result { + let still = Some(still.clone()); + let cancelled = |_: String| CANCELLED.to_string(); + check_still(&still).map_err(cancelled)?; + let request = agent.post(endpoint).header("Content-Type", "application/x-www-form-urlencoded").header("Accept", "application/json"); + check_still(&still).map_err(cancelled)?; + let mut response = request.send(body.expose()).map_err(|e| match e { + ureq::Error::Timeout(_) => TIMED_OUT.to_string(), + _ => FAILED.to_string(), + })?; + let status = response.status().as_u16(); + let mut reader = response.body_mut().with_config().limit(MAX_ANSWER as u64 + 1).reader(); + let mut answer = Vec::new(); + let mut chunk = [0u8; 16 << 10]; + loop { + check_still(&still).map_err(cancelled)?; + let n = reader.read(&mut chunk).map_err(|e| match e.kind() { + std::io::ErrorKind::TimedOut => TIMED_OUT.to_string(), + _ => FAILED.to_string(), + })?; + if n == 0 { + break; + } + answer.extend_from_slice(chunk.get(..n).ok_or(BAD_ANSWER)?); + if answer.len() > MAX_ANSWER { + return Err(TOO_LARGE.into()); + } + } + check_still(&still).map_err(cancelled)?; + from_answer(status, &answer, now_millis()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/tiles/src/arcgis_token/tests.rs b/crates/tiles/src/arcgis_token/tests.rs new file mode 100644 index 0000000000..27e11a0757 --- /dev/null +++ b/crates/tiles/src/arcgis_token/tests.rs @@ -0,0 +1,331 @@ +use super::*; + +const FUTURE: i64 = 4_102_444_800_000; // 2100-01-01 +const NOW: i64 = 1_800_000_000_000; +/// Long gone on any clock. +const PAST: i64 = 1_000_000_000_000; + +fn answer(token: &str, expires: i64) -> Vec { + json!({"token": token, "expires": expires, "ssl": true}).to_string().into_bytes() +} + +#[test] +fn arcgis_token_form_percent_encodes_the_password_in_the_body_only() { + let form = form("ana lyst", "p&= +%"); + assert_eq!( + form.expose(), + "f=json&username=ana%20lyst&password=p%26%3D%20%2B%25&client=requestip&expiration=60", + "exactly these fields, in this order, every value encoded" + ); + // Neither the endpoint nor a Debug print can reach the password. + assert_eq!(format!("{form:?}"), "Secret()"); +} + +#[test] +fn arcgis_token_answers_are_read_strictly() { + let token = from_answer(200, &answer("test-token", FUTURE), Some(NOW)).ok().unwrap(); + assert_eq!(token.token.expose(), "test-token"); + assert_eq!(token.expires_at, Some(FUTURE)); + // Tokens carry letters, digits and - _ . ~ + / =. + assert!(from_answer(200, &answer("Ab-_.~+/=09", FUTURE), Some(NOW)).is_ok()); + + let refused = |status: u16, body: &[u8], now: Option| from_answer(status, body, now).err().unwrap(); + // The server's error is never passed on, even when it echoes what was sent. + let echoed = + br#"{"error":{"code":400,"message":"Unable to generate token for user p&= +% with password hunter2","details":["hunter2"]}}"#; + let message = refused(200, echoed, Some(NOW)); + assert_eq!(message, REFUSED); + assert!(!message.contains("hunter2")); + // An error status says only its number. + assert_eq!(refused(500, echoed, Some(NOW)), "The server refused the token request (HTTP 500)."); + assert_eq!(refused(302, b"", Some(NOW)), "The server refused the token request (HTTP 302)."); + // OAuth's answer is another protocol; so is a JWT with no expiry, or any shape but the token's. + for body in [ + br#"{"access_token":"test-token","expires_in":1800}"#.as_slice(), + br#"{"token":"test-token"}"#, + br#"{"token":"test-token","expires":"4102444800000"}"#, + br#"{"token":"test-token","expires":4102444800000.5}"#, + br#"{"token":"test-token","expires":null}"#, + br#"{"token":"","expires":4102444800000}"#, + br#"{"token":"a b","expires":4102444800000}"#, + br#"{"token":"a\nb","expires":4102444800000}"#, + br#"{"token":7,"expires":4102444800000}"#, + br#"[]"#, + br#"not json"#, + b"", + ] { + assert_eq!(refused(200, body, Some(NOW)), BAD_ANSWER, "{}", String::from_utf8_lossy(body)); + } + // An expiry in the past, or at the moment, is not a token; an expiry is never made up. + assert_eq!(refused(200, &answer("test-token", NOW - 1), Some(NOW)), BAD_ANSWER); + assert_eq!(refused(200, &answer("test-token", NOW), Some(NOW)), BAD_ANSWER); + assert_eq!(refused(200, &answer("test-token", FUTURE), None), NO_CLOCK); + assert_eq!(refused(200, &answer("test-token", 0), Some(NOW)), BAD_ANSWER); + assert_eq!(refused(200, &answer(&"a".repeat(MAX_TOKEN + 1), FUTURE), Some(NOW)), BAD_ANSWER); +} + +#[test] +fn arcgis_token_payload_is_exact() { + let c = Credential::new("test-token", Some(FUTURE)).ok().unwrap(); + assert_eq!(c.payload().expose(), format!(r#"{{"expiresAt":{FUTURE},"token":"test-token","version":1}}"#)); + let back = Credential::parse(c.payload().expose()).ok().unwrap(); + assert_eq!((back.token.expose(), back.expires_at), ("test-token", Some(FUTURE))); + let unknown = Credential::new("test-token", None).ok().unwrap(); + assert_eq!(Credential::parse(unknown.payload().expose()).ok().unwrap().expires_at, None); + + for bad in [ + r#"{"version":1,"token":"test-token"}"#, + r#"{"version":1,"token":"test-token","expiresAt":null,"extra":1}"#, + r#"{"version":2,"token":"test-token","expiresAt":null}"#, + r#"{"version":"1","token":"test-token","expiresAt":null}"#, + r#"{"version":1,"token":"","expiresAt":null}"#, + r#"{"version":1,"token":"a b","expiresAt":null}"#, + r#"{"version":1,"token":"test-token","expiresAt":0}"#, + r#"{"version":1,"token":"test-token","expiresAt":-5}"#, + r#"{"version":1,"token":"test-token","expiresAt":"5"}"#, + r#"{"version":1,"token":"test-token","expiresAt":1.5}"#, + r#"{"version":1,"token":"test-token","expiresAt":18446744073709551615}"#, + r#"[1,"test-token",null]"#, + "test-token", + "", + ] { + assert!(Credential::parse(bad).is_err(), "{bad}"); + } + assert!(Credential::new(&"a".repeat(MAX_TOKEN), None).is_ok()); + assert!(Credential::new(&"a".repeat(MAX_TOKEN + 1), None).is_err()); + assert!(Credential::new("é", None).is_err()); +} + +#[test] +fn arcgis_token_expiry_follows_an_explicit_clock() { + let c = Credential::new("test-token", Some(NOW)).ok().unwrap(); + assert!(!c.is_expired(Some(NOW - 1))); + assert!(c.is_expired(Some(NOW))); + assert!(c.is_expired(Some(NOW + 1))); + // A clock that can't be read refuses an expiring token and still lets one with no expiry be. + assert!(c.is_expired(None)); + assert!(!Credential::new("test-token", None).ok().unwrap().is_expired(None)); +} + +#[test] +fn arcgis_token_endpoints_stay_on_the_connection_origin() { + let origin = "https://gis.example.test"; + for good in [ + "https://gis.example.test/arcgis/tokens/generateToken", + "HTTPS://GIS.example.test:443/arcgis/tokens/generateToken", + "https://gis.example.test/tokens", + ] { + assert_eq!(check_endpoint(good, origin), Ok(()), "{good}"); + } + for bad in [ + "http://gis.example.test/arcgis/tokens/generateToken", + "https://other.example.test/arcgis/tokens/generateToken", + "https://gis.example.test:8443/arcgis/tokens/generateToken", + "https://user:secret@gis.example.test/arcgis/tokens/generateToken", + "https://gis.example.test/arcgis/tokens/generateToken?f=json", + "https://gis.example.test/arcgis/tokens/generateToken#frag", + "https://gis.example.test/a b", + "/arcgis/tokens/generateToken", + "gis.example.test/arcgis/tokens/generateToken", + "", + ] { + assert_eq!(check_endpoint(bad, origin), Err(BAD_ENDPOINT), "{bad}"); + } + assert_eq!(check_endpoint(&format!("https://gis.example.test/{}", "a".repeat(MAX_ENDPOINT)), origin), Err(BAD_ENDPOINT)); +} + +#[test] +fn arcgis_token_generate_refuses_plain_http_and_calls_done_once() { + let (sent, received) = std::sync::mpsc::channel(); + let done: Done = Box::new(move |r| { + let _ = sent.send(r.err()); + }); + generate("http://gis.example.test/tokens".into(), Secret::new("f=json"), Arc::new(|| true), done); + assert_eq!(received.recv().unwrap().as_deref(), Some(BAD_ENDPOINT)); + assert!(received.try_recv().is_err()); +} + +#[cfg(not(target_arch = "wasm32"))] +mod native { + use super::*; + use std::io::{Read, Write}; + use std::net::TcpListener; + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst}; + use std::sync::mpsc; + use std::time::Duration; + + fn agent() -> ureq::Agent { + ureq::Agent::config_builder() + .http_status_as_error(false) + .timeout_global(Some(Duration::from_secs(10))) + .max_redirects(0) + .max_idle_connections(0) + .build() + .into() + } + + /// Reads a request whole, body included. + fn read_request(stream: &mut std::net::TcpStream) -> String { + let _ = stream.set_read_timeout(Some(Duration::from_secs(5))); + let mut request = Vec::new(); + let mut buf = [0u8; 2048]; + while let Ok(n) = stream.read(&mut buf) { + if n == 0 { + break; + } + request.extend_from_slice(&buf[..n]); + let text = String::from_utf8_lossy(&request).into_owned(); + if let Some((head, body)) = text.split_once("\r\n\r\n") { + let length = head + .lines() + .find_map(|l| l.to_ascii_lowercase().strip_prefix("content-length:").and_then(|v| v.trim().parse::().ok())) + .unwrap_or(0); + if body.len() >= length { + break; + } + } + } + String::from_utf8_lossy(&request).into_owned() + } + + /// A server on this computer answering each connection with the next of `answers`; the + /// requests it read come out of the receiver. + fn serve(answers: Vec>) -> (String, mpsc::Receiver) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let (sent, received) = mpsc::channel(); + std::thread::spawn(move || { + for answer in answers { + let Ok((mut stream, _)) = listener.accept() else { return }; + let _ = sent.send(read_request(&mut stream)); + let _ = stream.write_all(&answer); + } + }); + (format!("http://{address}/arcgis/tokens/generateToken"), received) + } + + fn ok(body: &[u8]) -> Vec { + let mut out = + format!("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len()) + .into_bytes(); + out.extend_from_slice(body); + out + } + + fn live() -> Still { + Arc::new(|| true) + } + + #[test] + fn arcgis_token_native_posts_the_form_and_reads_the_token() { + let (url, requests) = serve(vec![ok(&answer("test-token", FUTURE))]); + let body = form("analyst", "p&= +%"); + let credential = super::super::native::run(&agent(), &url, &body, &live()).ok().unwrap(); + assert_eq!((credential.token.expose(), credential.expires_at), ("test-token", Some(FUTURE))); + let request = requests.recv().unwrap(); + let (head, sent) = request.split_once("\r\n\r\n").unwrap(); + assert!(head.starts_with("POST /arcgis/tokens/generateToken HTTP/1.1"), "{head}"); + assert!(head.to_ascii_lowercase().contains("content-type: application/x-www-form-urlencoded")); + assert_eq!(sent, "f=json&username=analyst&password=p%26%3D%20%2B%25&client=requestip&expiration=60"); + // The password is in the body alone: not the address, not a header. + for part in [url.as_str(), head] { + assert!(!part.contains("p%26") && !part.contains("p&="), "{part}"); + } + let lower = head.to_ascii_lowercase(); + assert!(!lower.contains("cookie") && !lower.contains("authorization") && !lower.contains("referer"), "{head}"); + } + + #[test] + fn arcgis_token_native_never_follows_a_redirect() { + let (target, targets) = serve(vec![ok(&answer("test-token", FUTURE))]); + for status in ["302 Found", "307 Temporary Redirect", "308 Permanent Redirect"] { + // A redirect to the same origin or to another is the same refusal: the form is not replayed. + let redirect = + format!("HTTP/1.1 {status}\r\nLocation: {target}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").into_bytes(); + let (url, _) = serve(vec![redirect]); + let message = super::super::native::run(&agent(), &url, &form("analyst", "secret-password"), &live()).err().unwrap(); + assert!(!message.contains("secret-password") && !message.contains(&target), "{message}"); + } + assert!(targets.recv_timeout(Duration::from_millis(300)).is_err(), "the redirect target was asked"); + } + + #[test] + fn arcgis_token_native_refuses_errors_oversized_and_malformed_answers_without_echoing() { + let echo = br#"{"error":{"code":400,"message":"bad password secret-password for analyst"}}"#; + let mut large = br#"{"token":"test-token","expires":4102444800000,"pad":""#.to_vec(); + large.extend(std::iter::repeat_n(b'a', MAX_ANSWER)); + large.extend_from_slice(br#""}"#); + let cases: Vec<(Vec, &str)> = vec![ + (ok(echo), REFUSED), + ( + format!("HTTP/1.1 500 Internal Server Error\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", echo.len()) + .into_bytes() + .into_iter() + .chain(echo.iter().copied()) + .collect(), + "The server refused the token request (HTTP 500).", + ), + (ok(&large), TOO_LARGE), + (ok(b"secret-password"), BAD_ANSWER), + (ok(&answer("test-token", PAST)), BAD_ANSWER), + ]; + for (reply, expected) in cases { + let (url, _) = serve(vec![reply]); + let message = super::super::native::run(&agent(), &url, &form("analyst", "secret-password"), &live()).err().unwrap(); + assert_eq!(message, expected); + assert!(!message.contains("secret-password")); + } + // Nobody listening: a fixed sentence, not the transport's text. + let closed = TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap(); + let message = super::super::native::run(&agent(), &format!("http://{closed}/tokens"), &form("analyst", "secret-password"), &live()) + .err() + .unwrap(); + assert_eq!(message, FAILED); + } + + #[test] + fn arcgis_token_native_cancellation_stops_before_sending_and_discards_a_late_answer() { + // Cancelled before it is prepared: nothing is sent. + let (url, requests) = serve(vec![ok(&answer("test-token", FUTURE))]); + let cancelled: Still = Arc::new(|| false); + let message = super::super::native::run(&agent(), &url, &form("analyst", "secret-password"), &cancelled).err().unwrap(); + assert_eq!(message, CANCELLED); + assert!(requests.recv_timeout(Duration::from_millis(300)).is_err(), "a cancelled request was sent"); + + // Cancelled after it was sent: the answer that comes is discarded. + let (url, requests) = serve(vec![ok(&answer("test-token", FUTURE))]); + let flag = Arc::new(AtomicBool::new(true)); + let looks = Arc::new(AtomicUsize::new(0)); + let (seen, cancel) = (flag.clone(), flag.clone()); + let counted = looks.clone(); + // The first two looks (before preparing, immediately before sending) pass; the request is + // then sent, and what looks next finds it cancelled, as the server's answer arrives. + let still: Still = Arc::new(move || { + if counted.fetch_add(1, SeqCst) >= 2 { + cancel.store(false, SeqCst); + } + seen.load(SeqCst) + }); + let message = super::super::native::run(&agent(), &url, &form("analyst", "secret-password"), &still).err().unwrap(); + assert_eq!(message, CANCELLED); + assert!(requests.recv_timeout(Duration::from_secs(5)).is_ok(), "the request was sent"); + assert!(!flag.load(SeqCst)); + } + + #[test] + fn arcgis_token_generate_calls_done_once_with_the_token() { + let (url, _) = serve(vec![ok(&answer("test-token", FUTURE))]); + let (sent, received) = mpsc::channel(); + let done: Done = Box::new(move |r| { + let _ = sent.send(r.map(|c| (c.token.expose().to_string(), c.expires_at))); + }); + // The built-in transport is https-only; a plain address is the test server's, so it is + // run directly, and `generate` is shown to refuse it above. + let still = live(); + let slot = Arc::new(Mutex::new(Some(done))); + super::super::native::finish(&slot, super::super::native::run(&agent(), &url, &form("analyst", "pw"), &still)); + super::super::native::finish(&slot, Err("again".into())); + assert_eq!(received.recv().unwrap(), Ok(("test-token".to_string(), Some(FUTURE)))); + assert!(received.try_recv().is_err(), "done is called once"); + } +} diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index b761855eef..273b2ce7b6 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -2994,6 +2994,7 @@ mod native { } else { req.config().timeout_global(agent.config().timeouts().global).build() }; + crate::logins::check(url).map_err(str::to_string)?; let (resp, reached) = connected(|| req.call()).map_err(unanswered)?; ceiling = ceiling.min(reached); diagnostics.answered(resp.status().as_u16()); @@ -3085,6 +3086,7 @@ mod native { req = req.header(name.as_str(), value.as_str()); } super::check_current()?; + crate::logins::check(url).map_err(str::to_string)?; let (mut resp, reached) = connected(|| req.send(body)).map_err(unanswered)?; let status = resp.status().as_u16(); diagnostics.answered(status); @@ -3343,6 +3345,17 @@ mod native { len.trim().parse::().ok().filter(|len| *len > 0) } + /// The portal sign-in's `X-Esri-Authorization` value for a hop to `at` (renewed first when it + /// is about to expire), or `None`. A connection's own ArcGIS token outranks it: with one + /// applied (already in the address) the sign-in's token is neither added nor renewed, and a + /// refused or expired connection token never falls back to it. A signed S3 request has none. + pub(super) fn implicit_arcgis(signed: bool, login: Option<&crate::logins::Applied>, at: &str) -> Option { + if signed || login.is_some_and(|applied| applied.arcgis) { + return None; + } + crate::arcgis_auth::authorization_fresh(at) + } + /// The answer to `url`; a refusal keeps the server's answer only when `detail` asks for it, /// as tiles a server doesn't have are routine and only their status is read. A status in /// `omit` is an expected answer: its failure isn't recorded, only noted once per server. A @@ -3442,10 +3455,8 @@ mod native { req = req.header("Authorization", &auth); } // An ArcGIS sign-in's token goes to the portal and the servers it covers, asked for at - // each hop like Earthdata's. - if !prepared.signed - && let Some(auth) = crate::arcgis_auth::authorization_fresh(&at) - { + // each hop like Earthdata's, unless the connection's own ArcGIS token applies. + if let Some(auth) = implicit_arcgis(prepared.signed, login.as_ref(), &at) { req = req.header("X-Esri-Authorization", &auth); } diagnostics.sending(); @@ -3456,6 +3467,8 @@ mod native { } else { req.config().timeout_global(agent.config().timeouts().global).build() }; + // Recheck the original binding even after redirects dropped its credentials. + crate::logins::check(url).map_err(str::to_string)?; let (mut resp, reached) = connected(|| req.call()).map_err(unanswered)?; ceiling = ceiling.min(reached); let status = resp.status().as_u16(); @@ -5164,6 +5177,169 @@ Connection: close assert!(logins::deactivate(&second)); } + /// A connection's ArcGIS token goes in the address as its one `token`, on every hop at its + /// own origin (replacing a stale copy, any case or escaping), never to a redirect's other + /// origin, and with no portal sign-in header beside it. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn arcgis_token_goes_once_to_its_origin_and_never_a_redirect_target() { + let _turn = crate::cache::turn(); + use crate::logins::{self, Method}; + let (other, other_asked) = serve_recorded(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ntile".to_vec()]); + let (base, asked) = serve_recorded(vec![ + b"HTTP/1.1 302 Found\r\nLocation: /next?%54oken=stale-two&y=2\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec(), + format!("HTTP/1.1 302 Found\r\nLocation: {other}/tile?token=foreign\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") + .into_bytes(), + ]); + let token = "connection-token/+="; + let encoded = "connection-token%2F%2B%3D"; + let first = logins::activate(&base, "arcgis-first", Method::Arcgis { expires_at: None }, token, true).unwrap(); + let second = logins::activate(&other, "arcgis-second", Method::Arcgis { expires_at: None }, "second-token", true).unwrap(); + let url = logins::bind(&first, &format!("{base}/start?x=1&ToKeN=stale-one")).unwrap(); + assert_eq!(native::get(&native::agent(), &url, &[]).unwrap(), b"tile"); + let timeout = std::time::Duration::from_secs(5); + let one = asked.recv_timeout(timeout).unwrap(); + let two = asked.recv_timeout(timeout).unwrap(); + assert!(one.starts_with(&format!("GET /start?x=1&token={encoded} HTTP/1.1\r\n")), "{one}"); + assert!(two.starts_with(&format!("GET /next?y=2&token={encoded} HTTP/1.1\r\n")), "{two}"); + let foreign = other_asked.recv_timeout(timeout).unwrap(); + assert!(foreign.starts_with("GET /tile?token=foreign HTTP/1.1\r\n"), "{foreign}"); + for text in [&one, &two, &foreign] { + assert!(!text.contains("stale") && !text.contains("second-token"), "{text}"); + assert!(!text.to_ascii_lowercase().contains("x-esri-authorization"), "{text}"); + } + assert!(!foreign.contains(encoded) && !foreign.contains(token)); + assert!(logins::deactivate(&first)); + assert!(logins::deactivate(&second)); + } + + /// With a portal sign-in covering the address and the connection's own token applied, only the + /// connection's is used: the sign-in's is neither sent nor renewed. Another kind of login, or + /// none, still gets the sign-in's. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn arcgis_token_outranks_the_portal_sign_in() { + let _turn = crate::cache::turn(); + use crate::{ + arcgis_auth::{self, Grant}, + logins::{self, Method}, + }; + let portal = "https://explicit-over-portal.example/portal"; + let origin = "https://explicit-over-portal.example"; + let url = "https://explicit-over-portal.example/server/rest/services/A/MapServer"; + let soon = web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).unwrap().as_secs() as i64 + 3600; + let grant = Grant { access_token: "portal-token".into(), refresh_token: None, expires_at: soon, username: "ana".into() }; + arcgis_auth::sign_in(portal, "client", grant, arcgis_auth::scopes_for(portal, None, &[])); + let token = logins::activate(origin, "outranks-token", Method::Arcgis { expires_at: None }, "connection-token", false).unwrap(); + let key = logins::activate(origin, "outranks-key", Method::Header("X-Key".into()), "connection-key", false).unwrap(); + let explicit = logins::apply(&logins::bind(&token, url).unwrap()).unwrap().unwrap(); + let keyed = logins::apply(&logins::bind(&key, url).unwrap()).unwrap().unwrap(); + assert_eq!(explicit.url, format!("{url}?token=connection-token")); + assert_eq!(native::implicit_arcgis(false, None, url).as_deref(), Some("Bearer portal-token")); + assert_eq!(native::implicit_arcgis(false, Some(&keyed), url).as_deref(), Some("Bearer portal-token")); + assert_eq!(native::implicit_arcgis(false, Some(&explicit), url), None); + assert_eq!(native::implicit_arcgis(true, None, url), None); + assert!(logins::deactivate(&token)); + assert!(logins::deactivate(&key)); + arcgis_auth::sign_out(Some(portal)); + } + + /// Expiry during request preparation prevents the send, not just accepting its answer. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn arcgis_token_expiry_during_preparation_blocks_native_sends() { + if isolate_cache_fixture("cache::tests::arcgis_token_expiry_during_preparation_blocks_native_sends") { + return; + } + let _turn = crate::cache::turn(); + use crate::{ + arcgis_token, + logins::{self, Method}, + }; + use std::sync::atomic::{AtomicUsize, Ordering}; + let agent = native::download_agent(std::time::Duration::from_secs(1)); + for (method, final_check) in [("GET", 2), ("HEAD", 1), ("POST", 1)] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let now = arcgis_token::now_millis().unwrap(); + let expires = now + 60_000; + let binding = + logins::activate(&base, method, Method::Arcgis { expires_at: Some(expires) }, "preparation-expiry-token", true).unwrap(); + let clock = logins::clock_for_test(&binding, now).unwrap(); + let url = logins::bind(&binding, &format!("{base}/prepared")).unwrap(); + let checks = Arc::new(AtomicUsize::new(0)); + let observed = checks.clone(); + let result = with_still( + Arc::new(move || { + // GET also checks its continuation before preparing or applying the login. + if observed.fetch_add(1, Ordering::SeqCst) + 1 == final_check { + clock.store(expires, Ordering::SeqCst); + } + true + }), + || match method { + "GET" => native::get(&agent, &url, &[]).map(|_| ()), + "HEAD" => native::head_with(&agent, &url).map(|_| ()), + _ => native::post_with(&agent, &url, "{}").map(|_| ()), + }, + ); + assert_eq!(result.unwrap_err(), arcgis_token::EXPIRED, "{method}"); + assert_eq!(checks.load(Ordering::SeqCst), final_check, "{method}"); + assert_eq!(listener.accept().unwrap_err().kind(), std::io::ErrorKind::WouldBlock, "{method}"); + assert!(logins::deactivate(&binding)); + } + } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn arcgis_token_expiry_blocks_cached_and_in_flight_bytes() { + if isolate_cache_fixture("cache::tests::arcgis_token_expiry_blocks_cached_and_in_flight_bytes") { + return; + } + let _turn = crate::cache::turn(); + use crate::{ + arcgis_token, + logins::{self, Method}, + }; + use std::io::{Read, Write}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let now = arcgis_token::now_millis().unwrap(); + let expires = now + 60_000; + let binding = + logins::activate(&base, "expiry-cache-session", Method::Arcgis { expires_at: Some(expires) }, "expiry-cache-token", true) + .unwrap(); + let clock = logins::clock_for_test(&binding, now).unwrap(); + let cached = logins::bind(&binding, &format!("{base}/cached")).unwrap(); + store(&cached, Entry::Ready(Arc::new(Payload::Json(serde_json::json!({"private": true}))), 64, None)); + assert!(peek(&cached).is_some()); + let pending = logins::bind(&binding, &format!("{base}/pending")).unwrap(); + let (started_tx, started_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut buf = [0u8; 2048]; + let n = stream.read(&mut buf).unwrap(); + assert!(String::from_utf8_lossy(&buf[..n]).starts_with("GET /pending?token=expiry-cache-token HTTP/1.1")); + started_tx.send(()).unwrap(); + release_rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap(); + stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 8\r\nConnection: close\r\n\r\nold-body").unwrap(); + listener + }); + let worker = std::thread::spawn(move || native::get(&native::agent(), &pending, &[])); + started_rx.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); + clock.store(expires, std::sync::atomic::Ordering::SeqCst); + release_tx.send(()).unwrap(); + assert_eq!(worker.join().unwrap().unwrap_err(), arcgis_token::EXPIRED); + let listener = server.join().unwrap(); + assert!(peek(&cached).is_none()); + assert!(matches!(get(&cached, Want::Json), State::Failed(_))); + assert_eq!(native::get(&native::agent(), &cached, &[]).unwrap_err(), arcgis_token::EXPIRED); + listener.set_nonblocking(true).unwrap(); + assert_eq!(listener.accept().unwrap_err().kind(), std::io::ErrorKind::WouldBlock); + assert!(logins::deactivate(&binding)); + } + /// A HEAD and a POST carry the query key too, to the address the caller named. #[cfg(not(target_arch = "wasm32"))] #[test] diff --git a/crates/tiles/src/lib.rs b/crates/tiles/src/lib.rs index acda1869a1..61e877feea 100644 --- a/crates/tiles/src/lib.rs +++ b/crates/tiles/src/lib.rs @@ -9,6 +9,7 @@ pub mod address; pub mod arcgis_auth; +pub mod arcgis_token; pub mod boundaries; pub mod cache; pub mod cog; diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index 7dcc6a42be..da91e9e9eb 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -2,6 +2,11 @@ //! Bare addresses never acquire a service login. Each activation creates a fresh generation; //! the owning session revokes its token on replacement, logout and drop. +#[cfg(test)] +use std::sync::{ + Arc, + atomic::{AtomicI64, Ordering}, +}; use std::{ collections::BTreeMap, fmt, @@ -87,6 +92,14 @@ pub enum Method { Header(String), /// The credential is the value of this query parameter, added to each request to the origin. Query(String), + /// An ArcGIS Server token, sent as the request's one `token` query parameter (a web page can + /// send it without a CORS preflight). It stops working at `expires_at` (Unix milliseconds), + /// when that is known: bound, checked and applied requests then fail with + /// [`crate::arcgis_token::EXPIRED`]. An explicit ArcGIS login outranks the portal sign-in and + /// an address's own `token`, and its expiry never falls back to either. + Arcgis { + expires_at: Option, + }, } // Deliberately no Debug: neither credentials nor their derived redaction patterns are printable. @@ -96,6 +109,8 @@ struct Entry { method: Method, value: Secret, redactions: Vec, + #[cfg(test)] + clock: Option>, } struct Redaction { @@ -145,14 +160,27 @@ pub fn activate(origin: &str, owner: &str, method: Method, value: &str, insecure activate_with_random(origin, owner, method, value, insecure, fill_random) } +fn activate_with_random( + origin: &str, + owner: &str, + method: Method, + value: &str, + insecure: bool, + random: impl FnMut(&mut [u8; 16]) -> Result<(), &'static str>, +) -> Result { + activate_at(origin, owner, method, value, insecure, crate::arcgis_token::now_millis(), random) +} + /// Validates the login before requesting entropy, then installs a fresh binding and its active /// output redactions together; failed validation or entropy leaves the registry unchanged. -fn activate_with_random( +/// `now` (Unix milliseconds) is the clock an ArcGIS token's expiry is judged by. +fn activate_at( origin: &str, owner: &str, method: Method, value: &str, insecure: bool, + now: Option, mut random: impl FnMut(&mut [u8; 16]) -> Result<(), &'static str>, ) -> Result { rustgis_data::login_address::set_validator(check); @@ -165,11 +193,23 @@ fn activate_with_random( let valid_name = match &method { Method::Header(name) => valid_header_name(name), Method::Query(name) => valid_query_name(name), + Method::Arcgis { .. } => true, }; if !valid_name { return Err("The login header or query parameter name is not allowed."); } - if !valid_value(value) { + if let Method::Arcgis { expires_at } = &method { + // The token alphabet is narrower than any credential's: nonempty, bounded, no controls. + if !crate::arcgis_token::valid_token(value) { + return Err(crate::arcgis_token::BAD_TOKEN); + } + match (*expires_at, now) { + (Some(at), _) if at <= 0 => return Err(crate::arcgis_token::BAD_EXPIRY), + (Some(_), None) => return Err(crate::arcgis_token::NO_CLOCK), + (Some(at), Some(now)) if now >= at => return Err(crate::arcgis_token::EXPIRED), + _ => {} + } + } else if !valid_value(value) { return Err("The login credential must be nonempty, within the size limit, and contain no controls."); } let mut redactions = Vec::with_capacity(4); @@ -202,7 +242,15 @@ fn activate_with_random( } entries.insert( binding.clone(), - Entry { owner: owner.to_string(), origin: origin.to_string(), method, value: Secret::new(value), redactions }, + Entry { + owner: owner.to_string(), + origin: origin.to_string(), + method, + value: Secret::new(value), + redactions, + #[cfg(test)] + clock: None, + }, ); return Ok(binding); } @@ -244,8 +292,35 @@ pub fn owner(binding: &str) -> Option { entries().get(binding).map(|entry| entry.owner.clone()) } +/// Whether the login is live: it exists and, for an ArcGIS token, isn't past its expiry. pub fn is_active(binding: &str) -> bool { - entries().contains_key(binding) + is_active_at(binding, crate::arcgis_token::now_millis()) +} + +fn is_active_at(binding: &str, now: Option) -> bool { + entries().get(binding).is_some_and(|entry| live(entry, now).is_ok()) +} + +/// A binding-local clock for cache/transport expiry tests; other bindings keep the real clock. +#[cfg(test)] +pub(crate) fn clock_for_test(binding: &str, now: i64) -> Option> { + let clock = Arc::new(AtomicI64::new(now)); + entries().get_mut(binding)?.clock = Some(clock.clone()); + Some(clock) +} + +/// Whether `entry` can be used at `now` (Unix milliseconds): an ArcGIS token past its expiry, or +/// one that expires on a clock that can't be read, can't. The entry (and with it the token's +/// redaction) stays until its owner revokes it, so a response still on its way is scrubbed. +fn live(entry: &Entry, now: Option) -> Result<(), &'static str> { + #[cfg(test)] + let now = entry.clock.as_ref().map(|clock| clock.load(Ordering::SeqCst)).or(now); + match entry.method { + Method::Arcgis { expires_at } if crate::arcgis_token::expired(expires_at, now) => { + Err(if now.is_none() { crate::arcgis_token::NO_CLOCK } else { crate::arcgis_token::EXPIRED }) + } + _ => Ok(()), + } } pub use rustgis_data::login_address::inner; @@ -254,8 +329,13 @@ const REVOKED: &str = "The service login is no longer active. Sign in to this co /// Binds a same-origin address to a live login generation. pub fn bind(binding: &str, url: &str) -> Result { + bind_at(binding, url, crate::arcgis_token::now_millis()) +} + +fn bind_at(binding: &str, url: &str, now: Option) -> Result { let entries = entries(); let entry = entries.get(binding).ok_or(REVOKED)?; + live(entry, now)?; let url = inner(url); if origin(url).as_deref() != Some(entry.origin.as_str()) { return Err("The service login does not cover this address."); @@ -267,11 +347,16 @@ pub use rustgis_data::login_address::inherit; /// Checks runtime authority before any cache (including archive/style/sprite caches) is read. pub fn check(address: &str) -> Result<(), &'static str> { + check_at(address, crate::arcgis_token::now_millis()) +} + +fn check_at(address: &str, now: Option) -> Result<(), &'static str> { let address = if rustgis_data::login_address::is_wrapper(address) { address } else { address.split_once('@').map_or(address, |(_, a)| a) }; let Some(b) = rustgis_data::login_address::parse(address)? else { return Ok(()) }; let entries = entries(); let entry = entries.get(b.binding).ok_or(REVOKED)?; + live(entry, now)?; if entry.origin != b.origin || origin(b.url).as_deref() != Some(b.origin) { return Err("The service login does not cover this address."); } @@ -282,6 +367,9 @@ pub fn check(address: &str) -> Result<(), &'static str> { pub struct Applied { pub url: String, pub header: Option<(String, String)>, + /// The login is an explicit ArcGIS token (already in `url`): the portal sign-in's own token + /// is not added to this request, nor renewed for it. + pub arcgis: bool, } impl fmt::Debug for Applied { @@ -292,15 +380,25 @@ impl fmt::Debug for Applied { /// Prepares only an explicitly bound login; bare URLs are anonymous. pub fn apply(address: &str) -> Result, &'static str> { + apply_at(address, crate::arcgis_token::now_millis()) +} + +fn apply_at(address: &str, now: Option) -> Result, &'static str> { let Some(b) = rustgis_data::login_address::parse(address)? else { return Ok(None) }; let entries = entries(); let entry = entries.get(b.binding).ok_or(REVOKED)?; + live(entry, now)?; if entry.origin != b.origin || origin(b.url).as_deref() != Some(b.origin) { return Err("The service login does not cover this address."); } let applied = match &entry.method { - Method::Header(name) => Applied { url: b.url.to_string(), header: Some((name.clone(), entry.value.expose().to_string())) }, - Method::Query(name) => Applied { url: set_query_parameter(b.url, name, Some(entry.value.expose())), header: None }, + Method::Header(name) => { + Applied { url: b.url.to_string(), header: Some((name.clone(), entry.value.expose().to_string())), arcgis: false } + } + Method::Query(name) => Applied { url: set_query_parameter(b.url, name, Some(entry.value.expose())), header: None, arcgis: false }, + Method::Arcgis { .. } => { + Applied { url: set_query_parameter(b.url, "token", Some(entry.value.expose())), header: None, arcgis: true } + } }; Ok(Some(applied)) } @@ -860,4 +958,94 @@ mod tests { assert_eq!(marked, "https://marker.example/wmts?zqmarkerlogin9=dataset&layer=a"); assert!(deactivate(&binding)); } + + const EXPIRES: i64 = 2_000_000_000_000; + + fn token_login(origin: &str, owner: &str, token: &str, expires_at: Option, now: Option) -> Result { + activate_at(origin, owner, Method::Arcgis { expires_at }, token, false, now, fill_random) + } + + #[test] + fn arcgis_token_login_sends_one_request_only_token() { + let _turn = crate::cache::turn(); + let origin = "https://arcgis-token-login.example"; + let binding = token_login(origin, "arcgis-send", "sentinel-token", None, Some(1)).unwrap(); + let other = activate(origin, "arcgis-send-other", Method::Header("X-Key".into()), "other-key", false).unwrap(); + let bound = bind(&binding, &format!("{origin}/arcgis/rest/services?f=json&ToKeN=old&token=older#frag")).unwrap(); + let applied = apply(&bound).unwrap().unwrap(); + assert!(applied.arcgis && applied.header.is_none()); + // The login's token replaces the address's own, however it is spelled: one, once. + assert_eq!(applied.url, format!("{origin}/arcgis/rest/services?f=json&token=sentinel-token#frag")); + assert_eq!(applied.url.to_ascii_lowercase().matches("token=").count(), 1); + assert_eq!(format!("{applied:?}"), "Applied()"); + // The bound address holds no token of the login, and no other kind of login claims the flag. + assert!(!bound.contains("sentinel-token")); + let key = apply(&bind(&other, &format!("{origin}/x")).unwrap()).unwrap().unwrap(); + assert!(!key.arcgis); + // Another origin never gets it, bound or inherited. + assert!(bind(&binding, "https://foreign.example/arcgis/rest/services").is_err()); + assert_eq!(inherit(&bound, "https://foreign.example/x"), "https://foreign.example/x"); + assert!(apply("https://foreign.example/x?token=mine").unwrap().is_none()); + // Its text is redacted from output as any login's is. + assert_eq!(scrub("failed with sentinel-token"), "failed with [redacted]"); + assert!(deactivate(&binding)); + assert!(deactivate(&other)); + assert!(apply(&bound).is_err()); + } + + #[test] + fn arcgis_token_login_expires_on_an_explicit_clock() { + let _turn = crate::cache::turn(); + let origin = "https://arcgis-token-expiry.example"; + let binding = token_login(origin, "arcgis-expiry", "expiring-token", Some(EXPIRES), Some(EXPIRES - 1)).unwrap(); + let bound = bind_at(&binding, &format!("{origin}/rest/services"), Some(EXPIRES - 1)).unwrap(); + // Before the moment: live everywhere. + assert!(is_active_at(&binding, Some(EXPIRES - 1))); + assert!(check_at(&bound, Some(EXPIRES - 1)).is_ok()); + assert!(apply_at(&bound, Some(EXPIRES - 1)).unwrap().is_some()); + // At the moment and after it: nothing is read, sent or bound, however the cache stands. + for now in [EXPIRES, EXPIRES + 1, EXPIRES + 86_400_000] { + assert!(!is_active_at(&binding, Some(now)), "{now}"); + assert_eq!(check_at(&bound, Some(now)), Err(crate::arcgis_token::EXPIRED)); + assert_eq!(apply_at(&bound, Some(now)).err(), Some(crate::arcgis_token::EXPIRED)); + assert_eq!(bind_at(&binding, &format!("{origin}/rest/services"), Some(now)), Err(crate::arcgis_token::EXPIRED)); + } + // An expiring token on a clock nobody can read is refused, not trusted. + assert!(!is_active_at(&binding, None)); + assert_eq!(check_at(&bound, None), Err(crate::arcgis_token::NO_CLOCK)); + // Expiry doesn't drop the redaction: a late answer is still scrubbed until the owner revokes it. + assert_eq!(scrub("echo expiring-token"), "echo [redacted]"); + assert!(deactivate(&binding)); + assert_eq!(check_at(&bound, Some(EXPIRES - 1)), Err(REVOKED)); + + // A token of unknown expiry isn't judged by the clock. + let open = token_login(origin, "arcgis-open", "open-token", None, None).unwrap(); + let address = bind_at(&open, &format!("{origin}/rest/services"), None).unwrap(); + assert!(is_active_at(&open, None) && check_at(&address, None).is_ok() && apply_at(&address, None).unwrap().is_some()); + assert!(deactivate(&open)); + } + + #[test] + fn arcgis_token_activation_refuses_what_cannot_be_sent_or_is_already_over() { + let _turn = crate::cache::turn(); + let origin = "https://arcgis-token-refusals.example"; + let live = |token: &str, expires: Option, now: Option| token_login(origin, "arcgis-refusals", token, expires, now); + for (token, expires, now, expected) in [ + ("", None, Some(1), crate::arcgis_token::BAD_TOKEN), + ("has space", None, Some(1), crate::arcgis_token::BAD_TOKEN), + ("new\nline", None, Some(1), crate::arcgis_token::BAD_TOKEN), + ("not-ascii-é", None, Some(1), crate::arcgis_token::BAD_TOKEN), + ("token", Some(0), Some(1), crate::arcgis_token::BAD_EXPIRY), + ("token", Some(-5), Some(1), crate::arcgis_token::BAD_EXPIRY), + ("token", Some(EXPIRES), None, crate::arcgis_token::NO_CLOCK), + ("token", Some(EXPIRES), Some(EXPIRES), crate::arcgis_token::EXPIRED), + ("token", Some(EXPIRES), Some(EXPIRES + 1), crate::arcgis_token::EXPIRED), + ] { + assert_eq!(live(token, expires, now), Err(expected), "{token:?} {expires:?} {now:?}"); + } + assert_eq!(live(&"a".repeat(MAX_VALUE + 1), None, Some(1)), Err(crate::arcgis_token::BAD_TOKEN)); + assert_eq!(entries().values().filter(|entry| entry.owner == "arcgis-refusals").count(), 0, "a refusal installs nothing"); + let ok = live("Ab-_.~+/=09", Some(EXPIRES), Some(EXPIRES - 1)).unwrap(); + assert!(deactivate(&ok)); + } } diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index d10f681e0e..8628d31934 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -5,10 +5,11 @@ use egui::vec2; use rustgis_engine::connections::{ Connection, DATABASE_KINDS, LoginReason, LoginRequest, SERVICE_KINDS as WEB_KINDS, WEB, database_key, database_label, login_origin, }; -use rustgis_engine::render::rustgis_tiles::{cache, logins, s3}; +use rustgis_engine::render::rustgis_tiles::{arcgis_token, cache, logins, s3}; use rustgis_engine::s3_connections::STALE; use rustgis_engine::s3_keys as keys; use rustgis_engine::service_logins as web; +use rustgis_engine::service_tokens as tokens; use rustgis_engine::sources::Source; use serde_json::{Value, json}; @@ -32,6 +33,17 @@ pub struct State { pub api_key: String, pub allow_insecure: bool, pub remember: bool, + /// An ArcGIS token login: make the token from the user and a password (true) or paste it + /// (false). The choice is only the form's. + pub token_generate: bool, + /// A pasted ArcGIS token and its optional expiry (Unix milliseconds): never kept once sent. + pub token: String, + pub token_expires: String, + /// The https:// address a token is generated at. Not a secret, and never saved. + pub token_endpoint: String, + /// A token being generated for the saved connection the form is on: each frame asks how it + /// is getting on, and closing or changing the form cancels it. + pub generating: Option, /// The current project's login prompt, never a password. pub request: Option, pub selected: Option, @@ -50,6 +62,13 @@ pub struct State { pub resolving: Option<(String, String)>, } +/// A token generation this form started: the connection it is for, and whether the form's Test +/// follows it (a Test never keeps the token past this session). +pub struct Generating { + id: String, + then_test: bool, +} + /// Authority and form identity for one off-frame Test. Dropping the pending Test revokes its /// temporary login, including when a late download still owns the detached result slot. pub struct TestContext { @@ -94,10 +113,29 @@ pub(crate) fn translated(e: &str) -> String { web::NOT_REMEMBERED => tl!("The login is available for this session but was not remembered on this computer."), web::NOT_FORGOTTEN => tl!("The saved login could not be forgotten. The connection was not changed."), web::LOGGED_OUT => tl!("This connection is logged out. Log in explicitly to use it again."), + web::TOKEN_EXPIRED => tl!("The ArcGIS token has expired. Log in to this connection again."), + arcgis_token::BAD_TOKEN => tl!("The ArcGIS token must be at most 4096 ASCII letters, digits or - _ . ~ + / = characters."), + arcgis_token::BAD_EXPIRY => tl!("expiresAt must be a Unix time in milliseconds, in the future."), + arcgis_token::NO_CLOCK => tl!("This computer's clock can't be read, so an expiring ArcGIS token can't be used."), + arcgis_token::BAD_ENDPOINT => tl!( + "The token endpoint must be an https:// address at this connection's own origin, without user information, a query or a fragment." + ), + arcgis_token::REFUSED => tl!("The server didn't give a token. Check the user name, password and token endpoint."), + arcgis_token::BAD_ANSWER => tl!("The server's answer wasn't an ArcGIS token."), + arcgis_token::FAILED => tl!("The token request failed."), + arcgis_token::TIMED_OUT => tl!("The server didn't answer the token request in time."), + arcgis_token::TOO_LARGE => tl!("The server's answer to the token request was too large."), + arcgis_token::CANCELLED => tl!("The token request was cancelled."), + arcgis_token::NO_WORKER => tl!("The token request couldn't be started."), + arcgis_token::NO_TRANSPORT => tl!("This app can't request ArcGIS tokens."), + tokens::ALREADY => tl!("A token is already being generated for this connection. Cancel it first."), + tokens::NO_USER => tl!("This connection has no user: set user (connection.edit) to generate a token."), + tokens::BAD_PASSWORD => tl!("The password must be at most 4096 bytes, without control characters."), + tokens::CHANGED => tl!("The connection changed while its token was being generated. Generate it again."), web::INSECURE => tl!("This connection uses unencrypted http://. Tick the confirmation before sending its password or key."), web::BAD_SECRET => tl!("The password or key must fit a 4096-byte HTTP header, without control characters."), web::HEADER_ASCII => tl!("An API key sent in a header must use plain ASCII characters."), - web::REQUIRED => tl!("Enter this connection's password or API key to log in."), + web::REQUIRED => tl!("Enter this connection's password, API key or token to log in."), web::UNSAVED => tl!("Save the connection first: a login belongs to a saved connection."), web::NO_ORIGIN => tl!("The connection's address has no http:// or https:// origin a login can go to."), web::SAVE_BEFORE_TEST => tl!("Save changes before testing this connection."), @@ -245,29 +283,34 @@ pub fn open_new(app: &mut RustGisApp, kind: &str) -> Result<(), String> { if cfg!(target_arch = "wasm32") && DATABASE_KINDS.contains(&kind) { return Err(tl!("Database connections are used by the desktop app").into()); } - app.ui.connection = - State { kind: kind.into(), credentials: if kind == "s3" { "keys".into() } else { String::new() }, ..Default::default() }; + replace_form( + app, + State { kind: kind.into(), credentials: if kind == "s3" { "keys".into() } else { String::new() }, ..Default::default() }, + ); app.dialogs = crate::dialogs::Dialogs { open: Some("newConnection"), ..Default::default() }; Ok(()) } pub fn open_edit(app: &mut RustGisApp, id: &str) -> Result<(), String> { let c = app.session.connections.find(id).cloned().ok_or_else(|| format!("no connection '{id}'"))?; - app.ui.connection = State { - region: c.options.get("region").cloned().unwrap_or_default(), - buckets: c.options.get("buckets").cloned().unwrap_or_default(), - credentials: c.options.get("credentials").cloned().unwrap_or_else(|| "none".into()), - profile: c.options.get("profile").cloned().unwrap_or_default(), - auth: c.auth, - user: c.user, - key_name: c.key_name, - id: Some(c.id), - kind: c.kind, - name: c.name, - group: c.group, - address: c.address, - ..Default::default() - }; + replace_form( + app, + State { + region: c.options.get("region").cloned().unwrap_or_default(), + buckets: c.options.get("buckets").cloned().unwrap_or_default(), + credentials: c.options.get("credentials").cloned().unwrap_or_else(|| "none".into()), + profile: c.options.get("profile").cloned().unwrap_or_default(), + auth: c.auth, + user: c.user, + key_name: c.key_name, + id: Some(c.id), + kind: c.kind, + name: c.name, + group: c.group, + address: c.address, + ..Default::default() + }, + ); app.dialogs = crate::dialogs::Dialogs { open: Some("editConnection"), ..Default::default() }; Ok(()) } @@ -282,6 +325,8 @@ pub fn log_in(app: &mut RustGisApp, id: &str) -> bool { && app.ui.connection.id.as_deref().and_then(|id| app.session.connections.find(id)).is_some_and(|c| c.saved_keys); app.dialogs.info = Some(if DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()) { tl!("Type the password, then Save. Remember on this computer is optional.").to_string() + } else if WEB_KINDS.contains(&app.ui.connection.kind.as_str()) && app.ui.connection.auth == "arcgis" { + tl!("Paste a token, or type the password to make one, or leave it blank to use this connection's session or saved login, then Save.").to_string() } else if WEB_KINDS.contains(&app.ui.connection.kind.as_str()) { tl!("Type the password or key, or leave it blank to use this connection's session or saved login, then Save.").to_string() } else if saved { @@ -321,7 +366,7 @@ fn definition(st: &State) -> Value { p } -fn secrets(st: &State, p: &mut Value) { +fn secrets(st: &State, p: &mut Value) -> Result<(), String> { if DATABASE_KINDS.contains(&st.kind.as_str()) && !st.password.is_empty() { p["password"] = json!(st.password); } @@ -332,13 +377,94 @@ fn secrets(st: &State, p: &mut Value) { } } } - if WEB_KINDS.contains(&st.kind.as_str()) { + if WEB_KINDS.contains(&st.kind.as_str()) && st.auth == "arcgis" { + // What was typed is refused with its reason, never dropped to log in with another login. + if let Some(token) = pasted_token(st)? { + p["token"] = json!(token.token.expose()); + if let Some(at) = token.expires_at { + p["expiresAt"] = json!(at); + } + p["allowInsecure"] = json!(st.allow_insecure); + } + } else if WEB_KINDS.contains(&st.kind.as_str()) { let (key, value) = if st.auth == "basic" { ("password", &st.password) } else { ("apiKey", &st.api_key) }; if !st.auth.is_empty() && !value.is_empty() { p[key] = json!(value); p["allowInsecure"] = json!(st.allow_insecure); } } + Ok(()) +} + +/// The ArcGIS token typed in the form (pasted mode) with its expiry, checked: `None` when no token +/// is typed. An expiry that isn't a Unix time in milliseconds is an error, not a guess. +fn pasted_token(st: &State) -> Result, String> { + if st.token_generate { + return Ok(None); + } + let token = st.token.trim(); + let expires = st.token_expires.trim(); + if token.is_empty() { + return if expires.is_empty() { Ok(None) } else { Err(tl!("Type the token this expiry is for.").into()) }; + } + let expires_at = if expires.is_empty() { + None + } else { + Some( + expires + .parse::() + .ok() + .filter(|at| *at > 0) + .ok_or_else(|| tl!("Enter the expiry as a Unix time in milliseconds, or leave it blank.").to_string())?, + ) + }; + let credential = arcgis_token::Credential::new(token, expires_at).map_err(translated)?; + let now = arcgis_token::now_millis(); + if credential.is_expired(now) { + return Err(translated(if now.is_none() { arcgis_token::NO_CLOCK } else { arcgis_token::EXPIRED })); + } + Ok(Some(credential)) +} + +/// The secret a service login of the form sends as its method does: the password (basic), the +/// key, or an ArcGIS token's private payload. +fn login_secret(st: &State) -> Result { + match st.auth.as_str() { + "basic" => Ok(st.password.clone()), + "arcgis" => Ok(pasted_token(st)?.map(|token| token.payload().expose().to_string()).unwrap_or_default()), + _ => Ok(st.api_key.clone()), + } +} + +/// Whether the form generates an ArcGIS token from the typed password. +fn generating_form(st: &State) -> bool { + WEB_KINDS.contains(&st.kind.as_str()) && st.auth == "arcgis" && st.token_generate +} + +/// What generating a token needs, checked before anything is saved or sent: the user, the +/// password and a token endpoint at the connection's own origin. +fn check_generation(st: &State) -> Result<(), String> { + if st.user.trim().is_empty() { + return Err(tl!("Enter the user to generate a token for.").into()); + } + if st.password.is_empty() { + return Err(tl!("Enter the password to generate a token.").into()); + } + if !logins::valid_value(&st.password) { + return Err(translated(tokens::BAD_PASSWORD)); + } + if st.token_endpoint.trim().is_empty() { + return Err(tl!("Enter the token endpoint (https://host/arcgis/tokens/generateToken).").into()); + } + let (connection, _) = rustgis_engine::connections::clean(Connection { + kind: st.kind.clone(), + address: st.address.clone(), + auth: st.auth.clone(), + user: st.user.clone(), + ..Default::default() + })?; + let origin = login_origin(&connection).ok_or_else(|| translated(web::NO_ORIGIN))?; + arcgis_token::check_endpoint(st.token_endpoint.trim(), &origin).map_err(translated) } /// A saved service Test uses its saved login-relevant definition, never an edited endpoint @@ -371,14 +497,34 @@ pub fn start_test(app: &mut RustGisApp) { app.dialogs.error = Some(reason.into()); return; } + // A token made from a password goes through the same background command a Save uses, never + // remembered (remember is off); the Test runs once it lands. It belongs to a saved connection. + if generating_form(&app.ui.connection) && !app.ui.connection.password.is_empty() { + let Some(id) = app.ui.connection.id.clone() else { + clear_secrets(&mut app.ui.connection); + app.dialogs.error = Some(tl!("Save the connection before generating a token.").into()); + return; + }; + if let Err(e) = check_generation(&app.ui.connection).and_then(|()| start_generating(app, &id, false, true)) { + app.dialogs.error = Some(e); + } + return; + } if service && typed_secrets(&app.ui.connection) && insecure(&app.ui.connection) && !app.ui.connection.allow_insecure { app.dialogs.error = Some(translated(web::INSECURE)); return; } - if service && let Some(id) = &app.ui.connection.id { + // A pasted ArcGIS token is tested through a temporary login too, saved connection or not: the + // saved one (and its session login) is neither used nor replaced, and the probe ends with the Test. + let typed_token = + service && app.ui.connection.auth == "arcgis" && !app.ui.connection.token_generate && !app.ui.connection.token.trim().is_empty(); + let temporary = service && !matches!(app.ui.connection.auth.as_str(), "" | "none") && (app.ui.connection.id.is_none() || typed_token); + if service + && !temporary + && let Some(id) = &app.ui.connection.id + { p["id"] = json!(id); } - let temporary = service && app.ui.connection.id.is_none() && !matches!(app.ui.connection.auth.as_str(), "" | "none"); if temporary && app.ui.connection.auth == "query" && !logins::valid_query_name(app.ui.connection.key_name.trim()) { clear_secrets(&mut app.ui.connection); app.dialogs.error = Some(tl!("Enter a valid query parameter name before testing this connection.").into()); @@ -395,8 +541,10 @@ pub fn start_test(app: &mut RustGisApp) { if let Some(address) = &query_address { p["address"] = json!(address); } - } else { - secrets(&app.ui.connection, &mut p); + } else if let Err(e) = secrets(&app.ui.connection, &mut p) { + clear_secrets(&mut app.ui.connection); + app.dialogs.error = Some(e); + return; } if DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()) { match app.run("connection.test", p) { @@ -433,8 +581,15 @@ pub fn start_test(app: &mut RustGisApp) { if let Some(address) = &query_address { q["address"] = json!(address); } + let secret = match login_secret(&app.ui.connection) { + Ok(secret) => secret, + Err(e) => { + clear_secrets(&mut app.ui.connection); + app.dialogs.error = Some(e); + return; + } + }; let st = &app.ui.connection; - let secret = if st.auth == "basic" { &st.password } else { &st.api_key }; let connection = Connection { kind: st.kind.clone(), address: query_address.as_ref().unwrap_or(&st.address).clone(), @@ -443,7 +598,7 @@ pub fn start_test(app: &mut RustGisApp) { key_name: st.key_name.clone(), ..Default::default() }; - let result = web::temporary_probe(&connection, secret, st.allow_insecure, url); + let result = web::temporary_probe(&connection, &secret, st.allow_insecure, url); clear_secrets(&mut app.ui.connection); match result { Ok((url, login)) => (url, Some(login)), @@ -455,7 +610,10 @@ pub fn start_test(app: &mut RustGisApp) { } else { (url.to_string(), None) }; - if service && let Some(id) = &app.ui.connection.id { + if service + && !temporary + && let Some(id) = &app.ui.connection.id + { q["id"] = json!(id); } let status = tl!("Testing…"); @@ -500,8 +658,12 @@ fn password_prompt(app: &mut RustGisApp, id: &str, request: Option app.ui.connection.request = request; if WEB_KINDS.contains(&app.ui.connection.kind.as_str()) { app.ui.connection.login = true; - app.dialogs.info = - Some(tl!("Enter the password or key for this connection. Remember on this computer is optional.").into()); + app.dialogs.info = Some(if app.ui.connection.auth == "arcgis" { + tl!("Paste a token for this connection, or make one from its user and password. Remember on this computer is optional.") + .into() + } else { + tl!("Enter the password or key for this connection. Remember on this computer is optional.").into() + }); app.dialogs.error = error.map(|e| translated(&e)); return; } @@ -516,7 +678,7 @@ pub(crate) fn choose(app: &mut RustGisApp, request: LoginRequest) { if candidates(app, &request).is_empty() { return; } - app.ui.connection = State { request: Some(request), ..Default::default() }; + replace_form(app, State { request: Some(request), ..Default::default() }); app.dialogs = crate::dialogs::Dialogs { open: Some("chooseConnection"), ..Default::default() }; } @@ -588,7 +750,7 @@ fn poll_requests(app: &mut RustGisApp) { && (request.opened != app.session.opened || request.reason != LoginReason::Connection && candidates(app, request).is_empty()) { app.dialogs.open = None; - app.ui.connection = State::default(); + replace_form(app, State::default()); } // A pending request never displaces an unrelated dialog, including a form being edited. if app.dialogs.open.is_some() { @@ -607,7 +769,7 @@ fn poll_requests(app: &mut RustGisApp) { continue; } if request.kind == WEB && request.reason == LoginReason::Connection { - app.ui.connection = State { request: Some(request), ..Default::default() }; + replace_form(app, State { request: Some(request), ..Default::default() }); app.dialogs = crate::dialogs::Dialogs { open: Some("chooseConnection"), ..Default::default() }; break; } @@ -626,6 +788,7 @@ fn poll_requests(app: &mut RustGisApp) { pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { poll_requests(app); + poll_generating(app, ctx); if !matches!(app.dialogs.open, Some("newConnection" | "editConnection")) { if let Some((_, _, context)) = app.ui.connection.test.take() { if app.dialogs.open.is_none() { @@ -720,6 +883,13 @@ fn typed_secrets(st: &State) -> bool { && match st.auth.as_str() { "basic" => !st.password.is_empty(), "header" | "query" => !st.api_key.is_empty(), + "arcgis" => { + if st.token_generate { + !st.password.is_empty() + } else { + !st.token.trim().is_empty() + } + } _ => false, }) || (st.kind == "s3" @@ -730,6 +900,8 @@ fn typed_secrets(st: &State) -> bool { fn clear_secrets(st: &mut State) { st.password.clear(); st.api_key.clear(); + st.token.clear(); + st.token_expires.clear(); st.allow_insecure = false; st.access_key.clear(); st.secret_key.clear(); @@ -753,14 +925,28 @@ fn save(app: &mut RustGisApp) -> Result<(), String> { key_name: st.key_name.clone(), ..Default::default() })?; - let secret = if st.auth == "basic" { &st.password } else { &st.api_key }; - web::check_login_secret(&connection, secret).map_err(|e| translated(&e))?; + if generating_form(st) { + check_generation(st)?; + } else { + let secret = login_secret(st)?; + web::check_login_secret(&connection, &secret).map_err(|e| translated(&e))?; + } + } else if service && st.auth == "arcgis" { + // An expiry typed with no token is a mistake to say, not to drop. + pasted_token(st)?; + } + if st.generating.is_some() { + return Err(tl!("A token is being generated. Cancel it before saving again.").into()); } - if service && typed_secrets(st) && insecure(st) && !st.allow_insecure { + if service && typed_secrets(st) && insecure(st) && !st.allow_insecure && !generating_form(st) { return Err(translated(web::INSECURE)); } if service && st.remember && !typed_secrets(st) { - return Err(tl!("Type the password or key to remember it on this computer.").into()); + return Err(if st.auth == "arcgis" { + tl!("Type the token, or the password to generate one, to remember it on this computer.").into() + } else { + tl!("Type the password or key to remember it on this computer.").into() + }); } let keys_mode = st.kind == "s3" && st.credentials == "keys"; let remember = keys_mode && st.remember && remember_blocked(store, &st.session_token).is_none(); @@ -794,13 +980,24 @@ fn save(app: &mut RustGisApp) -> Result<(), String> { // Kept at once: a Save tried again edits this connection rather than adding another. let created = app.ui.connection.id.is_none(); app.ui.connection.id = Some(id.clone()); + // A token made from a password is asked of the server in the background: the password leaves + // the form with the request, and the dialog stays to say how it is going (see `poll_generating`). + if generating_form(&app.ui.connection) && !app.ui.connection.password.is_empty() { + let remember = store && app.ui.connection.remember; + crate::connectiontree::refresh(app, Some(&id)); + return start_generating(app, &id, remember, false).inspect_err(|_| { + if created { + app.dialogs.open = Some("editConnection"); + } + }); + } if !wants_login { clear_secrets(&mut app.ui.connection); crate::connectiontree::refresh(app, Some(&id)); return Ok(()); } let mut login = json!({"id": id}); - secrets(&app.ui.connection, &mut login); + secrets(&app.ui.connection, &mut login)?; let database = DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()); if database || service && store { login["remember"] = json!(app.ui.connection.remember); @@ -848,6 +1045,72 @@ fn save(app: &mut RustGisApp) -> Result<(), String> { Ok(()) } +/// Asks for a token for the saved connection `id` from the typed password, which leaves the form +/// with the request (a failure doesn't keep it either). The form then waits for the answer. +fn start_generating(app: &mut RustGisApp, id: &str, remember: bool, then_test: bool) -> Result<(), String> { + let st = &mut app.ui.connection; + let params = json!({"id": id, "tokenEndpoint": st.token_endpoint.trim(), "password": st.password, "remember": remember}); + clear_secrets(st); + app.dialogs.error = None; + app.run("connection.generateToken", params).map_err(|e| translated(&e))?; + app.ui.connection.generating = Some(Generating { id: id.to_string(), then_test }); + app.dialogs.info = None; + Ok(()) +} + +/// Stops the token this form is generating, if any; the login the connection had stays. +fn cancel_generating(app: &mut RustGisApp) { + if let Some(generating) = app.ui.connection.generating.take() { + // The connection may be gone already, with its generation: nothing is left to stop. + let _ = app.run("connection.cancelToken", json!({"id": generating.id})); + } +} + +/// Replaces the form, first ending a token generation it started. +fn replace_form(app: &mut RustGisApp, next: State) { + cancel_generating(app); + app.ui.connection = next; +} + +/// Asks how the generation of this form is getting on, once a frame, until it lands. Closing the +/// form, or changing to another, cancels it. +fn poll_generating(app: &mut RustGisApp, ctx: &egui::Context) { + let Some(id) = app.ui.connection.generating.as_ref().map(|g| g.id.clone()) else { return }; + let open = matches!(app.dialogs.open, Some("newConnection" | "editConnection")) && app.ui.connection.id.as_deref() == Some(id.as_str()); + if !open { + cancel_generating(app); + return; + } + match app.run("connection.tokenStatus", json!({"id": id})) { + Ok(v) if v["pending"] == true => ctx.request_repaint_after(std::time::Duration::from_millis(100)), + Ok(v) => token_landed(app, &id, &v), + Err(e) => { + app.ui.connection.generating = None; + app.dialogs.info = None; + app.dialogs.error = Some(translated(&e)); + } + } +} + +/// A generated token is the connection's login: refresh what shows it, say what it couldn't do, +/// log the waiting layers in and either run the Test that asked for it or close the form. +fn token_landed(app: &mut RustGisApp, id: &str, v: &Value) { + let then_test = app.ui.connection.generating.take().is_some_and(|g| g.then_test); + app.dialogs.info = None; + crate::connectiontree::refresh(app, Some(id)); + app.ui.connection.login = false; + let warning = v["warning"].as_str().map(translated); + resolve_waiting(app, id); + if let Some(warning) = warning { + app.ui.connection.warned = Some(warning); + } else if then_test { + start_test(app); + } else { + app.dialogs.open = None; + replace_form(app, State::default()); + } +} + pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { let intro = if DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()) { tl!( @@ -870,6 +1133,7 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { let current = app.ui.connection.id.as_deref().and_then(|id| app.session.connections.find(id)); let remembered = current.is_some_and(|c| web::is_service(c) && c.remembered); let logged_in = current.is_some_and(|c| web::logged_in(&app.session, c)); + let generating = app.ui.connection.generating.is_some(); let st = &mut app.ui.connection; let database = DATABASE_KINDS.contains(&st.kind.as_str()); let service = WEB_KINDS.contains(&st.kind.as_str()); @@ -877,6 +1141,7 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { let mut source_changed = false; let mut forget = false; let mut logout = false; + ui.add_enabled_ui(!generating, |ui| { egui::Grid::new("connection").num_columns(2).spacing(vec2(10.0, 8.0)).show(ui, |ui| { ui.label(tl!("Type")); ui.label(tl!(label(&st.kind))); @@ -1000,6 +1265,7 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { ("basic", tl!("Basic authentication")), ("header", tl!("API key in a header")), ("query", tl!("API key in the query")), + ("arcgis", tl!("ArcGIS token")), ]; let shown = methods.iter().find(|(code, _)| *code == st.auth).map_or(tl!("None"), |(_, name)| *name); let mut method_changed = false; @@ -1018,9 +1284,13 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { clear_secrets(st); changed = true; } - if st.auth == "basic" { + if matches!(st.auth.as_str(), "basic" | "arcgis") { let input_label = ui.label(tl!("User")); - changed |= ui.add(egui::TextEdit::singleline(&mut st.user).desired_width(360.0)).labelled_by(input_label.id).changed(); + let mut user = egui::TextEdit::singleline(&mut st.user).desired_width(360.0); + if st.auth == "arcgis" { + user = user.hint_text(tl!("Needed to make a token from a password")); + } + changed |= ui.add(user).labelled_by(input_label.id).changed(); ui.end_row(); } else if matches!(st.auth.as_str(), "header" | "query") { let query = st.auth == "query"; @@ -1035,30 +1305,98 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { .changed(); ui.end_row(); } - if !st.auth.is_empty() { - let basic = st.auth == "basic"; - let input_label = ui.label(if basic { tl!("Password") } else { tl!("Key") }); - let field = if basic { &mut st.password } else { &mut st.api_key }; - changed |= - ui.add(egui::TextEdit::singleline(field).password(true).desired_width(360.0)).labelled_by(input_label.id).changed(); + if !st.auth.is_empty() && st.auth == "arcgis" { + let input_label = ui.label(tl!("Token from")); + let mut mode_changed = false; + ui.horizontal(|ui| { + mode_changed |= ui.radio_value(&mut st.token_generate, false, tl!("Paste token")).changed(); + mode_changed |= ui.radio_value(&mut st.token_generate, true, tl!("Username and password")).changed(); + }) + .response + .labelled_by(input_label.id); ui.end_row(); - if st.auth == "query" { + if mode_changed { + clear_secrets(st); + changed = true; + } + if st.token_generate { + let input_label = ui.label(tl!("Token endpoint")); + changed |= ui + .add( + egui::TextEdit::singleline(&mut st.token_endpoint) + .hint_text("https://host/arcgis/tokens/generateToken") + .desired_width(360.0), + ) + .labelled_by(input_label.id) + .changed(); + ui.end_row(); + let input_label = ui.label(tl!("Password")); + changed |= ui + .add(egui::TextEdit::singleline(&mut st.password).password(true).desired_width(360.0)) + .labelled_by(input_label.id) + .changed(); + ui.end_row(); + ui.label(""); + ui.label( + egui::RichText::new(tl!( + "Asks this server for a token, over https:// only, and keeps the token and its expiry, never the password." + )) + .small() + .weak(), + ); + ui.end_row(); + } else { + let input_label = ui.label(tl!("Token")); + changed |= ui + .add(egui::TextEdit::singleline(&mut st.token).password(true).desired_width(360.0)) + .labelled_by(input_label.id) + .changed(); + ui.end_row(); + let input_label = ui.label(tl!("Expires at (Unix milliseconds)")); + changed |= ui + .add(egui::TextEdit::singleline(&mut st.token_expires).hint_text(tl!("Optional")).desired_width(360.0)) + .labelled_by(input_label.id) + .changed(); + ui.end_row(); ui.label(""); ui.label( egui::RichText::new(tl!( - "The key is added to each request to this address as this parameter. It is never saved in the connection or project." + "The token is added to each request to this address as its token parameter. With no expiry it is used until the server refuses it." )) .small() .weak(), ); ui.end_row(); } + } + if !st.auth.is_empty() { + if st.auth != "arcgis" { + let basic = st.auth == "basic"; + let input_label = ui.label(if basic { tl!("Password") } else { tl!("Key") }); + let field = if basic { &mut st.password } else { &mut st.api_key }; + changed |= ui + .add(egui::TextEdit::singleline(field).password(true).desired_width(360.0)) + .labelled_by(input_label.id) + .changed(); + ui.end_row(); + if st.auth == "query" { + ui.label(""); + ui.label( + egui::RichText::new(tl!( + "The key is added to each request to this address as this parameter. It is never saved in the connection or project." + )) + .small() + .weak(), + ); + ui.end_row(); + } + } if store { ui.label(""); changed |= ui.checkbox(&mut st.remember, tl!("Remember on this computer")).changed(); ui.end_row(); } - if insecure(st) { + if insecure(st) && !generating_form(st) { ui.label(""); changed |= ui.checkbox(&mut st.allow_insecure, tl!("Send the login over unencrypted http://")).changed(); ui.end_row(); @@ -1083,6 +1421,7 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { } } }); + }); // A credential-source switch never carries opt-in to the newly chosen source. if source_changed || !(database || service && !st.auth.is_empty() && store || st.kind == "s3" && st.credentials == "keys") { st.remember = false; @@ -1122,6 +1461,19 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { Err(e) => app.dialogs.error = Some(translated(&e)), } } + // A token being generated: said, with a way to stop it (closing the form stops it too). + let mut stop = false; + if app.ui.connection.generating.is_some() { + ui.horizontal(|ui| { + ui.spinner(); + ui.label(tl!("Generating token…")); + stop = ui.button(tl!("Cancel token request")).clicked(); + }); + } + if stop { + cancel_generating(app); + app.dialogs.info = Some(tl!("The token request was cancelled.").into()); + } // A login that worked but couldn't do all it was asked: said until closed (or edited again). if let Some(warning) = app.ui.connection.warned.clone() { ui.add_space(4.0); @@ -1135,7 +1487,7 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { .inner; } let st = &app.ui.connection; - let testing = st.test.is_some(); + let testing = st.test.is_some() || st.generating.is_some(); let unavailable = database && cfg!(target_arch = "wasm32"); let blocked = test_blocked(app); let clicked = ui @@ -1152,7 +1504,7 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { } else { response }; - if testing { + if st.test.is_some() { ui.spinner(); } response.clicked() @@ -1161,9 +1513,12 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { if clicked { start_test(app); } - let close = crate::dialogs::buttons(app, ui, "Save", save); + let was_generating = app.ui.connection.generating.is_some(); + let close = crate::dialogs::buttons_enabled(app, ui, "Save", !was_generating, save); + // A Save that started a token request stays, to say how it goes (`poll_generating` lands it). + let started = !was_generating && app.ui.connection.generating.is_some(); // A warning to read keeps it open. - let close = close && app.ui.connection.warned.is_none(); + let close = close && app.ui.connection.warned.is_none() && !started; if close && app.ui.connection.test.take().is_some() { app.dialogs.info = None; app.dialogs.error = None; diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index f538532fa3..5216c93841 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -2252,12 +2252,24 @@ pub(crate) fn text_box(text: &mut String) -> egui::TextEdit<'_> { /// OK/Cancel row; returns true when the dialog should close. pub(crate) fn buttons(app: &mut RustGisApp, ui: &mut egui::Ui, ok: &str, f: impl FnOnce(&mut RustGisApp) -> Result<(), String>) -> bool { + buttons_enabled(app, ui, ok, true, f) +} + +/// OK/Cancel row whose submit can be disabled without disabling Cancel. +pub(crate) fn buttons_enabled( + app: &mut RustGisApp, + ui: &mut egui::Ui, + ok: &str, + enabled: bool, + f: impl FnOnce(&mut RustGisApp) -> Result<(), String>, +) -> bool { let mut close = false; ui.add_space(4.0); ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| { let t = Tokens::get(ui.ctx()); let enter = ui.input(|i| i.key_pressed(egui::Key::Enter) && !i.modifiers.shift); - if crate::widgets::dialog_button(ui, tl!(ok), Some(t.accent), vec2(84.0, 28.0)).clicked() || enter { + let submit = ui.add_enabled_ui(enabled, |ui| crate::widgets::dialog_button(ui, tl!(ok), Some(t.accent), vec2(84.0, 28.0))).inner; + if enabled && (submit.clicked() || enter) { match f(app) { Ok(()) => close = true, Err(e) => app.dialogs.error = Some(e), diff --git a/crates/ui-egui/tests/remembered_service/mod.rs b/crates/ui-egui/tests/remembered_service/mod.rs index dbd3d66bf7..545d5ddab8 100644 --- a/crates/ui-egui/tests/remembered_service/mod.rs +++ b/crates/ui-egui/tests/remembered_service/mod.rs @@ -322,3 +322,324 @@ fn service_without_a_store_hides_remember_and_marks_saved_login_unavailable() { assert!(h.get_by_role_and_label(Role::Button, "Forget Saved Login").accesskit_node().is_disabled()); assert!(h.state().session.connections.find(id).unwrap().remembered, "drawing never tries to read the saved login"); } + +mod arcgis_token { + use super::*; + use rustgis_engine::render::rustgis_tiles::arcgis_token::{self, Credential, Done}; + use rustgis_engine::render::rustgis_tiles::cache::Still; + use rustgis_engine::render::rustgis_tiles::logins; + use std::collections::BTreeMap; + use std::sync::{LazyLock, Mutex, Once}; + + const TOKEN: &str = "pasted-arcgis-widget-token-sentinel"; + const PASSWORD: &str = "generate-widget-password-sentinel"; + const FUTURE: i64 = 4_102_444_800_000; + + type Handler = Arc; + static HANDLERS: LazyLock>> = LazyLock::new(Mutex::default); + + /// A token server on this computer that holds each request until the test answers it. Each + /// test has its own endpoint, so none shares another's requests. + #[derive(Clone, Default)] + struct Server { + forms: Arc>>, + held: Arc>>, + } + + impl Server { + fn at(endpoint: &str) -> Server { + static ONCE: Once = Once::new(); + ONCE.call_once(|| { + arcgis_token::set_generator(|endpoint, body, still, done| { + let handler = HANDLERS.lock().unwrap().get(&endpoint).cloned(); + match handler { + Some(handler) => handler(body.expose().to_string(), still, done), + None => done(Err("no fake answers this endpoint".into())), + } + }); + }); + let server = Server::default(); + let state = server.clone(); + let handler: Handler = Arc::new(move |form, still, done| { + state.forms.lock().unwrap().push(form); + state.held.lock().unwrap().push((done, still)); + }); + HANDLERS.lock().unwrap().insert(endpoint.to_string(), handler); + server + } + + fn forms(&self) -> Vec { + self.forms.lock().unwrap().clone() + } + + fn take(&self) -> (Done, Still) { + self.held.lock().unwrap().remove(0) + } + } + + fn form(h: &mut Harness, address: &str) { + h.state_mut().run("ui.connection", json!({"kind":"arcgis"})).unwrap(); + settle(h); + type_field(h, "Name", "ArcGIS token login", false); + type_field(h, "Address", address, false); + h.get_by_role_and_label(Role::ComboBox, "Method").click(); + settle(h); + h.get_by_label("ArcGIS token").click(); + settle(h); + type_field(h, "User", "alice", false); + } + + fn generate_mode(h: &mut Harness, endpoint: &str) { + h.get_by_label("Username and password").click(); + settle(h); + type_field(h, "Token endpoint", endpoint, false); + type_field(h, "Password", PASSWORD, true); + } + + fn assert_clean(h: &Harness) { + let app = h.state(); + let st = &app.ui.connection; + assert!(st.password.is_empty() && st.token.is_empty() && st.token_expires.is_empty() && !st.remember); + let visible = format!( + "{}\n{:?}\n{}\n{}\n{}", + app.session.connections.to_json(), + app.session.notices, + app.dialogs.error.as_deref().unwrap_or(""), + app.dialogs.info.as_deref().unwrap_or(""), + st.warned.as_deref().unwrap_or("") + ); + for private in [TOKEN, PASSWORD, "rustgis-login"] { + assert!(!visible.contains(private), "{private} is visible: {visible}"); + } + for entry in app.session.log.entries().filter(|entry| entry.command.starts_with("connection.")) { + let params = entry.params.to_string(); + assert!(!params.contains(TOKEN) && !params.contains(PASSWORD), "{} kept a secret: {params}", entry.command); + } + } + + #[test] + fn arcgis_token_form_pastes_a_masked_token_and_remembers_only_when_asked() { + let store = Arc::new(MemorySecrets::new()); + let mut h = with_store(Some(store.clone())); + form(&mut h, "https://widget-arcgis-paste.example/arcgis/rest/services"); + // Paste is the way in; a token and its expiry are masked and plain; Remember is an opt-in. + h.get_by_label("Paste token"); + assert!(!h.state().ui.connection.token_generate && !h.state().ui.connection.remember); + assert!(h.query_by_label("Token endpoint").is_none()); + type_field(&mut h, "Token", TOKEN, true); + type_field(&mut h, "Expires at (Unix milliseconds)", "soon", false); + save(&mut h); + // An expiry that isn't a time is said, not dropped; nothing is saved. + assert!(h.state().dialogs.error.as_deref().unwrap().contains("Unix time in milliseconds")); + assert!(h.state().session.connections.find("ArcGIS token login").is_none()); + h.state_mut().ui.connection.token_expires = FUTURE.to_string(); + settle(&mut h); + h.get_by_label("Remember on this computer").click(); + settle(&mut h); + save(&mut h); + assert_eq!(h.state().dialogs.open, None); + let c = h.state().session.connections.find("ArcGIS token login").unwrap().clone(); + assert_eq!((c.auth.as_str(), c.user.as_str(), c.remembered), ("arcgis", "alice", true)); + assert!(web::logged_in(&h.state().session, &c)); + let saved = store.load(&format!("connection/{}", c.id)).unwrap().unwrap(); + assert!(saved.expose().contains(TOKEN) && saved.expose().contains(&FUTURE.to_string())); + assert_clean(&h); + } + + #[test] + fn arcgis_token_invalid_replacement_keeps_the_remembered_definition_and_login() { + if crate::in_own_process( + "remembered_service::arcgis_token::arcgis_token_invalid_replacement_keeps_the_remembered_definition_and_login", + ) { + return; + } + let store = Arc::new(MemorySecrets::new()); + let mut h = with_store(Some(store.clone())); + let id = h + .state_mut() + .run( + "connection.new", + json!({ + "kind":"arcgis", "name":"Existing ArcGIS login", + "address":"https://widget-arcgis-presave.example/arcgis/rest/services", + "auth":"arcgis", "user":"alice" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + h.state_mut().run("connection.login", json!({"id":id, "token":TOKEN, "remember":true})).unwrap(); + let c = h.state().session.connections.find(&id).unwrap().clone(); + let definition = h.state().session.connections.to_json(); + let account = format!("connection/{id}"); + let saved = store.load(&account).unwrap().unwrap().expose().to_string(); + let bound = web::request(&mut h.state_mut().session, Some(&id), &c.address).unwrap(); + + for (generate, password) in [(false, String::new()), (true, "x".repeat(logins::MAX_VALUE + 1)), (true, "invalid\npassword".into())] + { + h.state_mut().run("ui.connection", json!({"id":id})).unwrap(); + { + let st = &mut h.state_mut().ui.connection; + // Either edit changes login identity and would discard the old remembered login. + st.user = "bob".into(); + st.address = "https://widget-arcgis-presave-changed.example/arcgis/rest/services".into(); + st.token_generate = generate; + if generate { + st.password = password; + st.token_endpoint = "https://widget-arcgis-presave-changed.example/arcgis/tokens/generateToken".into(); + } else { + st.token = "replacement-widget-token".into(); + st.token_expires = "1".into(); // Positive, but already expired. + } + } + settle(&mut h); + save(&mut h); + assert_eq!( + h.state().dialogs.error.as_deref(), + Some(if generate { rustgis_engine::service_tokens::BAD_PASSWORD } else { arcgis_token::EXPIRED }) + ); + assert_eq!(h.state().dialogs.open, Some("editConnection")); + assert!(h.state().ui.connection.generating.is_none()); + assert_eq!(h.state().session.connections.to_json(), definition); + assert_eq!(store.load(&account).unwrap().unwrap().expose(), saved); + assert!(web::logged_in(&h.state().session, &c)); + assert_eq!(logins::check(&bound), Ok(()), "the original active binding survives"); + } + } + + #[test] + fn arcgis_token_form_generates_in_the_background_and_never_holds_the_password() { + if crate::in_own_process( + "remembered_service::arcgis_token::arcgis_token_form_generates_in_the_background_and_never_holds_the_password", + ) { + return; + } + let endpoint = "https://widget-arcgis-generate.example/arcgis/tokens/generateToken"; + let server = Server::at(endpoint); + let store = Arc::new(MemorySecrets::new()); + let mut h = with_store(Some(store.clone())); + form(&mut h, "https://widget-arcgis-generate.example/arcgis/rest/services"); + generate_mode(&mut h, endpoint); + assert!(h.query_by_label("Expires at (Unix milliseconds)").is_none(), "pasting is another mode"); + save(&mut h); + // The form stays to say how it goes; the password left it with the request. + assert!(h.state().dialogs.open.is_some()); + assert!(h.state().ui.connection.generating.is_some()); + assert!(h.state().ui.connection.password.is_empty()); + h.get_by_role_and_label(Role::Button, "Cancel token request"); + assert_eq!(server.forms(), [format!("f=json&username=alice&password={PASSWORD}&client=requestip&expiration=60")]); + // No second Save while it is under way. + assert!(h.get_by_role_and_label(Role::Button, "Save").accesskit_node().is_disabled()); + assert_eq!(server.forms().len(), 1); + let c = h.state().session.connections.find("ArcGIS token login").unwrap().clone(); + assert!(!web::logged_in(&h.state().session, &c)); + + let (done, _) = server.take(); + done(Ok(Credential::new("generated-widget-token", Some(FUTURE)).ok().unwrap())); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); + assert!(h.state().ui.connection.generating.is_none()); + assert!(web::logged_in(&h.state().session, &c)); + // Remember was off: only this session has the token. + assert_eq!(store.load(&format!("connection/{}", c.id)).unwrap(), None); + assert!(!h.state().session.connections.find(&c.id).unwrap().remembered); + assert_clean(&h); + } + + #[test] + fn arcgis_token_form_says_what_is_missing_before_saving_or_sending() { + let endpoint = "https://widget-arcgis-missing.example/arcgis/tokens/generateToken"; + let server = Server::at(endpoint); + let mut h = with_store(Some(Arc::new(MemorySecrets::new()))); + form(&mut h, "https://widget-arcgis-missing.example/arcgis/rest/services"); + h.get_by_label("Username and password").click(); + settle(&mut h); + type_field(&mut h, "Password", PASSWORD, true); + save(&mut h); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("token endpoint")); + type_field(&mut h, "Token endpoint", "http://widget-arcgis-missing.example/arcgis/tokens/generateToken", false); + save(&mut h); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("https://"), "{:?}", h.state().dialogs.error); + h.state_mut().ui.connection.token_endpoint = endpoint.into(); + h.state_mut().ui.connection.user.clear(); + settle(&mut h); + save(&mut h); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("user")); + assert!(h.state().session.connections.find("ArcGIS token login").is_none(), "nothing was saved"); + assert!(server.forms().is_empty(), "nothing was sent"); + // A Test of a token made from a password belongs to a saved connection. + h.state_mut().ui.connection.user = "alice".into(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Test").click(); + settle(&mut h); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("Save the connection")); + assert!(h.state().ui.connection.password.is_empty()); + assert!(server.forms().is_empty()); + } + + #[test] + fn arcgis_token_test_with_an_expiry_but_no_token_says_so_instead_of_using_the_saved_login() { + let mut h = with_store(Some(Arc::new(MemorySecrets::new()))); + let id = h + .state_mut() + .run( + "connection.new", + json!({"kind":"arcgis","name":"Orphan expiry","address":"https://widget-arcgis-orphan.example/arcgis/rest/services","auth":"arcgis","user":"alice"}), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + h.state_mut().run("connection.login", json!({"id":id,"token":TOKEN})).unwrap(); + h.state_mut().run("ui.connection", json!({"id":id})).unwrap(); + settle(&mut h); + h.state_mut().ui.connection.token_expires = FUTURE.to_string(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Test").click(); + settle(&mut h); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("this expiry is for"), "{:?}", h.state().dialogs.error); + assert!(h.state().ui.connection.test.is_none(), "no test ran with the saved login"); + assert!(h.state().ui.connection.token_expires.is_empty()); + } + + #[test] + fn arcgis_token_form_cancels_its_request_and_closing_it_does_too() { + if crate::in_own_process("remembered_service::arcgis_token::arcgis_token_form_cancels_its_request_and_closing_it_does_too") { + return; + } + let endpoint = "https://widget-arcgis-cancel.example/arcgis/tokens/generateToken"; + let server = Server::at(endpoint); + let mut h = with_store(Some(Arc::new(MemorySecrets::new()))); + form(&mut h, "https://widget-arcgis-cancel.example/arcgis/rest/services"); + generate_mode(&mut h, endpoint); + save(&mut h); + let c = h.state().session.connections.find("ArcGIS token login").unwrap().clone(); + let (done, still) = server.take(); + assert!(still()); + h.get_by_role_and_label(Role::Button, "Cancel token request").click(); + settle(&mut h); + assert!(!still(), "Cancel stops the request"); + assert!(h.state().ui.connection.generating.is_none()); + assert!(h.state().dialogs.open.is_some(), "the form stays to try again"); + // What the server then says lands nowhere. + done(Ok(Credential::new("late-widget-token", Some(FUTURE)).ok().unwrap())); + settle(&mut h); + assert!(!web::logged_in(&h.state().session, &c)); + + // Closing the form stops a request too, and leaves an earlier login alone. + h.state_mut().run("connection.login", json!({"id":c.id,"token":"earlier-widget-token"})).unwrap(); + h.state_mut().ui.connection.password = PASSWORD.into(); + h.state_mut().ui.connection.token_generate = true; + settle(&mut h); + save(&mut h); + let (_, still) = server.take(); + assert!(still()); + h.get_by_role_and_label(Role::Button, "Cancel").click(); + settle(&mut h); + assert!(!still(), "closing the form stops the request"); + assert_eq!(h.state().dialogs.open, None); + assert!(web::logged_in(&h.state().session, &c), "the earlier login stays"); + assert_clean(&h); + } +} diff --git a/docs/cli.md b/docs/cli.md index 907e5a97cf..16a0c3f6ee 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -89,6 +89,10 @@ If saving fails, the session login still works and a warning says it was not rem session until an explicit `connection.login`. See [Web-service logins](user-guide/data.md#web-service-logins) for exact-origin matching and HTTP confirmation. +An ArcGIS token is pasted with `connection.login {"id":"c1","token":"…"}`. A token made from a user +name and password (`connection.generateToken`) lands only when `connection.tokenStatus` asks, so it +suits a session that keeps asking, such as Python or the app, more than one `--cmd` per run. + Nothing waits on `run` the way a window does, so a long run takes the bounds the app's panes give it on a thread: a trend over a data cube (`cube.trend`) may compare up to 20 billion pairs, ten times what the Script pane allows, and a trend's or a summary's STAC search up to 10,000 items, diff --git a/docs/python.md b/docs/python.md index c480314816..91f4a602fb 100644 --- a/docs/python.md +++ b/docs/python.md @@ -277,6 +277,11 @@ login usable. `connection.logout` retains the saved login but blocks automatic r rest of the session until an explicit `connection.login`. See [Web-service logins](user-guide/data.md#web-service-logins) for matching and HTTP confirmation. +An ArcGIS-token connection (`auth="arcgis"`) takes `s.run("connection.login", id="c1", token="…")`, or +`s.run("connection.generateToken", id="c1", tokenEndpoint="https://host/arcgis/tokens/generateToken", password="…")` +and then `s.run("connection.tokenStatus", id="c1")` until its `pending` is false (`connection.cancelToken` +stops it); the password is sent in that one request and kept nowhere. + ## Data in and out ```python diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index cf1c08ddfa..ca7904534c 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1155,6 +1155,47 @@ or the browser's `crypto.getRandomValues` in the web app. If that source is miss RustGIS refuses the login rather than using a predictable fallback. A failed replacement leaves the previous active login usable. +**ArcGIS token** (`"auth": "arcgis"`, on any web-service kind: an ArcGIS-hosted WMS or WMTS needs it +too) logs in to ArcGIS Server or ArcGIS Enterprise with a token, sent as each request's one `token` +parameter (a parameter of that name in an address is replaced, whatever its case or escaping, and +an explicit connection token outranks a portal sign-in's, which is then neither sent nor renewed for +that request). The connection keeps the method and an optional user; the token is given in one of +two ways. + +- **Paste token**: `connection.login {"id":"c1","token":"…","expiresAt":1893456000000}`. The token is + ASCII letters, digits and `- _ . ~ + / =`, at most 4096 bytes. `expiresAt` is optional, in Unix + milliseconds and in the future; without it the token is used until the server refuses it, and + nothing is renewed or guessed. **Test** with a pasted token uses a temporary login that ends with + the test: the saved connection, its session login and its saved login are not used or replaced. +- **Username and password**: `connection.generateToken {"id":"c1","tokenEndpoint":"https://host/arcgis/tokens/generateToken","password":"…"}` + asks the server's `generateToken` for a token for the connection's user (`client=requestip`, 60 + minutes) and answers `{"id","pending":true}`. The endpoint must be an `https://` address at the + connection's own origin, with no user information, query or fragment, and is never taken from a + service's answer; a federated portal's sign-in uses the portal sign-in instead. The request runs + in the background on the desktop and in the web app (30 seconds at most, answers up to 1 MiB, no + redirects, cookies or other login). `connection.tokenStatus {"id":"c1"}` answers + `{"pending":true}` until it lands, then `connection.login`'s result with `"pending":false`; + `connection.cancelToken {"id":"c1"}` stops it, leaving any earlier login. Only the caller that + started it can ask or cancel, and a replaced project, a changed login (user, method or origin), a + logout, a removal, a pasted token, a closed form, or logging in or out of another connection ends + it. Failures say a fixed sentence, never the + server's text. The password is in that one request and is kept nowhere: not in the form once sent, + the connection, the session, the saved login, History, recordings or notices. + **Test** requires a saved connection, generates through the same commands with Remember off, and + leaves the resulting session-only token logged in before testing the service. Cancelling a native + request never waits for the worker; one blocking network operation can finish at its 30-second + deadline before the worker notices cancellation. + +With **Remember on this computer** (`"remember": true`, on `connection.login` or +`connection.generateToken`) the token and its expiry, never a password, go to the credential store as +the same versioned login described above. A saved or session token that has expired is not used: the +login is revoked, cached authenticated responses are not read, nothing falls back to anonymous +access or a portal sign-in, and the connection asks to log in again (`connection.login` with a new +token, or `connection.generateToken`). An expired saved token is neither deleted nor rewritten until +it is forgotten or replaced, and is not read again that session. A token is never renewed +automatically, since that would need the password. From the command line and Python, poll +`connection.tokenStatus` until `pending` is false. + A login is sent only to the connection's origin: the same scheme, host and port, for its capabilities, listings, tiles, feature pages and refreshes. On the desktop a redirect within that origin keeps the login, and a redirect to another origin is followed without it, never getting