TurboMCP Auth
OAuth 2.1 and authentication for TurboMCP with MCP protocol compliance.
Features
- OAuth 2.1 Flows - RFC 8707/9728/7591 compliant with PKCE support
- Authorization Code flow (with PKCE for public/confidential clients)
- Client Credentials flow (server-to-server)
- Token refresh and validation
- Multi-Provider Support - Google, GitHub, Microsoft, GitLab (with provider-specific configurations)
- OAuth2Provider - Full AuthProvider implementation for OAuth 2.1
- API Key Authentication - Simple API key-based authentication
- Server-Side Helpers - RFC 9728 Protected Resource Metadata and WWW-Authenticate headers
- Session Management - Secure token management with configurable storage
- DPoP Support - Optional RFC 9449 proof-of-possession tokens
- Comprehensive Validation - RFC 8707 canonical URI validation, token format validation
Quick Start
Client: OAuth 2.1 Authorization Code Flow
use ;
async
Server: Protected Resource with RFC 9728 Metadata
use ;
// Serve Protected Resource Metadata at /.well-known/protected-resource
// Handle 401 Unauthorized responses
// Validate incoming bearer tokens
Usage
[]
= "2.1.0"
# With DPoP support for enhanced security
= { = "2.1.0", = ["dpop"] }
# With tokio runtime
= { = "1", = ["full"] }
= { = "1", = ["v4"] }
Feature Flags
default- Core authentication (no optional features)dpop- Enable DPoP (RFC 9449) token binding support viaturbomcp-dpop
Architecture
Core Components
-
OAuth2Client (
oauth2::OAuth2Client)- Authorization Code flow with PKCE (RFC 7636)
- Client Credentials flow (server-to-server)
- Token refresh and validation
- Provider-specific configurations for Google, GitHub, Microsoft, GitLab
-
OAuth2Provider (
providers::OAuth2Provider)- Implements AuthProvider trait
- Token validation via userinfo endpoint
- Token caching and refresh management
- Integration with authentication manager
-
AuthManager (
manager::AuthManager)- Coordinates multiple authentication providers
- Stateless authentication (MCP compliant)
- Token validation on every request
-
Server Helpers (
server::*)ProtectedResourceMetadataBuilder- RFC 9728 metadata generationWwwAuthenticateBuilder- RFC 9728 401 response headersBearerTokenValidator- Token extraction and validation
RFC Compliance
- RFC 7636 - PKCE (Proof Key for Public OAuth Clients)
- RFC 7591 - Dynamic Client Registration Protocol
- RFC 8707 - Resource Indicators for OAuth 2.0
- RFC 9728 - OAuth 2.0 Protected Resource Metadata
- RFC 9449 - DPoP (optional, via
turbomcp-dpop)
Examples
Run the examples to see the implementations in action:
# OAuth 2.1 Authorization Code Flow
# Protected Resource Server with RFC 9728
Security Best Practices
- Use HTTPS - Always use HTTPS for redirect URIs and token endpoints
- PKCE - Automatically enabled for Authorization Code flow (RFC 7636)
- Token Storage - Tokens are never logged or serialized unnecessarily
- Constant-Time Comparison - Token validation uses constant-time comparison
- DPoP - Enable DPoP feature for enhanced security (RFC 9449)
- Scope Validation - Always validate token scopes server-side
- Short Expiration - Use short-lived access tokens with refresh tokens
Testing
License
MIT