rivven-core 0.0.10

Core library for Rivven distributed event streaming platform
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
//! OpenID Connect (OIDC) Authentication for Rivven
//!
//! This module provides OIDC-based authentication, allowing Rivven to integrate
//! with identity providers like Keycloak, Auth0, Okta, Azure AD, and more.
//!
//! ## Features
//!
//! - **JWT validation** with JWKS (JSON Web Key Set) support
//! - **Claims mapping** to Rivven principals and groups
//! - **Multiple provider** support with per-provider configuration
//! - **Token refresh** handling
//! - **Custom claims** extraction for fine-grained authorization
//!
//! ## Example Configuration
//!
//! ```yaml
//! authentication:
//!   oidc:
//!     providers:
//!       - name: keycloak
//!         issuer: https://auth.example.com/realms/rivven
//!         audience: rivven-api
//!         username_claim: preferred_username
//!         groups_claim: groups
//!         roles_claim: realm_access.roles
//!
//!       - name: azure-ad
//!         issuer: https://login.microsoftonline.com/{tenant-id}/v2.0
//!         audience: api://rivven
//!         username_claim: upn
//!         groups_claim: groups
//! ```
//!
//! ## Flow
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────────────────────┐
//! │                        OIDC AUTHENTICATION FLOW                          │
//! ├──────────────────────────────────────────────────────────────────────────┤
//! │                                                                          │
//! │  1. Client obtains JWT from Identity Provider                            │
//! │     ┌─────────┐         ┌─────────────────┐                              │
//! │     │ Client  │ ──────► │ Identity        │                              │
//! │     │         │ ◄────── │ Provider (IdP)  │  ← Returns JWT               │
//! │     └─────────┘         └─────────────────┘                              │
//! │                                                                          │
//! │  2. Client sends JWT to Rivven                                           │
//! │     ┌─────────┐         ┌─────────────────┐                              │
//! │     │ Client  │ ──JWT──►│ Rivven Server   │                              │
//! │     │         │         │                 │                              │
//! │     └─────────┘         └─────────────────┘                              │
//! │                                 │                                         │
//! │  3. Rivven validates JWT         │                                         │
//! │                                 ▼                                         │
//! │     ┌─────────────────────────────────────────────────────────────┐       │
//! │     │ a) Fetch JWKS from IdP (cached)                             │       │
//! │     │ b) Verify signature with public key                         │       │
//! │     │ c) Validate claims (iss, aud, exp, nbf)                     │       │
//! │     │ d) Extract username, groups, roles                          │       │
//! │     │ e) Create Rivven Principal                                  │       │
//! │     └─────────────────────────────────────────────────────────────┘       │
//! │                                                                          │
//! └──────────────────────────────────────────────────────────────────────────┘
//! ```

#[cfg(feature = "oidc")]
mod oidc_impl {
    use jsonwebtoken::{
        decode, decode_header, jwk::JwkSet, Algorithm, DecodingKey, TokenData, Validation,
    };
    use parking_lot::RwLock;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;
    use std::time::{Duration, Instant};
    use thiserror::Error;
    use tracing::{debug, info};

    // ============================================================================
    // Error Types
    // ============================================================================

    #[derive(Error, Debug)]
    pub enum OidcError {
        #[error("Provider not found: {0}")]
        ProviderNotFound(String),

        #[error("Invalid token: {0}")]
        InvalidToken(String),

        #[error("Token expired")]
        TokenExpired,

        #[error("Invalid issuer: expected {expected}, got {actual}")]
        InvalidIssuer { expected: String, actual: String },

        #[error("Invalid audience: {0}")]
        InvalidAudience(String),

        #[error("Missing claim: {0}")]
        MissingClaim(String),

        #[error("JWKS fetch error: {0}")]
        JwksFetch(String),

        #[error("Key not found for kid: {0}")]
        KeyNotFound(String),

        #[error("Configuration error: {0}")]
        Configuration(String),

        #[error("HTTP error: {0}")]
        Http(String),

        #[error("JSON error: {0}")]
        Json(String),
    }

    pub type OidcResult<T> = Result<T, OidcError>;

    // ============================================================================
    // Configuration
    // ============================================================================

