grpc_graphql_gateway 1.2.4

A Rust implementation of gRPC-GraphQL gateway - generates GraphQL execution code from gRPC services
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
#![allow(clippy::should_implement_trait)]
//! Middleware support for the gateway
//!
//! This module provides extensible middleware for authentication, logging, rate limiting,
//! and other cross-cutting concerns. Middleware is applied to all GraphQL requests
//! before they reach the resolver layer.

use crate::error::Result;
use axum::http::Request;
use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Instant;

/// Context passed to middleware
///
/// Contains request metadata and an extensible data store for middleware
/// to communicate with each other and with resolvers.
#[derive(Debug, Clone)]
pub struct Context {
    /// Request headers and metadata
    pub headers: axum::http::HeaderMap,

    /// Additional context data (user claims, request ID, etc.)
    pub extensions: HashMap<String, serde_json::Value>,

    /// Request start time for timing
    pub request_start: Instant,

    /// Unique request identifier
    pub request_id: String,

    /// Client IP address (if available)
    pub client_ip: Option<String>,

    /// Encryption key for field-level encryption
    pub encryption_key: Option<Vec<u8>>,
}

impl Context {
    /// Create a new context from request
    ///
    /// # Security
    ///
    /// IP address extraction validates the format to prevent spoofing.
    /// Invalid IPs are discarded. In production behind trusted proxies,
    /// configure your proxy to set X-Real-IP correctly.
    pub fn from_request<B>(req: &Request<B>) -> Self {
        // Extract or generate request ID
        let request_id = req
            .headers()
            .get("x-request-id")
            .and_then(|v| v.to_str().ok())
            .map(String::from)
            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

        // Try to extract client IP from headers (supports proxies)
        // SECURITY: Validate IP format to prevent spoofing
        let client_ip = req
            .headers()
            .get("x-forwarded-for")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.split(',').next())
            .map(|s| s.trim())
            .filter(|ip| Self::is_valid_ip(ip))
            .map(String::from)
            .or_else(|| {
                req.headers()
                    .get("x-real-ip")
                    .and_then(|v| v.to_str().ok())
                    .filter(|ip| Self::is_valid_ip(ip))
                    .map(String::from)
            });

        Self {
            headers: req.headers().clone(),
            extensions: HashMap::new(),
            request_start: Instant::now(),
            request_id,
            client_ip,
            encryption_key: None,
        }
    }

    /// Set encryption key for the request scope
    pub fn set_encryption_key(&mut self, key: Vec<u8>) {
        self.encryption_key = Some(key);
    }

    /// Get encryption key if set
    pub fn encryption_key(&self) -> Option<&[u8]> {
        self.encryption_key.as_deref()
    }

    /// Validate IP address format
    ///
    /// # Security
    ///
    /// Validates both IPv4 and IPv6 formats to prevent header injection attacks.
    fn is_valid_ip(ip: &str) -> bool {
        use std::net::IpAddr;
        ip.parse::<IpAddr>().is_ok()
    }

    /// Insert extension data
    pub fn insert(&mut self, key: String, value: serde_json::Value) {
        self.extensions.insert(key, value);
    }

    /// Get extension data
    pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
        self.extensions.get(key)
    }

    /// Get typed extension data
    pub fn get_typed<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
        self.extensions
            .get(key)
            .and_then(|v| serde_json::from_value(v.clone()).ok())
    }

    /// Check if a key exists in extensions
    pub fn contains(&self, key: &str) -> bool {
        self.extensions.contains_key(key)
    }

    /// Get elapsed time since request start
    pub fn elapsed(&self) -> std::time::Duration {
        self.request_start.elapsed()
    }

    /// Get user ID from auth context (convenience method)
    pub fn user_id(&self) -> Option<String> {
        self.get("auth.user_id")
            .and_then(|v| v.as_str())
            .map(String::from)
    }

    /// Get user roles from auth context (convenience method)
    pub fn user_roles(&self) -> Vec<String> {
        self.get("auth.roles")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(String::from))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Encrypt a value if an encryption key is present
    ///
    /// # Security Warning
    ///
    /// This method previously used XOR which provides NO real encryption.
    /// Use a proper AEAD cipher (AES-GCM, ChaCha20-Poly1305) for production.
    #[deprecated(
        note = "XOR cipher is not real encryption. Use AES-GCM or ChaCha20-Poly1305 instead."
    )]
    pub fn encrypt_value(&self, _value: &str) -> crate::error::Result<String> {
        Err(crate::error::Error::Internal(
            "encrypt_value is deprecated: XOR provides no real encryption. \
             Use a proper AEAD cipher (e.g., aes-gcm or chacha20poly1305 crates)."
                .to_string(),
        ))
    }

    /// Decrypt a value if an encryption key is present
    ///
    /// # Security Warning
    ///
    /// This method previously used XOR which provides NO real encryption.
    /// Use a proper AEAD cipher (AES-GCM, ChaCha20-Poly1305) for production.
    #[deprecated(
        note = "XOR cipher is not real encryption. Use AES-GCM or ChaCha20-Poly1305 instead."
    )]
    pub fn decrypt_value(&self, _value: &str) -> crate::error::Result<String> {
        Err(crate::error::Error::Internal(
            "decrypt_value is deprecated: XOR provides no real encryption. \
             Use a proper AEAD cipher (e.g., aes-gcm or chacha20poly1305 crates)."
                .to_string(),
        ))
    }
}

