1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! # TurboMCP Auth - Unified Authentication Framework
//!
//! World-class authentication and authorization for TurboMCP with standards-compliant
//! implementations of OAuth 2.1, JWT, API keys, and DPoP token binding.
// Allow missing error/panic docs - auth errors are self-documenting through dedicated error types
//!
//! ## Design Principles
//!
//! - **Single Source of Truth**: ONE canonical `AuthContext` type used everywhere
//! - **Feature-Gated Complexity**: Simple by default, powerful when needed
//! - **Zero-Cost Abstractions**: No overhead for unused features
//! - **Standards-Compliant**: OAuth 2.1, RFC 7519 (JWT), RFC 9449 (DPoP), RFC 9728
//!
//! ## Key Features
//!
//! - **Unified AuthContext** - Single type for all authentication scenarios
//! - **OAuth 2.1** - RFC 8707/9728/7591 compliant with PKCE support
//! - **Multi-Provider** - Google, GitHub, Microsoft, GitLab out of the box
//! - **API Key Auth** - Simple and secure API key authentication
//! - **RBAC Support** - Role-based access control with fine-grained permissions
//! - **Session Management** - Flexible token storage and lifecycle management
//! - **DPoP Support** - Optional RFC 9449 proof-of-possession tokens
//!
//! ## Architecture
//!
//! - [`context`] - Unified `AuthContext` type (THE canonical auth representation)
//! - [`types`] - Core types (UserInfo, TokenInfo, provider traits)
//! - [`config`] - Configuration types for authentication providers
//! - [`providers`] - Authentication provider implementations
//! - `api_key` - API key authentication
//! - `oauth2` - OAuth 2.1 provider
//! - [`manager`] - Authentication manager for provider orchestration
//! - [`oauth2`] - OAuth 2.1 client with authorization flows
//! - [`server`] - Server-side authentication helpers (RFC 9728 Protected Resource)
//!
//! ## Quick Start
//!
//! ```rust
//! use turbomcp_auth::{AuthContext, UserInfo};
//! use std::time::SystemTime;
//! use std::collections::HashMap;
//!
//! // Create an auth context using the builder
//! let user = UserInfo {
//! id: "user123".to_string(),
//! username: "alice".to_string(),
//! email: Some("alice@example.com".to_string()),
//! display_name: Some("Alice".to_string()),
//! avatar_url: None,
//! metadata: HashMap::new(),
//! };
//!
//! let auth = AuthContext::builder()
//! .subject("user123")
//! .user(user)
//! .provider("api-key")
//! .roles(vec!["admin".to_string(), "user".to_string()])
//! .permissions(vec!["write:data".to_string()])
//! .build()
//! .unwrap();
//!
//! // Check authorization
//! if auth.has_role("admin") && auth.has_permission("write:data") {
//! println!("User {} has write access", auth.sub);
//! }
//! ```
//!
//! ## Feature Flags
//!
//! ### Default Features
//! - `api-key` - API key authentication
//! - `oauth2` - OAuth 2.1 flows and providers
//!
//! ### Core Authentication Methods
//! - `jwt` - JWT token validation
//! - `custom` - Custom auth provider support (traits only)
//!
//! ### Advanced Features
//! - `dpop` - RFC 9449 DPoP token binding
//! - `rbac` - Role-based access control helpers
//!
//! ### Token Lifecycle
//! - `token-refresh` - Automatic token refresh
//! - `token-revocation` - Token revocation support
//!
//! ### Observability
//! - `metrics` - Metrics collection (future)
//! - `tracing-ext` - Extended tracing support
//!
//! ### Middleware
//! - `middleware` - Tower middleware support (future)
//!
//! ### Batteries-Included
//! - `full` - All features enabled
//!
//! ## Standards Compliance
//!
//! - **RFC 7519** - JSON Web Token (JWT)
//! - **RFC 6749** - OAuth 2.0 Authorization Framework
//! - **RFC 7636** - Proof Key for Code Exchange (PKCE)
//! - **RFC 8707** - OAuth 2.0 Resource Indicators
//! - **RFC 9449** - OAuth 2.0 Demonstrating Proof-of-Possession (DPoP)
//! - **RFC 9728** - OAuth 2.0 Protected Resource Metadata
// Submodules
// Sprint 2.3: Constant-time API key validation
// Structured audit logging for auth events
// Metrics collection for auth operations
// Rate limiting for auth endpoints
// Tower middleware integration
// SSRF protection - always available for JWT/JWKS validation security
// MCP 2025-11-25 Draft Specification modules
// Client ID Metadata Documents (SEP-991)
// OpenID Connect Discovery 1.0 and RFC 8414 (Authorization Server Metadata)
// Incremental Scope Consent via WWW-Authenticate (SEP-835)
// Re-export configuration types
pub use *;
// Re-export legacy types (excluding old AuthContext to avoid conflict with unified version)
pub use ;
// Re-export unified context types (this is the canonical AuthContext)
pub use ;
// Re-export providers
pub use *;
// Re-export manager
pub use AuthManager;
// Re-export audit logging
pub use ;
// Re-export rate limiting
pub use ;
// Re-export metrics initialization
pub use init_auth_metrics;
// Re-export DPoP types when feature is enabled
pub use turbomcp_dpop as dpop;