    /// OIDC provider configuration
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct OidcProviderConfig {
        /// Unique name for this provider
        pub name: String,

        /// OIDC issuer URL (e.g., `https://auth.example.com/realms/rivven`)
        pub issuer: String,

        /// Expected audience (client_id or API identifier)
        pub audience: String,

        /// Claim to extract username from (default: "sub")
        #[serde(default = "default_username_claim")]
        pub username_claim: String,

        /// Claim to extract groups from (optional)
        pub groups_claim: Option<String>,

        /// Claim to extract roles from (optional)
        pub roles_claim: Option<String>,

        /// Additional required claims
        #[serde(default)]
        pub required_claims: Vec<String>,

        /// Clock skew tolerance in seconds
        #[serde(default = "default_clock_skew")]
        pub clock_skew_seconds: u64,

        /// JWKS cache TTL in seconds
        #[serde(default = "default_jwks_cache_ttl")]
        pub jwks_cache_ttl_seconds: u64,

        /// Allowed algorithms (default: RS256)
        #[serde(default = "default_algorithms")]
        pub algorithms: Vec<String>,

        /// Custom JWKS endpoint (if different from standard discovery)
        pub jwks_uri: Option<String>,

        /// Whether this provider is enabled
        #[serde(default = "default_enabled")]
        pub enabled: bool,
    }

    fn default_username_claim() -> String {
        "sub".to_string()
    }

    fn default_clock_skew() -> u64 {
        60
    }

    fn default_jwks_cache_ttl() -> u64 {
        3600
    }

    fn default_algorithms() -> Vec<String> {
        vec!["RS256".to_string()]
    }

    fn default_enabled() -> bool {
        true
    }

    impl OidcProviderConfig {
        pub fn new(
            name: impl Into<String>,
            issuer: impl Into<String>,
            audience: impl Into<String>,
        ) -> Self {
            Self {
                name: name.into(),
                issuer: issuer.into(),
                audience: audience.into(),
                username_claim: default_username_claim(),
                groups_claim: None,
                roles_claim: None,
                required_claims: vec![],
                clock_skew_seconds: default_clock_skew(),
                jwks_cache_ttl_seconds: default_jwks_cache_ttl(),
                algorithms: default_algorithms(),
                jwks_uri: None,
                enabled: true,
            }
        }

        pub fn with_groups_claim(mut self, claim: impl Into<String>) -> Self {
            self.groups_claim = Some(claim.into());
            self
        }

        pub fn with_roles_claim(mut self, claim: impl Into<String>) -> Self {
            self.roles_claim = Some(claim.into());
            self
        }

        pub fn with_username_claim(mut self, claim: impl Into<String>) -> Self {
            self.username_claim = claim.into();
            self
        }

        pub fn jwks_url(&self) -> String {
            self.jwks_uri.clone().unwrap_or_else(|| {
                format!(
                    "{}/.well-known/jwks.json",
                    self.issuer.trim_end_matches('/')
                )
            })
        }
    }

    // ============================================================================
    // Claims
    // ============================================================================

    /// Standard JWT claims
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct StandardClaims {
        /// Subject (user identifier)
        pub sub: String,

        /// Issuer
        pub iss: String,

        /// Audience
        #[serde(default)]
        pub aud: ClaimValue,

        /// Expiration time
        pub exp: u64,

        /// Issued at time
        #[serde(default)]
        pub iat: Option<u64>,

        /// Not before time
        #[serde(default)]
        pub nbf: Option<u64>,

        /// JWT ID
        #[serde(default)]
        pub jti: Option<String>,

        /// Additional claims
        #[serde(flatten)]
        pub extra: HashMap<String, serde_json::Value>,
    }

    /// Claims can be string or array
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[serde(untagged)]
    pub enum ClaimValue {
        Single(String),
        Multiple(Vec<String>),
    }

    impl Default for ClaimValue {
        fn default() -> Self {
            ClaimValue::Multiple(vec![])
        }
    }

    impl ClaimValue {
        pub fn contains(&self, value: &str) -> bool {
            match self {
                ClaimValue::Single(s) => s == value,
                ClaimValue::Multiple(v) => v.iter().any(|s| s == value),
            }
        }
    }

    // ============================================================================
    // Validated Identity
    // ============================================================================

