use turbomcp_auth::{
config::{OAuth2Config, OAuth2FlowType, ProviderType},
oauth2::OAuth2Client,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let oauth_config = OAuth2Config {
client_id: "my-client-id".to_string(),
client_secret: "my-client-secret".to_string().into(), auth_url: "https://provider.example.com/oauth/authorize".to_string(),
token_url: "https://provider.example.com/oauth/token".to_string(),
revocation_url: Some("https://provider.example.com/oauth/revoke".to_string()), redirect_uri: "http://localhost:8080/callback".to_string(),
scopes: vec![
"openid".to_string(),
"profile".to_string(),
"email".to_string(),
],
flow_type: OAuth2FlowType::AuthorizationCode,
additional_params: std::collections::HashMap::new(),
security_level: Default::default(),
#[cfg(feature = "dpop")]
dpop_config: None,
mcp_resource_uri: None,
auto_resource_indicators: true,
};
let oauth_client = OAuth2Client::new(&oauth_config, ProviderType::Generic)?;
println!("=== OAuth 2.1 Authorization Code Flow ===\n");
let state = uuid::Uuid::new_v4().to_string(); let (auth_url, code_verifier) =
oauth_client.authorization_code_flow(oauth_config.scopes.clone(), state);
println!("1. Authorization URL (open in browser):");
println!(" {}\n", auth_url);
println!("2. Code Verifier (save for token exchange):");
println!(" {}\n", code_verifier);
println!("3. After user authorizes, authorization server redirects to:");
println!(
" {redirect_uri}?code=AUTH_CODE&state=STATE\n",
redirect_uri = oauth_config.redirect_uri
);
println!("4. To exchange code for token, call:");
println!(
" oauth_client.exchange_code_for_token(code, \"{}\").await?",
code_verifier
);
println!("\nThis returns TokenInfo with:");
println!(" - access_token: Bearer token for API requests");
println!(" - refresh_token: Token to refresh access_token (if provided by provider)");
println!(" - expires_in: Token expiration in seconds");
println!(" - scope: Granted scopes");
println!("\n5. Use access token in API requests:");
println!(" Authorization: Bearer {{access_token}}");
Ok(())
}