auth-framework 0.5.0-rc18

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
//! Open Policy Agent (OPA) integration for externalized authorization.
//!
//! Delegates fine-grained policy evaluation to an OPA server using its
//! REST API, enabling dynamic, data-driven authorization decisions
//! expressed in Rego.
//!
//! # Architecture
//!
//! ```text
//! Application ──► OpaClient ──► POST /v1/data/{path} ──► OPA Server
//!//!                                                     Rego policies
//!//!                                                  ◄── Decision ──►
//! ```
//!
//! # References
//!
//! - [OPA REST API](https://www.openpolicyagent.org/docs/latest/rest-api/)
//! - [Rego Policy Language](https://www.openpolicyagent.org/docs/latest/policy-language/)

use crate::errors::{AuthError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use url::Url;

// ── Configuration ───────────────────────────────────────────────────

/// OPA client configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpaConfig {
    /// Base URL of the OPA server (e.g., `http://localhost:8181`).
    pub base_url: String,
    /// Default policy path for queries (e.g., `authz/allow`).
    pub default_policy_path: String,
    /// HTTP request timeout in seconds.
    pub timeout_secs: u64,
    /// Optional bearer token for authenticating with OPA.
    pub auth_token: Option<String>,
    /// Whether to cache policy decisions.
    pub enable_cache: bool,
    /// Cache TTL in seconds (0 = no expiry).
    pub cache_ttl_secs: u64,
}

impl Default for OpaConfig {
    fn default() -> Self {
        Self {
            base_url: "http://localhost:8181".to_string(),
            default_policy_path: "authz/allow".to_string(),
            timeout_secs: 5,
            auth_token: None,
            enable_cache: false,
            cache_ttl_secs: 60,
        }
    }
}

// ── OPA request / response ──────────────────────────────────────────

/// Input payload sent to OPA for policy evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpaInput {
    /// The input object passed to the Rego policy.
    pub input: serde_json::Value,
}

/// Response from an OPA policy query.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpaResponse {
    /// The policy decision result.
    #[serde(default)]
    pub result: serde_json::Value,
    /// Decision ID for audit logging.
    #[serde(default)]
    pub decision_id: Option<String>,
}

impl OpaResponse {
    /// Check if the result is a simple boolean `true`.
    pub fn is_allowed(&self) -> bool {
        self.result.as_bool().unwrap_or(false)
    }

    /// Extract a boolean from a nested path in the result.
    pub fn get_bool(&self, path: &str) -> Option<bool> {
        let mut current = &self.result;
        for segment in path.split('.') {
            current = current.get(segment)?;
        }
        current.as_bool()
    }

    /// Extract a string from a nested path in the result.
    pub fn get_str(&self, path: &str) -> Option<&str> {
        let mut current = &self.result;
        for segment in path.split('.') {
            current = current.get(segment)?;
        }
        current.as_str()
    }
}

// ── Cache entry ─────────────────────────────────────────────────────

struct CacheEntry {
    response: OpaResponse,
    expires_at: u64,
}

// ── OPA Client ──────────────────────────────────────────────────────

/// Client for evaluating authorization decisions against an OPA server.
pub struct OpaClient {
    config: OpaConfig,
    base_url: Url,
    http: reqwest::Client,
    cache: Arc<RwLock<HashMap<String, CacheEntry>>>,
}

