auth-framework 0.5.0-rc19

A comprehensive, production-ready authentication and authorization framework for Rust applications
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
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
//! Additional server capability modules
//!
//! Note: WebAuthn/FIDO2 support is provided via the `PasskeyAuthMethod`
//! in `src/methods/passkey/mod.rs` using the production-grade `passkey` crate.
//! No separate WebAuthn server module is needed.

/// JWT token server for issuing and validating JWT tokens
pub mod jwt_server {
    use crate::errors::Result;
    use crate::storage::AuthStorage;
    use serde::{Deserialize, Serialize};
    use std::sync::Arc;

    /// Configuration for the JWT token server.
    ///
    /// # Example
    /// ```rust,ignore
    /// let config = JwtServerConfig { issuer: "https://auth.example.com".into(), key_id: "k1".into() };
    /// ```
    #[derive(Debug, Clone)]
    pub struct JwtServerConfig {
        pub issuer: String,
        pub key_id: String,
    }

    impl Default for JwtServerConfig {
        fn default() -> Self {
            Self {
                issuer: "https://auth.example.com".to_string(),
                key_id: "default".to_string(),
            }
        }
    }

    pub struct JwtServer {
        config: JwtServerConfig,
        storage: Arc<dyn AuthStorage>,
    }

    impl JwtServer {
        /// Create a new JWT server.
        ///
        /// # Example
        /// ```rust,ignore
        /// let server = JwtServer::new(JwtServerConfig::default(), storage).await?;
        /// ```
        pub async fn new(config: JwtServerConfig, storage: Arc<dyn AuthStorage>) -> Result<Self> {
            Ok(Self { config, storage })
        }

        /// Perform any async initialization after construction.
        ///
        /// `JwtServer` requires no async startup work — all state is set up in [`Self::new`].
        /// This method is provided for API symmetry with other server modules that
        /// do require an initialization step (e.g., connecting to external services).
        ///
        /// # Example
        /// ```rust,ignore
        /// server.initialize().await?;
        /// ```
        pub async fn initialize(&self) -> Result<()> {
            Ok(())
        }

        /// Get the well-known JWT configuration for discovery.
        ///
        /// # Example
        /// ```rust,ignore
        /// let config = server.get_well_known_jwt_configuration().await?;
        /// println!("issuer: {}", config.issuer);
        /// ```
        pub async fn get_well_known_jwt_configuration(&self) -> Result<JwtWellKnownConfiguration> {
            Ok(JwtWellKnownConfiguration {
                issuer: self.config.issuer.clone(),
                jwks_uri: format!("{}/jwks", self.config.issuer),
            })
        }

        /// Store JWT metadata in storage.
        ///
        /// # Example
        /// ```rust,ignore
        /// let meta = server.get_well_known_jwt_configuration().await?;
        /// server.store_jwt_metadata(&meta).await?;
        /// ```
        pub async fn store_jwt_metadata(&self, metadata: &JwtWellKnownConfiguration) -> Result<()> {
            let key = format!("jwt_metadata:{}", self.config.issuer);
            let value = serde_json::to_string(metadata).map_err(|e| {
                crate::errors::AuthError::internal(format!("Serialization error: {}", e))
            })?;

            self.storage.store_kv(&key, value.as_bytes(), None).await?;
            tracing::info!("Stored JWT metadata for issuer: {}", self.config.issuer);
            Ok(())
        }

        /// Retrieve JWT metadata from storage.
        ///
        /// # Example
        /// ```rust,ignore
        /// if let Some(meta) = server.get_stored_metadata().await? {
        ///     println!("stored issuer: {}", meta.issuer);
        /// }
        /// ```
        pub async fn get_stored_metadata(&self) -> Result<Option<JwtWellKnownConfiguration>> {
            let key = format!("jwt_metadata:{}", self.config.issuer);

            if let Some(value_bytes) = self.storage.get_kv(&key).await? {
                let value = String::from_utf8(value_bytes).map_err(|e| {
                    crate::errors::AuthError::internal(format!("UTF-8 conversion error: {}", e))
                })?;
                let metadata: JwtWellKnownConfiguration =
                    serde_json::from_str(&value).map_err(|e| {
                        crate::errors::AuthError::internal(format!("Deserialization error: {}", e))
                    })?;
                Ok(Some(metadata))
            } else {
                Ok(None)
            }
        }

        /// Store JWT signing key information.
        ///
        /// # Example
        /// ```rust,ignore
        /// server.store_signing_key("pem-encoded-key-data").await?;
        /// ```
        pub async fn store_signing_key(&self, key_data: &str) -> Result<()> {
            let key = format!("jwt_key:{}", self.config.key_id);
            self.storage
                .store_kv(&key, key_data.as_bytes(), None)
                .await?;
            tracing::info!("Stored JWT signing key: {}", self.config.key_id);
            Ok(())
        }
    }