    /// Identity extracted from validated JWT
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct OidcIdentity {
        /// Username extracted from configured claim
        pub username: String,

        /// Subject from token
        pub subject: String,

        /// Issuer (provider) that issued the token
        pub issuer: String,

        /// Provider name that validated this token
        pub provider: String,

        /// Groups extracted from token (if configured)
        pub groups: Vec<String>,

        /// Roles extracted from token (if configured)
        pub roles: Vec<String>,

        /// Email (if present)
        pub email: Option<String>,

        /// Full name (if present)
        pub name: Option<String>,

        /// Token expiration time
        pub expires_at: u64,

        /// All claims from the token
        pub claims: HashMap<String, serde_json::Value>,
    }

    impl OidcIdentity {
        /// Check if identity has a specific group
        pub fn has_group(&self, group: &str) -> bool {
            self.groups.iter().any(|g| g == group)
        }

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

        /// Check if identity has any of the specified groups
        pub fn has_any_group(&self, groups: &[&str]) -> bool {
            groups.iter().any(|g| self.has_group(g))
        }

        /// Check if identity has all of the specified groups
        pub fn has_all_groups(&self, groups: &[&str]) -> bool {
            groups.iter().all(|g| self.has_group(g))
        }
    }

    // ============================================================================
    // JWKS Cache
    // ============================================================================

    #[derive(Debug)]
    struct CachedJwks {
        jwks: JwkSet,
        fetched_at: Instant,
        ttl: Duration,
    }

    impl CachedJwks {
        fn is_expired(&self) -> bool {
            self.fetched_at.elapsed() > self.ttl
        }
    }

    // ============================================================================
    // OIDC Authenticator
    // ============================================================================

    /// OIDC authenticator supporting multiple providers
    pub struct OidcAuthenticator {
        /// Configured providers
        providers: HashMap<String, OidcProviderConfig>,

        /// JWKS cache per provider
        jwks_cache: RwLock<HashMap<String, CachedJwks>>,

        /// HTTP client for JWKS fetching
        http_client: reqwest::Client,
    }

    impl OidcAuthenticator {
        /// Create a new authenticator with no providers
        pub fn new() -> Self {
            Self {
                providers: HashMap::new(),
                jwks_cache: RwLock::new(HashMap::new()),
                http_client: reqwest::Client::builder()
                    .timeout(Duration::from_secs(10))
                    .build()
                    .expect("Failed to create HTTP client"),
            }
        }

        /// Add a provider
        pub fn add_provider(&mut self, config: OidcProviderConfig) {
            if config.enabled {
                info!("Added OIDC provider: {} ({})", config.name, config.issuer);
                self.providers.insert(config.name.clone(), config);
            }
        }

        /// Get list of provider names
        pub fn provider_names(&self) -> Vec<&str> {
            self.providers.keys().map(|s| s.as_str()).collect()
        }

        /// Validate a JWT token and extract identity
        ///
        /// Tries each provider in order until one succeeds
        pub async fn validate_token(&self, token: &str) -> OidcResult<OidcIdentity> {
            // Decode header to get issuer hint
            let header = decode_header(token)
                .map_err(|e| OidcError::InvalidToken(format!("Invalid header: {}", e)))?;

            // Try to find provider by issuer in token (peek at claims)
            let insecure_claims = self.peek_claims(token)?;

            // Find matching provider by issuer
            let provider = self
                .providers
                .values()
                .find(|p| p.issuer == insecure_claims.iss)
                .ok_or_else(|| OidcError::ProviderNotFound(insecure_claims.iss.clone()))?;

            // Validate with the matching provider
            self.validate_with_provider(token, provider, &header.kid)
                .await
        }

        /// Validate a JWT token with a specific provider
        pub async fn validate_with_provider_name(
            &self,
            token: &str,
            provider_name: &str,
        ) -> OidcResult<OidcIdentity> {
            let provider = self
                .providers
                .get(provider_name)
                .ok_or_else(|| OidcError::ProviderNotFound(provider_name.to_string()))?;

            let header = decode_header(token)
                .map_err(|e| OidcError::InvalidToken(format!("Invalid header: {}", e)))?;

            self.validate_with_provider(token, provider, &header.kid)
                .await
        }

        /// Peek at claims without validation (for issuer discovery)
        fn peek_claims(&self, token: &str) -> OidcResult<StandardClaims> {
            let mut validation = Validation::default();
            validation.insecure_disable_signature_validation();
            validation.validate_exp = false;
            validation.validate_aud = false;

            let data: TokenData<StandardClaims> =
                decode(token, &DecodingKey::from_secret(&[]), &validation)
                    .map_err(|e| OidcError::InvalidToken(format!("Cannot parse token: {}", e)))?;

            Ok(data.claims)
        }

