authoravatarclover caruso<git@paperclover.net>2026-10-23 15:05:55-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-24 19:01:23-07:00
log7fc742ba86e4d7175659d4a5c5fade792c175d96
treeee11ef8abfc4863ee498dbc4bcd250f9d7121dce
parent78daa503d015fc5d1b70bd8a1927000f47ceb64b
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Keep MCP access tokens valid while agents use them

Knife passes each agent session a fixed bearer header, so tokens that expired an hour after issue cut long-running agents off from the Computer MCP. Access tokens now expire after a week without use; clients are still told to renew hourly, which keeps refresh tokens rotating. Assisted-by: claude-opus-5.5

1 files changed, 36 insertions(+), 2 deletions(-)

dashboard/src/mcp.rs+36-2
...@@ -21,6 +21,10 @@ const CATALOGS: &[(&str, &str, &[&str])] = &[...@@ -21,6 +21,10 @@ const CATALOGS: &[(&str, &str, &[&str])] = &[
21 ),21 ),
22];22];
2323
24/// Agents keep the access token their session started with, so it lives while used rather
25/// than for the hour clients are told to renew within.
26const ACCESS_IDLE: i64 = 7 * 86400;
27
24pub struct Store {28pub struct Store {
25 pub(crate) db: Mutex<Connection>,29 pub(crate) db: Mutex<Connection>,
26 pub origin: url::Url,30 pub origin: url::Url,
...@@ -211,7 +215,7 @@ impl Store {...@@ -211,7 +215,7 @@ impl Store {
211 db,215 db,
212 &format!("access:{}", hash(&access)),216 &format!("access:{}", hash(&access)),
213 &json!({"grant":grant["id"],"resource":grant["resource"]}),217 &json!({"grant":grant["id"],"resource":grant["resource"]}),
214 3600,218 ACCESS_IDLE,
215 )?;219 )?;
216 let mut response = json!({"access_token":access,"token_type":"Bearer","expires_in":3600,"scope":array(&grant["scopes"]).iter().map(string).collect::<Vec<_>>().join(" ")});220 let mut response = json!({"access_token":access,"token_type":"Bearer","expires_in":3600,"scope":array(&grant["scopes"]).iter().map(string).collect::<Vec<_>>().join(" ")});
217 if array(&grant["scopes"])221 if array(&grant["scopes"])
...@@ -237,11 +241,17 @@ impl Store {...@@ -237,11 +241,17 @@ impl Store {
237 .filter(|s| !s.is_empty() && s.len() <= 256)241 .filter(|s| !s.is_empty() && s.len() <= 256)
238 .ok_or_else(|| Error::new(401, "invalid_token"))?;242 .ok_or_else(|| Error::new(401, "invalid_token"))?;
239 let db = self.db.lock().unwrap();243 let db = self.db.lock().unwrap();
240 let access = get(&db, &format!("access:{}", hash(token)))?;244 let key = format!("access:{}", hash(token));
245 let access = get(&db, &key)?;
241 let grant = get(&db, &format!("grant:{}", string(&access["grant"])))?;246 let grant = get(&db, &format!("grant:{}", string(&access["grant"])))?;
242 if grant.is_null() || access["resource"] != resource || grant["resource"] != resource {247 if grant.is_null() || access["resource"] != resource || grant["resource"] != resource {
243 return Err(Error::new(401, "invalid_token"));248 return Err(Error::new(401, "invalid_token"));
244 }249 }
250 let expires = now() as i64 + ACCESS_IDLE;
251 db.execute(
252 "UPDATE records SET expires=? WHERE key=? AND expires<?",
253 rusqlite::params![expires, key, expires - 3600],
254 )?;
245 Ok(grant)255 Ok(grant)
246 }256 }
247 fn exchange(257 fn exchange(
...@@ -1030,6 +1040,30 @@ mod tests {...@@ -1030,6 +1040,30 @@ mod tests {
1030 assert!(!get(&db, &format!("{kind}:other")).unwrap().is_null());1040 assert!(!get(&db, &format!("{kind}:other")).unwrap().is_null());
1031 }1041 }
1032 }1042 }
1043 #[test]
1044 fn access_tokens_live_while_used_and_lapse_when_idle() {
1045 let fixture = Fixture::new();
1046 let resource = fixture.store.resource("computer");
1047 let grant = json!({"id":"live","resource":resource,"scopes":["computer:read"]});
1048 let db = fixture.store.db.lock().unwrap();
1049 put(&db, "grant:live", &grant, 0).unwrap();
1050 let tokens = fixture.store.issue(&db, &grant).unwrap();
1051 assert_eq!(tokens["expires_in"], 3600);
1052 let key = format!("access:{}", hash(string(&tokens["access_token"])));
1053 let expires = |db: &Connection| -> i64 {
1054 db.query_row("SELECT expires FROM records WHERE key=?", [&key], |r| r.get(0)).unwrap()
1055 };
1056 let set = |db: &Connection, at: i64| {
1057 db.execute("UPDATE records SET expires=? WHERE key=?", rusqlite::params![at, key]).unwrap();
1058 };
1059 set(&db, now() as i64 + 60);
1060 drop(db);
1061 let headers = bearer(string(&tokens["access_token"]));
1062 assert!(fixture.store.authenticate(&headers, &resource).is_ok());
1063 assert!(expires(&fixture.store.db.lock().unwrap()) >= now() as i64 + ACCESS_IDLE - 5);
1064 set(&fixture.store.db.lock().unwrap(), now() as i64 - 1);
1065 assert!(fixture.store.authenticate(&headers, &resource).is_err());
1066 }
1033 #[tokio::test]1067 #[tokio::test]
1034 async fn authorization_restricts_issuer_scopes_to_the_requested_resource() {1068 async fn authorization_restricts_issuer_scopes_to_the_requested_resource() {
1035 let fixture = Fixture::new();1069 let fixture = Fixture::new();