impl OpaClient {
    /// Create a new OPA client.
    pub fn new(config: OpaConfig) -> Result<Self> {
        let base_url = normalize_opa_base_url(&config.base_url)?;

        let http = reqwest::Client::builder()
            .timeout(Duration::from_secs(config.timeout_secs))
            .build()
            .map_err(|e| AuthError::internal(&format!("HTTP client init failed: {e}")))?;

        Ok(Self {
            config,
            base_url,
            http,
            cache: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Evaluate a policy at the given path with the provided input.
    ///
    /// # Arguments
    ///
    /// * `policy_path` — The Rego package/rule path (e.g., `authz/allow`)
    /// * `input` — Arbitrary JSON input for the policy
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use auth_framework::protocols::opa::*;
    /// # async fn example() -> auth_framework::errors::Result<()> {
    /// let client = OpaClient::new(OpaConfig::default())?;
    /// let input = serde_json::json!({
    ///     "user": "alice",
    ///     "action": "read",
    ///     "resource": "/documents/secret"
    /// });
    /// let response = client.query("authz/allow", input).await?;
    /// if response.is_allowed() {
    ///     println!("Access granted");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn query(&self, policy_path: &str, input: serde_json::Value) -> Result<OpaResponse> {
        // Check cache first
        if self.config.enable_cache {
            let cache_key = format!("{}:{}", policy_path, input);
            let cache = self.cache.read().await;
            if let Some(entry) = cache.get(&cache_key) {
                let now = now_secs();
                if self.config.cache_ttl_secs == 0 || entry.expires_at > now {
                    return Ok(entry.response.clone());
                }
            }
            drop(cache);
        }

        let url = self.build_api_url("v1/data", policy_path)?;
        let payload = OpaInput {
            input: input.clone(),
        };

        let mut request = self.http.post(url).json(&payload);
        if let Some(ref token) = self.config.auth_token {
            request = request.bearer_auth(token);
        }

        let resp = request
            .send()
            .await
            .map_err(|e| AuthError::internal(&format!("OPA request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = read_error_body(resp).await;
            return Err(AuthError::internal(&format!(
                "OPA returned HTTP {status}: {body}"
            )));
        }

        let opa_response: OpaResponse = resp
            .json()
            .await
            .map_err(|e| AuthError::internal(&format!("Invalid OPA response: {e}")))?;

        // Update cache
        if self.config.enable_cache {
            let cache_key = format!("{}:{}", policy_path, input);
            let entry = CacheEntry {
                response: opa_response.clone(),
                expires_at: now_secs() + self.config.cache_ttl_secs,
            };
            self.cache.write().await.insert(cache_key, entry);
        }

        Ok(opa_response)
    }

    /// Evaluate the default policy path.
    pub async fn evaluate(&self, input: serde_json::Value) -> Result<OpaResponse> {
        self.query(&self.config.default_policy_path, input).await
    }

    /// Convenience: check if the default policy allows the given input.
    pub async fn is_allowed(&self, input: serde_json::Value) -> Result<bool> {
        let resp = self.evaluate(input).await?;
        Ok(resp.is_allowed())
    }

    /// Check OPA server health.
    pub async fn health_check(&self) -> Result<bool> {
        let url = self.build_static_url("health")?;
        let mut request = self.http.get(url);
        if let Some(ref token) = self.config.auth_token {
            request = request.bearer_auth(token);
        }
        let resp = request
            .send()
            .await
            .map_err(|e| AuthError::internal(&format!("OPA health check failed: {e}")))?;
        Ok(resp.status().is_success())
    }

    /// Upload a Rego policy to OPA.
    pub async fn put_policy(&self, policy_id: &str, rego: &str) -> Result<()> {
        let url = self.build_api_url("v1/policies", policy_id)?;
        let mut request = self
            .http
            .put(url)
            .header("Content-Type", "text/plain")
            .body(rego.to_string());
        if let Some(ref token) = self.config.auth_token {
            request = request.bearer_auth(token);
        }

        let resp = request
            .send()
            .await
            .map_err(|e| AuthError::internal(&format!("OPA policy upload failed: {e}")))?;

        if !resp.status().is_success() {
            let body = read_error_body(resp).await;
            return Err(AuthError::internal(&format!(
                "OPA policy upload returned error: {body}"
            )));
        }
        Ok(())
    }

    /// Delete a policy from OPA.
    pub async fn delete_policy(&self, policy_id: &str) -> Result<()> {
        let url = self.build_api_url("v1/policies", policy_id)?;
        let mut request = self.http.delete(url);
        if let Some(ref token) = self.config.auth_token {
            request = request.bearer_auth(token);
        }

        let resp = request
            .send()
            .await
            .map_err(|e| AuthError::internal(&format!("OPA policy delete failed: {e}")))?;

        if !resp.status().is_success() {
            let body = read_error_body(resp).await;
            return Err(AuthError::internal(&format!(
                "OPA policy delete returned error: {body}"
            )));
        }
        Ok(())
    }

    /// Upload data to OPA's data store.
    pub async fn put_data(&self, data_path: &str, data: serde_json::Value) -> Result<()> {
        let url = self.build_api_url("v1/data", data_path)?;
        let mut request = self.http.put(url).json(&data);
        if let Some(ref token) = self.config.auth_token {
            request = request.bearer_auth(token);
        }

        let resp = request
            .send()
            .await
            .map_err(|e| AuthError::internal(&format!("OPA data upload failed: {e}")))?;

        if !resp.status().is_success() {
            let body = read_error_body(resp).await;
            return Err(AuthError::internal(&format!(
                "OPA data upload error: {body}"
            )));
        }
        Ok(())
    }

    fn build_static_url(&self, path: &str) -> Result<Url> {
        self.base_url
            .join(path)
            .map_err(|e| AuthError::internal(&format!("Failed to build OPA URL: {e}")))
    }

    fn build_api_url(&self, prefix: &str, path: &str) -> Result<Url> {
        let sanitized_path = sanitize_opa_path(path)?;
        let joined = if sanitized_path.is_empty() {
            prefix.to_string()
        } else {
            format!("{}/{}", prefix.trim_end_matches('/'), sanitized_path)
        };
        self.build_static_url(&joined)
    }

    /// Clear the response cache.
    pub async fn clear_cache(&self) {
        self.cache.write().await.clear();
    }

    /// Get the number of cached entries.
    pub async fn cache_size(&self) -> usize {
        self.cache.read().await.len()
    }
}

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

fn normalize_opa_base_url(base_url: &str) -> Result<Url> {
    if base_url.is_empty() {
        return Err(AuthError::validation("OPA base URL cannot be empty"));
    }

    let mut parsed = Url::parse(base_url)
        .map_err(|e| AuthError::validation(format!("Invalid OPA base URL: {e}")))?;

    if !matches!(parsed.scheme(), "http" | "https") {
        return Err(AuthError::validation("OPA base URL must use http or https"));
    }

    if parsed.host_str().is_none() {
        return Err(AuthError::validation("OPA base URL must include a host"));
    }

    if !parsed.username().is_empty() || parsed.password().is_some() {
        return Err(AuthError::validation(
            "OPA base URL must not embed credentials",
        ));
    }

    if parsed.query().is_some() || parsed.fragment().is_some() {
        return Err(AuthError::validation(
            "OPA base URL must not include query parameters or fragments",
        ));
    }

    if !parsed.path().ends_with('/') {
        let new_path = format!("{}/", parsed.path().trim_end_matches('/'));
        parsed.set_path(&new_path);
    }

    Ok(parsed)
}

fn sanitize_opa_path(path: &str) -> Result<String> {
    let segments: Vec<&str> = path
        .split('/')
        .filter(|segment| !segment.is_empty())
        .collect();

    if segments.is_empty() {
        return Err(AuthError::validation("OPA path cannot be empty"));
    }

    for segment in &segments {
        if matches!(*segment, "." | "..")
            || segment.contains('\\')
            || segment.contains('?')
            || segment.contains('#')
        {
            return Err(AuthError::validation("OPA path contains invalid segments"));
        }
    }

    Ok(segments.join("/"))
}

async fn read_error_body(response: reqwest::Response) -> String {
    match response.text().await {
        Ok(body) if !body.is_empty() => body,
        Ok(_) => "<empty response body>".to_string(),
        Err(error) => format!("<failed to read response body: {error}>"),
    }
}

// ── Rego-style local evaluator (for embedded policies) ──────────────

/// A lightweight local policy evaluator for simple attribute-based checks.
///
/// Useful when a full OPA server is not deployed. Supports rule evaluation
/// against a set of named attributes.
pub struct LocalPolicyEvaluator {
    rules: Vec<PolicyRule>,
}

/// A policy rule consisting of conditions that must all be true.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyRule {
    /// Human-readable rule name.
    pub name: String,
    /// Conditions that must ALL be satisfied.
    pub conditions: Vec<PolicyCondition>,
    /// Effect when all conditions are met.
    pub effect: PolicyEffect,
}

/// A single condition in a policy rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyCondition {
    /// Attribute path (dot-separated, e.g. "user.role").
    pub attribute: String,
    /// Comparison operator.
    pub operator: ConditionOperator,
    /// Expected value.
    pub value: serde_json::Value,
}

/// Comparison operators for policy conditions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConditionOperator {
    Equals,
    NotEquals,
    Contains,
    In,
    Exists,
}

/// Policy decision effect.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PolicyEffect {
    Allow,
    Deny,
}