    /// Well-known JWT configuration for OIDC discovery.
    ///
    /// # Example
    /// ```rust,ignore
    /// let wk = JwtWellKnownConfiguration { issuer: "https://auth.example.com".into(), jwks_uri: "https://auth.example.com/jwks".into() };
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct JwtWellKnownConfiguration {
        pub issuer: String,
        pub jwks_uri: String,
    }
}

/// API Gateway authentication and authorization
pub mod api_gateway {
    use crate::errors::Result;
    use crate::storage::AuthStorage;
    use std::sync::Arc;

    /// Configuration for the API gateway module.
    ///
    /// # Example
    /// ```rust,ignore
    /// let config = ApiGatewayConfig { name: "my-gateway".into() };
    /// ```
    #[derive(Debug, Clone)]
    pub struct ApiGatewayConfig {
        pub name: String,
    }

    impl Default for ApiGatewayConfig {
        fn default() -> Self {
            Self {
                name: "API Gateway".to_string(),
            }
        }
    }

    pub struct ApiGateway {
        config: ApiGatewayConfig,
        storage: Arc<dyn AuthStorage>,
    }

    impl ApiGateway {
        /// Create a new API gateway.
        ///
        /// # Example
        /// ```rust,ignore
        /// let gw = ApiGateway::new(ApiGatewayConfig::default(), storage).await?;
        /// ```
        pub async fn new(config: ApiGatewayConfig, storage: Arc<dyn AuthStorage>) -> Result<Self> {
            Ok(Self { config, storage })
        }

        /// Perform any async initialization after construction.
        ///
        /// `ApiGateway` requires no async startup work — all state is set up in [`Self::new`].
        /// This method is provided for API symmetry with other server modules that
        /// do require an initialization step (e.g., connecting to external services).
        ///
        /// # Example
        /// ```rust,ignore
        /// gw.initialize().await?;
        /// ```
        pub async fn initialize(&self) -> Result<()> {
            Ok(())
        }

        /// Store API gateway configuration metadata.
        ///
        /// # Example
        /// ```rust,ignore
        /// gw.store_gateway_metadata().await?;
        /// ```
        pub async fn store_gateway_metadata(&self) -> Result<()> {
            let key = format!("api_gateway_config:{}", self.config.name);
            let metadata = serde_json::json!({
                "name": self.config.name,
                "initialized_at": chrono::Utc::now().to_rfc3339()
            });
            let value = serde_json::to_string(&metadata).map_err(|e| {
                crate::errors::AuthError::internal(format!("Serialization error: {}", e))
            })?;

            self.storage.store_kv(&key, value.as_bytes(), None).await?;
            tracing::info!("Stored API Gateway metadata for: {}", self.config.name);
            Ok(())
        }

        /// Store API route configuration.
        ///
        /// # Example
        /// ```rust,ignore
        /// gw.store_route_config("/api/users", "{\"auth\": true}").await?;
        /// ```
        pub async fn store_route_config(&self, route_path: &str, config_data: &str) -> Result<()> {
            let key = format!("api_gateway_route:{}:{}", self.config.name, route_path);
            self.storage
                .store_kv(&key, config_data.as_bytes(), None)
                .await?;
            tracing::info!(
                "Stored route config for {} on gateway: {}",
                route_path,
                self.config.name
            );
            Ok(())
        }

        /// Get API route configuration.
        ///
        /// # Example
        /// ```rust,ignore
        /// if let Some(cfg) = gw.get_route_config("/api/users").await? {
        ///     println!("route config: {}", cfg);
        /// }
        /// ```
        pub async fn get_route_config(&self, route_path: &str) -> Result<Option<String>> {
            let key = format!("api_gateway_route:{}:{}", self.config.name, route_path);

            if let Some(config_bytes) = self.storage.get_kv(&key).await? {
                let config = String::from_utf8(config_bytes).map_err(|e| {
                    crate::errors::AuthError::internal(format!("UTF-8 conversion error: {}", e))
                })?;
                Ok(Some(config))
            } else {
                Ok(None)
            }
        }

        /// Get gateway name from config.
        ///
        /// # Example
        /// ```rust,ignore
        /// let name = gw.get_gateway_name();
        /// ```
        pub fn get_gateway_name(&self) -> &str {
            &self.config.name
        }
    }
}

/// SAML Identity Provider
pub mod saml_idp {
    use crate::errors::Result;
    use crate::storage::AuthStorage;
    use serde::{Deserialize, Serialize};
    use std::sync::Arc;

    /// Configuration for the SAML Identity Provider.
    ///
    /// # Example
    /// ```rust,ignore
    /// let config = SamlIdpConfig { entity_id: "https://idp.example.com".into() };
    /// ```
    #[derive(Debug, Clone)]
    pub struct SamlIdpConfig {
        pub entity_id: String,
    }

