diff --git a/crates/devstack-hub/src/upstreams.rs b/crates/devstack-hub/src/upstreams.rs index 6a671cc8..1dbb515f 100644 --- a/crates/devstack-hub/src/upstreams.rs +++ b/crates/devstack-hub/src/upstreams.rs @@ -1020,7 +1020,7 @@ impl Upstreams { /// The bearer token for a call to `name` now: a static one, or the /// login's access token, refreshed when it is about to expire (or when /// `stale`, after the upstream refused it). `None`: it needs none, or it - /// is logged out. + /// is logged out, which a refresh the server refuses for good makes it. pub fn bearer( &self, http: &ureq::Agent, @@ -1059,7 +1059,17 @@ impl Upstreams { if let Some(c) = &client_secret { form.push(("client_secret", c)); } - let t = token_request(http, &token_endpoint, &form)?; + let t = match token_request(http, &token_endpoint, &form) { + Ok(t) => t, + // The login is dead: dropped, so the dashboard + // says logged out and the agent is sent there. + Err(Refused { gone: true, .. }) => { + self.secrets.delete(name)?; + self.logged_in_anew(name); + return Ok(None); + } + Err(r) => return Err(r.into()), + }; let access = t.access_token.clone(); self.put_secret( name, @@ -1507,11 +1517,25 @@ struct Tokens { expires_in: Option, } +/// A token endpoint's answer that held no tokens. +struct Refused { + /// The grant or the client is gone for good (RFC 6749, 5.2): only a new + /// login helps. + gone: bool, + why: String, +} + +impl From for String { + fn from(r: Refused) -> String { + r.why + } +} + fn token_request( http: &ureq::Agent, endpoint: &str, form: &[(&str, &str)], -) -> Result { +) -> Result { let body: Vec = form .iter() .map(|(k, v)| format!("{k}={}", encode(v))) @@ -1521,7 +1545,10 @@ fn token_request( .header("Content-Type", "application/x-www-form-urlencoded") .header("Accept", "application/json") .send(body.join("&")) - .map_err(|e| format!("{endpoint}: {e}"))?; + .map_err(|e| Refused { + gone: false, + why: format!("{endpoint}: {e}"), + })?; let text = r.body_mut().read_to_string().unwrap_or_default(); // The server's error, never the request: it held the code. GitHub // answers one with `200`. @@ -1529,10 +1556,22 @@ fn token_request( .ok() .and_then(|v| v["error"].as_str().map(str::to_string)); if r.status() != 200 || error.is_some() { + let gone = error.as_deref().is_some_and(|e| { + matches!( + e, + "invalid_grant" | "invalid_client" | "unauthorized_client" + ) + }); let why = error.unwrap_or_else(|| r.status().to_string()); - return Err(format!("{endpoint} refused: {why}")); + return Err(Refused { + gone, + why: format!("{endpoint} refused: {why}"), + }); } - serde_json::from_str(&text).map_err(|e| format!("{endpoint}: {e}")) + serde_json::from_str(&text).map_err(|e| Refused { + gone: false, + why: format!("{endpoint}: {e}"), + }) } /// The agent a tool call goes out with: a tool may work for minutes. @@ -1707,6 +1746,9 @@ mod tests { 200, serde_json::json!({ "error": "incorrect_client_credentials" }), ), + "/auth/token" if body.contains("refresh_token=revoked") => { + (400, serde_json::json!({ "error": "invalid_grant" })) + } "/auth/token" if body.contains("grant_type=refresh_token") => ( 200, serde_json::json!({ "access_token": "access-2", "refresh_token": "refresh-2", "expires_in": 3600 }), @@ -1795,6 +1837,50 @@ mod tests { let _ = ups.remove(&name); } + #[test] + fn a_refused_refresh_logs_out() { + let (base, _) = fake_server(); + let d = tempfile::tempdir().unwrap(); + let ups = Upstreams::for_test(d.path().to_path_buf(), Vec::new); + let name = format!("r{}", std::process::id()); + ups.put(Upstream { + name: name.clone(), + url: format!("{base}/mcp"), + auth: Auth::Oauth { + client_id: None, + client_secret: None, + scopes: Vec::new(), + }, + ..Default::default() + }) + .unwrap(); + let login = |refresh: &str| Secret::Oauth { + access_token: "old".into(), + refresh_token: Some(refresh.into()), + expires_at: Some(0), + client_id: "hub-client".into(), + client_secret: None, + token_endpoint: format!("{base}/auth/token"), + resource: format!("{base}/mcp"), + }; + let http = http(); + + // The token endpoint is away: the login stays, the call fails. + let mut away = login("revoked"); + if let Secret::Oauth { token_endpoint, .. } = &mut away { + *token_endpoint = "http://127.0.0.1:1/auth/token".into(); + } + ups.put_secret(&name, &away).unwrap(); + assert!(ups.bearer(&http, &name, false).is_err()); + assert!(ups.views().unwrap()[0].logged_in); + + // The refresh token is revoked: logged out, and shown so. + ups.put_secret(&name, &login("revoked")).unwrap(); + assert_eq!(ups.bearer(&http, &name, false).unwrap(), None); + assert!(!ups.views().unwrap()[0].logged_in); + let _ = ups.remove(&name); + } + #[test] fn a_client_secret_goes_to_the_token_endpoint_only_and_is_never_shown() { let (base, seen) = fake_server(); diff --git a/docs/dashboard-api.md b/docs/dashboard-api.md index 132d8591..6c5a8a4c 100644 --- a/docs/dashboard-api.md +++ b/docs/dashboard-api.md @@ -112,7 +112,7 @@ and `Referrer-Policy: no-referrer`. The dashboard has no inline script. | `POST /hub/upstreams/{name}/token {token}` | cookie + Origin | the static token of an upstream with `auth.kind = "token"` and no `env`, into the keychain: `204` | | `POST /hub/upstreams/{name}/login` | cookie + Origin | start its OAuth login: the authorization server found from the upstream (RFC 9728, RFC 8414), the hub registered where the server allows it (RFC 7591), else the configured client id (with its client secret, sent to the token endpoint only), PKCE with S256, the upstream as `resource`: `{url}` to open in the browser; `502 {"error"}` when the server cannot be found or refuses | | `GET /hub/upstreams/tools` | user token (or cookie) | the upstreams' tools as `devstack mcp` offers them: `{tools: [Tool]}`, each named `__`, the allowlist applied, the description saying which upstream. One that cannot be asked (logged out, refused, unreachable) keeps the tools it listed last (kept in `upstream-tools.json` beside `upstreams.toml`), its description saying why; one never listed shows `__login`. Asked at most once a minute per upstream | -| `POST /hub/upstreams/call {name, arguments}` | user token (or cookie + Origin) | call the tool `__` on its upstream with the hub's credential (refreshed once when refused): the upstream's `CallToolResult`, or one with `isError` and why (not logged in, with the dashboard's page; not allowed; unreachable). The credential never leaves the hub | +| `POST /hub/upstreams/call {name, arguments}` | user token (or cookie + Origin) | call the tool `__` on its upstream with the hub's credential (refreshed once when refused; a refresh the authorization server refuses for good, such as a revoked refresh token, logs the upstream out): the upstream's `CallToolResult`, or one with `isError` and why (not logged in, with the dashboard's page; not allowed; unreachable). The credential never leaves the hub | | `POST /hub/upstreams/{name}/logout` | cookie + Origin | its credential goes: `204` | | `GET /oauth/upstream/callback?code&state` | none | where the authorization server sends the browser back: a navigation from another site, so without a session; the one-time `state` of a login the hub started (within ten minutes) admits it. Exchanges the code for tokens, kept in the keychain; a page that says whether it worked | | `GET /hub/pool` | cookie | `PoolOverview`: this machine's pool, read from the coordinator with its member certificate (ADR-0032); `{"joined":false}` in no pool; `502 {"error"}` when the coordinator does not answer |