Skip to main content

rustapi_extras/
lib.rs

1//! # rustapi-extras
2//!
3//! Optional security and utility features for the RustAPI framework.
4//!
5//! This crate provides production-ready middleware and utilities that are
6//! opt-in via Cargo feature flags to minimize binary size when not needed.
7//!
8//! ## Features
9//!
10//! - `jwt` - JWT authentication middleware and `AuthUser<T>` extractor
11//! - `cors` - CORS middleware with builder pattern configuration
12//! - `rate-limit` - IP-based rate limiting middleware
13//! - `csrf` - CSRF protection using Double-Submit Cookie pattern
14//! - `config` - Configuration management with `.env` file support
15//! - `cookies` - Cookie parsing extractor
16//! - `sqlx` - SQLx database error conversion to ApiError
17//! - `insight` - Traffic insight middleware for analytics and debugging
18//! - `extras` - Meta feature enabling jwt, cors, and rate-limit
19//! - `full` - All features enabled
20//!
21//! ## CSRF Protection Example
22//!
23//! ```rust,ignore
24//! use rustapi_core::RustApi;
25//! use rustapi_extras::csrf::{CsrfConfig, CsrfLayer, CsrfToken};
26//!
27//! let config = CsrfConfig::new()
28//!     .cookie_name("csrf_token")
29//!     .header_name("X-CSRF-Token");
30//!
31//! let app = RustApi::new()
32//!     .layer(CsrfLayer::new(config));
33//! ```
34//!
35//! ## Example
36//!
37//! ```toml
38//! [dependencies]
39//! rustapi-extras = { version = "0.1", features = ["jwt", "cors", "csrf"] }
40//! ```
41
42#![warn(missing_docs)]
43#![warn(rustdoc::missing_crate_level_docs)]
44
45// JWT authentication module
46#[cfg(feature = "jwt")]
47pub mod jwt;
48
49// CORS middleware module
50#[cfg(feature = "cors")]
51pub mod cors;
52
53// Rate limiting module
54#[cfg(feature = "rate-limit")]
55pub mod rate_limit;
56
57// Configuration management module
58#[cfg(feature = "config")]
59pub mod config;
60
61// SQLx database integration module
62#[cfg(feature = "sqlx")]
63pub mod sqlx;
64
65// Diesel database integration module
66#[cfg(feature = "diesel")]
67pub mod diesel;
68
69// Traffic insight module
70#[cfg(feature = "insight")]
71pub mod insight;
72
73// Request timeout middleware
74#[cfg(feature = "timeout")]
75pub mod timeout;
76
77// Request guards (authorization)
78#[cfg(feature = "guard")]
79pub mod guard;
80
81// Request/Response logging middleware
82#[cfg(feature = "logging")]
83pub mod logging;
84
85// Circuit breaker middleware
86#[cfg(feature = "circuit-breaker")]
87pub mod circuit_breaker;
88
89// Retry middleware
90#[cfg(feature = "retry")]
91pub mod retry;
92
93// Request deduplication
94#[cfg(feature = "dedup")]
95pub mod dedup;
96
97// Input sanitization
98#[cfg(feature = "sanitization")]
99pub mod sanitization;
100
101// Security headers middleware
102#[cfg(feature = "security-headers")]
103pub mod security_headers;
104
105// API Key authentication
106#[cfg(feature = "api-key")]
107pub mod api_key;
108
109// Response caching
110#[cfg(feature = "cache")]
111pub mod cache;
112
113// OpenTelemetry integration
114#[cfg(feature = "otel")]
115pub mod otel;
116
117// Structured logging
118#[cfg(feature = "structured-logging")]
119pub mod structured_logging;
120
121// Re-exports for convenience
122#[cfg(feature = "jwt")]
123pub use jwt::{create_token, AuthUser, JwtError, JwtLayer, JwtValidation, ValidatedClaims};
124
125#[cfg(feature = "cors")]
126pub use cors::{AllowedOrigins, CorsLayer};
127
128#[cfg(feature = "rate-limit")]
129pub use rate_limit::RateLimitLayer;
130
131#[cfg(feature = "config")]
132pub use config::{
133    env_or, env_parse, load_dotenv, load_dotenv_from, require_env, try_require_env, Config,
134    ConfigError, Environment,
135};
136
137#[cfg(feature = "sqlx")]
138pub use sqlx::{convert_sqlx_error, PoolError, SqlxErrorExt, SqlxPoolBuilder, SqlxPoolConfig};
139
140#[cfg(feature = "diesel")]
141pub use diesel::{DieselPoolBuilder, DieselPoolConfig, DieselPoolError};
142
143#[cfg(feature = "insight")]
144pub use insight::{
145    InMemoryInsightStore, InsightConfig, InsightData, InsightLayer, InsightStats, InsightStore,
146};
147
148// Phase 11 re-exports
149#[cfg(feature = "timeout")]
150pub use timeout::TimeoutLayer;
151
152#[cfg(feature = "guard")]
153pub use guard::{PermissionGuard, RoleGuard};
154
155#[cfg(feature = "logging")]
156pub use logging::{LogFormat, LoggingConfig, LoggingLayer};
157
158#[cfg(feature = "circuit-breaker")]
159pub use circuit_breaker::{CircuitBreakerLayer, CircuitBreakerStats, CircuitState};
160
161#[cfg(feature = "retry")]
162pub use retry::{RetryLayer, RetryStrategy};
163
164#[cfg(feature = "security-headers")]
165pub use security_headers::{HstsConfig, ReferrerPolicy, SecurityHeadersLayer, XFrameOptions};
166
167#[cfg(feature = "api-key")]
168pub use api_key::ApiKeyLayer;
169
170#[cfg(feature = "cache")]
171pub use cache::{CacheConfig, CacheLayer};
172
173#[cfg(feature = "dedup")]
174pub use dedup::{DedupConfig, DedupLayer};
175
176#[cfg(feature = "sanitization")]
177pub use sanitization::{sanitize_html, sanitize_json, strip_tags};
178
179// Phase 5: Observability re-exports
180#[cfg(feature = "otel")]
181pub use otel::{
182    extract_trace_context, inject_trace_context, propagate_trace_context, OtelConfig,
183    OtelConfigBuilder, OtelExporter, OtelLayer, TraceContext, TraceSampler,
184};
185
186#[cfg(feature = "structured-logging")]
187pub use structured_logging::{
188    DatadogFormatter, JsonFormatter, LogFormatter, LogOutputFormat, LogfmtFormatter,
189    SplunkFormatter, StructuredLoggingConfig, StructuredLoggingConfigBuilder,
190    StructuredLoggingLayer,
191};
192
193// Phase 6: Security features
194#[cfg(feature = "csrf")]
195pub mod csrf;
196
197#[cfg(feature = "csrf")]
198pub use csrf::{CsrfConfig, CsrfLayer, CsrfToken};
199
200#[cfg(feature = "oauth2-client")]
201pub mod oauth2;
202
203#[cfg(feature = "oauth2-client")]
204pub use oauth2::{
205    AuthorizationRequest, CsrfState, OAuth2Client, OAuth2Config, PkceVerifier, Provider,
206    TokenError, TokenResponse,
207};
208
209#[cfg(feature = "audit")]
210pub mod audit;
211
212#[cfg(feature = "audit")]
213pub use audit::{
214    AuditAction, AuditEvent, AuditQuery, AuditQueryBuilder, AuditSeverity, AuditStore,
215    ComplianceInfo, FileAuditStore, InMemoryAuditStore,
216};