    impl Default for SamlIdpConfig {
        fn default() -> Self {
            Self {
                entity_id: "https://auth.example.com".to_string(),
            }
        }
    }

    pub struct SamlIdentityProvider {
        config: SamlIdpConfig,
        storage: Arc<dyn AuthStorage>,
    }

    impl SamlIdentityProvider {
        /// Create a new SAML Identity Provider.
        ///
        /// # Example
        /// ```rust,ignore
        /// let idp = SamlIdentityProvider::new(SamlIdpConfig::default(), storage).await?;
        /// ```
        pub async fn new(config: SamlIdpConfig, storage: Arc<dyn AuthStorage>) -> Result<Self> {
            Ok(Self { config, storage })
        }

        /// Perform any async initialization after construction.
        ///
        /// `SamlIdentityProvider` requires no async startup work — all state is set up in [`Self::new`].
        /// This method is provided for API symmetry with other server modules that
        /// do require an initialization step (e.g., connecting to external services).
        ///
        /// # Example
        /// ```rust,ignore
        /// idp.initialize().await?;
        /// ```
        pub async fn initialize(&self) -> Result<()> {
            Ok(())
        }

        /// Get the SAML metadata for this identity provider.
        ///
        /// # Example
        /// ```rust,ignore
        /// let meta = idp.get_metadata().await?;
        /// println!("entity: {}", meta.entity_id);
        /// ```
        pub async fn get_metadata(&self) -> Result<SamlMetadata> {
            Ok(SamlMetadata {
                entity_id: self.config.entity_id.clone(),
            })
        }

        /// Store SAML metadata in storage.
        ///
        /// # Example
        /// ```rust,ignore
        /// idp.store_saml_metadata(&meta).await?;
        /// ```
        pub async fn store_saml_metadata(&self, metadata: &SamlMetadata) -> Result<()> {
            let key = format!("saml_metadata:{}", self.config.entity_id);
            let value = serde_json::to_string(metadata).map_err(|e| {
                crate::errors::AuthError::internal(format!("Serialization error: {}", e))
            })?;

            self.storage.store_kv(&key, value.as_bytes(), None).await?;
            tracing::info!("Stored SAML metadata for entity: {}", self.config.entity_id);
            Ok(())
        }

        /// Store SAML assertion.
        ///
        /// # Example
        /// ```rust,ignore
        /// idp.store_assertion("assertion-1", "<xml>...</xml>").await?;
        /// ```
        pub async fn store_assertion(
            &self,
            assertion_id: &str,
            assertion_data: &str,
        ) -> Result<()> {
            let key = format!("saml_assertion:{}:{}", self.config.entity_id, assertion_id);
            self.storage
                .store_kv(
                    &key,
                    assertion_data.as_bytes(),
                    Some(std::time::Duration::from_secs(3600)),
                )
                .await?;
            tracing::info!(
                "Stored SAML assertion {} for entity: {}",
                assertion_id,
                self.config.entity_id
            );
            Ok(())
        }

        /// Retrieve SAML assertion.
        ///
        /// # Example
        /// ```rust,ignore
        /// if let Some(xml) = idp.get_assertion("assertion-1").await? {
        ///     println!("assertion: {}", xml);
        /// }
        /// ```
        pub async fn get_assertion(&self, assertion_id: &str) -> Result<Option<String>> {
            let key = format!("saml_assertion:{}:{}", self.config.entity_id, assertion_id);

            if let Some(assertion_bytes) = self.storage.get_kv(&key).await? {
                let assertion = String::from_utf8(assertion_bytes).map_err(|e| {
                    crate::errors::AuthError::internal(format!("UTF-8 conversion error: {}", e))
                })?;
                Ok(Some(assertion))
            } else {
                Ok(None)
            }
        }
    }

    /// SAML metadata document.
    ///
    /// # Example
    /// ```rust,ignore
    /// let meta = SamlMetadata { entity_id: "https://idp.example.com".into() };
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct SamlMetadata {
        pub entity_id: String,
    }
}

// Additional server-side modules

pub mod consent {
    //! User consent management (OAuth 2.0 / OIDC consent screen logic)

    use crate::errors::Result;
    use crate::storage::AuthStorage;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;
    use std::sync::Arc;

    /// Configuration for the consent module.
    ///
    /// # Example
    /// ```rust,ignore
    /// let config = ConsentConfig { require_explicit_consent: true, remember_consent_ttl_secs: 3600 };
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ConsentConfig {
        /// Require explicit consent for every OAuth 2.0 authorization request.
        pub require_explicit_consent: bool,
        /// Remember consent decisions for this many seconds (0 = never remember).
        pub remember_consent_ttl_secs: u64,
    }

    impl Default for ConsentConfig {
        fn default() -> Self {
            Self {
                require_explicit_consent: true,
                remember_consent_ttl_secs: 86_400, // 24 hours
            }
        }
    }