/// Middleware trait for processing requests
///
/// Middleware can intercept requests before they are processed by the GraphQL engine.
/// Middleware is executed in order, and any error will short-circuit the chain.
///
/// # Example
///
/// ```rust
/// use grpc_graphql_gateway::middleware::{Middleware, Context};
/// use grpc_graphql_gateway::Result;
///
/// struct MyMiddleware;
///
/// #[async_trait::async_trait]
/// impl Middleware for MyMiddleware {
///     async fn call(&self, ctx: &mut Context) -> Result<()> {
///         println!("Processing request {}", ctx.request_id);
///         Ok(())
///     }
/// }
/// ```
#[async_trait::async_trait]
pub trait Middleware: Send + Sync {
    /// Process the request context
    async fn call(&self, ctx: &mut Context) -> Result<()>;

    /// Middleware name for logging/debugging
    fn name(&self) -> &'static str {
        std::any::type_name::<Self>()
    }
}

/// Type alias for boxed middleware
pub type BoxMiddleware = Box<dyn Middleware>;

/// Middleware function type
pub type MiddlewareFn =
    Arc<dyn Fn(&mut Context) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> + Send + Sync>;

// ============================================================================
// CORS Middleware
// ============================================================================

/// CORS middleware
///
/// Handles Cross-Origin Resource Sharing (CORS) headers.
/// Note: This is a placeholder implementation. In a real application,
/// you should use `tower_http::cors::CorsLayer` with Axum.
#[derive(Debug, Clone)]
pub struct CorsMiddleware {
    pub allow_origins: Vec<String>,
    pub allow_methods: Vec<String>,
    pub allow_headers: Vec<String>,
}

impl Default for CorsMiddleware {
    fn default() -> Self {
        Self {
            allow_origins: vec!["*".to_string()],
            allow_methods: vec!["GET".to_string(), "POST".to_string()],
            allow_headers: vec!["Content-Type".to_string(), "Authorization".to_string()],
        }
    }
}

#[async_trait::async_trait]
impl Middleware for CorsMiddleware {
    async fn call(&self, _ctx: &mut Context) -> Result<()> {
        // CORS is handled by tower-http, this is just a placeholder
        Ok(())
    }

    fn name(&self) -> &'static str {
        "CorsMiddleware"
    }
}

// ============================================================================
// Authentication Middleware
// ============================================================================

/// Authentication scheme types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthScheme {
    /// Bearer token (JWT or opaque)
    Bearer,
    /// Basic authentication
    Basic,
    /// API key in header
    ApiKey,
    /// Custom scheme
    Custom(String),
}

impl AuthScheme {
    /// Parse scheme from Authorization header
    pub fn from_header(header: &str) -> Option<(Self, String)> {
        let parts: Vec<&str> = header.splitn(2, ' ').collect();
        if parts.len() != 2 {
            return None;
        }

        let scheme = match parts[0].to_lowercase().as_str() {
            "bearer" => AuthScheme::Bearer,
            "basic" => AuthScheme::Basic,
            other => AuthScheme::Custom(other.to_string()),
        };

        Some((scheme, parts[1].to_string()))
    }
}

/// Authentication result containing validated claims
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct AuthClaims {
    /// Subject (user ID)
    pub sub: Option<String>,
    /// Issuer
    pub iss: Option<String>,
    /// Audience
    pub aud: Option<Vec<String>>,
    /// Expiration time (Unix timestamp)
    pub exp: Option<i64>,
    /// Issued at (Unix timestamp)
    pub iat: Option<i64>,
    /// Roles/permissions
    pub roles: Vec<String>,
    /// Custom claims
    #[serde(flatten)]
    pub custom: HashMap<String, serde_json::Value>,
}

impl AuthClaims {
    /// Check if claims have expired
    pub fn is_expired(&self) -> bool {
        if let Some(exp) = self.exp {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_secs() as i64)
                .unwrap_or(0);
            return exp < now;
        }
        false
    }

    /// Check if user has a specific role
    pub fn has_role(&self, role: &str) -> bool {
        self.roles.iter().any(|r| r == role)
    }

    /// Check if user has any of the specified roles
    pub fn has_any_role(&self, roles: &[&str]) -> bool {
        roles.iter().any(|r| self.has_role(r))
    }
}

/// Token validator trait for custom validation logic
#[async_trait::async_trait]
pub trait TokenValidator: Send + Sync {
    /// Validate token and return claims
    async fn validate(&self, token: &str) -> Result<AuthClaims>;
}

