Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Apps of others: OAuth and MCP

use std::sync::Arc;
use auth_chad::axum::{Auth, ConnectedApp, Refused};
use auth_chad::provider::{Provider, Scope};
use auth_chad::sqlite::SqliteStore;
use auth_chad::Code;

fn connect(auth: Auth<SqliteStore>) -> Arc<Auth<SqliteStore>> {
    let read = Scope { name: "notes:read".into(), description: "Read your notes".into(), connected: true, write: false };
    let provider = Provider {
        scopes: vec![read],
        audiences: vec!["https://app.example.com/mcp".into()],
        dynamic_registration: true,   // some MCP clients still need it
        ..Provider::default()
    };
    Arc::new(auth.with_provider(provider).with_pages("/consent", "/device"))
}

async fn mcp(app: ConnectedApp) -> Result<&'static str, Refused> {
    if app.0.has_scope("notes:read") { Ok("data") } else { Err(Code::Forbidden.into()) }
}
  • Merge auth.oauth_router() at the root: metadata, authorize, token, revoke, register.
  • Consent page: it gets ?request=<id>. GET /auth/consent/{id} shows the client, its redirect host and the scopes; POST {"allow": true} answers {"redirect": "…"}. Show the host of redirect_uri: a name proves nothing.
  • Client metadata documents: implement Hooks::fetch_client with your HTTP client. Public addresses only, no redirects, 64 KiB, a timeout: the URL comes from a stranger.
  • MCP: answer 401 with WWW-Authenticate: Bearer resource_metadata="https://app.example.com/.well-known/oauth-protected-resource".
  • A connected app never reaches a FirstParty handler.