    /// A stored consent decision.
    ///
    /// # Example
    /// ```rust,ignore
    /// let record = ConsentRecord {
    ///     user_id: "u1".into(), client_id: "c1".into(),
    ///     scopes: vec!["openid".into()], granted_at: 0, expires_at: None,
    /// };
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ConsentRecord {
        pub user_id: String,
        pub client_id: String,
        pub scopes: Vec<String>,
        pub granted_at: u64,
        pub expires_at: Option<u64>,
    }

    /// Manages user consent decisions with optional storage backend for persistence.
    ///
    /// When constructed with [`ConsentManager::new_with_storage`] consent decisions
    /// are written through to `AuthStorage` so they survive process restarts.
    /// When constructed with [`ConsentManager::new`] decisions are kept in-process
    /// only (suitable for testing or single-node scenarios with short TTLs).
    pub struct ConsentManager {
        config: ConsentConfig,
        /// Write-through in-process cache.  Always populated on reads.
        records: HashMap<String, ConsentRecord>,
        storage: Option<Arc<dyn AuthStorage>>,
    }

    impl std::fmt::Debug for ConsentManager {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("ConsentManager")
                .field("config", &self.config)
                .field("records", &self.records)
                .field("storage", &self.storage.is_some())
                .finish()
        }
    }

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

    impl ConsentManager {
        /// Create an in-memory-only `ConsentManager`.
        ///
        /// # Example
        /// ```rust,ignore
        /// let mgr = ConsentManager::new(ConsentConfig::default());
        /// ```
        pub fn new(config: ConsentConfig) -> Self {
            Self {
                config,
                records: HashMap::new(),
                storage: None,
            }
        }

        /// Create a storage-backed `ConsentManager` that persists decisions across
        /// restarts using the provided `AuthStorage` implementation.
        ///
        /// # Example
        /// ```rust,ignore
        /// let mgr = ConsentManager::new_with_storage(ConsentConfig::default(), storage);
        /// ```
        pub fn new_with_storage(config: ConsentConfig, storage: Arc<dyn AuthStorage>) -> Self {
            Self {
                config,
                records: HashMap::new(),
                storage: Some(storage),
            }
        }

        /// Storage key for a consent record.
        fn storage_key(user_id: &str, client_id: &str) -> String {
            format!("consent:{}:{}", user_id, client_id)
        }

        /// Record a consent decision, persisting to storage if configured.
        ///
        /// # Example
        /// ```rust,ignore
        /// mgr.grant(record).await?;
        /// ```
        pub async fn grant(&mut self, record: ConsentRecord) -> Result<()> {
            let key = format!("{}:{}", record.user_id, record.client_id);
            if let Some(storage) = &self.storage {
                let storage_key = Self::storage_key(&record.user_id, &record.client_id);
                let ttl = if record.expires_at.is_some() {
                    Some(std::time::Duration::from_secs(
                        self.config.remember_consent_ttl_secs,
                    ))
                } else {
                    None
                };
                let bytes = serde_json::to_vec(&record).map_err(|e| {
                    crate::errors::AuthError::internal(format!(
                        "Consent serialization error: {}",
                        e
                    ))
                })?;
                storage.store_kv(&storage_key, &bytes, ttl).await?;
            }
            self.records.insert(key, record);
            Ok(())
        }

        /// Revoke a previously granted consent, removing it from storage if configured.
        ///
        /// # Example
        /// ```rust,ignore
        /// let was_present = mgr.revoke("user-1", "client-1").await?;
        /// ```
        pub async fn revoke(&mut self, user_id: &str, client_id: &str) -> Result<bool> {
            let key = format!("{}:{}", user_id, client_id);
            if let Some(storage) = &self.storage {
                let storage_key = Self::storage_key(user_id, client_id);
                // Best-effort delete; ignore "not found" errors.
                let _ = storage.delete_kv(&storage_key).await;
            }
            Ok(self.records.remove(&key).is_some())
        }

        /// Check whether consent for the given scopes has already been granted.
        /// Checks the in-process cache first; falls back to storage on a cache miss.
        ///
        /// # Example
        /// ```rust,ignore
        /// let ok = mgr.has_consent("user-1", "client-1", &["openid".into()]).await?;
        /// ```
        pub async fn has_consent(
            &mut self,
            user_id: &str,
            client_id: &str,
            scopes: &[String],
        ) -> Result<bool> {
            if !self.config.require_explicit_consent {
                return Ok(true);
            }
            let key = format!("{}:{}", user_id, client_id);

            // Cache miss: try storage.
            if !self.records.contains_key(&key)
                && let Some(storage) = &self.storage
            {
                let storage_key = Self::storage_key(user_id, client_id);
                if let Ok(Some(bytes)) = storage.get_kv(&storage_key).await
                    && let Ok(record) = serde_json::from_slice::<ConsentRecord>(&bytes)
                {
                    self.records.insert(key.clone(), record);
                }
            }

            Ok(self
                .records
                .get(&key)
                .is_some_and(|record| scopes.iter().all(|s| record.scopes.contains(s))))
        }

        /// Return the configuration in use.
        ///
        /// # Example
        /// ```rust,ignore
        /// let cfg = mgr.config();
        /// assert!(cfg.require_explicit_consent);
        /// ```
        pub fn config(&self) -> &ConsentConfig {
            &self.config
        }
    }
}