/// Simple closure-based token validator
pub struct FnValidator<F>
where
    F: Fn(&str) -> std::pin::Pin<Box<dyn Future<Output = Result<AuthClaims>> + Send>> + Send + Sync,
{
    validate_fn: F,
}

impl<F> FnValidator<F>
where
    F: Fn(&str) -> std::pin::Pin<Box<dyn Future<Output = Result<AuthClaims>> + Send>> + Send + Sync,
{
    pub fn new(f: F) -> Self {
        Self { validate_fn: f }
    }
}

#[async_trait::async_trait]
impl<F> TokenValidator for FnValidator<F>
where
    F: Fn(&str) -> std::pin::Pin<Box<dyn Future<Output = Result<AuthClaims>> + Send>> + Send + Sync,
{
    async fn validate(&self, token: &str) -> Result<AuthClaims> {
        (self.validate_fn)(token).await
    }
}

/// Configuration for authentication middleware
#[derive(Clone)]
pub struct AuthConfig {
    /// Required authentication (if false, unauthenticated requests pass through)
    pub required: bool,
    /// Allowed authentication schemes
    pub allowed_schemes: Vec<AuthScheme>,
    /// Header name for API key authentication
    pub api_key_header: String,
    /// Skip authentication for these paths (e.g., health checks)
    pub skip_paths: HashSet<String>,
    /// Skip authentication for introspection queries
    pub skip_introspection: bool,
}

impl Default for AuthConfig {
    fn default() -> Self {
        Self {
            required: true,
            allowed_schemes: vec![AuthScheme::Bearer],
            api_key_header: "x-api-key".to_string(),
            skip_paths: HashSet::new(),
            skip_introspection: false,
        }
    }
}

impl AuthConfig {
    /// Create a new auth config with required authentication
    pub fn required() -> Self {
        Self::default()
    }

    /// Create a new auth config with optional authentication
    pub fn optional() -> Self {
        Self {
            required: false,
            ..Self::default()
        }
    }

    /// Add a scheme to allowed schemes
    pub fn with_scheme(mut self, scheme: AuthScheme) -> Self {
        if !self.allowed_schemes.contains(&scheme) {
            self.allowed_schemes.push(scheme);
        }
        self
    }

    /// Set custom API key header
    pub fn with_api_key_header(mut self, header: impl Into<String>) -> Self {
        self.api_key_header = header.into();
        self
    }

    /// Add a path to skip authentication
    pub fn skip_path(mut self, path: impl Into<String>) -> Self {
        self.skip_paths.insert(path.into());
        self
    }

    /// Skip authentication for introspection
    pub fn with_skip_introspection(mut self, skip: bool) -> Self {
        self.skip_introspection = skip;
        self
    }
}

/// Enhanced Authentication middleware
///
/// Validates authentication using configurable validators and schemes.
/// Supports Bearer tokens (JWT), Basic auth, and API keys.
///
/// # Example
///
/// ```rust,no_run
/// use grpc_graphql_gateway::middleware::{EnhancedAuthMiddleware, AuthConfig, AuthClaims};
/// use std::sync::Arc;
///
/// // Simple token validation
/// let auth = EnhancedAuthMiddleware::new(
///     AuthConfig::required().with_scheme(grpc_graphql_gateway::middleware::AuthScheme::Bearer),
///     Arc::new(|token: &str| {
///         Box::pin(async move {
///             // Validate JWT token here
///             Ok(AuthClaims::default())
///         })
///     }),
/// );
/// ```
pub struct EnhancedAuthMiddleware {
    config: AuthConfig,
    validator: Arc<dyn TokenValidator>,
}

impl EnhancedAuthMiddleware {
    /// Create a new auth middleware with custom validator
    pub fn new(config: AuthConfig, validator: Arc<dyn TokenValidator>) -> Self {
        Self { config, validator }
    }

    /// Create with a simple validation function
    pub fn with_fn<F>(config: AuthConfig, f: F) -> Self
    where
        F: Fn(&str) -> std::pin::Pin<Box<dyn Future<Output = Result<AuthClaims>> + Send>>
            + Send
            + Sync
            + 'static,
    {
        Self {
            config,
            validator: Arc::new(FnValidator::new(f)),
        }
    }

    /// Extract token from request based on configured schemes
    fn extract_token(&self, ctx: &Context) -> Option<(AuthScheme, String)> {
        // Try Authorization header first
        if let Some(auth_header) = ctx.headers.get("authorization") {
            if let Ok(header_str) = auth_header.to_str() {
                if let Some((scheme, token)) = AuthScheme::from_header(header_str) {
                    if self.config.allowed_schemes.contains(&scheme) {
                        return Some((scheme, token));
                    }
                }
            }
        }

        // Try API key header
        if self.config.allowed_schemes.contains(&AuthScheme::ApiKey) {
            if let Some(api_key) = ctx.headers.get(self.config.api_key_header.as_str()) {
                if let Ok(key) = api_key.to_str() {
                    return Some((AuthScheme::ApiKey, key.to_string()));
                }
            }
        }

        None
    }
}

