rustapi-extras 0.1.478

Production-ready middleware collection for RustAPI. Includes JWT auth, CORS, Rate Limiting, SQLx integration, and OpenTelemetry observability.
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! OAuth2 token types and errors

use std::time::{Duration, Instant};
use thiserror::Error;

/// OAuth2 token response from the authorization server.
#[derive(Debug, Clone)]
pub struct TokenResponse {
    /// The access token.
    access_token: String,
    /// The token type (usually "Bearer").
    token_type: String,
    /// Token expiration time (if provided).
    expires_at: Option<Instant>,
    /// Refresh token (if provided).
    refresh_token: Option<String>,
    /// Scopes granted (if different from requested).
    scopes: Option<Vec<String>>,
    /// ID token for OpenID Connect (if provided).
    id_token: Option<String>,
}

impl TokenResponse {
    /// Create a new token response.
    pub fn new(access_token: String, token_type: String) -> Self {
        Self {
            access_token,
            token_type,
            expires_at: None,
            refresh_token: None,
            scopes: None,
            id_token: None,
        }
    }

    /// Set the expiration time.
    pub fn with_expires_in(mut self, expires_in: Duration) -> Self {
        self.expires_at = Some(Instant::now() + expires_in);
        self
    }

    /// Set the refresh token.
    pub fn with_refresh_token(mut self, refresh_token: String) -> Self {
        self.refresh_token = Some(refresh_token);
        self
    }

    /// Set the scopes.
    pub fn with_scopes(mut self, scopes: Vec<String>) -> Self {
        self.scopes = Some(scopes);
        self
    }

    /// Set the ID token.
    pub fn with_id_token(mut self, id_token: String) -> Self {
        self.id_token = Some(id_token);
        self
    }

    /// Get the access token.
    pub fn access_token(&self) -> &str {
        &self.access_token
    }

    /// Get the token type.
    pub fn token_type(&self) -> &str {
        &self.token_type
    }

    /// Check if the token is expired.
    pub fn is_expired(&self) -> bool {
        match self.expires_at {
            Some(expires_at) => Instant::now() >= expires_at,
            None => false, // If no expiration, assume not expired
        }
    }

    /// Get the refresh token (if present).
    pub fn refresh_token(&self) -> Option<&str> {
        self.refresh_token.as_deref()
    }

    /// Get the ID token (if present, for OpenID Connect).
    pub fn id_token(&self) -> Option<&str> {
        self.id_token.as_deref()
    }

    /// Get the scopes (if provided in response).
    pub fn scopes(&self) -> Option<&[String]> {
        self.scopes.as_deref()
    }

    /// Get the time remaining until expiration.
    pub fn expires_in(&self) -> Option<Duration> {
        self.expires_at
            .and_then(|exp| exp.checked_duration_since(Instant::now()))
    }

    /// Get the Authorization header value.
    pub fn authorization_header(&self) -> String {
        format!("{} {}", self.token_type, self.access_token)
    }
}

/// Errors that can occur during OAuth2 operations.
#[derive(Debug, Error)]
pub enum TokenError {
    /// The authorization request was denied.
    #[error("Authorization denied: {0}")]
    AuthorizationDenied(String),

    /// Invalid authorization code.
    #[error("Invalid authorization code")]
    InvalidCode,

    /// Invalid CSRF state.
    #[error("Invalid CSRF state - possible CSRF attack")]
    InvalidState,

    /// Token exchange failed.
    #[error("Token exchange failed: {0}")]
    ExchangeFailed(String),

    /// Token refresh failed.
    #[error("Token refresh failed: {0}")]
    RefreshFailed(String),

    /// Network error.
    #[error("Network error: {0}")]
    NetworkError(String),

    /// Invalid response from the authorization server.
    #[error("Invalid response: {0}")]
    InvalidResponse(String),

    /// Token is expired.
    #[error("Token is expired")]
    TokenExpired,

    /// Missing required field in response.
    #[error("Missing required field: {0}")]
    MissingField(String),
}