        /// Validate with a specific provider
        async fn validate_with_provider(
            &self,
            token: &str,
            provider: &OidcProviderConfig,
            kid: &Option<String>,
        ) -> OidcResult<OidcIdentity> {
            // Get JWKS (from cache or fetch)
            let jwks = self.get_jwks(provider).await?;

            // Find the key
            let key = if let Some(kid) = kid {
                jwks.find(kid)
                    .ok_or_else(|| OidcError::KeyNotFound(kid.clone()))?
            } else {
                // Use first key if no kid
                jwks.keys
                    .first()
                    .ok_or_else(|| OidcError::KeyNotFound("no keys in JWKS".to_string()))?
            };

            // Build decoding key
            let decoding_key = DecodingKey::from_jwk(key)
                .map_err(|e| OidcError::InvalidToken(format!("Invalid JWK: {}", e)))?;

            // Build validation
            // Parse configured algorithms (default to RS256)
            let algorithms: Vec<Algorithm> = provider
                .algorithms
                .iter()
                .filter_map(|a| match a.as_str() {
                    "RS256" => Some(Algorithm::RS256),
                    "RS384" => Some(Algorithm::RS384),
                    "RS512" => Some(Algorithm::RS512),
                    "ES256" => Some(Algorithm::ES256),
                    "ES384" => Some(Algorithm::ES384),
                    "PS256" => Some(Algorithm::PS256),
                    "PS384" => Some(Algorithm::PS384),
                    "PS512" => Some(Algorithm::PS512),
                    _ => None,
                })
                .collect();
            let primary_alg = algorithms.first().copied().unwrap_or(Algorithm::RS256);
            let mut validation = Validation::new(primary_alg);
            if algorithms.len() > 1 {
                validation.algorithms = algorithms;
            }
            validation.set_issuer(&[&provider.issuer]);
            validation.set_audience(&[&provider.audience]);
            validation.leeway = provider.clock_skew_seconds;

            // Decode and validate
            let data: TokenData<StandardClaims> = decode(token, &decoding_key, &validation)
                .map_err(|e| match e.kind() {
                    jsonwebtoken::errors::ErrorKind::ExpiredSignature => OidcError::TokenExpired,
                    jsonwebtoken::errors::ErrorKind::InvalidIssuer => OidcError::InvalidIssuer {
                        expected: provider.issuer.clone(),
                        actual: "unknown".to_string(),
                    },
                    jsonwebtoken::errors::ErrorKind::InvalidAudience => {
                        OidcError::InvalidAudience(provider.audience.clone())
                    }
                    _ => OidcError::InvalidToken(format!("{}", e)),
                })?;

            let claims = data.claims;

            // Extract username
            let username = self.extract_claim(&claims, &provider.username_claim)?;

            // Extract groups
            let groups = provider
                .groups_claim
                .as_ref()
                .and_then(|claim| self.extract_string_array(&claims, claim))
                .unwrap_or_default();

            // Extract roles
            let roles = provider
                .roles_claim
                .as_ref()
                .and_then(|claim| self.extract_string_array(&claims, claim))
                .unwrap_or_default();

            // Extract common claims
            let email = claims
                .extra
                .get("email")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());
            let name = claims
                .extra
                .get("name")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            // Validate required claims
            for claim in &provider.required_claims {
                if !claims.extra.contains_key(claim) {
                    return Err(OidcError::MissingClaim(claim.clone()));
                }
            }

            debug!(
                "Validated OIDC token for user '{}' from provider '{}'",
                username, provider.name
            );

            Ok(OidcIdentity {
                username,
                subject: claims.sub,
                issuer: claims.iss,
                provider: provider.name.clone(),
                groups,
                roles,
                email,
                name,
                expires_at: claims.exp,
                claims: claims.extra,
            })
        }