#[async_trait::async_trait]
impl Middleware for EnhancedAuthMiddleware {
    async fn call(&self, ctx: &mut Context) -> Result<()> {
        // Extract and validate token
        match self.extract_token(ctx) {
            Some((scheme, token)) => {
                // Validate token
                let claims = self.validator.validate(&token).await.map_err(|e| {
                    tracing::warn!(
                        request_id = %ctx.request_id,
                        scheme = ?scheme,
                        error = %e,
                        "Authentication failed"
                    );
                    crate::error::Error::Unauthorized(format!("Token validation failed: {}", e))
                })?;

                // Check expiration
                if claims.is_expired() {
                    tracing::warn!(
                        request_id = %ctx.request_id,
                        user_id = ?claims.sub,
                        "Token expired"
                    );
                    return Err(crate::error::Error::Unauthorized(
                        "Token has expired".to_string(),
                    ));
                }

                // Store claims in context
                ctx.insert(
                    "auth.scheme".to_string(),
                    serde_json::json!(format!("{:?}", scheme)),
                );
                if let Some(ref user_id) = claims.sub {
                    ctx.insert("auth.user_id".to_string(), serde_json::json!(user_id));
                }
                ctx.insert("auth.roles".to_string(), serde_json::json!(claims.roles));
                ctx.insert(
                    "auth.claims".to_string(),
                    serde_json::to_value(&claims).unwrap_or_default(),
                );
                ctx.insert("auth.authenticated".to_string(), serde_json::json!(true));

                tracing::debug!(
                    request_id = %ctx.request_id,
                    user_id = ?claims.sub,
                    roles = ?claims.roles,
                    "Authentication successful"
                );

                Ok(())
            }
            None => {
                if self.config.required {
                    tracing::warn!(
                        request_id = %ctx.request_id,
                        client_ip = ?ctx.client_ip,
                        "Missing authentication"
                    );
                    Err(crate::error::Error::Unauthorized(
                        "Authentication required".to_string(),
                    ))
                } else {
                    // Optional auth - allow unauthenticated requests
                    ctx.insert("auth.authenticated".to_string(), serde_json::json!(false));
                    Ok(())
                }
            }
        }
    }

    fn name(&self) -> &'static str {
        "EnhancedAuthMiddleware"
    }
}

/// Simple authentication middleware (backwards compatible)
///
/// Validates the `Authorization` header using a provided validation function.
/// If validation fails, it returns an `Unauthorized` error.
#[derive(Clone)]
pub struct AuthMiddleware {
    pub validate: Arc<dyn Fn(&str) -> bool + Send + Sync>,
    /// When true, requests without an Authorization header are allowed through
    /// (with `auth.authenticated = false` in context).
    allow_unauthenticated: bool,
}

impl AuthMiddleware {
    /// Create a new auth middleware with a validation function.
    /// Requires an Authorization header to be present.
    pub fn new<F>(validate: F) -> Self
    where
        F: Fn(&str) -> bool + Send + Sync + 'static,
    {
        Self {
            validate: Arc::new(validate),
            allow_unauthenticated: false,
        }
    }

    /// Create an auth middleware that accepts any token.
    /// Still requires an Authorization header to be present.
    pub fn allow_any_token() -> Self {
        Self::new(|_| true)
    }

    /// Create an auth middleware that allows all requests through,
    /// including those with no Authorization header.
    /// If a token IS present, it will be validated (and any token will pass).
    pub fn allow_all() -> Self {
        Self {
            validate: Arc::new(|_| true),
            allow_unauthenticated: true,
        }
    }

    /// Create an auth middleware that requires a specific token
    pub fn require_token(expected_token: String) -> Self {
        Self::new(move |token| {
            token
                .strip_prefix("Bearer ")
                .map(|t| t == expected_token)
                .unwrap_or(false)
        })
    }
}

#[async_trait::async_trait]
impl Middleware for AuthMiddleware {
    async fn call(&self, ctx: &mut Context) -> Result<()> {
        if let Some(auth_header) = ctx.headers.get("authorization") {
            if let Ok(token) = auth_header.to_str() {
                if (self.validate)(token) {
                    ctx.insert("auth.authenticated".to_string(), serde_json::json!(true));
                    return Ok(());
                }
            }
            // Header present but invalid
            return Err(crate::error::Error::Unauthorized(
                "Invalid authorization token".to_string(),
            ));
        }

        // No Authorization header
        if self.allow_unauthenticated {
            ctx.insert("auth.authenticated".to_string(), serde_json::json!(false));
            return Ok(());
        }

        Err(crate::error::Error::Unauthorized(
            "Missing authorization header".to_string(),
        ))
    }

    fn name(&self) -> &'static str {
        "AuthMiddleware"
    }
}

