Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
//! Generic OIDC Device Authorization Grant provider.
//!
//! Lets a developer sign the ai-memory CLI in against any OIDC issuer (e.g.
//! Keycloak) with the device-authorization grant, so headless lifecycle hooks
//! can POST to the server's `/hook` and `/handoff` routes carrying a
//! **per-developer** JWT instead of a shared static token. The stored token is
//! refreshed on demand from the hook fast-path.
//!
//! Storage uses the shared [`crate::stored_token`] core: the same shared
//! `auth.json` file as [`crate::openai_oauth`], under the `"oidc"` key, with
//! `type: "oauth"` so the shared loader accepts it. The server side is
//! unchanged — its auth layer already validates a Keycloak JWT on the hook
//! routes (requiring the `mcp:read` realm role).
use std::path::Path;
use secrecy::{ExposeSecret as _, SecretString};
use serde::{Deserialize, Serialize};
use crate::auth_file::now_ms;
use crate::error::{LlmError, LlmResult};
use crate::response::{provider_error_body, response_json_limited};
use crate::stored_token::{
CredentialEndpoint, StoredOAuthToken, credential_endpoint, refresh_grant,
};
/// Default device-flow scopes: `openid` (ID token), `profile` (so the access
/// token carries `preferred_username` → `X-Memory-Actor-User` on the server),
/// and `offline_access` (refresh token). The `mcp:read` authorization the
/// server requires is a **realm role** on the user, not a scope.
pub const OIDC_DEFAULT_SCOPE: &str = "openid profile offline_access";
/// OIDC-specific fields persisted next to the shared token triple.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OidcExtras {
/// OIDC issuer this token was minted by (kept for `status` display).
pub issuer: String,
/// Public client id used for the device + refresh grants.
pub client_id: String,
/// Token endpoint resolved at login, so refresh needs no re-discovery.
pub token_endpoint: String,
}
/// Stored OIDC device-grant token.
pub type OidcToken = StoredOAuthToken<OidcExtras>;
impl OidcToken {
/// Build a token from a token-endpoint response. `previous_refresh` is used
/// when a refresh response omits a new `refresh_token` (the old one stays
/// valid), mirroring the OpenAI OAuth provider.
///
/// # Errors
/// Returns [`LlmError::Auth`] when no refresh token is available — the
/// device flow must request the `offline_access` scope.
pub fn from_token_response(
resp: &OidcTokenResponse,
issuer: impl Into<String>,
client_id: impl Into<String>,
token_endpoint: impl Into<String>,
previous_refresh: Option<&str>,
) -> LlmResult<Self> {
let refresh = resp
.refresh_token
.clone()
.or_else(|| previous_refresh.map(str::to_string))
.ok_or_else(|| {
LlmError::Auth(
"OIDC token response carried no refresh_token; request the \
`offline_access` scope and enable it on the Keycloak client"
.to_string(),
)
})?;
Ok(Self {
access: SecretString::from(resp.access_token.clone()),
refresh: SecretString::from(refresh),
expires_at_ms: now_ms()
.saturating_add(resp.expires_in.unwrap_or(300).saturating_mul(1000)),
extra: OidcExtras {
issuer: issuer.into(),
client_id: client_id.into(),
token_endpoint: token_endpoint.into(),
},
})
}
/// Load the OIDC token from the shared `auth.json` file.
///
/// # Errors
/// Returns [`LlmError::Auth`] when the file exists but cannot be read/parsed.
pub fn load(path: &Path) -> LlmResult<Option<Self>> {
Self::load_key(path, "oidc")
}
/// Save the token into the shared `auth.json` file, preserving other keys.
///
/// # Errors
/// Returns [`LlmError::Auth`] when the file cannot be written.
pub fn save(&self, path: &Path) -> LlmResult<()> {
self.save_key(path, "oidc")
}
/// Remove the OIDC entry from the shared `auth.json` file.
///
/// # Errors
/// Returns [`LlmError::Auth`] when the file cannot be updated.
pub fn remove(path: &Path) -> LlmResult<()> {
Self::remove_key(path, "oidc")
}
}
/// The two endpoints we need from `<issuer>/.well-known/openid-configuration`.
#[derive(Debug, Deserialize)]
pub struct OidcDiscovery {
/// Device-authorization endpoint (RFC 8628).
pub device_authorization_endpoint: String,
/// Token endpoint (device-code + refresh grants).
pub token_endpoint: String,
}
/// Device-authorization response (RFC 8628 §3.2).
#[derive(Debug, Deserialize)]
pub struct DeviceAuthorizationResponse {
/// Opaque device verification code, polled at the token endpoint.
pub device_code: String,
/// Short code the user types at the verification URI.
pub user_code: String,
/// URI the user opens to authorize the device.
pub verification_uri: String,
/// Verification URI with the `user_code` pre-filled, when provided.
#[serde(default)]
pub verification_uri_complete: Option<String>,
/// Minimum seconds to wait between polls (default 5).
#[serde(default)]
pub interval: Option<u64>,
/// Lifetime of the device code in seconds.
#[serde(default)]
pub expires_in: Option<u64>,
}
/// Token-endpoint success response.
#[derive(Debug, Deserialize)]
pub struct OidcTokenResponse {
/// Bearer access token for the ai-memory server.
pub access_token: String,
/// Refresh token (present when `offline_access` was granted).
#[serde(default)]
pub refresh_token: Option<String>,
/// Access-token lifetime in seconds.
#[serde(default)]
pub expires_in: Option<u64>,
}
#[derive(Debug, Deserialize)]
struct TokenErrorResponse {
#[serde(default)]
error: Option<String>,
}
/// Outcome of one device-token poll (RFC 8628 §3.5).
#[derive(Debug)]
pub enum PollOutcome {
/// Keep polling at the current interval.
Pending,
/// Back off: increase the interval by 5s, per the spec.
SlowDown,
/// Authorization complete.
Token(Box<OidcTokenResponse>),
/// The user denied the request.
Denied,
/// The device code expired before authorization.
Expired,
/// Any other terminal `error` code from the token endpoint.
Other(String),
}
/// Refuse an OIDC URL that would carry the device code or tokens in clear
/// text. OIDC requires an `https` issuer and OAuth requires TLS at the token
/// endpoint; plain `http` stays usable on loopback for local test issuers.
fn require_tls(what: &str, url: &str) -> LlmResult<CredentialEndpoint> {
credential_endpoint(&format!("OIDC {what}"), url)
}
/// Resolve the device + token endpoints from the issuer's discovery document.
///
/// # Errors
/// Returns [`LlmError`] on transport failure or a non-success status, and
/// [`LlmError::Auth`] when the issuer or a discovered endpoint is not `https`
/// outside loopback.
pub async fn discover(client: &reqwest::Client, issuer: &str) -> LlmResult<OidcDiscovery> {
require_tls("issuer", issuer)?;
let url = format!(
"{}/.well-known/openid-configuration",
issuer.trim_end_matches('/')
);
let endpoint = require_tls("discovery endpoint", &url)?;
let resp = client
.get(endpoint.as_url().clone())
.send()
.await
.map_err(LlmError::from)?;
let status = resp.status();
if !status.is_success() {
let body = provider_error_body(resp).await;
return Err(LlmError::Auth(format!(
"OIDC discovery failed ({status}) for {url}: {body}"
)));
}
let discovery = response_json_limited::<OidcDiscovery>(resp).await?;
require_tls(
"device_authorization_endpoint",
&discovery.device_authorization_endpoint,
)?;
require_tls("token_endpoint", &discovery.token_endpoint)?;
Ok(discovery)
}
/// Start the device-authorization grant for a public client.
///
/// # Errors
/// Returns [`LlmError`] on transport failure or a non-success status.
pub async fn request_device_code(
client: &reqwest::Client,
discovery: &OidcDiscovery,
client_id: &str,
scope: &str,
) -> LlmResult<DeviceAuthorizationResponse> {
let endpoint = require_tls(
"device_authorization_endpoint",
&discovery.device_authorization_endpoint,
)?;
let resp = client
.post(endpoint.as_url().clone())
.form(&[("client_id", client_id), ("scope", scope)])
.send()
.await
.map_err(LlmError::from)?;
let status = resp.status();
if !status.is_success() {
let body = provider_error_body(resp).await;
return Err(LlmError::Auth(format!(
"device authorization request failed ({status}): {body}"
)));
}
response_json_limited::<DeviceAuthorizationResponse>(resp).await
}
/// Poll the token endpoint once with a device code.
///
/// # Errors
/// Returns [`LlmError`] only on transport failure; OAuth `error` codes
/// (`authorization_pending`, `slow_down`, …) are returned as [`PollOutcome`].
pub async fn poll_token_once(
client: &reqwest::Client,
discovery: &OidcDiscovery,
client_id: &str,
device_code: &str,
) -> LlmResult<PollOutcome> {
let endpoint = require_tls("token_endpoint", &discovery.token_endpoint)?;
let resp = client
.post(endpoint.as_url().clone())
.form(&[
("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
("device_code", device_code),
("client_id", client_id),
])
.send()
.await
.map_err(LlmError::from)?;
if resp.status().is_success() {
let token = response_json_limited::<OidcTokenResponse>(resp).await?;
return Ok(PollOutcome::Token(Box::new(token)));
}
let body = provider_error_body(resp).await;
let code = serde_json::from_str::<TokenErrorResponse>(&body)
.ok()
.and_then(|e| e.error);
Ok(match code.as_deref() {
Some("authorization_pending") => PollOutcome::Pending,
Some("slow_down") => PollOutcome::SlowDown,
Some("access_denied") => PollOutcome::Denied,
Some("expired_token") => PollOutcome::Expired,
Some(other) => PollOutcome::Other(other.to_string()),
None => PollOutcome::Other(body),
})
}
/// Exchange the refresh token for a fresh access token.
///
/// # Errors
/// Returns [`LlmError::Auth`] when the refresh grant is rejected (the caller
/// should fall back to the stale token or prompt a re-login).
pub async fn refresh_access_token(token: &OidcToken) -> LlmResult<OidcToken> {
let token_endpoint = require_tls("token_endpoint", &token.extra.token_endpoint)?;
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let token_response = refresh_grant::<OidcTokenResponse>(
&client,
&token_endpoint,
token.refresh.expose_secret(),
&token.extra.client_id,
"OIDC refresh failed",
"Run `ai-memory auth login oidc-device` again.",
)
.await?;
OidcToken::from_token_response(
&token_response,
token.extra.issuer.clone(),
token.extra.client_id.clone(),
token.extra.token_endpoint.clone(),
Some(token.refresh.expose_secret()),
)
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> OidcToken {
OidcToken {
access: SecretString::from("access-x".to_string()),
refresh: SecretString::from("refresh-y".to_string()),
expires_at_ms: now_ms() + 3_600_000,
extra: OidcExtras {
issuer: "https://kc.example.com/realms/ai-memory".to_string(),
client_id: "ai-memory-dev".to_string(),
token_endpoint:
"https://kc.example.com/realms/ai-memory/protocol/openid-connect/token"
.to_string(),
},
}
}
#[test]
fn token_round_trips_through_auth_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("auth.json");
let token = sample();
token.save(&path).unwrap();
let loaded = OidcToken::load(&path).unwrap().unwrap();
assert_eq!(loaded.access.expose_secret(), "access-x");
assert_eq!(loaded.refresh.expose_secret(), "refresh-y");
assert_eq!(loaded.extra.client_id, "ai-memory-dev");
assert_eq!(
loaded.extra.token_endpoint,
"https://kc.example.com/realms/ai-memory/protocol/openid-connect/token"
);
}
#[test]
fn save_preserves_sibling_entries() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("auth.json");
std::fs::write(
&path,
serde_json::to_vec_pretty(&serde_json::json!({
"openai": { "type": "oauth", "access": "a", "refresh": "b", "expires": 1 }
}))
.unwrap(),
)
.unwrap();
sample().save(&path).unwrap();
let value =
serde_json::from_slice::<serde_json::Value>(&std::fs::read(&path).unwrap()).unwrap();
assert!(value.get("openai").is_some(), "sibling entry preserved");
assert_eq!(value["oidc"]["client_id"], "ai-memory-dev");
assert_eq!(value["oidc"]["type"], "oauth");
}
#[test]
fn remove_clears_only_the_oidc_entry() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("auth.json");
sample().save(&path).unwrap();
OidcToken::remove(&path).unwrap();
assert!(OidcToken::load(&path).unwrap().is_none());
}
#[test]
fn needs_refresh_true_when_expired() {
let mut token = sample();
token.expires_at_ms = now_ms();
assert!(token.needs_refresh());
}
#[test]
fn from_response_requires_a_refresh_token() {
let resp = OidcTokenResponse {
access_token: "a".into(),
refresh_token: None,
expires_in: Some(300),
};
let err = OidcToken::from_token_response(&resp, "iss", "cid", "tok", None);
assert!(err.is_err(), "missing refresh token must error");
// ...unless a previous refresh token is carried forward.
let ok = OidcToken::from_token_response(&resp, "iss", "cid", "tok", Some("old-refresh"));
assert_eq!(ok.unwrap().refresh.expose_secret(), "old-refresh");
}
#[test]
fn require_tls_accepts_https_and_loopback_http_only() {
for (url, accepted) in [
("https://idp.example.com/realms/team", true),
("http://localhost:8080/realms/team", true),
("http://LOCALHOST:8080/realms/team", true),
("http://127.0.0.1:8080/realms/team", true),
("http://[::1]:8080/realms/team", true),
("http://idp.example.com/realms/team", false),
("http://localhost.example.com/realms/team", false),
("http://192.168.1.10:8080/realms/team", false),
("ftp://idp.example.com/realms/team", false),
("idp.example.com/realms/team", false),
] {
assert_eq!(require_tls("issuer", url).is_ok(), accepted, "{url}");
}
}
fn discovery_json(device: &str, token: &str) -> serde_json::Value {
serde_json::json!({
"device_authorization_endpoint": device,
"token_endpoint": token,
})
}
#[tokio::test]
async fn discover_refuses_plain_http_issuer_before_any_request() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
// The mock stands in for the remote issuer through an HTTP proxy, so
// without the guard discovery would succeed instead of failing on DNS.
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/realms/team/.well-known/openid-configuration"))
.respond_with(ResponseTemplate::new(200).set_body_json(discovery_json(
"https://idp.example.com/device",
"https://idp.example.com/token",
)))
.mount(&server)
.await;
let client = reqwest::Client::builder()
.proxy(reqwest::Proxy::http(server.uri()).unwrap())
.build()
.unwrap();
let err = discover(&client, "http://idp.example.com/realms/team")
.await
.expect_err("plain-http issuer off loopback must be refused");
assert!(
matches!(&err, LlmError::Auth(msg) if msg.contains("must use https")),
"{err:?}"
);
let requests = server.received_requests().await.unwrap_or_default();
assert!(requests.is_empty(), "discovery must not be fetched");
}
#[tokio::test]
async fn discover_refuses_plain_http_endpoints_from_the_document() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
for (device, token) in [
(
"https://idp.example.com/device",
"http://idp.example.com/token",
),
(
"http://idp.example.com/device",
"https://idp.example.com/token",
),
] {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/.well-known/openid-configuration"))
.respond_with(
ResponseTemplate::new(200).set_body_json(discovery_json(device, token)),
)
.mount(&server)
.await;
let err = discover(&reqwest::Client::new(), &server.uri())
.await
.expect_err("plain-http endpoint off loopback must be refused");
assert!(
matches!(&err, LlmError::Auth(msg) if msg.contains("must use https")),
"{device} {token}: {err:?}"
);
}
}
#[tokio::test]
async fn discover_accepts_a_loopback_issuer_and_endpoints() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let server = MockServer::start().await;
let device = format!("{}/device", server.uri());
let token = format!("{}/token", server.uri());
Mock::given(method("GET"))
.and(path("/.well-known/openid-configuration"))
.respond_with(ResponseTemplate::new(200).set_body_json(discovery_json(&device, &token)))
.mount(&server)
.await;
let found = discover(&reqwest::Client::new(), &server.uri())
.await
.unwrap();
assert_eq!(found.token_endpoint, token);
assert_eq!(found.device_authorization_endpoint, device);
}
#[tokio::test]
async fn credential_sinks_revalidate_mutated_discovery_endpoints() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
// OidcDiscovery has public fields, so login-time validation is not a
// sufficient boundary: another caller can construct or mutate one.
// Route remote names through a local proxy to prove the guards reject
// before either credential-bearing request reaches the wire.
let proxy = MockServer::start().await;
let client = reqwest::Client::builder()
.proxy(reqwest::Proxy::http(proxy.uri()).unwrap())
.build()
.unwrap();
let remote = OidcDiscovery {
device_authorization_endpoint: "http://idp.example.com/device".into(),
token_endpoint: "http://idp.example.com/token".into(),
};
let device_error = request_device_code(&client, &remote, "client", OIDC_DEFAULT_SCOPE)
.await
.expect_err("a mutated remote HTTP device endpoint must be refused");
let token_error = poll_token_once(&client, &remote, "client", "device-code")
.await
.expect_err("a mutated remote HTTP token endpoint must be refused");
for error in [device_error, token_error] {
assert!(matches!(error, LlmError::Auth(message) if message.contains("must use https")));
}
assert!(
proxy
.received_requests()
.await
.unwrap_or_default()
.is_empty(),
"a rejected endpoint reached the proxy"
);
// Plain HTTP remains supported for an explicitly loopback test IdP.
let local = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/device"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"device_code": "device-code",
"user_code": "ABCD",
"verification_uri": "https://idp.example.com/verify"
})))
.mount(&local)
.await;
Mock::given(method("POST"))
.and(path("/token"))
.respond_with(ResponseTemplate::new(400).set_body_json(serde_json::json!({
"error": "authorization_pending"
})))
.mount(&local)
.await;
let loopback = OidcDiscovery {
device_authorization_endpoint: format!("{}/device", local.uri()),
token_endpoint: format!("{}/token", local.uri()),
};
request_device_code(
&reqwest::Client::new(),
&loopback,
"client",
OIDC_DEFAULT_SCOPE,
)
.await
.expect("loopback device endpoint remains supported");
assert!(matches!(
poll_token_once(&reqwest::Client::new(), &loopback, "client", "device-code")
.await
.unwrap(),
PollOutcome::Pending
));
}
#[tokio::test]
async fn refresh_refuses_a_persisted_plain_http_remote_endpoint() {
let mut token = sample();
token.extra.token_endpoint = "http://idp.example.com/token".into();
let err = refresh_access_token(&token)
.await
.expect_err("a persisted remote http endpoint must fail before a request");
assert!(
matches!(&err, LlmError::Auth(message) if message.contains("must use https")),
"{err:?}"
);
}
#[tokio::test]
async fn refresh_does_not_forward_the_refresh_token_through_redirects() {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
let target = MockServer::start().await;
let source = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/token"))
.respond_with(
ResponseTemplate::new(307)
.insert_header("Location", format!("{}/received", target.uri())),
)
.mount(&source)
.await;
let mut token = sample();
token.extra.token_endpoint = format!("{}/token", source.uri());
refresh_access_token(&token)
.await
.expect_err("a token endpoint redirect is not followed");
assert!(
target
.received_requests()
.await
.unwrap_or_default()
.is_empty(),
"the persisted refresh token reached the redirect target"
);
}
}