        /// Get JWKS for provider (cached)
        async fn get_jwks(&self, provider: &OidcProviderConfig) -> OidcResult<JwkSet> {
            // Check cache
            {
                let cache = self.jwks_cache.read();
                if let Some(cached) = cache.get(&provider.name) {
                    if !cached.is_expired() {
                        return Ok(cached.jwks.clone());
                    }
                }
            }

            // Fetch JWKS
            let jwks_url = provider.jwks_url();
            debug!("Fetching JWKS from: {}", jwks_url);

            let response = self
                .http_client
                .get(&jwks_url)
                .send()
                .await
                .map_err(|e| OidcError::JwksFetch(format!("HTTP error: {}", e)))?;

            if !response.status().is_success() {
                return Err(OidcError::JwksFetch(format!(
                    "HTTP {} from {}",
                    response.status(),
                    jwks_url
                )));
            }

            let jwks: JwkSet = response
                .json()
                .await
                .map_err(|e| OidcError::Json(format!("Invalid JWKS: {}", e)))?;

            // Update cache
            {
                let mut cache = self.jwks_cache.write();
                cache.insert(
                    provider.name.clone(),
                    CachedJwks {
                        jwks: jwks.clone(),
                        fetched_at: Instant::now(),
                        ttl: Duration::from_secs(provider.jwks_cache_ttl_seconds),
                    },
                );
            }

            info!(
                "Fetched and cached JWKS for provider '{}' ({} keys)",
                provider.name,
                jwks.keys.len()
            );

            Ok(jwks)
        }

        /// Extract a string claim, supporting nested paths like "realm_access.roles"
        fn extract_claim(&self, claims: &StandardClaims, path: &str) -> OidcResult<String> {
            // Handle direct standard claims first
            match path {
                "sub" => return Ok(claims.sub.clone()),
                "iss" => return Ok(claims.iss.clone()),
                _ => {}
            }

            // Handle nested paths
            let parts: Vec<&str> = path.split('.').collect();
            let mut current: &serde_json::Value = claims
                .extra
                .get(parts[0])
                .ok_or_else(|| OidcError::MissingClaim(path.to_string()))?;

            for part in &parts[1..] {
                current = current
                    .get(part)
                    .ok_or_else(|| OidcError::MissingClaim(path.to_string()))?;
            }

            current
                .as_str()
                .map(|s| s.to_string())
                .ok_or_else(|| OidcError::MissingClaim(format!("{} is not a string", path)))
        }