// ============================================================================
// Logging Middleware
// ============================================================================

/// Log level for the logging middleware
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LogLevel {
    /// Trace level (most verbose)
    Trace,
    /// Debug level
    Debug,
    /// Info level (recommended for production)
    #[default]
    Info,
    /// Warn level
    Warn,
    /// Error level (errors only)
    Error,
}

/// Headers that should be masked in logs
const DEFAULT_SENSITIVE_HEADERS: &[&str] = &[
    "authorization",
    "x-api-key",
    "cookie",
    "set-cookie",
    "x-auth-token",
    "x-access-token",
    "x-refresh-token",
    "proxy-authorization",
];

/// Configuration for logging middleware
#[derive(Clone)]
pub struct LoggingConfig {
    /// Log level
    pub level: LogLevel,
    /// Whether to log request headers
    pub log_headers: bool,
    /// Headers to mask (sensitive data)
    pub sensitive_headers: HashSet<String>,
    /// Whether to log request body (be careful with PII)
    pub log_body: bool,
    /// Maximum body length to log
    pub max_body_log_length: usize,
    /// Whether to log timing information
    pub log_timing: bool,
    /// Slow request threshold (log as warning if exceeded)
    pub slow_request_threshold: std::time::Duration,
    /// Whether to include structured fields
    pub structured: bool,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::Info,
            log_headers: true,
            sensitive_headers: DEFAULT_SENSITIVE_HEADERS
                .iter()
                .map(|s| s.to_lowercase())
                .collect(),
            log_body: false,
            max_body_log_length: 1024,
            log_timing: true,
            slow_request_threshold: std::time::Duration::from_secs(3),
            structured: true,
        }
    }
}

impl LoggingConfig {
    /// Create a minimal logging config (less verbose)
    pub fn minimal() -> Self {
        Self {
            level: LogLevel::Info,
            log_headers: false,
            log_body: false,
            log_timing: true,
            structured: true,
            ..Self::default()
        }
    }

    /// Create a verbose logging config (for debugging)
    pub fn verbose() -> Self {
        Self {
            level: LogLevel::Debug,
            log_headers: true,
            log_body: true,
            log_timing: true,
            structured: true,
            ..Self::default()
        }
    }

    /// Add a sensitive header to mask
    pub fn mask_header(mut self, header: impl Into<String>) -> Self {
        self.sensitive_headers.insert(header.into().to_lowercase());
        self
    }

    /// Set log level
    pub fn with_level(mut self, level: LogLevel) -> Self {
        self.level = level;
        self
    }

    /// Enable/disable header logging
    pub fn with_headers(mut self, log: bool) -> Self {
        self.log_headers = log;
        self
    }

    /// Set slow request threshold
    pub fn with_slow_threshold(mut self, threshold: std::time::Duration) -> Self {
        self.slow_request_threshold = threshold;
        self
    }
}

/// Enhanced Logging middleware
///
/// Provides structured logging with request IDs, timing, and sensitive data masking.
///
/// # Example
///
/// ```rust,no_run
/// use grpc_graphql_gateway::middleware::{EnhancedLoggingMiddleware, LoggingConfig, LogLevel};
///
/// // Production config
/// let logging = EnhancedLoggingMiddleware::new(
///     LoggingConfig::default().with_level(LogLevel::Info)
/// );
///
/// // Debug config
/// let debug_logging = EnhancedLoggingMiddleware::new(
///     LoggingConfig::verbose()
/// );
/// ```
#[derive(Clone)]
pub struct EnhancedLoggingMiddleware {
    config: LoggingConfig,
}

impl EnhancedLoggingMiddleware {
    /// Create a new logging middleware with config
    pub fn new(config: LoggingConfig) -> Self {
        Self { config }
    }

    /// Create with default config
    pub fn default_config() -> Self {
        Self::new(LoggingConfig::default())
    }

    /// Mask sensitive header values
    fn mask_headers(&self, headers: &axum::http::HeaderMap) -> HashMap<String, String> {
        headers
            .iter()
            .map(|(name, value)| {
                let name_str = name.as_str().to_lowercase();
                let value_str = if self.config.sensitive_headers.contains(&name_str) {
                    "[REDACTED]".to_string()
                } else {
                    value.to_str().unwrap_or("[binary]").to_string()
                };
                (name_str, value_str)
            })
            .collect()
    }
}

impl Default for EnhancedLoggingMiddleware {
    fn default() -> Self {
        Self::default_config()
    }
}