pub mod introspection {
    //! Token introspection endpoint (RFC 7662)

    use serde::{Deserialize, Serialize};

    /// Configuration for the token introspection module.
    ///
    /// # Example
    /// ```rust,ignore
    /// let config = IntrospectionConfig { restrict_to_registered_servers: true, include_claims: false };
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct IntrospectionConfig {
        /// Allow only registered resource servers to call the introspection endpoint.
        pub restrict_to_registered_servers: bool,
        /// Include full JWT claims in the response.
        pub include_claims: bool,
    }

    impl Default for IntrospectionConfig {
        fn default() -> Self {
            Self {
                restrict_to_registered_servers: false,
                include_claims: true,
            }
        }
    }

    /// Response body for RFC 7662 token introspection.
    ///
    /// # Example
    /// ```rust,ignore
    /// let resp = IntrospectionResponse::inactive();
    /// assert!(!resp.active);
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct IntrospectionResponse {
        pub active: bool,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub scope: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub client_id: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub sub: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub exp: Option<i64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub iat: Option<i64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub token_type: Option<String>,
    }

    impl IntrospectionResponse {
        /// Return an `active: false` response (e.g., for invalid/expired tokens).
        ///
        /// # Example
        /// ```rust,ignore
        /// let resp = IntrospectionResponse::inactive();
        /// ```
        pub fn inactive() -> Self {
            Self {
                active: false,
                scope: None,
                client_id: None,
                sub: None,
                exp: None,
                iat: None,
                token_type: None,
            }
        }
    }

    /// Handles token introspection logic for the server.
    #[derive(Debug, Default)]
    pub struct IntrospectionManager {
        config: IntrospectionConfig,
    }

    impl IntrospectionManager {
        /// Create a new `IntrospectionManager`.
        ///
        /// # Example
        /// ```rust,ignore
        /// let mgr = IntrospectionManager::new(IntrospectionConfig::default());
        /// ```
        pub fn new(config: IntrospectionConfig) -> Self {
            Self { config }
        }

        /// Return the configuration in use.
        ///
        /// # Example
        /// ```rust,ignore
        /// let cfg = mgr.config();
        /// ```
        pub fn config(&self) -> &IntrospectionConfig {
            &self.config
        }
    }
}

pub mod device_flow_server {
    //! Device Authorization Grant server-side implementation (RFC 8628)

    use crate::errors::Result;
    use crate::storage::AuthStorage;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;
    use std::sync::Arc;

    /// Configuration for the device flow module.
    ///
    /// # Example
    /// ```rust,ignore
    /// let config = DeviceFlowConfig::default();
    /// assert_eq!(config.user_code_length, 8);
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct DeviceFlowConfig {
        /// Length of the user code (e.g., 8 characters).
        pub user_code_length: usize,
        /// How long a device code is valid (seconds).
        pub device_code_ttl_secs: u64,
        /// Minimum interval between polling requests (seconds).
        pub polling_interval_secs: u64,
        /// Verification URI shown to the user.
        pub verification_uri: String,
    }

    impl Default for DeviceFlowConfig {
        fn default() -> Self {
            Self {
                user_code_length: 8,
                device_code_ttl_secs: 1800, // 30 minutes
                polling_interval_secs: 5,
                verification_uri: "https://example.com/device".to_string(),
            }
        }
    }

    /// State of a pending device authorization request.
    ///
    /// # Example
    /// ```rust,ignore
    /// let state = DeviceAuthState::Pending;
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum DeviceAuthState {
        Pending,
        Authorized { access_token: String },
        Denied,
        Expired,
    }

    /// A device authorization record.
    ///
    /// # Example
    /// ```rust,ignore
    /// let rec = DeviceAuthRecord {
    ///     device_code: "dc".into(), user_code: "UC".into(),
    ///     client_id: "c1".into(), scopes: vec![], state: DeviceAuthState::Pending, expires_at: 0,
    /// };
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct DeviceAuthRecord {
        pub device_code: String,
        pub user_code: String,
        pub client_id: String,
        pub scopes: Vec<String>,
        pub state: DeviceAuthState,
        pub expires_at: u64,
    }