        /// Extract a string array from claims
        fn extract_string_array(&self, claims: &StandardClaims, path: &str) -> Option<Vec<String>> {
            let parts: Vec<&str> = path.split('.').collect();
            let mut current: Option<&serde_json::Value> = claims.extra.get(parts[0]);

            for part in &parts[1..] {
                current = current.and_then(|v| v.get(part));
            }

            current.and_then(|v| {
                v.as_array().map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
                        .collect()
                })
            })
        }

        /// Clear JWKS cache (force refresh on next validation)
        pub fn clear_cache(&self) {
            let mut cache = self.jwks_cache.write();
            cache.clear();
            info!("Cleared OIDC JWKS cache");
        }

        /// Clear cache for a specific provider
        pub fn clear_provider_cache(&self, provider_name: &str) {
            let mut cache = self.jwks_cache.write();
            cache.remove(provider_name);
            info!("Cleared OIDC JWKS cache for provider: {}", provider_name);
        }
    }

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

    // ============================================================================
    // OIDC SASL Mechanism
    // ============================================================================

    /// SASL mechanism name for OIDC (bearer token)
    pub const OIDC_SASL_MECHANISM: &str = "OAUTHBEARER";

    /// Parse OAUTHBEARER SASL message
    ///
    /// Format: `n,,\x01auth=Bearer <token>\x01\x01`
    pub fn parse_oauthbearer_message(data: &[u8]) -> OidcResult<String> {
        let message = std::str::from_utf8(data)
            .map_err(|_| OidcError::InvalidToken("Invalid UTF-8 in SASL message".to_string()))?;

        // Parse GS2 header and extract token
        // Format: n,,<SOH>auth=Bearer <token><SOH><SOH>
        let parts: Vec<&str> = message.split('\x01').collect();

        for part in parts {
            if let Some(auth) = part.strip_prefix("auth=Bearer ") {
                return Ok(auth.trim().to_string());
            }
        }

        Err(OidcError::InvalidToken(
            "No bearer token found in SASL message".to_string(),
        ))
    }

    /// Build OAUTHBEARER SASL response for successful auth
    pub fn build_oauthbearer_success() -> Vec<u8> {
        vec![]
    }

    /// Build OAUTHBEARER SASL error response
    pub fn build_oauthbearer_error(status: &str, scope: Option<&str>) -> Vec<u8> {
        let mut response = format!("{{\"status\":\"{}\"}}", status);
        if let Some(s) = scope {
            response = format!("{{\"status\":\"{}\",\"scope\":\"{}\"}}", status, s);
        }
        response.into_bytes()
    }

    // ============================================================================
    // Tests
    // ============================================================================

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

        #[test]
        fn test_provider_config() {
            let config = OidcProviderConfig::new("test", "https://auth.example.com", "test-client")
                .with_groups_claim("groups")
                .with_roles_claim("realm_access.roles");

            assert_eq!(config.name, "test");
            assert_eq!(config.issuer, "https://auth.example.com");
            assert_eq!(config.audience, "test-client");
            assert_eq!(config.groups_claim, Some("groups".to_string()));
            assert_eq!(config.roles_claim, Some("realm_access.roles".to_string()));
            assert_eq!(
                config.jwks_url(),
                "https://auth.example.com/.well-known/jwks.json"
            );
        }

        #[test]
        fn test_claim_value() {
            let single = ClaimValue::Single("test".to_string());
            assert!(single.contains("test"));
            assert!(!single.contains("other"));

            let multiple = ClaimValue::Multiple(vec!["a".to_string(), "b".to_string()]);
            assert!(multiple.contains("a"));
            assert!(multiple.contains("b"));
            assert!(!multiple.contains("c"));
        }

        #[test]
        fn test_identity_methods() {
            let identity = OidcIdentity {
                username: "alice".to_string(),
                subject: "alice-uuid".to_string(),
                issuer: "https://auth.example.com".to_string(),
                provider: "test".to_string(),
                groups: vec!["admins".to_string(), "developers".to_string()],
                roles: vec!["admin".to_string()],
                email: Some("alice@example.com".to_string()),
                name: Some("Alice".to_string()),
                expires_at: 9999999999,
                claims: HashMap::new(),
            };

            assert!(identity.has_group("admins"));
            assert!(identity.has_group("developers"));
            assert!(!identity.has_group("users"));

            assert!(identity.has_role("admin"));
            assert!(!identity.has_role("user"));

            assert!(identity.has_any_group(&["admins", "users"]));
            assert!(identity.has_all_groups(&["admins", "developers"]));
            assert!(!identity.has_all_groups(&["admins", "users"]));
        }

        #[test]
        fn test_parse_oauthbearer() {
            let message = b"n,,\x01auth=Bearer eyJhbGciOiJSUzI1NiJ9.test\x01\x01";
            let token = parse_oauthbearer_message(message).unwrap();
            assert_eq!(token, "eyJhbGciOiJSUzI1NiJ9.test");
        }

        #[test]
        fn test_parse_oauthbearer_invalid() {
            let message = b"n,,\x01invalid\x01\x01";
            let result = parse_oauthbearer_message(message);
            assert!(result.is_err());
        }
    }
}

#[cfg(feature = "oidc")]
pub use oidc_impl::*;

#[cfg(not(feature = "oidc"))]
mod no_oidc {
    //! Stub module when OIDC feature is disabled

    use std::collections::HashMap;
    use thiserror::Error;

    #[derive(Error, Debug)]
    pub enum OidcError {
        #[error("OIDC not enabled. Build with 'oidc' feature.")]
        NotEnabled,
    }

    pub type OidcResult<T> = Result<T, OidcError>;

    #[derive(Debug, Clone)]
    pub struct OidcProviderConfig {
        pub name: String,
        pub issuer: String,
        pub audience: String,
    }

    #[derive(Debug, Clone)]
    pub struct OidcIdentity {
        pub username: String,
        pub subject: String,
        pub issuer: String,
        pub provider: String,
        pub groups: Vec<String>,
        pub roles: Vec<String>,
        pub email: Option<String>,
        pub name: Option<String>,
        pub expires_at: u64,
        pub claims: HashMap<String, serde_json::Value>,
    }

    pub struct OidcAuthenticator;

    impl OidcAuthenticator {
        pub fn new() -> Self {
            Self
        }

        pub async fn validate_token(&self, _token: &str) -> OidcResult<OidcIdentity> {
            Err(OidcError::NotEnabled)
        }
    }

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

#[cfg(not(feature = "oidc"))]
pub use no_oidc::*;