#[async_trait::async_trait]
impl Middleware for EnhancedLoggingMiddleware {
    async fn call(&self, ctx: &mut Context) -> Result<()> {
        let masked_headers = if self.config.log_headers {
            Some(self.mask_headers(&ctx.headers))
        } else {
            None
        };

        // Log based on configured level
        match self.config.level {
            LogLevel::Trace => {
                tracing::trace!(
                    request_id = %ctx.request_id,
                    client_ip = ?ctx.client_ip,
                    headers = ?masked_headers,
                    "GraphQL request received"
                );
            }
            LogLevel::Debug => {
                tracing::debug!(
                    request_id = %ctx.request_id,
                    client_ip = ?ctx.client_ip,
                    headers = ?masked_headers,
                    "GraphQL request received"
                );
            }
            LogLevel::Info => {
                if self.config.structured {
                    tracing::info!(
                        request_id = %ctx.request_id,
                        client_ip = ?ctx.client_ip,
                        "GraphQL request received"
                    );
                } else {
                    tracing::info!(
                        "GraphQL request {} from {:?}",
                        ctx.request_id,
                        ctx.client_ip
                    );
                }
            }
            LogLevel::Warn | LogLevel::Error => {
                // Only log warnings/errors for slow requests or errors
            }
        }

        // Store logging context for later use
        ctx.insert(
            "logging.headers_logged".to_string(),
            serde_json::json!(self.config.log_headers),
        );

        Ok(())
    }

    fn name(&self) -> &'static str {
        "EnhancedLoggingMiddleware"
    }
}

/// Simple logging middleware (backwards compatible)
///
/// Logs incoming GraphQL requests using the `tracing` crate.
#[derive(Debug, Clone, Default)]
pub struct LoggingMiddleware;

impl LoggingMiddleware {
    /// Create a new logging middleware
    pub fn new() -> Self {
        Self
    }
}

#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
    async fn call(&self, ctx: &mut Context) -> Result<()> {
        tracing::debug!(
            request_id = %ctx.request_id,
            "Processing GraphQL request"
        );
        Ok(())
    }

    fn name(&self) -> &'static str {
        "LoggingMiddleware"
    }
}

// ============================================================================
// Rate Limiting Middleware
// ============================================================================

/// Rate Limiting middleware
///
/// Limits the number of requests using a token bucket algorithm.
/// This implementation uses a global rate limiter.
pub struct RateLimitMiddleware {
    limiter: Arc<
        governor::RateLimiter<
            governor::state::NotKeyed,
            governor::state::InMemoryState,
            governor::clock::DefaultClock,
        >,
    >,
}

impl RateLimitMiddleware {
    /// Create a new rate limiter with the specified requests per second capacity
    pub fn new(requests_per_second: u32, burst_size: u32) -> Self {
        use governor::{Quota, RateLimiter};
        use std::num::NonZeroU32;

        let quota = Quota::per_second(NonZeroU32::new(requests_per_second).unwrap())
            .allow_burst(NonZeroU32::new(burst_size).unwrap());

        Self {
            limiter: Arc::new(RateLimiter::direct(quota)),
        }
    }

    /// Create a per-minute rate limiter
    pub fn per_minute(requests_per_minute: u32, burst_size: u32) -> Self {
        use governor::{Quota, RateLimiter};
        use std::num::NonZeroU32;

        let quota = Quota::per_minute(NonZeroU32::new(requests_per_minute).unwrap())
            .allow_burst(NonZeroU32::new(burst_size).unwrap());

        Self {
            limiter: Arc::new(RateLimiter::direct(quota)),
        }
    }
}

#[async_trait::async_trait]
impl Middleware for RateLimitMiddleware {
    async fn call(&self, ctx: &mut Context) -> Result<()> {
        if self.limiter.check().is_err() {
            tracing::warn!(
                request_id = %ctx.request_id,
                client_ip = ?ctx.client_ip,
                "Rate limit exceeded"
            );
            return Err(crate::error::Error::TooManyRequests(
                "Rate limit exceeded".to_string(),
            ));
        }
        Ok(())
    }

    fn name(&self) -> &'static str {
        "RateLimitMiddleware"
    }
}

// ============================================================================
// Middleware Chain
// ============================================================================

/// Middleware chain for combining multiple middleware
pub struct MiddlewareChain {
    middlewares: Vec<Arc<dyn Middleware>>,
}

impl MiddlewareChain {
    /// Create a new empty middleware chain
    pub fn new() -> Self {
        Self {
            middlewares: Vec::new(),
        }
    }

    /// Add a middleware to the chain
    #[allow(clippy::should_implement_trait)]
    pub fn add<M: Middleware + 'static>(mut self, middleware: M) -> Self {
        self.middlewares.push(Arc::new(middleware));
        self
    }

    /// Add an Arc-wrapped middleware
    pub fn add_arc(mut self, middleware: Arc<dyn Middleware>) -> Self {
        self.middlewares.push(middleware);
        self
    }

    /// Execute all middleware in order
    pub async fn execute(&self, ctx: &mut Context) -> Result<()> {
        for middleware in &self.middlewares {
            middleware.call(ctx).await?;
        }
        Ok(())
    }

    /// Get middleware count
    pub fn len(&self) -> usize {
        self.middlewares.len()
    }

    /// Check if chain is empty
    pub fn is_empty(&self) -> bool {
        self.middlewares.is_empty()
    }
}