impl LocalPolicyEvaluator {
    /// Create a new evaluator with no rules (default-deny).
    pub fn new() -> Self {
        Self { rules: Vec::new() }
    }

    /// Add a policy rule.
    pub fn add_rule(&mut self, rule: PolicyRule) {
        self.rules.push(rule);
    }

    /// Evaluate an input against all rules.
    ///
    /// Returns `Allow` if at least one Allow rule matches and no Deny rule matches.
    /// Returns `Deny` if no rules match or a Deny rule fires.
    pub fn evaluate(&self, input: &serde_json::Value) -> PolicyEffect {
        let mut any_allow = false;

        for rule in &self.rules {
            if self.evaluate_rule(rule, input) {
                match rule.effect {
                    PolicyEffect::Deny => return PolicyEffect::Deny,
                    PolicyEffect::Allow => any_allow = true,
                }
            }
        }

        if any_allow {
            PolicyEffect::Allow
        } else {
            PolicyEffect::Deny
        }
    }

    fn evaluate_rule(&self, rule: &PolicyRule, input: &serde_json::Value) -> bool {
        rule.conditions
            .iter()
            .all(|cond| self.evaluate_condition(cond, input))
    }

    fn evaluate_condition(&self, cond: &PolicyCondition, input: &serde_json::Value) -> bool {
        let actual = resolve_path(input, &cond.attribute);

        match cond.operator {
            ConditionOperator::Equals => match actual {
                Some(v) => *v == cond.value,
                None => false,
            },
            ConditionOperator::NotEquals => match actual {
                Some(v) => *v != cond.value,
                None => true,
            },
            ConditionOperator::Contains => match actual {
                Some(v) => {
                    if let (Some(arr), Some(needle)) = (v.as_array(), cond.value.as_str()) {
                        arr.iter().any(|e| e.as_str() == Some(needle))
                    } else if let (Some(s), Some(needle)) = (v.as_str(), cond.value.as_str()) {
                        s.contains(needle)
                    } else {
                        false
                    }
                }
                None => false,
            },
            ConditionOperator::In => match actual {
                Some(v) => {
                    if let Some(arr) = cond.value.as_array() {
                        arr.contains(v)
                    } else {
                        false
                    }
                }
                None => false,
            },
            ConditionOperator::Exists => actual.is_some(),
        }
    }
}

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