/// PKCE (Proof Key for Code Exchange) verifier.
#[derive(Debug, Clone)]
pub struct PkceVerifier {
    verifier: String,
    challenge: String,
    method: String,
}

impl PkceVerifier {
    /// Rebuild a PKCE verifier from an existing verifier string.
    pub fn new(verifier: impl Into<String>) -> Self {
        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
        use sha2::{Digest, Sha256};

        let verifier = verifier.into();
        let mut hasher = Sha256::new();
        hasher.update(verifier.as_bytes());
        let hash = hasher.finalize();
        let challenge = URL_SAFE_NO_PAD.encode(hash);

        Self {
            verifier,
            challenge,
            method: "S256".to_string(),
        }
    }

    /// Generate a new PKCE verifier with S256 challenge.
    pub fn generate() -> Self {
        use rand::{rngs::OsRng, RngCore};

        // Generate 32 random bytes for the verifier
        let mut verifier_bytes = [0u8; 32];
        OsRng.fill_bytes(&mut verifier_bytes);
        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
        Self::new(URL_SAFE_NO_PAD.encode(verifier_bytes))
    }

    /// Get the code verifier (for token exchange).
    pub fn verifier(&self) -> &str {
        &self.verifier
    }

    /// Get the code challenge (for authorization request).
    pub fn challenge(&self) -> &str {
        &self.challenge
    }

    /// Get the challenge method (S256).
    pub fn method(&self) -> &str {
        &self.method
    }
}

/// CSRF state token for OAuth2 authorization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsrfState(String);

impl CsrfState {
    /// Generate a new random CSRF state.
    pub fn generate() -> Self {
        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
        use rand::{rngs::OsRng, RngCore};

        let mut bytes = [0u8; 16];
        OsRng.fill_bytes(&mut bytes);
        Self(URL_SAFE_NO_PAD.encode(bytes))
    }

    /// Create from an existing string.
    pub fn new(state: String) -> Self {
        Self(state)
    }

    /// Get the state value.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Verify that this state matches another.
    pub fn verify(&self, other: &str) -> bool {
        // Use constant-time comparison to prevent timing attacks
        // For simplicity, we use direct comparison here
        self.0 == other
    }
}

impl std::fmt::Display for CsrfState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_token_response() {
        let token = TokenResponse::new("access123".to_string(), "Bearer".to_string())
            .with_refresh_token("refresh456".to_string())
            .with_expires_in(Duration::from_secs(3600));