impl Default for MiddlewareChain {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_auth_scheme_parsing() {
        let (scheme, token) = AuthScheme::from_header("Bearer abc123").unwrap();
        assert_eq!(scheme, AuthScheme::Bearer);
        assert_eq!(token, "abc123");

        let (scheme, _) = AuthScheme::from_header("Basic dXNlcjpwYXNz").unwrap();
        assert_eq!(scheme, AuthScheme::Basic);

        assert!(AuthScheme::from_header("InvalidHeader").is_none());
    }

    #[test]
    fn test_auth_claims_expiration() {
        let claims = AuthClaims {
            exp: Some(0), // Expired in 1970
            ..Default::default()
        };
        assert!(claims.is_expired());

        let claims = AuthClaims {
            exp: Some(i64::MAX), // Far future
            ..Default::default()
        };
        assert!(!claims.is_expired());
    }

    #[test]
    fn test_auth_claims_roles() {
        let claims = AuthClaims {
            roles: vec!["admin".to_string(), "user".to_string()],
            ..Default::default()
        };

        assert!(claims.has_role("admin"));
        assert!(claims.has_role("user"));
        assert!(!claims.has_role("superadmin"));
        assert!(claims.has_any_role(&["admin", "superadmin"]));
        assert!(!claims.has_any_role(&["superadmin", "guest"]));
    }

    #[test]
    fn test_logging_config_masking() {
        let config = LoggingConfig::default().mask_header("x-custom-secret");

        assert!(config.sensitive_headers.contains("authorization"));
        assert!(config.sensitive_headers.contains("x-custom-secret"));
    }

    #[test]
    fn test_auth_config_builder() {
        let config = AuthConfig::required()
            .with_scheme(AuthScheme::Bearer)
            .with_scheme(AuthScheme::ApiKey)
            .with_api_key_header("x-my-api-key")
            .skip_path("/health");

        assert!(config.required);
        assert!(config.allowed_schemes.contains(&AuthScheme::Bearer));
        assert!(config.allowed_schemes.contains(&AuthScheme::ApiKey));
        assert_eq!(config.api_key_header, "x-my-api-key");
        assert!(config.skip_paths.contains("/health"));
    }