/// Resolve a dot-separated path in a JSON value.
fn resolve_path<'a>(value: &'a serde_json::Value, path: &str) -> Option<&'a serde_json::Value> {
    let mut current = value;
    for segment in path.split('.') {
        current = current.get(segment)?;
    }
    Some(current)
}

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

    // ── OPA config ──────────────────────────────────────────────

    #[test]
    fn test_config_defaults() {
        let cfg = OpaConfig::default();
        assert_eq!(cfg.base_url, "http://localhost:8181");
        assert_eq!(cfg.default_policy_path, "authz/allow");
        assert_eq!(cfg.timeout_secs, 5);
        assert!(cfg.auth_token.is_none());
        assert!(!cfg.enable_cache);
    }

    // ── OpaResponse ─────────────────────────────────────────────

    #[test]
    fn test_response_is_allowed_true() {
        let resp = OpaResponse {
            result: serde_json::json!(true),
            decision_id: None,
        };
        assert!(resp.is_allowed());
    }

    #[test]
    fn test_response_is_allowed_false() {
        let resp = OpaResponse {
            result: serde_json::json!(false),
            decision_id: None,
        };
        assert!(!resp.is_allowed());
    }

    #[test]
    fn test_response_is_allowed_non_bool() {
        let resp = OpaResponse {
            result: serde_json::json!({"allow": true}),
            decision_id: None,
        };
        assert!(!resp.is_allowed());
    }

    #[test]
    fn test_response_get_bool() {
        let resp = OpaResponse {
            result: serde_json::json!({"authz": {"allow": true, "admin": false}}),
            decision_id: Some("dec-1".to_string()),
        };
        assert_eq!(resp.get_bool("authz.allow"), Some(true));
        assert_eq!(resp.get_bool("authz.admin"), Some(false));
        assert_eq!(resp.get_bool("authz.missing"), None);
    }

    #[test]
    fn test_response_get_str() {
        let resp = OpaResponse {
            result: serde_json::json!({"reason": "policy XYZ"}),
            decision_id: None,
        };
        assert_eq!(resp.get_str("reason"), Some("policy XYZ"));
    }

    // ── OPA Client creation ─────────────────────────────────────

    #[test]
    fn test_client_creation_valid() {
        let client = OpaClient::new(OpaConfig::default());
        assert!(client.is_ok());
    }

    #[test]
    fn test_client_creation_empty_url() {
        let cfg = OpaConfig {
            base_url: String::new(),
            ..Default::default()
        };
        assert!(OpaClient::new(cfg).is_err());
    }

    #[test]
    fn test_client_creation_rejects_embedded_credentials() {
        let cfg = OpaConfig {
            base_url: "https://user:pass@opa.example.com".to_string(),
            ..Default::default()
        };
        assert!(OpaClient::new(cfg).is_err());
    }

    #[test]
    fn test_client_creation_rejects_query_string_base_url() {
        let cfg = OpaConfig {
            base_url: "https://opa.example.com?target=internal".to_string(),
            ..Default::default()
        };
        assert!(OpaClient::new(cfg).is_err());
    }

    #[test]
    fn test_sanitize_opa_path_rejects_traversal() {
        assert!(sanitize_opa_path("../system/main").is_err());
        assert!(sanitize_opa_path("authz/../../admin").is_err());
    }

    // ── Local Policy Evaluator ──────────────────────────────────

    #[test]
    fn test_local_evaluator_default_deny() {
        let eval = LocalPolicyEvaluator::new();
        let input = serde_json::json!({"user": "alice"});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_allow_rule() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "allow admins".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "user.role".to_string(),
                operator: ConditionOperator::Equals,
                value: serde_json::json!("admin"),
            }],
            effect: PolicyEffect::Allow,
        });

        let input = serde_json::json!({"user": {"role": "admin"}});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Allow);

        let input2 = serde_json::json!({"user": {"role": "viewer"}});
        assert_eq!(eval.evaluate(&input2), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_deny_overrides_allow() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "allow all".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "user.active".to_string(),
                operator: ConditionOperator::Equals,
                value: serde_json::json!(true),
            }],
            effect: PolicyEffect::Allow,
        });
        eval.add_rule(PolicyRule {
            name: "deny blocked".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "user.blocked".to_string(),
                operator: ConditionOperator::Equals,
                value: serde_json::json!(true),
            }],
            effect: PolicyEffect::Deny,
        });

        let input = serde_json::json!({"user": {"active": true, "blocked": true}});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_contains_operator() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "role check".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "user.roles".to_string(),
                operator: ConditionOperator::Contains,
                value: serde_json::json!("editor"),
            }],
            effect: PolicyEffect::Allow,
        });

        let input = serde_json::json!({"user": {"roles": ["viewer", "editor"]}});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Allow);

        let input2 = serde_json::json!({"user": {"roles": ["viewer"]}});
        assert_eq!(eval.evaluate(&input2), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_in_operator() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "allowed actions".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "action".to_string(),
                operator: ConditionOperator::In,
                value: serde_json::json!(["read", "list"]),
            }],
            effect: PolicyEffect::Allow,
        });

        let input = serde_json::json!({"action": "read"});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Allow);

        let input2 = serde_json::json!({"action": "delete"});
        assert_eq!(eval.evaluate(&input2), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_exists_operator() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "has token".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "auth.token".to_string(),
                operator: ConditionOperator::Exists,
                value: serde_json::json!(null),
            }],
            effect: PolicyEffect::Allow,
        });

        let input = serde_json::json!({"auth": {"token": "abc"}});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Allow);

        let input2 = serde_json::json!({"auth": {}});
        assert_eq!(eval.evaluate(&input2), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_not_equals() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "not guest".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "user.role".to_string(),
                operator: ConditionOperator::NotEquals,
                value: serde_json::json!("guest"),
            }],
            effect: PolicyEffect::Allow,
        });

        let input = serde_json::json!({"user": {"role": "admin"}});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Allow);

        let guest = serde_json::json!({"user": {"role": "guest"}});
        assert_eq!(eval.evaluate(&guest), PolicyEffect::Deny);
    }

    #[test]
    fn test_local_evaluator_multiple_conditions() {
        let mut eval = LocalPolicyEvaluator::new();
        eval.add_rule(PolicyRule {
            name: "admin write".to_string(),
            conditions: vec![
                PolicyCondition {
                    attribute: "user.role".to_string(),
                    operator: ConditionOperator::Equals,
                    value: serde_json::json!("admin"),
                },
                PolicyCondition {
                    attribute: "action".to_string(),
                    operator: ConditionOperator::Equals,
                    value: serde_json::json!("write"),
                },
            ],
            effect: PolicyEffect::Allow,
        });

        // Both conditions met
        let input = serde_json::json!({"user": {"role": "admin"}, "action": "write"});
        assert_eq!(eval.evaluate(&input), PolicyEffect::Allow);

        // Only one condition met
        let input2 = serde_json::json!({"user": {"role": "admin"}, "action": "read"});
        assert_eq!(eval.evaluate(&input2), PolicyEffect::Deny);
    }

    #[test]
    fn test_resolve_path() {
        let v = serde_json::json!({"a": {"b": {"c": 42}}});
        assert_eq!(resolve_path(&v, "a.b.c"), Some(&serde_json::json!(42)));
        assert_eq!(resolve_path(&v, "a.b"), Some(&serde_json::json!({"c": 42})));
        assert_eq!(resolve_path(&v, "x.y"), None);
    }

    #[test]
    fn test_policy_rule_serialization() {
        let rule = PolicyRule {
            name: "test".to_string(),
            conditions: vec![PolicyCondition {
                attribute: "user.role".to_string(),
                operator: ConditionOperator::Equals,
                value: serde_json::json!("admin"),
            }],
            effect: PolicyEffect::Allow,
        };
        let json = serde_json::to_string(&rule).unwrap();
        let parsed: PolicyRule = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.name, "test");
        assert_eq!(parsed.effect, PolicyEffect::Allow);
    }

    #[test]
    fn test_opa_input_serialization() {
        let input = OpaInput {
            input: serde_json::json!({"user": "alice"}),
        };
        let json = serde_json::to_value(&input).unwrap();
        assert_eq!(json["input"]["user"], "alice");
    }
}