Skip to main content

things_mcp/
oauth.rs

1//! OAuth 2.1 authorization surface for the streamable-HTTP transport.
2//!
3//! Claude.ai's MCP connector follows the spec-canonical flow: authorization
4//! code with PKCE (RFC 7636, S256). Even though the connector UI exposes
5//! "Client ID" / "Client Secret" fields, the actual grant is
6//! `authorization_code` — the secret is just an additional bearer of trust on
7//! the token request. The flow we observed in practice:
8//!
9//!   1. Client (Anthropic backend) hits `/mcp` without a token →
10//!      `401 Unauthorized` with a `WWW-Authenticate: Bearer
11//!      resource_metadata="…"` challenge (RFC 9728 §5.1).
12//!   2. Client fetches `/.well-known/oauth-protected-resource` and
13//!      `/.well-known/oauth-authorization-server` to discover this endpoint set.
14//!   3. The user's browser is opened to `/authorize?response_type=code&…&
15//!      code_challenge=…&code_challenge_method=S256&state=…&redirect_uri=
16//!      https://claude.ai/api/mcp/auth_callback`.
17//!   4. We 302-redirect to that `redirect_uri` with a one-time code + state.
18//!   5. Claude.ai's backend posts `grant_type=authorization_code` to
19//!      `/oauth/token` with the code + `code_verifier`. We verify
20//!      `SHA256(code_verifier)` (base64url, no pad) matches the stored
21//!      `code_challenge` and mint an opaque access token.
22//!
23//! Both grant types — `authorization_code` and `client_credentials` — are
24//! supported. The latter is retained for headless scripting and tests.
25
26mod token_store;
27pub use token_store::{ChainId, MintedPair, RefreshError, TokenStore};
28
29use std::collections::HashMap;
30use std::path::PathBuf;
31use std::sync::Arc;
32use std::time::{SystemTime, UNIX_EPOCH};
33
34use axum::{
35    extract::{Query, State},
36    http::{header, HeaderMap, StatusCode},
37    response::IntoResponse,
38    routing::{get, post},
39    Form, Json, Router,
40};
41use base64::Engine;
42use serde::{Deserialize, Serialize};
43use sha2::{Digest, Sha256};
44use tokio::sync::RwLock;
45
46const AUTH_CODE_TTL_SECS: u64 = 300;
47
48/// Redirect URIs we'll accept on the authorization endpoint. The OAuth 2.1
49/// spec requires exact-match validation against pre-registered values — for a
50/// single-purpose connector talking only to Claude.ai we hardcode the two
51/// hostnames Anthropic actually uses.
52const ALLOWED_REDIRECT_URI_PREFIXES: &[&str] =
53    &["https://claude.ai/api/mcp/", "https://claude.com/api/mcp/"];
54
55pub const DEFAULT_ACCESS_TOKEN_TTL_SECS: u64 = 7 * 24 * 3600; // 7 days
56pub const DEFAULT_REFRESH_TOKEN_TTL_SECS: u64 = 90 * 24 * 3600; // 90 days
57
58/// Pre-shared OAuth client credentials. Persisted at
59/// `<config_dir>/oauth.toml` with mode 0600 so the secret never lands in a
60/// world-readable location.
61#[derive(Clone, Debug, Serialize, Deserialize)]
62pub struct OAuthConfig {
63    pub client_id: String,
64    pub client_secret: String,
65    /// Public origin advertised in discovery + used to compute the resource
66    /// metadata URL in 401 challenges. Must match what the client believes the
67    /// canonical URI is (e.g. the Tailscale Funnel hostname).
68    pub issuer: String,
69    #[serde(default)]
70    pub access_token_ttl_secs: Option<u64>,
71    #[serde(default)]
72    pub refresh_token_ttl_secs: Option<u64>,
73}
74
75impl OAuthConfig {
76    pub fn effective_access_ttl(&self) -> std::time::Duration {
77        std::time::Duration::from_secs(
78            self.access_token_ttl_secs
79                .unwrap_or(DEFAULT_ACCESS_TOKEN_TTL_SECS),
80        )
81    }
82    pub fn effective_refresh_ttl(&self) -> std::time::Duration {
83        std::time::Duration::from_secs(
84            self.refresh_token_ttl_secs
85                .unwrap_or(DEFAULT_REFRESH_TOKEN_TTL_SECS),
86        )
87    }
88}
89
90/// Location of the on-disk OAuth config. Uses the same ProjectDirs convention
91/// as `things-mcp`'s config path so users find both files in the same
92/// directory (`~/Library/Application Support/dev.things-mcp.things-mcp` on
93/// macOS, `~/.config/things-mcp` on Linux).
94pub fn config_path() -> Option<PathBuf> {
95    directories::ProjectDirs::from("dev", "things-mcp", "things-mcp")
96        .map(|d| d.config_dir().join("oauth.toml"))
97}
98
99impl OAuthConfig {
100    /// Resolve credentials. Precedence:
101    /// 1. If `<config_dir>/oauth.toml` exists, load it.
102    /// 2. Otherwise, if `issuer_hint` is `Some`, generate a fresh credential
103    ///    pair (random client_id + 32-byte hex client_secret), persist it
104    ///    with mode 0600, and return it. The generated values are logged to
105    ///    stderr so the user can paste them into the Claude.ai connector.
106    /// 3. Otherwise, return `Ok(None)` — OAuth is opt-in; without an issuer
107    ///    we cannot generate a sensible config.
108    pub fn load_or_generate(issuer_hint: Option<String>) -> anyhow::Result<Option<Self>> {
109        let Some(path) = config_path() else {
110            tracing::warn!("could not resolve ProjectDirs for OAuth config; OAuth disabled");
111            return Ok(None);
112        };
113
114        if path.exists() {
115            let bytes = std::fs::read(&path)
116                .map_err(|e| anyhow::anyhow!("read {}: {e}", path.display()))?;
117            let config: OAuthConfig = toml::from_str(std::str::from_utf8(&bytes)?)
118                .map_err(|e| anyhow::anyhow!("parse {}: {e}", path.display()))?;
119            tracing::info!(path = %path.display(), "loaded OAuth config");
120            return Ok(Some(config));
121        }
122
123        let Some(issuer) = issuer_hint else {
124            tracing::warn!(
125                path = %path.display(),
126                "OAuth config not found and no THINGS_MCP_OAUTH_ISSUER set; OAuth disabled"
127            );
128            return Ok(None);
129        };
130
131        let config = OAuthConfig {
132            client_id: format!("things-mcp-{}", short_id()),
133            client_secret: format!("{:032x}", rand::random::<u128>()),
134            issuer,
135            access_token_ttl_secs: None,
136            refresh_token_ttl_secs: None,
137        };
138        Self::write_secure(&path, &config)?;
139        tracing::warn!(
140            path = %path.display(),
141            client_id = %config.client_id,
142            "generated OAuth credentials — paste these into the Claude.ai connector's Advanced fields"
143        );
144        // Also print to stderr so the message is visible on the very first run
145        // even if the logger threshold filters out warnings.
146        eprintln!(
147            "\n=== things-mcp OAuth credentials generated at {} ===\n  client_id     = {}\n  client_secret = {}\n  issuer        = {}\n  → paste client_id + client_secret into Claude.ai connector → Advanced → OAuth fields\n",
148            path.display(),
149            config.client_id,
150            config.client_secret,
151            config.issuer,
152        );
153        Ok(Some(config))
154    }
155
156    fn write_secure(path: &std::path::Path, config: &OAuthConfig) -> anyhow::Result<()> {
157        if let Some(parent) = path.parent() {
158            std::fs::create_dir_all(parent)
159                .map_err(|e| anyhow::anyhow!("mkdir {}: {e}", parent.display()))?;
160        }
161        let serialized = toml::to_string_pretty(config)?;
162        std::fs::write(path, serialized)
163            .map_err(|e| anyhow::anyhow!("write {}: {e}", path.display()))?;
164        #[cfg(unix)]
165        {
166            use std::os::unix::fs::PermissionsExt;
167            let perms = std::fs::Permissions::from_mode(0o600);
168            std::fs::set_permissions(path, perms)
169                .map_err(|e| anyhow::anyhow!("chmod 0600 {}: {e}", path.display()))?;
170        }
171        Ok(())
172    }
173}
174
175fn short_id() -> String {
176    format!("{:08x}", rand::random::<u32>())
177}
178
179/// Shared runtime state for the OAuth surface. Cheaply cloneable.
180#[derive(Clone)]
181pub struct OAuthState {
182    inner: Arc<Inner>,
183}
184
185struct Inner {
186    config: OAuthConfig,
187    /// In-memory authorization-code store. Codes are single-use, 5-minute TTL —
188    /// surviving a server restart is not a goal for this short-lived state.
189    codes: RwLock<HashMap<String, AuthCode>>,
190    tokens: TokenStore,
191}
192
193#[derive(Clone)]
194struct AuthCode {
195    code_challenge: String,
196    redirect_uri: String,
197    expires_at: u64,
198}
199
200impl OAuthState {
201    /// Construct an OAuthState backed by a TokenStore at `tokens_path`.
202    /// Use `OAuthState::with_tokens_path` to supply the path explicitly,
203    /// or `OAuthState::from_default_path` to derive it from the standard
204    /// ProjectDirs location (test code uses the former, production uses the latter).
205    pub fn with_tokens_path(config: OAuthConfig, tokens_path: PathBuf) -> anyhow::Result<Self> {
206        let access_ttl = config.effective_access_ttl();
207        let refresh_ttl = config.effective_refresh_ttl();
208        let tokens = TokenStore::load(tokens_path, &config.client_id, access_ttl, refresh_ttl)?;
209        Ok(Self {
210            inner: Arc::new(Inner {
211                config,
212                codes: RwLock::new(HashMap::new()),
213                tokens,
214            }),
215        })
216    }
217
218    /// Standard production constructor: derive the tokens path from the
219    /// same ProjectDirs base used by `oauth.toml`.
220    pub fn from_default_path(config: OAuthConfig) -> anyhow::Result<Self> {
221        let dir = directories::ProjectDirs::from("dev", "things-mcp", "things-mcp")
222            .ok_or_else(|| anyhow::anyhow!("could not resolve ProjectDirs for tokens.json"))?
223            .config_dir()
224            .to_path_buf();
225        Self::with_tokens_path(config, dir.join("tokens.json"))
226    }
227
228    pub fn issuer(&self) -> &str {
229        &self.inner.config.issuer
230    }
231
232    /// URL the WWW-Authenticate challenge points clients at for resource
233    /// metadata. Defined by RFC 9728 §5.1.
234    pub fn resource_metadata_url(&self) -> String {
235        format!(
236            "{}/.well-known/oauth-protected-resource",
237            self.inner.config.issuer
238        )
239    }
240
241    /// Returns true iff the token was previously issued and is not expired.
242    pub async fn validate_token(&self, token: &str) -> bool {
243        self.inner.tokens.validate_access(token).await
244    }
245
246    /// Crate-internal access to the token store — used by tests in sibling
247    /// modules that need to mint a token without going through the HTTP layer.
248    #[cfg(test)]
249    pub(crate) fn token_store(&self) -> &TokenStore {
250        &self.inner.tokens
251    }
252}
253
254fn unix_now() -> u64 {
255    SystemTime::now()
256        .duration_since(UNIX_EPOCH)
257        .unwrap_or_default()
258        .as_secs()
259}
260
261#[derive(Serialize)]
262struct AuthorizationServerMetadata {
263    issuer: String,
264    authorization_endpoint: String,
265    token_endpoint: String,
266    grant_types_supported: &'static [&'static str],
267    token_endpoint_auth_methods_supported: &'static [&'static str],
268    response_types_supported: &'static [&'static str],
269    code_challenge_methods_supported: &'static [&'static str],
270    scopes_supported: &'static [&'static str],
271}
272
273#[derive(Serialize)]
274struct ProtectedResourceMetadata {
275    resource: String,
276    authorization_servers: Vec<String>,
277    bearer_methods_supported: &'static [&'static str],
278    scopes_supported: &'static [&'static str],
279}
280
281async fn authorization_server_metadata(
282    State(state): State<OAuthState>,
283) -> Json<AuthorizationServerMetadata> {
284    let issuer = state.issuer().to_string();
285    Json(AuthorizationServerMetadata {
286        authorization_endpoint: format!("{issuer}/authorize"),
287        token_endpoint: format!("{issuer}/oauth/token"),
288        issuer,
289        grant_types_supported: &["authorization_code", "refresh_token", "client_credentials"],
290        token_endpoint_auth_methods_supported: &["client_secret_post", "client_secret_basic"],
291        response_types_supported: &["code", "token"],
292        code_challenge_methods_supported: &["S256"],
293        scopes_supported: &["mcp"],
294    })
295}
296
297async fn protected_resource_metadata(
298    State(state): State<OAuthState>,
299) -> Json<ProtectedResourceMetadata> {
300    let issuer = state.issuer().to_string();
301    Json(ProtectedResourceMetadata {
302        authorization_servers: vec![issuer.clone()],
303        resource: issuer,
304        bearer_methods_supported: &["header"],
305        scopes_supported: &["mcp"],
306    })
307}
308
309#[derive(Deserialize)]
310struct TokenRequest {
311    grant_type: String,
312    // client_credentials inputs
313    client_id: Option<String>,
314    client_secret: Option<String>,
315    // authorization_code inputs
316    #[serde(default)]
317    code: Option<String>,
318    #[serde(default)]
319    code_verifier: Option<String>,
320    #[serde(default)]
321    redirect_uri: Option<String>,
322    /// RFC 8707 Resource Indicator. Accepted but not enforced — single-resource
323    /// server.
324    #[allow(dead_code)]
325    #[serde(default)]
326    resource: Option<String>,
327    #[allow(dead_code)]
328    #[serde(default)]
329    scope: Option<String>,
330    #[serde(default)]
331    refresh_token: Option<String>,
332}
333
334#[derive(Serialize)]
335struct TokenResponse {
336    access_token: String,
337    token_type: &'static str,
338    expires_in: u64,
339    #[serde(skip_serializing_if = "Option::is_none")]
340    refresh_token: Option<String>,
341    #[serde(skip_serializing_if = "Option::is_none")]
342    refresh_expires_in: Option<u64>,
343    scope: &'static str,
344}
345
346#[derive(Serialize)]
347struct OAuthError {
348    error: &'static str,
349    #[serde(skip_serializing_if = "Option::is_none")]
350    error_description: Option<&'static str>,
351}
352
353async fn token_handler(
354    State(state): State<OAuthState>,
355    headers: HeaderMap,
356    Form(body): Form<TokenRequest>,
357) -> axum::response::Response {
358    match body.grant_type.as_str() {
359        "authorization_code" => handle_authorization_code(state, headers, body).await,
360        "client_credentials" => handle_client_credentials(state, headers, body).await,
361        "refresh_token" => handle_refresh_token(state, headers, body).await,
362        _ => (
363            StatusCode::BAD_REQUEST,
364            Json(OAuthError {
365                error: "unsupported_grant_type",
366                error_description: Some(
367                    "supported grant types: authorization_code, refresh_token, client_credentials",
368                ),
369            }),
370        )
371            .into_response(),
372    }
373}
374
375async fn handle_client_credentials(
376    state: OAuthState,
377    headers: HeaderMap,
378    body: TokenRequest,
379) -> axum::response::Response {
380    let Some((client_id, client_secret)) = resolve_client_credentials(&headers, &body) else {
381        return invalid_client();
382    };
383
384    let expected = &state.inner.config;
385    if !constant_time_eq(client_id.as_bytes(), expected.client_id.as_bytes())
386        || !constant_time_eq(client_secret.as_bytes(), expected.client_secret.as_bytes())
387    {
388        return invalid_client();
389    }
390
391    let pair = match state.inner.tokens.mint_pair(None).await {
392        Ok(p) => p,
393        Err(e) => {
394            tracing::error!(error = %e, "mint_pair failed for client_credentials");
395            return server_error();
396        }
397    };
398    let token = pair.access_token;
399    let ttl = pair.access_ttl.as_secs();
400    tracing::info!(
401        client_id = %client_id,
402        grant = "client_credentials",
403        expires_in = ttl,
404        "OAuth token minted"
405    );
406    token_ok_access_only(token, ttl)
407}
408
409async fn handle_authorization_code(
410    state: OAuthState,
411    headers: HeaderMap,
412    body: TokenRequest,
413) -> axum::response::Response {
414    let Some(code) = body.code.as_deref() else {
415        return invalid_grant("missing code");
416    };
417    let Some(verifier) = body.code_verifier.as_deref() else {
418        return invalid_grant("missing code_verifier");
419    };
420    let Some(redirect_uri) = body.redirect_uri.as_deref() else {
421        return invalid_grant("missing redirect_uri");
422    };
423
424    // Client authentication is optional with PKCE per RFC 6749 §4.1.3 (the
425    // code_verifier is proof of possession) but if the caller did present
426    // credentials, validate them — Claude.ai may send the client_secret it
427    // was given.
428    if let Some((client_id, client_secret)) = resolve_client_credentials(&headers, &body) {
429        let expected = &state.inner.config;
430        if !constant_time_eq(client_id.as_bytes(), expected.client_id.as_bytes())
431            || !constant_time_eq(client_secret.as_bytes(), expected.client_secret.as_bytes())
432        {
433            return invalid_client();
434        }
435    } else if let Some(client_id) = body.client_id.as_deref() {
436        // client_id alone (public client) — must still match.
437        if !constant_time_eq(
438            client_id.as_bytes(),
439            state.inner.config.client_id.as_bytes(),
440        ) {
441            return invalid_client();
442        }
443    }
444
445    let info = state.inner.codes.write().await.remove(code);
446    let Some(info) = info else {
447        return invalid_grant("unknown or already-used code");
448    };
449    if info.expires_at < unix_now() {
450        return invalid_grant("code expired");
451    }
452    if info.redirect_uri != redirect_uri {
453        return invalid_grant("redirect_uri mismatch");
454    }
455    let computed = pkce_s256(verifier);
456    if !constant_time_eq(computed.as_bytes(), info.code_challenge.as_bytes()) {
457        return invalid_grant("PKCE verification failed");
458    }
459
460    let pair = match state.inner.tokens.mint_pair(None).await {
461        Ok(p) => p,
462        Err(e) => {
463            tracing::error!(error = %e, "mint_pair failed for authorization_code");
464            return server_error();
465        }
466    };
467    tracing::info!(
468        grant = "authorization_code",
469        chain_id = %pair.chain_id,
470        expires_in = pair.access_ttl.as_secs(),
471        "OAuth token pair minted"
472    );
473    token_ok_pair(pair)
474}
475
476async fn handle_refresh_token(
477    state: OAuthState,
478    headers: HeaderMap,
479    body: TokenRequest,
480) -> axum::response::Response {
481    // Optional client authentication — same logic as handle_authorization_code.
482    if let Some((client_id, client_secret)) = resolve_client_credentials(&headers, &body) {
483        let expected = &state.inner.config;
484        if !constant_time_eq(client_id.as_bytes(), expected.client_id.as_bytes())
485            || !constant_time_eq(client_secret.as_bytes(), expected.client_secret.as_bytes())
486        {
487            return invalid_client();
488        }
489    } else if let Some(client_id) = body.client_id.as_deref() {
490        if !constant_time_eq(
491            client_id.as_bytes(),
492            state.inner.config.client_id.as_bytes(),
493        ) {
494            return invalid_client();
495        }
496    }
497
498    let Some(presented) = body.refresh_token.as_deref() else {
499        return invalid_grant("missing refresh_token");
500    };
501
502    let chain_id = match state.inner.tokens.consume_refresh(presented).await {
503        Ok(chain) => chain,
504        Err(RefreshError::Replayed(chain)) => {
505            tracing::warn!(chain_id = %chain, "refresh-token replay detected; revoking chain");
506            state.inner.tokens.revoke_chain(chain).await;
507            return invalid_grant("refresh token replay");
508        }
509        Err(RefreshError::Expired) => return invalid_grant("refresh token expired"),
510        Err(RefreshError::Unknown) => return invalid_grant("unknown refresh token"),
511    };
512
513    let pair = match state.inner.tokens.mint_pair(Some(chain_id.clone())).await {
514        Ok(p) => p,
515        Err(e) => {
516            tracing::error!(error = %e, "mint_pair failed during refresh_token grant");
517            return server_error();
518        }
519    };
520    tracing::info!(
521        grant = "refresh_token",
522        chain_id = %chain_id,
523        expires_in = pair.access_ttl.as_secs(),
524        "OAuth token pair minted (refreshed)"
525    );
526    token_ok_pair(pair)
527}
528
529fn token_ok_access_only(token: String, ttl: u64) -> axum::response::Response {
530    (
531        StatusCode::OK,
532        Json(TokenResponse {
533            access_token: token,
534            token_type: "Bearer",
535            expires_in: ttl,
536            refresh_token: None,
537            refresh_expires_in: None,
538            scope: "mcp",
539        }),
540    )
541        .into_response()
542}
543
544fn token_ok_pair(pair: MintedPair) -> axum::response::Response {
545    (
546        StatusCode::OK,
547        Json(TokenResponse {
548            access_token: pair.access_token,
549            token_type: "Bearer",
550            expires_in: pair.access_ttl.as_secs(),
551            refresh_token: Some(pair.refresh_token),
552            refresh_expires_in: Some(pair.refresh_ttl.as_secs()),
553            scope: "mcp",
554        }),
555    )
556        .into_response()
557}
558
559fn invalid_grant(detail: &'static str) -> axum::response::Response {
560    tracing::info!(detail, "OAuth grant rejected");
561    (
562        StatusCode::BAD_REQUEST,
563        Json(OAuthError {
564            error: "invalid_grant",
565            error_description: Some(detail),
566        }),
567    )
568        .into_response()
569}
570
571/// `BASE64URL(SHA256(verifier))` with no padding, per RFC 7636 §4.6.
572fn pkce_s256(verifier: &str) -> String {
573    let digest = Sha256::digest(verifier.as_bytes());
574    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(digest)
575}
576
577#[derive(Deserialize)]
578struct AuthorizeQuery {
579    response_type: String,
580    client_id: String,
581    redirect_uri: String,
582    code_challenge: String,
583    code_challenge_method: String,
584    state: String,
585    #[allow(dead_code)]
586    #[serde(default)]
587    scope: Option<String>,
588    #[allow(dead_code)]
589    #[serde(default)]
590    resource: Option<String>,
591}
592
593async fn authorize_handler(
594    State(state): State<OAuthState>,
595    Query(q): Query<AuthorizeQuery>,
596) -> axum::response::Response {
597    // We validate the redirect_uri BEFORE responding via redirect — sending
598    // any params to an unvetted URI would be an open-redirect bug.
599    let redirect_ok = ALLOWED_REDIRECT_URI_PREFIXES
600        .iter()
601        .any(|p| q.redirect_uri.starts_with(p));
602    if !redirect_ok {
603        tracing::warn!(redirect_uri = %q.redirect_uri, "authorize: redirect_uri not allowed");
604        return (StatusCode::BAD_REQUEST, "invalid_redirect_uri").into_response();
605    }
606    if q.response_type != "code" {
607        return redirect_with_error(&q.redirect_uri, &q.state, "unsupported_response_type");
608    }
609    if q.code_challenge_method != "S256" {
610        return redirect_with_error(&q.redirect_uri, &q.state, "invalid_request");
611    }
612    if !constant_time_eq(
613        q.client_id.as_bytes(),
614        state.inner.config.client_id.as_bytes(),
615    ) {
616        return redirect_with_error(&q.redirect_uri, &q.state, "unauthorized_client");
617    }
618    if q.code_challenge.is_empty() {
619        return redirect_with_error(&q.redirect_uri, &q.state, "invalid_request");
620    }
621
622    let code = format!("{:032x}", rand::random::<u128>());
623    let info = AuthCode {
624        code_challenge: q.code_challenge,
625        redirect_uri: q.redirect_uri.clone(),
626        expires_at: unix_now() + AUTH_CODE_TTL_SECS,
627    };
628    state.inner.codes.write().await.insert(code.clone(), info);
629    tracing::info!(redirect_uri = %q.redirect_uri, "authorization code issued");
630
631    let location = format!(
632        "{}?code={}&state={}",
633        q.redirect_uri,
634        urlencoding_minimal(&code),
635        urlencoding_minimal(&q.state),
636    );
637    (StatusCode::FOUND, [(header::LOCATION, location.as_str())]).into_response()
638}
639
640fn redirect_with_error(redirect_uri: &str, state: &str, error: &str) -> axum::response::Response {
641    let location = format!(
642        "{redirect_uri}?error={}&state={}",
643        urlencoding_minimal(error),
644        urlencoding_minimal(state)
645    );
646    (StatusCode::FOUND, [(header::LOCATION, location.as_str())]).into_response()
647}
648
649/// Minimal URL-encoding for the small set of characters that appear in our
650/// outputs (state tokens are alphanumeric+`_-`, codes are hex). Reaching for
651/// the `urlencoding` crate just to encode `&`, `=`, `+`, ` ` would be
652/// disproportionate.
653fn urlencoding_minimal(s: &str) -> String {
654    let mut out = String::with_capacity(s.len());
655    for c in s.chars() {
656        match c {
657            'A'..='Z' | 'a'..='z' | '0'..='9' | '-' | '_' | '.' | '~' => out.push(c),
658            _ => out.push_str(&format!("%{:02X}", c as u32)),
659        }
660    }
661    out
662}
663
664/// Pull `client_id`/`client_secret` from the form body (client_secret_post)
665/// or fall back to the `Authorization: Basic …` header (client_secret_basic).
666fn resolve_client_credentials(
667    headers: &HeaderMap,
668    body: &TokenRequest,
669) -> Option<(String, String)> {
670    if let (Some(id), Some(secret)) = (body.client_id.as_ref(), body.client_secret.as_ref()) {
671        return Some((id.clone(), secret.clone()));
672    }
673    let auth = headers.get(header::AUTHORIZATION)?.to_str().ok()?;
674    let encoded = auth.strip_prefix("Basic ")?;
675    let bytes = base64::engine::general_purpose::STANDARD
676        .decode(encoded.trim())
677        .ok()?;
678    let decoded = String::from_utf8(bytes).ok()?;
679    let (id, secret) = decoded.split_once(':')?;
680    Some((id.to_string(), secret.to_string()))
681}
682
683fn invalid_client() -> axum::response::Response {
684    (
685        StatusCode::UNAUTHORIZED,
686        [(header::WWW_AUTHENTICATE, "Basic realm=\"oauth/token\"")],
687        Json(OAuthError {
688            error: "invalid_client",
689            error_description: None,
690        }),
691    )
692        .into_response()
693}
694
695fn server_error() -> axum::response::Response {
696    (
697        StatusCode::INTERNAL_SERVER_ERROR,
698        Json(OAuthError {
699            error: "server_error",
700            error_description: None,
701        }),
702    )
703        .into_response()
704}
705
706/// Length-aware equality that avoids byte-by-byte short-circuit. For
707/// fixed-length pre-shared secrets the length disclosure is irrelevant.
708fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
709    if a.len() != b.len() {
710        return false;
711    }
712    let mut diff: u8 = 0;
713    for (x, y) in a.iter().zip(b.iter()) {
714        diff |= x ^ y;
715    }
716    diff == 0
717}
718
719/// Build the public, unauthenticated OAuth surface.
720///
721/// Routes:
722///   - `GET /.well-known/oauth-authorization-server` — RFC 8414 metadata
723///   - `GET /.well-known/oauth-protected-resource`    — RFC 9728 metadata
724///   - `GET /authorize`                               — auth-code start (PKCE)
725///   - `POST /oauth/token`                            — token issuance
726pub fn router(state: OAuthState) -> Router {
727    Router::new()
728        .route(
729            "/.well-known/oauth-authorization-server",
730            get(authorization_server_metadata),
731        )
732        .route(
733            "/.well-known/oauth-protected-resource",
734            get(protected_resource_metadata),
735        )
736        .route("/authorize", get(authorize_handler))
737        .route("/oauth/token", post(token_handler))
738        .with_state(state)
739}
740
741#[cfg(test)]
742mod tests {
743    use super::*;
744    use axum::body::{to_bytes, Body};
745    use axum::http::Request;
746    use tower::ServiceExt;
747
748    #[test]
749    fn config_loads_with_default_ttls_when_unset() {
750        let toml_str = r#"
751            client_id = "x"
752            client_secret = "y"
753            issuer = "https://example.test"
754        "#;
755        let cfg: OAuthConfig = toml::from_str(toml_str).unwrap();
756        assert_eq!(cfg.access_token_ttl_secs, None);
757        assert_eq!(cfg.refresh_token_ttl_secs, None);
758        assert_eq!(cfg.effective_access_ttl().as_secs(), 7 * 24 * 3600);
759        assert_eq!(cfg.effective_refresh_ttl().as_secs(), 90 * 24 * 3600);
760    }
761
762    #[test]
763    fn config_loads_with_explicit_ttls() {
764        let toml_str = r#"
765            client_id = "x"
766            client_secret = "y"
767            issuer = "https://example.test"
768            access_token_ttl_secs = 3600
769            refresh_token_ttl_secs = 86400
770        "#;
771        let cfg: OAuthConfig = toml::from_str(toml_str).unwrap();
772        assert_eq!(cfg.effective_access_ttl().as_secs(), 3600);
773        assert_eq!(cfg.effective_refresh_ttl().as_secs(), 86400);
774    }
775
776    #[test]
777    fn config_roundtrips_through_disk_with_secure_perms() {
778        let dir = tempdir();
779        let path = dir.join("oauth.toml");
780        let original = OAuthConfig {
781            client_id: "id-x".into(),
782            client_secret: "secret-y".into(),
783            issuer: "https://example.test".into(),
784            access_token_ttl_secs: None,
785            refresh_token_ttl_secs: None,
786        };
787        OAuthConfig::write_secure(&path, &original).unwrap();
788
789        let bytes = std::fs::read(&path).unwrap();
790        let parsed: OAuthConfig = toml::from_str(std::str::from_utf8(&bytes).unwrap()).unwrap();
791        assert_eq!(parsed.client_id, "id-x");
792        assert_eq!(parsed.client_secret, "secret-y");
793        assert_eq!(parsed.issuer, "https://example.test");
794
795        #[cfg(unix)]
796        {
797            use std::os::unix::fs::PermissionsExt;
798            let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
799            assert_eq!(mode, 0o600, "file should be readable only by owner");
800        }
801    }
802
803    fn tempdir() -> PathBuf {
804        let p = std::env::temp_dir().join(format!("things-mcp-test-{}", rand::random::<u64>()));
805        std::fs::create_dir_all(&p).unwrap();
806        p
807    }
808
809    fn test_state() -> OAuthState {
810        let dir = tempdir();
811        OAuthState::with_tokens_path(
812            OAuthConfig {
813                client_id: "test-id".into(),
814                client_secret: "test-secret".into(),
815                issuer: "https://example.test".into(),
816                access_token_ttl_secs: None,
817                refresh_token_ttl_secs: None,
818            },
819            dir.join("tokens.json"),
820        )
821        .unwrap()
822    }
823
824    async fn body_string(resp: axum::response::Response) -> String {
825        let bytes = to_bytes(resp.into_body(), 64 * 1024).await.unwrap();
826        String::from_utf8(bytes.to_vec()).unwrap()
827    }
828
829    #[tokio::test]
830    async fn token_endpoint_issues_for_valid_credentials_via_body() {
831        let app = router(test_state());
832        let resp = app
833            .oneshot(
834                Request::builder()
835                    .method("POST")
836                    .uri("/oauth/token")
837                    .header("content-type", "application/x-www-form-urlencoded")
838                    .body(Body::from(
839                        "grant_type=client_credentials&client_id=test-id&client_secret=test-secret",
840                    ))
841                    .unwrap(),
842            )
843            .await
844            .unwrap();
845        assert_eq!(resp.status(), StatusCode::OK);
846        let body = body_string(resp).await;
847        assert!(body.contains("\"access_token\""), "body was: {body}");
848        assert!(body.contains("\"token_type\":\"Bearer\""));
849        assert!(body.contains("\"expires_in\":604800"));
850    }
851
852    #[tokio::test]
853    async fn token_endpoint_accepts_basic_auth() {
854        let app = router(test_state());
855        let basic = base64::engine::general_purpose::STANDARD.encode("test-id:test-secret");
856        let resp = app
857            .oneshot(
858                Request::builder()
859                    .method("POST")
860                    .uri("/oauth/token")
861                    .header("content-type", "application/x-www-form-urlencoded")
862                    .header("authorization", format!("Basic {basic}"))
863                    .body(Body::from("grant_type=client_credentials"))
864                    .unwrap(),
865            )
866            .await
867            .unwrap();
868        assert_eq!(resp.status(), StatusCode::OK);
869    }
870
871    #[tokio::test]
872    async fn token_endpoint_rejects_bad_secret() {
873        let app = router(test_state());
874        let resp = app
875            .oneshot(
876                Request::builder()
877                    .method("POST")
878                    .uri("/oauth/token")
879                    .header("content-type", "application/x-www-form-urlencoded")
880                    .body(Body::from(
881                        "grant_type=client_credentials&client_id=test-id&client_secret=WRONG",
882                    ))
883                    .unwrap(),
884            )
885            .await
886            .unwrap();
887        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
888        let body = body_string(resp).await;
889        assert!(body.contains("\"error\":\"invalid_client\""));
890    }
891
892    #[tokio::test]
893    async fn token_endpoint_rejects_unsupported_grant() {
894        let app = router(test_state());
895        let resp = app
896            .oneshot(
897                Request::builder()
898                    .method("POST")
899                    .uri("/oauth/token")
900                    .header("content-type", "application/x-www-form-urlencoded")
901                    .body(Body::from(
902                        "grant_type=password&client_id=test-id&client_secret=test-secret",
903                    ))
904                    .unwrap(),
905            )
906            .await
907            .unwrap();
908        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
909        let body = body_string(resp).await;
910        assert!(body.contains("unsupported_grant_type"));
911    }
912
913    #[tokio::test]
914    async fn minted_token_validates_then_expires() {
915        let state = test_state();
916        let pair = state.inner.tokens.mint_pair(None).await.unwrap();
917        assert!(state.validate_token(&pair.access_token).await);
918        assert!(!state.validate_token("not-issued").await);
919    }
920
921    #[tokio::test]
922    async fn discovery_documents_advertise_correct_endpoints() {
923        let app = router(test_state());
924        let resp = app
925            .clone()
926            .oneshot(
927                Request::builder()
928                    .uri("/.well-known/oauth-authorization-server")
929                    .body(Body::empty())
930                    .unwrap(),
931            )
932            .await
933            .unwrap();
934        assert_eq!(resp.status(), StatusCode::OK);
935        let body = body_string(resp).await;
936        assert!(body.contains("\"issuer\":\"https://example.test\""));
937        assert!(body.contains("\"authorization_endpoint\":\"https://example.test/authorize\""));
938        assert!(body.contains("\"token_endpoint\":\"https://example.test/oauth/token\""));
939        assert!(body.contains("\"authorization_code\""));
940        assert!(body.contains("\"client_credentials\""));
941        assert!(
942            body.contains("\"refresh_token\""),
943            "discovery must advertise refresh_token grant; body was: {body}"
944        );
945        assert!(body.contains("\"code_challenge_methods_supported\":[\"S256\"]"));
946
947        let resp = app
948            .oneshot(
949                Request::builder()
950                    .uri("/.well-known/oauth-protected-resource")
951                    .body(Body::empty())
952                    .unwrap(),
953            )
954            .await
955            .unwrap();
956        assert_eq!(resp.status(), StatusCode::OK);
957        let body = body_string(resp).await;
958        assert!(body.contains("\"resource\":\"https://example.test\""));
959        assert!(body.contains("\"authorization_servers\":[\"https://example.test\"]"));
960    }
961
962    #[test]
963    fn pkce_s256_matches_rfc7636_example() {
964        // RFC 7636 Appendix B test vector
965        let verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
966        let expected = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM";
967        assert_eq!(pkce_s256(verifier), expected);
968    }
969
970    fn challenge_for(verifier: &str) -> String {
971        pkce_s256(verifier)
972    }
973
974    #[tokio::test]
975    async fn authorize_endpoint_redirects_with_code() {
976        let app = router(test_state());
977        let verifier = "test-verifier-string-of-reasonable-length-1234";
978        let challenge = challenge_for(verifier);
979        let uri = format!(
980            "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge={challenge}&code_challenge_method=S256&state=xyz&scope=mcp",
981        );
982        let resp = app
983            .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
984            .await
985            .unwrap();
986        assert_eq!(resp.status(), StatusCode::FOUND);
987        let location = resp
988            .headers()
989            .get(header::LOCATION)
990            .unwrap()
991            .to_str()
992            .unwrap();
993        assert!(location.starts_with("https://claude.ai/api/mcp/auth_callback?code="));
994        assert!(location.contains("&state=xyz"));
995    }
996
997    #[tokio::test]
998    async fn authorize_rejects_disallowed_redirect_uri() {
999        let app = router(test_state());
1000        let uri = "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fattacker.example%2Fcb&code_challenge=abc&code_challenge_method=S256&state=z";
1001        let resp = app
1002            .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
1003            .await
1004            .unwrap();
1005        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
1006    }
1007
1008    #[tokio::test]
1009    async fn authorize_rejects_unknown_client_id() {
1010        let app = router(test_state());
1011        let uri = "/authorize?response_type=code&client_id=WRONG&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge=abc&code_challenge_method=S256&state=z";
1012        let resp = app
1013            .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
1014            .await
1015            .unwrap();
1016        // Error is reported via the redirect callback per OAuth spec
1017        assert_eq!(resp.status(), StatusCode::FOUND);
1018        let location = resp
1019            .headers()
1020            .get(header::LOCATION)
1021            .unwrap()
1022            .to_str()
1023            .unwrap();
1024        assert!(location.contains("error=unauthorized_client"));
1025    }
1026
1027    /// Round-trip the full auth-code + PKCE flow through the public router.
1028    #[tokio::test]
1029    async fn auth_code_grant_full_flow_succeeds() {
1030        let state = test_state();
1031        let verifier = "the-verifier-anthropic-would-have-generated";
1032        let challenge = challenge_for(verifier);
1033        let redirect_uri = "https://claude.ai/api/mcp/auth_callback";
1034
1035        // Step 1: /authorize, extract code from Location header.
1036        let auth_uri = format!(
1037            "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge={challenge}&code_challenge_method=S256&state=opaque-state",
1038        );
1039        let resp = router(state.clone())
1040            .oneshot(
1041                Request::builder()
1042                    .uri(auth_uri)
1043                    .body(Body::empty())
1044                    .unwrap(),
1045            )
1046            .await
1047            .unwrap();
1048        assert_eq!(resp.status(), StatusCode::FOUND);
1049        let location = resp
1050            .headers()
1051            .get(header::LOCATION)
1052            .unwrap()
1053            .to_str()
1054            .unwrap()
1055            .to_string();
1056        let code = location
1057            .split_once("code=")
1058            .and_then(|(_, rest)| rest.split('&').next())
1059            .unwrap()
1060            .to_string();
1061
1062        // Step 2: /oauth/token with grant_type=authorization_code.
1063        let body = format!(
1064            "grant_type=authorization_code&code={code}&redirect_uri={}&code_verifier={verifier}&client_id=test-id",
1065            urlencoding_minimal(redirect_uri)
1066        );
1067        let resp = router(state)
1068            .oneshot(
1069                Request::builder()
1070                    .method("POST")
1071                    .uri("/oauth/token")
1072                    .header("content-type", "application/x-www-form-urlencoded")
1073                    .body(Body::from(body))
1074                    .unwrap(),
1075            )
1076            .await
1077            .unwrap();
1078        assert_eq!(resp.status(), StatusCode::OK);
1079        let body = body_string(resp).await;
1080        assert!(body.contains("\"access_token\""));
1081        assert!(body.contains("\"token_type\":\"Bearer\""));
1082    }
1083
1084    #[tokio::test]
1085    async fn auth_code_rejects_bad_verifier() {
1086        let state = test_state();
1087        let verifier = "correct-verifier";
1088        let challenge = challenge_for(verifier);
1089        let auth_uri = format!(
1090            "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge={challenge}&code_challenge_method=S256&state=s",
1091        );
1092        let resp = router(state.clone())
1093            .oneshot(
1094                Request::builder()
1095                    .uri(auth_uri)
1096                    .body(Body::empty())
1097                    .unwrap(),
1098            )
1099            .await
1100            .unwrap();
1101        let location = resp
1102            .headers()
1103            .get(header::LOCATION)
1104            .unwrap()
1105            .to_str()
1106            .unwrap()
1107            .to_string();
1108        let code = location
1109            .split_once("code=")
1110            .and_then(|(_, rest)| rest.split('&').next())
1111            .unwrap()
1112            .to_string();
1113
1114        let body = format!(
1115            "grant_type=authorization_code&code={code}&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_verifier=WRONG&client_id=test-id"
1116        );
1117        let resp = router(state)
1118            .oneshot(
1119                Request::builder()
1120                    .method("POST")
1121                    .uri("/oauth/token")
1122                    .header("content-type", "application/x-www-form-urlencoded")
1123                    .body(Body::from(body))
1124                    .unwrap(),
1125            )
1126            .await
1127            .unwrap();
1128        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
1129        let body = body_string(resp).await;
1130        assert!(body.contains("invalid_grant"));
1131    }
1132
1133    #[tokio::test]
1134    async fn auth_code_response_includes_refresh_token() {
1135        let state = test_state();
1136        let verifier = "the-verifier-of-reasonable-length";
1137        let challenge = challenge_for(verifier);
1138        let auth_uri = format!(
1139            "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge={challenge}&code_challenge_method=S256&state=s",
1140        );
1141        let resp = router(state.clone())
1142            .oneshot(
1143                Request::builder()
1144                    .uri(auth_uri)
1145                    .body(Body::empty())
1146                    .unwrap(),
1147            )
1148            .await
1149            .unwrap();
1150        let location = resp
1151            .headers()
1152            .get(header::LOCATION)
1153            .unwrap()
1154            .to_str()
1155            .unwrap()
1156            .to_string();
1157        let code = location
1158            .split_once("code=")
1159            .and_then(|(_, r)| r.split('&').next())
1160            .unwrap()
1161            .to_string();
1162
1163        let body = format!(
1164            "grant_type=authorization_code&code={code}&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_verifier={verifier}&client_id=test-id"
1165        );
1166        let resp = router(state)
1167            .oneshot(
1168                Request::builder()
1169                    .method("POST")
1170                    .uri("/oauth/token")
1171                    .header("content-type", "application/x-www-form-urlencoded")
1172                    .body(Body::from(body))
1173                    .unwrap(),
1174            )
1175            .await
1176            .unwrap();
1177        assert_eq!(resp.status(), StatusCode::OK);
1178        let body = body_string(resp).await;
1179        assert!(body.contains("\"access_token\""), "body was: {body}");
1180        assert!(body.contains("\"refresh_token\""), "body was: {body}");
1181        assert!(
1182            body.contains("\"refresh_expires_in\":7776000"),
1183            "body was: {body}"
1184        );
1185    }
1186
1187    async fn auth_code_full_flow(state: OAuthState) -> (String, String) {
1188        let verifier = "verifier-string-of-decent-length";
1189        let challenge = challenge_for(verifier);
1190        let auth_uri = format!(
1191            "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge={challenge}&code_challenge_method=S256&state=s",
1192        );
1193        let resp = router(state.clone())
1194            .oneshot(
1195                Request::builder()
1196                    .uri(auth_uri)
1197                    .body(Body::empty())
1198                    .unwrap(),
1199            )
1200            .await
1201            .unwrap();
1202        let location = resp
1203            .headers()
1204            .get(header::LOCATION)
1205            .unwrap()
1206            .to_str()
1207            .unwrap()
1208            .to_string();
1209        let code = location
1210            .split_once("code=")
1211            .and_then(|(_, r)| r.split('&').next())
1212            .unwrap()
1213            .to_string();
1214        let body = format!(
1215            "grant_type=authorization_code&code={code}&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_verifier={verifier}&client_id=test-id"
1216        );
1217        let resp = router(state)
1218            .oneshot(
1219                Request::builder()
1220                    .method("POST")
1221                    .uri("/oauth/token")
1222                    .header("content-type", "application/x-www-form-urlencoded")
1223                    .body(Body::from(body))
1224                    .unwrap(),
1225            )
1226            .await
1227            .unwrap();
1228        let body_str = body_string(resp).await;
1229        let parsed: serde_json::Value = serde_json::from_str(&body_str).unwrap();
1230        let access = parsed["access_token"].as_str().unwrap().to_string();
1231        let refresh = parsed["refresh_token"].as_str().unwrap().to_string();
1232        (access, refresh)
1233    }
1234
1235    async fn post_token(state: OAuthState, body: &str) -> axum::response::Response {
1236        router(state)
1237            .oneshot(
1238                Request::builder()
1239                    .method("POST")
1240                    .uri("/oauth/token")
1241                    .header("content-type", "application/x-www-form-urlencoded")
1242                    .body(Body::from(body.to_string()))
1243                    .unwrap(),
1244            )
1245            .await
1246            .unwrap()
1247    }
1248
1249    #[tokio::test]
1250    async fn refresh_token_grant_returns_new_access_and_refresh() {
1251        let state = test_state();
1252        let (orig_access, refresh) = auth_code_full_flow(state.clone()).await;
1253        let resp = post_token(
1254            state.clone(),
1255            &format!("grant_type=refresh_token&refresh_token={refresh}&client_id=test-id"),
1256        )
1257        .await;
1258        assert_eq!(resp.status(), StatusCode::OK);
1259        let body = body_string(resp).await;
1260        let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
1261        let new_access = parsed["access_token"].as_str().unwrap();
1262        let new_refresh = parsed["refresh_token"].as_str().unwrap();
1263        assert_ne!(new_access, orig_access);
1264        assert_ne!(new_refresh, refresh);
1265        assert!(state.validate_token(new_access).await);
1266    }
1267
1268    #[tokio::test]
1269    async fn refresh_token_grant_invalidates_old_refresh_token() {
1270        let state = test_state();
1271        let (_, refresh) = auth_code_full_flow(state.clone()).await;
1272        // First use OK
1273        let r1 = post_token(
1274            state.clone(),
1275            &format!("grant_type=refresh_token&refresh_token={refresh}&client_id=test-id"),
1276        )
1277        .await;
1278        assert_eq!(r1.status(), StatusCode::OK);
1279        // Second use of same refresh token must fail
1280        let r2 = post_token(
1281            state,
1282            &format!("grant_type=refresh_token&refresh_token={refresh}&client_id=test-id"),
1283        )
1284        .await;
1285        assert_eq!(r2.status(), StatusCode::BAD_REQUEST);
1286        let body = body_string(r2).await;
1287        assert!(body.contains("invalid_grant"));
1288    }
1289
1290    #[tokio::test]
1291    async fn refresh_token_replay_revokes_chain() {
1292        let state = test_state();
1293        let (orig_access, refresh) = auth_code_full_flow(state.clone()).await;
1294        let r1 = post_token(
1295            state.clone(),
1296            &format!("grant_type=refresh_token&refresh_token={refresh}&client_id=test-id"),
1297        )
1298        .await;
1299        let body = body_string(r1).await;
1300        let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
1301        let new_access = parsed["access_token"].as_str().unwrap().to_string();
1302        // Replay original refresh — must revoke chain.
1303        let _ = post_token(
1304            state.clone(),
1305            &format!("grant_type=refresh_token&refresh_token={refresh}&client_id=test-id"),
1306        )
1307        .await;
1308        // Both the new access AND the original access must now be invalid.
1309        assert!(
1310            !state.validate_token(&new_access).await,
1311            "new access should be revoked after replay"
1312        );
1313        assert!(
1314            !state.validate_token(&orig_access).await,
1315            "original access should be revoked after replay"
1316        );
1317    }
1318
1319    #[tokio::test]
1320    async fn refresh_token_grant_with_unknown_token_returns_invalid_grant() {
1321        let state = test_state();
1322        let resp = post_token(
1323            state,
1324            "grant_type=refresh_token&refresh_token=never-issued&client_id=test-id",
1325        )
1326        .await;
1327        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
1328        let body = body_string(resp).await;
1329        assert!(body.contains("invalid_grant"));
1330    }
1331
1332    #[tokio::test]
1333    async fn auth_code_is_single_use() {
1334        let state = test_state();
1335        let verifier = "vvv";
1336        let challenge = challenge_for(verifier);
1337        let auth_uri = format!(
1338            "/authorize?response_type=code&client_id=test-id&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_challenge={challenge}&code_challenge_method=S256&state=s",
1339        );
1340        let resp = router(state.clone())
1341            .oneshot(
1342                Request::builder()
1343                    .uri(auth_uri)
1344                    .body(Body::empty())
1345                    .unwrap(),
1346            )
1347            .await
1348            .unwrap();
1349        let location = resp
1350            .headers()
1351            .get(header::LOCATION)
1352            .unwrap()
1353            .to_str()
1354            .unwrap()
1355            .to_string();
1356        let code = location
1357            .split_once("code=")
1358            .and_then(|(_, r)| r.split('&').next())
1359            .unwrap()
1360            .to_string();
1361        let body = format!(
1362            "grant_type=authorization_code&code={code}&redirect_uri=https%3A%2F%2Fclaude.ai%2Fapi%2Fmcp%2Fauth_callback&code_verifier={verifier}&client_id=test-id"
1363        );
1364        let make_req = || {
1365            Request::builder()
1366                .method("POST")
1367                .uri("/oauth/token")
1368                .header("content-type", "application/x-www-form-urlencoded")
1369                .body(Body::from(body.clone()))
1370                .unwrap()
1371        };
1372        let first = router(state.clone()).oneshot(make_req()).await.unwrap();
1373        assert_eq!(first.status(), StatusCode::OK);
1374        let second = router(state).oneshot(make_req()).await.unwrap();
1375        assert_eq!(second.status(), StatusCode::BAD_REQUEST);
1376    }
1377}