    #[tokio::test]
    async fn test_middleware_chain() {
        use axum::http::Request;

        struct CounterMiddleware {
            counter: Arc<std::sync::atomic::AtomicUsize>,
        }

        #[async_trait::async_trait]
        impl Middleware for CounterMiddleware {
            async fn call(&self, _ctx: &mut Context) -> Result<()> {
                self.counter
                    .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Ok(())
            }
        }

        let counter = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let chain = MiddlewareChain::new()
            .add(CounterMiddleware {
                counter: counter.clone(),
            })
            .add(CounterMiddleware {
                counter: counter.clone(),
            });

        let req = Request::builder().uri("/graphql").body(()).unwrap();
        let mut ctx = Context::from_request(&req);

        chain.execute(&mut ctx).await.unwrap();
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 2);
    }

    #[test]
    fn test_auth_config_optional() {
        let config = AuthConfig::optional();
        assert!(!config.required);
    }

    #[test]
    fn test_auth_config_required() {
        let config = AuthConfig::required();
        assert!(config.required);
    }

    #[test]
    fn test_auth_config_with_scheme_no_duplicates() {
        let config = AuthConfig::default()
            .with_scheme(AuthScheme::Bearer)
            .with_scheme(AuthScheme::Bearer);

        assert_eq!(config.allowed_schemes.len(), 1);
    }

    #[test]
    fn test_auth_config_skip_introspection() {
        let config = AuthConfig::default().with_skip_introspection(true);
        assert!(config.skip_introspection);
    }

    #[test]
    fn test_auth_claims_default() {
        let claims = AuthClaims::default();
        assert!(claims.sub.is_none());
        assert!(claims.exp.is_none());
        assert!(claims.roles.is_empty());
    }

    #[test]
    fn test_logging_config_default() {
        let config = LoggingConfig::default();
        assert_eq!(config.level, LogLevel::Info);
        assert!(config.log_headers);
        assert!(!config.log_body);
        assert!(config.log_timing);
    }

    #[test]
    fn test_logging_config_minimal() {
        let config = LoggingConfig::minimal();
        assert!(!config.log_headers);
        assert!(!config.log_body);
        assert!(config.log_timing);
    }

    #[test]
    fn test_logging_config_verbose() {
        let config = LoggingConfig::verbose();
        assert_eq!(config.level, LogLevel::Debug);
        assert!(config.log_headers);
        assert!(config.log_body);
    }

    #[test]
    fn test_logging_config_builder() {
        let config = LoggingConfig::default()
            .with_level(LogLevel::Warn)
            .with_headers(false)
            .with_slow_threshold(std::time::Duration::from_secs(5));

        assert_eq!(config.level, LogLevel::Warn);
        assert!(!config.log_headers);
        assert_eq!(
            config.slow_request_threshold,
            std::time::Duration::from_secs(5)
        );
    }

    #[test]
    fn test_log_level_default() {
        assert_eq!(LogLevel::default(), LogLevel::Info);
    }

    #[test]
    fn test_log_level_equality() {
        assert_eq!(LogLevel::Info, LogLevel::Info);
        assert_ne!(LogLevel::Info, LogLevel::Debug);
    }

    #[tokio::test]
    async fn test_logging_middleware_new() {
        let middleware = LoggingMiddleware::new();
        assert_eq!(middleware.name(), "LoggingMiddleware");
    }

    #[tokio::test]
    async fn test_logging_middleware_default() {
        let middleware = LoggingMiddleware;
        assert_eq!(middleware.name(), "LoggingMiddleware");
    }

    #[tokio::test]
    async fn test_enhanced_logging_middleware_new() {
        let middleware = EnhancedLoggingMiddleware::new(LoggingConfig::default());
        assert_eq!(middleware.name(), "EnhancedLoggingMiddleware");
    }

    #[tokio::test]
    async fn test_enhanced_logging_middleware_default() {
        let middleware = EnhancedLoggingMiddleware::default();
        assert_eq!(middleware.name(), "EnhancedLoggingMiddleware");
    }

    #[tokio::test]
    async fn test_auth_middleware_allow_all() {
        use axum::http::Request;

        let middleware = AuthMiddleware::allow_all();
        let req = Request::builder()
            .header("authorization", "Bearer any-token")
            .uri("/graphql")
            .body(())
            .unwrap();
        let mut ctx = Context::from_request(&req);

        assert!(middleware.call(&mut ctx).await.is_ok());
    }

    #[tokio::test]
    async fn test_auth_middleware_require_token() {
        use axum::http::Request;

        let middleware = AuthMiddleware::require_token("secret123".to_string());

        // Valid token
        let req = Request::builder()
            .header("authorization", "Bearer secret123")
            .uri("/graphql")
            .body(())
            .unwrap();
        let mut ctx = Context::from_request(&req);
        assert!(middleware.call(&mut ctx).await.is_ok());

        // Invalid token
        let req = Request::builder()
            .header("authorization", "Bearer wrong")
            .uri("/graphql")
            .body(())
            .unwrap();
        let mut ctx = Context::from_request(&req);
        assert!(middleware.call(&mut ctx).await.is_err());
    }

    #[tokio::test]
    async fn test_auth_middleware_missing_header_allow_all() {
        use axum::http::Request;

        // allow_all() now truly allows unauthenticated requests
        let middleware = AuthMiddleware::allow_all();
        let req = Request::builder().uri("/graphql").body(()).unwrap();
        let mut ctx = Context::from_request(&req);

        assert!(middleware.call(&mut ctx).await.is_ok());
        // Should mark as not authenticated
        assert_eq!(
            ctx.extensions.get("auth.authenticated"),
            Some(&serde_json::json!(false))
        );
    }

    #[tokio::test]
    async fn test_auth_middleware_missing_header_allow_any_token() {
        use axum::http::Request;

        // allow_any_token() still requires a header to be present
        let middleware = AuthMiddleware::allow_any_token();
        let req = Request::builder().uri("/graphql").body(()).unwrap();
        let mut ctx = Context::from_request(&req);

        assert!(middleware.call(&mut ctx).await.is_err());
    }

    #[tokio::test]
    async fn test_rate_limit_middleware() {
        use axum::http::Request;

        let middleware = RateLimitMiddleware::new(10, 10);
        let req = Request::builder().uri("/graphql").body(()).unwrap();
        let mut ctx = Context::from_request(&req);

        // First request should succeed
        assert!(middleware.call(&mut ctx).await.is_ok());
        assert_eq!(middleware.name(), "RateLimitMiddleware");
    }

    #[tokio::test]
    async fn test_rate_limit_per_minute() {
        let middleware = RateLimitMiddleware::per_minute(60, 10);
        assert_eq!(middleware.name(), "RateLimitMiddleware");
    }

    #[test]
    fn test_middleware_chain_new() {
        let chain = MiddlewareChain::new();
        assert_eq!(chain.len(), 0);
        assert!(chain.is_empty());
    }

    #[test]
    fn test_middleware_chain_default() {
        let chain = MiddlewareChain::default();
        assert_eq!(chain.len(), 0);
    }

    #[tokio::test]
    async fn test_middleware_chain_add() {
        let chain = MiddlewareChain::new()
            .add(LoggingMiddleware::new())
            .add(LoggingMiddleware::new());

        assert_eq!(chain.len(), 2);
        assert!(!chain.is_empty());
    }

    #[tokio::test]
    async fn test_middleware_chain_add_arc() {
        use std::sync::Arc;

        let chain = MiddlewareChain::new().add_arc(Arc::new(LoggingMiddleware::new()));

        assert_eq!(chain.len(), 1);
    }
}