        assert_eq!(token.access_token(), "access123");
        assert_eq!(token.token_type(), "Bearer");
        assert_eq!(token.refresh_token(), Some("refresh456"));
        assert!(!token.is_expired());
        assert_eq!(token.authorization_header(), "Bearer access123");
    }

    #[test]
    fn test_pkce_verifier() {
        let pkce = PkceVerifier::generate();
        assert!(!pkce.verifier().is_empty());
        assert!(!pkce.challenge().is_empty());
        assert_eq!(pkce.method(), "S256");

        // Verifier and challenge should be different
        assert_ne!(pkce.verifier(), pkce.challenge());

        let rebuilt = PkceVerifier::new(pkce.verifier().to_string());
        assert_eq!(rebuilt.verifier(), pkce.verifier());
        assert_eq!(rebuilt.challenge(), pkce.challenge());
    }

    #[test]
    fn test_csrf_state() {
        let state1 = CsrfState::generate();
        let state2 = CsrfState::generate();

        // Each generated state should be unique
        assert_ne!(state1, state2);

        // Verification should work
        assert!(state1.verify(state1.as_str()));
        assert!(!state1.verify(state2.as_str()));
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    /// **Feature: v1-features-roadmap, Property 16: OAuth2 token exchange**
    /// **Validates: Requirements 10.1, 10.4**
    ///
    /// For any valid OAuth2 token exchange:
    /// - Authorization code SHALL successfully exchange for access token
    /// - Token response SHALL contain valid access token and token type
    /// - PKCE verifier/challenge pairs SHALL validate correctly
    /// - CSRF state tokens SHALL prevent cross-site request forgery

    /// Strategy for generating access tokens
    fn access_token_strategy() -> impl Strategy<Value = String> {
        prop::string::string_regex("[a-zA-Z0-9_.-]{20,100}").unwrap()
    }

    /// Strategy for generating token types
    fn token_type_strategy() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("Bearer".to_string()),
            Just("bearer".to_string()),
            Just("MAC".to_string()),
        ]
    }

    /// Strategy for generating refresh tokens
    fn refresh_token_strategy() -> impl Strategy<Value = Option<String>> {
        prop_oneof![
            Just(None),
            prop::string::string_regex("[a-zA-Z0-9_.-]{20,100}")
                .unwrap()
                .prop_map(Some),
        ]
    }

    /// Strategy for generating expiration durations
    fn expires_in_strategy() -> impl Strategy<Value = Option<Duration>> {
        prop_oneof![
            Just(None),
            (300u64..86400).prop_map(|secs| Some(Duration::from_secs(secs))),
        ]
    }

    /// Strategy for generating scopes
    fn scopes_strategy() -> impl Strategy<Value = Option<Vec<String>>> {
        prop_oneof![
            Just(None),
            prop::collection::vec("[a-z]{3,10}", 0..5).prop_map(Some),
        ]
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property 16: Token response contains valid access token
        #[test]
        fn prop_token_response_has_access_token(
            access_token in access_token_strategy(),
            token_type in token_type_strategy(),
        ) {
            let response = TokenResponse::new(access_token.clone(), token_type.clone());

            prop_assert_eq!(response.access_token(), access_token.as_str());
            prop_assert_eq!(response.token_type(), token_type.as_str());
        }

        /// Property 16: Token response with expiration tracks time correctly
        #[test]
        fn prop_token_expiration_tracking(
            access_token in access_token_strategy(),
            token_type in token_type_strategy(),
            expires_in_secs in 1u64..3600,
        ) {
            let expires_in = Duration::from_secs(expires_in_secs);
            let response = TokenResponse::new(access_token, token_type)
                .with_expires_in(expires_in);

            // Token should not be expired immediately after creation
            prop_assert!(!response.is_expired());

            // Token should have expiration time
            let remaining = response.expires_in();
            prop_assert!(remaining.is_some());

            // Remaining time should be close to expires_in (within a few seconds)
            let remaining_secs = remaining.unwrap().as_secs();
            prop_assert!(remaining_secs <= expires_in_secs);
            prop_assert!(remaining_secs >= expires_in_secs.saturating_sub(2)); // Allow 2 sec tolerance
        }

        /// Property 16: Token response builder pattern works correctly
        #[test]
        fn prop_token_response_builder(
            access_token in access_token_strategy(),
            token_type in token_type_strategy(),
            refresh_token in refresh_token_strategy(),
            scopes in scopes_strategy(),
        ) {
            let mut response = TokenResponse::new(access_token.clone(), token_type.clone());

            if let Some(ref rt) = refresh_token {
                response = response.with_refresh_token(rt.clone());
            }

            if let Some(ref sc) = scopes {
                response = response.with_scopes(sc.clone());
            }

            prop_assert_eq!(response.access_token(), access_token.as_str());
            prop_assert_eq!(response.refresh_token(), refresh_token.as_deref());

            match (response.scopes(), scopes.as_ref()) {
                (Some(got), Some(expected)) => prop_assert_eq!(got, expected.as_slice()),
                (None, None) => {},
                _ => prop_assert!(false, "Scope mismatch"),
            }
        }

        /// Property 16: Authorization header format is correct
        #[test]
        fn prop_authorization_header_format(
            access_token in access_token_strategy(),
            token_type in token_type_strategy(),
        ) {
            let response = TokenResponse::new(access_token.clone(), token_type.clone());
            let header = response.authorization_header();

            let expected = format!("{} {}", token_type, access_token);
            prop_assert_eq!(header.clone(), expected);

            // Header should start with token type
            prop_assert!(header.starts_with(&token_type));
            // Header should end with access token
            prop_assert!(header.ends_with(&access_token));
        }

        /// Property 16: PKCE verifier generates unique challenges
        #[test]
        fn prop_pkce_generates_unique_challenges(_seed in 0u32..100) {
            let pkce1 = PkceVerifier::generate();
            let pkce2 = PkceVerifier::generate();

            // Each generation should produce unique verifiers and challenges
            prop_assert_ne!(pkce1.verifier(), pkce2.verifier());
            prop_assert_ne!(pkce1.challenge(), pkce2.challenge());

            // Method should always be S256
            prop_assert_eq!(pkce1.method(), "S256");
            prop_assert_eq!(pkce2.method(), "S256");
        }

        /// Property 16: PKCE verifier and challenge are different
        #[test]
        fn prop_pkce_verifier_challenge_different(_seed in 0u32..100) {
            let pkce = PkceVerifier::generate();

            // Verifier and challenge must be different (challenge is hash of verifier)
            prop_assert_ne!(pkce.verifier(), pkce.challenge());

            // Both should be non-empty
            prop_assert!(!pkce.verifier().is_empty());
            prop_assert!(!pkce.challenge().is_empty());

            // Both should be URL-safe base64
            prop_assert!(!pkce.verifier().contains('='));
            prop_assert!(!pkce.challenge().contains('='));
        }

        /// Property 16: PKCE challenge is deterministic for same verifier
        #[test]
        fn prop_pkce_challenge_deterministic(verifier_input in "[a-zA-Z0-9_-]{32,64}") {
            use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
            use sha2::{Digest, Sha256};

            // Create challenge from verifier
            let mut hasher = Sha256::new();
            hasher.update(verifier_input.as_bytes());
            let hash = hasher.finalize();
            let expected_challenge = URL_SAFE_NO_PAD.encode(hash);

            // Generate again with same verifier - should produce same challenge
            let mut hasher2 = Sha256::new();
            hasher2.update(verifier_input.as_bytes());
            let hash2 = hasher2.finalize();
            let challenge2 = URL_SAFE_NO_PAD.encode(hash2);

            prop_assert_eq!(expected_challenge, challenge2);
        }

        /// Property 16: CSRF state tokens are unique
        #[test]
        fn prop_csrf_state_unique(_seed in 0u32..100) {
            let state1 = CsrfState::generate();
            let state2 = CsrfState::generate();

            // Each state should be unique
            prop_assert_ne!(state1.clone(), state2.clone());
            prop_assert_ne!(state1.as_str(), state2.as_str());
        }

        /// Property 16: CSRF state verification is accurate
        #[test]
        fn prop_csrf_state_verification(
            valid_state_str in "[a-zA-Z0-9_-]{10,50}",
            invalid_state_str in "[a-zA-Z0-9_-]{10,50}",
        ) {
            prop_assume!(valid_state_str != invalid_state_str);

            let state = CsrfState::new(valid_state_str.clone());

            // Should verify against itself
            prop_assert!(state.verify(&valid_state_str));

            // Should not verify against different string
            prop_assert!(!state.verify(&invalid_state_str));
        }

        /// Property 16: CSRF state round-trip preserves value
        #[test]
        fn prop_csrf_state_roundtrip(state_str in "[a-zA-Z0-9_-]{10,50}") {
            let state1 = CsrfState::new(state_str.clone());
            let state2 = CsrfState::new(state1.as_str().to_string());

            prop_assert_eq!(state1.clone(), state2.clone());
            prop_assert_eq!(state1.as_str(), state2.as_str());
        }

        /// Property 16: Token expiration behaves correctly
        #[test]
        fn prop_token_expiration_behavior(
            access_token in access_token_strategy(),
            has_expiration in proptest::bool::ANY,
        ) {
            let mut response = TokenResponse::new(access_token, "Bearer".to_string());

            if has_expiration {
                response = response.with_expires_in(Duration::from_secs(3600));
                prop_assert!(!response.is_expired());
                prop_assert!(response.expires_in().is_some());
            } else {
                // Without expiration, should never be expired
                prop_assert!(!response.is_expired());
                prop_assert!(response.expires_in().is_none());
            }
        }
    }
}