    /// Manages device authorization flow state with optional storage backend.
    ///
    /// When constructed with [`DeviceFlowManager::new_with_storage`] device auth
    /// records are written through to `AuthStorage` with TTL equal to
    /// `device_code_ttl_secs`, surviving process restarts and enabling
    /// multi-instance deployments.  When constructed with [`DeviceFlowManager::new`]
    /// records are kept in-process only.
    pub struct DeviceFlowManager {
        config: DeviceFlowConfig,
        /// Write-through in-process cache keyed by `device_code`.
        records: HashMap<String, DeviceAuthRecord>,
        storage: Option<Arc<dyn AuthStorage>>,
    }

    impl std::fmt::Debug for DeviceFlowManager {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("DeviceFlowManager")
                .field("config", &self.config)
                .field("records", &self.records)
                .field("storage", &self.storage.is_some())
                .finish()
        }
    }

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

    impl DeviceFlowManager {
        /// Create an in-memory-only `DeviceFlowManager`.
        ///
        /// # Example
        /// ```rust,ignore
        /// let mgr = DeviceFlowManager::new(DeviceFlowConfig::default());
        /// ```
        pub fn new(config: DeviceFlowConfig) -> Self {
            Self {
                config,
                records: HashMap::new(),
                storage: None,
            }
        }

        /// Create a storage-backed `DeviceFlowManager` that persists device
        /// authorization records across restarts.
        ///
        /// # Example
        /// ```rust,ignore
        /// let mgr = DeviceFlowManager::new_with_storage(DeviceFlowConfig::default(), storage);
        /// ```
        pub fn new_with_storage(config: DeviceFlowConfig, storage: Arc<dyn AuthStorage>) -> Self {
            Self {
                config,
                records: HashMap::new(),
                storage: Some(storage),
            }
        }

        /// Storage key for a device auth record.
        fn storage_key(device_code: &str) -> String {
            format!("device_flow:{}", device_code)
        }

        /// Persist a new device authorization record.
        ///
        /// # Example
        /// ```rust,ignore
        /// mgr.register(record).await?;
        /// ```
        pub async fn register(&mut self, record: DeviceAuthRecord) -> Result<()> {
            if let Some(storage) = &self.storage {
                let key = Self::storage_key(&record.device_code);
                let ttl = Some(std::time::Duration::from_secs(
                    self.config.device_code_ttl_secs,
                ));
                let bytes = serde_json::to_vec(&record).map_err(|e| {
                    crate::errors::AuthError::internal(format!(
                        "DeviceFlowRecord serialization error: {}",
                        e
                    ))
                })?;
                storage.store_kv(&key, &bytes, ttl).await?;
            }
            self.records.insert(record.device_code.clone(), record);
            Ok(())
        }

        /// Look up a record by device code, checking storage on a cache miss.
        ///
        /// # Example
        /// ```rust,ignore
        /// if let Some(rec) = mgr.get("device-code-1").await? {
        ///     println!("state: {:?}", rec.state);
        /// }
        /// ```
        pub async fn get(&mut self, device_code: &str) -> Result<Option<DeviceAuthRecord>> {
            if let Some(record) = self.records.get(device_code) {
                return Ok(Some(record.clone()));
            }
            if let Some(storage) = &self.storage {
                let key = Self::storage_key(device_code);
                if let Some(bytes) = storage.get_kv(&key).await?
                    && let Ok(record) = serde_json::from_slice::<DeviceAuthRecord>(&bytes)
                {
                    self.records.insert(device_code.to_string(), record.clone());
                    return Ok(Some(record));
                }
            }
            Ok(None)
        }

        /// Approve the authorization request for a given user code.
        ///
        /// # Example
        /// ```rust,ignore
        /// let approved = mgr.approve("USER-CODE", "access-token".into()).await?;
        /// ```
        pub async fn approve(&mut self, user_code: &str, access_token: String) -> Result<bool> {
            // Find the matching record (by user_code, not device_code).
            let device_code = self
                .records
                .values()
                .find(|r| r.user_code == user_code)
                .map(|r| r.device_code.clone());

            if let Some(dc) = device_code {
                if let Some(record) = self.records.get_mut(&dc) {
                    record.state = DeviceAuthState::Authorized {
                        access_token: access_token.clone(),
                    };
                    if let Some(storage) = &self.storage {
                        let key = Self::storage_key(&dc);
                        let bytes = serde_json::to_vec(&*record).map_err(|e| {
                            crate::errors::AuthError::internal(format!(
                                "DeviceFlowRecord serialization error: {}",
                                e
                            ))
                        })?;
                        // Preserve remaining TTL rather than resetting it.
                        storage.store_kv(&key, &bytes, None).await?;
                    }
                }
                return Ok(true);
            }
            Ok(false)
        }

        /// Return the configuration in use.
        ///
        /// # Example
        /// ```rust,ignore
        /// let cfg = mgr.config();
        /// assert_eq!(cfg.user_code_length, 8);
        /// ```
        pub fn config(&self) -> &DeviceFlowConfig {
            &self.config
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::MemoryStorage;
    use std::sync::Arc;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn now_secs() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }

    // ── JwtServer ───────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_jwt_server_store_and_get_metadata() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = jwt_server::JwtServerConfig {
            issuer: "https://auth.example.com".into(),
            key_id: "key_1".into(),
        };
        let server = jwt_server::JwtServer::new(config, storage).await.unwrap();
        server.initialize().await.unwrap();

        let wkc = server.get_well_known_jwt_configuration().await.unwrap();
        assert_eq!(wkc.issuer, "https://auth.example.com");

        server.store_jwt_metadata(&wkc).await.unwrap();
        let retrieved = server.get_stored_metadata().await.unwrap();
        assert!(retrieved.is_some());
    }

    #[tokio::test]
    async fn test_jwt_server_store_signing_key() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = jwt_server::JwtServerConfig {
            issuer: "https://auth.example.com".into(),
            key_id: "key_2".into(),
        };
        let server = jwt_server::JwtServer::new(config, storage).await.unwrap();
        server.store_signing_key("test-key-data").await.unwrap();
    }

    // ── ApiGateway ──────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_api_gateway_store_and_get_route() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = api_gateway::ApiGatewayConfig {
            name: "test-gw".into(),
        };
        let gw = api_gateway::ApiGateway::new(config, storage).await.unwrap();
        gw.initialize().await.unwrap();
        assert_eq!(gw.get_gateway_name(), "test-gw");

        gw.store_route_config("/api/v1/users", r#"{"auth":"required"}"#)
            .await
            .unwrap();
        let route = gw.get_route_config("/api/v1/users").await.unwrap();
        assert!(route.is_some());
    }

    #[tokio::test]
    async fn test_api_gateway_get_route_not_found() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = api_gateway::ApiGatewayConfig { name: "gw2".into() };
        let gw = api_gateway::ApiGateway::new(config, storage).await.unwrap();
        assert!(gw.get_route_config("/nope").await.unwrap().is_none());
    }

    // ── SAML IdP ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_saml_idp_store_and_get_assertion() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = saml_idp::SamlIdpConfig {
            entity_id: "urn:example:idp".into(),
        };
        let saml = saml_idp::SamlIdentityProvider::new(config, storage)
            .await
            .unwrap();
        saml.initialize().await.unwrap();
        saml.store_assertion("assert_1", "<Assertion/>")
            .await
            .unwrap();
        let a = saml.get_assertion("assert_1").await.unwrap();
        assert_eq!(a.as_deref(), Some("<Assertion/>"));
    }

    #[tokio::test]
    async fn test_saml_idp_get_assertion_not_found() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = saml_idp::SamlIdpConfig {
            entity_id: "urn:example:idp2".into(),
        };
        let saml = saml_idp::SamlIdentityProvider::new(config, storage)
            .await
            .unwrap();
        assert!(saml.get_assertion("nope").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_saml_idp_metadata() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = saml_idp::SamlIdpConfig {
            entity_id: "urn:example:idp3".into(),
        };
        let saml = saml_idp::SamlIdentityProvider::new(config, storage)
            .await
            .unwrap();
        let meta = saml.get_metadata().await.unwrap();
        assert_eq!(meta.entity_id, "urn:example:idp3");
    }

    // ── ConsentManager ──────────────────────────────────────────────────

    #[tokio::test]
    async fn test_consent_grant_and_check() {
        let config = consent::ConsentConfig {
            require_explicit_consent: true,
            remember_consent_ttl_secs: 3600,
        };
        let mut cm = consent::ConsentManager::new(config);
        let record = consent::ConsentRecord {
            user_id: "user1".into(),
            client_id: "client1".into(),
            scopes: vec!["read".into(), "write".into()],
            granted_at: now_secs(),
            expires_at: Some(now_secs() + 3600),
        };
        cm.grant(record).await.unwrap();
        assert!(
            cm.has_consent("user1", "client1", &["read".into()])
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_consent_no_consent_by_default() {
        let config = consent::ConsentConfig {
            require_explicit_consent: true,
            remember_consent_ttl_secs: 3600,
        };
        let mut cm = consent::ConsentManager::new(config);
        assert!(
            !cm.has_consent("user1", "client1", &["read".into()])
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_consent_revoke() {
        let config = consent::ConsentConfig {
            require_explicit_consent: true,
            remember_consent_ttl_secs: 3600,
        };
        let mut cm = consent::ConsentManager::new(config);
        cm.grant(consent::ConsentRecord {
            user_id: "user2".into(),
            client_id: "client2".into(),
            scopes: vec!["read".into()],
            granted_at: now_secs(),
            expires_at: None,
        })
        .await
        .unwrap();
        let revoked = cm.revoke("user2", "client2").await.unwrap();
        assert!(revoked);
        assert!(
            !cm.has_consent("user2", "client2", &["read".into()])
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_consent_revoke_nonexistent() {
        let config = consent::ConsentConfig {
            require_explicit_consent: true,
            remember_consent_ttl_secs: 3600,
        };
        let mut cm = consent::ConsentManager::new(config);
        let revoked = cm.revoke("ghost", "ghost_client").await.unwrap();
        assert!(!revoked);
    }

    #[tokio::test]
    async fn test_consent_with_storage() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = consent::ConsentConfig {
            require_explicit_consent: true,
            remember_consent_ttl_secs: 3600,
        };
        let mut cm = consent::ConsentManager::new_with_storage(config, storage);
        cm.grant(consent::ConsentRecord {
            user_id: "user3".into(),
            client_id: "client3".into(),
            scopes: vec!["openid".into()],
            granted_at: now_secs(),
            expires_at: None,
        })
        .await
        .unwrap();
        assert!(
            cm.has_consent("user3", "client3", &["openid".into()])
                .await
                .unwrap()
        );
    }

    // ── DeviceFlowManager ───────────────────────────────────────────────

    #[tokio::test]
    async fn test_device_flow_register_and_get() {
        let config = device_flow_server::DeviceFlowConfig {
            user_code_length: 8,
            device_code_ttl_secs: 300,
            polling_interval_secs: 5,
            verification_uri: "https://auth.example.com/device".into(),
        };
        let mut df = device_flow_server::DeviceFlowManager::new(config);
        df.register(device_flow_server::DeviceAuthRecord {
            device_code: "dev_code_1".into(),
            user_code: "ABCD-EFGH".into(),
            client_id: "mobile_app".into(),
            scopes: vec!["read".into()],
            state: device_flow_server::DeviceAuthState::Pending,
            expires_at: now_secs() + 300,
        })
        .await
        .unwrap();
        let record = df.get("dev_code_1").await.unwrap();
        assert!(record.is_some());
        assert_eq!(record.unwrap().user_code, "ABCD-EFGH");
    }

    #[tokio::test]
    async fn test_device_flow_get_not_found() {
        let config = device_flow_server::DeviceFlowConfig {
            user_code_length: 8,
            device_code_ttl_secs: 300,
            polling_interval_secs: 5,
            verification_uri: "https://auth.example.com/device".into(),
        };
        let mut df = device_flow_server::DeviceFlowManager::new(config);
        assert!(df.get("nonexistent").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_device_flow_approve() {
        let config = device_flow_server::DeviceFlowConfig {
            user_code_length: 8,
            device_code_ttl_secs: 300,
            polling_interval_secs: 5,
            verification_uri: "https://auth.example.com/device".into(),
        };
        let mut df = device_flow_server::DeviceFlowManager::new(config);
        df.register(device_flow_server::DeviceAuthRecord {
            device_code: "dev_code_2".into(),
            user_code: "WXYZ-1234".into(),
            client_id: "tv_app".into(),
            scopes: vec!["read".into()],
            state: device_flow_server::DeviceAuthState::Pending,
            expires_at: now_secs() + 300,
        })
        .await
        .unwrap();
        let approved = df
            .approve("WXYZ-1234", "access_token_here".into())
            .await
            .unwrap();
        assert!(approved);

        let record = df.get("dev_code_2").await.unwrap().unwrap();
        matches!(
            record.state,
            device_flow_server::DeviceAuthState::Authorized { .. }
        );
    }

    #[tokio::test]
    async fn test_device_flow_approve_nonexistent() {
        let config = device_flow_server::DeviceFlowConfig {
            user_code_length: 8,
            device_code_ttl_secs: 300,
            polling_interval_secs: 5,
            verification_uri: "https://auth.example.com/device".into(),
        };
        let mut df = device_flow_server::DeviceFlowManager::new(config);
        let approved = df.approve("NO_CODE", "token".into()).await.unwrap();
        assert!(!approved);
    }

    #[tokio::test]
    async fn test_device_flow_with_storage() {
        let storage: Arc<dyn crate::storage::AuthStorage> = Arc::new(MemoryStorage::new());
        let config = device_flow_server::DeviceFlowConfig {
            user_code_length: 8,
            device_code_ttl_secs: 300,
            polling_interval_secs: 5,
            verification_uri: "https://auth.example.com/device".into(),
        };
        let mut df = device_flow_server::DeviceFlowManager::new_with_storage(config, storage);
        df.register(device_flow_server::DeviceAuthRecord {
            device_code: "stored_code".into(),
            user_code: "ST0R-ED00".into(),
            client_id: "app".into(),
            scopes: vec![],
            state: device_flow_server::DeviceAuthState::Pending,
            expires_at: now_secs() + 300,
        })
        .await
        .unwrap();
        let record = df.get("stored_code").await.unwrap();
        assert!(record.is_some());
    }

    // ── IntrospectionManager ────────────────────────────────────────────

    #[test]
    fn test_introspection_inactive_response() {
        let resp = introspection::IntrospectionResponse::inactive();
        assert!(!resp.active);
    }
}