cedarling 0.0.64

The Cedarling: a high-performance local authorization service powered by the Rust Cedar Engine.
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
1300
1301
1302
// This software is available under the Apache-2.0 license.
// See https://www.apache.org/licenses/LICENSE-2.0.txt for full text.
//
// Copyright (c) 2024, Gluu, Inc.

use super::entity_id_getters::{EntityIdSrc, get_first_valid_entity_id};
use super::{
    BuildEntityError, BuiltEntities, DEFAULT_ENTITY_TYPE_NAME, EntityBuilder,
    default_tkn_entity_name,
};
use crate::common::default_entities::DefaultEntities;
use crate::common::issuer_utils::IssClaim;
use crate::common::policy_store::token_entity_metadata::DEFAULT_TKN_ID;
use crate::entity_builder::{BuildAttrsErrorVec, schema};
use crate::jwt::Token;
use crate::log::interface::LogWriter;
use crate::log::{BaseLogEntry, LogEntry, LogLevel};
use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid, RestrictedExpression};
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use std::sync::Arc;

/// Errors that can occur during multi-issuer entity building
#[derive(Debug, thiserror::Error)]
pub enum MultiIssuerEntityError {
    #[error("Missing issuer claim in JWT")]
    MissingIssuer,

    #[error("Missing exp claim in JWT")]
    MissingExpClaim,

    #[error("Invalid entity UID: {0}")]
    InvalidEntityUid(String),

    #[error("Entity creation failed: {0}")]
    EntityCreationFailed(String),

    #[error("No valid tokens found")]
    NoValidTokens,

    #[error("Could not create cedar uid for trusted issuer: {0}")]
    BuildTrustedIssuerUid(#[from] BuildEntityError),

    #[error("Could not build entity attributes: {0}")]
    BuildAttrs(#[from] BuildAttrsErrorVec),
}

/// Sanitize issuer name for Cedar compatibility
fn sanitize_issuer_name(name: &str) -> String {
    name.replace(['.', ' ', '-'], "_").to_lowercase()
}

/// Simplify token type for Cedar compatibility
fn simplify_token_type(mapping: &str) -> String {
    // Split by namespace separator and use the last part
    // Keep underscores in token type names as specified in design
    mapping.split("::").last().unwrap_or(mapping).to_lowercase()
}

/// Create a Set of String from a single string value
fn create_string_set(value: &str) -> RestrictedExpression {
    RestrictedExpression::new_set(vec![RestrictedExpression::new_string(value.to_string())])
}

/// Create a Set of String from an array of strings
fn create_string_set_array(values: &[String]) -> RestrictedExpression {
    RestrictedExpression::new_set(
        values
            .iter()
            .map(|s| RestrictedExpression::new_string(s.clone()))
            .collect::<Vec<_>>(),
    )
}

const RESERVED_CLAIMS: [&str; 3] = ["iss", "jti", "exp"];

/// Add reserved claims to entity attributes based on schema shape.
fn add_reserved_claims(
    attrs: &mut HashMap<String, RestrictedExpression>,
    token: &Token,
    entity_id: &str,
    attrs_shape_opt: Option<&HashMap<smol_str::SmolStr, schema::AttrsShape>>,
    validated_at_ts: i64,
) -> Result<(), MultiIssuerEntityError> {
    const TOKEN_TYPE: &str = "token_type";
    const JTI_CLAIM: &str = "jti";
    const ISS_CLAIM: &str = "iss";
    const EXP_CLAIM: &str = "exp";
    const VALIDATED_AT_CLAIM: &str = "validated_at";

    if let Some(attrs_shape) = attrs_shape_opt {
        // add token_type claim
        if attrs_shape.contains_key(TOKEN_TYPE) {
            attrs.insert(
                TOKEN_TYPE.to_string(),
                RestrictedExpression::new_string(token.name.clone()),
            );
        }

        // add jti claim
        if attrs_shape.contains_key(JTI_CLAIM) {
            attrs.insert(
                JTI_CLAIM.to_string(),
                RestrictedExpression::new_string(entity_id.to_string()),
            );
        }

        // add iss claim
        if let Some(shape) = attrs_shape.get(ISS_CLAIM) {
            const UNDEFINED_ISSUER: &str = "undefined";

            if let Some(token_iss) = &token.iss {
                let issuer = token.extract_normalized_issuer()
                    // it should never be None here since token iss exists
                    .unwrap_or_else(|| IssClaim::new(UNDEFINED_ISSUER));

                attrs.insert(
                    ISS_CLAIM.to_string(),
                    RestrictedExpression::new_entity_uid(EntityBuilder::trusted_issuer_cedar_uid(
                        &token_iss.name,
                        &issuer,
                    )?),
                );
            } else if shape.is_required() {
                // iss is required but token has no issuer (in trusted issuer)
                attrs.insert(
                    "iss".to_string(),
                    RestrictedExpression::new_string(
                        token
                            .get_claim(ISS_CLAIM)
                            .and_then(|v| v.value().as_str().map(str::to_string))
                            .unwrap_or_else(|| UNDEFINED_ISSUER.to_string()),
                    ),
                );
            }
        }

        // add exp claim
        if let Some(shape) = attrs_shape.get(EXP_CLAIM) {
            if let Some(exp) = token
                .get_claim_val(EXP_CLAIM)
                .and_then(serde_json::Value::as_i64)
            {
                attrs.insert(EXP_CLAIM.to_string(), RestrictedExpression::new_long(exp));
            } else if shape.is_required() {
                // exp is required but missing in token
                return Err(MultiIssuerEntityError::MissingExpClaim);
            }
        }

        // add validated_at claim
        if attrs_shape.contains_key(VALIDATED_AT_CLAIM) {
            attrs.insert(
                VALIDATED_AT_CLAIM.to_string(),
                RestrictedExpression::new_long(validated_at_ts),
            );
        }
    } else {
        // No schema shape provided, add all reserved claims as is

        attrs.insert(
            TOKEN_TYPE.to_string(),
            RestrictedExpression::new_string(token.name.clone()),
        );

        attrs.insert(
            JTI_CLAIM.to_string(),
            RestrictedExpression::new_string(entity_id.to_string()),
        );

        if let Some(token_iss) = &token.iss {
            let issuer = token
                .extract_normalized_issuer()
                .ok_or(MultiIssuerEntityError::MissingIssuer)?;

            attrs.insert(
                ISS_CLAIM.to_string(),
                RestrictedExpression::new_entity_uid(EntityBuilder::trusted_issuer_cedar_uid(
                    &token_iss.name,
                    &issuer,
                )?),
            );
        }

        if let Some(exp) = token
            .get_claim_val(EXP_CLAIM)
            .and_then(serde_json::Value::as_i64)
        {
            attrs.insert(EXP_CLAIM.to_string(), RestrictedExpression::new_long(exp));
        }

        attrs.insert(
            VALIDATED_AT_CLAIM.to_string(),
            RestrictedExpression::new_long(validated_at_ts),
        );
    }

    Ok(())
}

/// Convert a claim value to a Set of String
fn convert_claim_to_string_set(value: &Value) -> RestrictedExpression {
    match value {
        Value::String(s) => create_string_set(s),
        Value::Number(n) => create_string_set(&n.to_string()),
        Value::Bool(b) => create_string_set(&b.to_string()),
        Value::Array(arr) => {
            let string_values: Vec<String> = arr
                .iter()
                .map(|v| match v {
                    Value::String(s) => s.clone(),
                    Value::Number(n) => n.to_string(),
                    Value::Bool(b) => b.to_string(),
                    _ => v.to_string(),
                })
                .collect();
            create_string_set_array(&string_values)
        },
        _ => create_string_set(&value.to_string()),
    }
}

/// Determine the entity type for a token dynamically
fn determine_token_entity_type(token: &Token) -> String {
    if let Some(issuer) = token.iss.as_ref()
        && let Some(metadata) = issuer.token_metadata.get(&token.name)
    {
        return metadata.entity_type_name.clone();
    }

    if token.name.contains("::") {
        return token.name.clone();
    }

    if let Some(default_type) = default_tkn_entity_name(&token.name) {
        return default_type.to_string();
    }

    DEFAULT_ENTITY_TYPE_NAME.to_string()
}

/// Resource-independent multi-issuer entities produced by
/// [`EntityBuilder::build_multi_issuer_setup_entities`]. Built once per
/// authorization call (single-item or batch) and combined with a per-item
/// resource entity by the caller.
#[derive(Debug, Clone)]
pub(crate) struct MultiIssuerSetupEntities {
    pub tokens: HashMap<String, Entity>,
    pub issuers: HashSet<Entity>,
    pub default_entities: DefaultEntities,
}

impl EntityBuilder {
    /// Build the resource-independent multi-issuer entities (tokens + issuers +
    /// default entities). Used by both single-item and batch multi-issuer
    /// authorization paths — the caller pairs the result with a per-item
    /// [`Self::build_resource_entity`] call to complete each decision.
    pub(crate) fn build_multi_issuer_setup_entities(
        &self,
        tokens: &HashMap<String, Arc<Token>>,
        log_service: &impl LogWriter,
    ) -> Result<MultiIssuerSetupEntities, MultiIssuerEntityError> {
        let mut built_entities = BuiltEntities::from(&self.iss_entities);

        let mut token_entities = HashMap::new();
        for (token_name, token) in tokens {
            match self.build_single_token_entity(token, &built_entities) {
                Ok(entity) => match self.generate_entity_key(token_name, token) {
                    Ok(entity_key) => {
                        built_entities.insert(&entity.uid());
                        token_entities.insert(entity_key, entity);
                    },
                    Err(e) => {
                        log_service.log_any(
                            LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                                LogLevel::ERROR,
                                None,
                            ))
                            .set_message(format!(
                                "Failed to generate entity key for token '{token_name}'"
                            ))
                            .set_error(e.to_string()),
                        );
                    },
                },
                Err(e) => {
                    log_service.log_any(
                        LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                            LogLevel::ERROR,
                            None,
                        ))
                        .set_message(format!(
                            "Failed to build token entity for token '{token_name}'"
                        ))
                        .set_error(e.to_string()),
                    );
                },
            }
        }

        if token_entities.is_empty() {
            log_service.log_any(
                LogEntry::new(BaseLogEntry::new_system_opt_request_id(
                    LogLevel::ERROR,
                    None,
                ))
                .set_message("No valid tokens found for multi-issuer authorization".to_string())
                .set_error("All tokens failed validation or entity building".to_string()),
            );
            return Err(MultiIssuerEntityError::NoValidTokens);
        }

        let issuers = self.iss_entities.values().cloned().collect();

        Ok(MultiIssuerSetupEntities {
            tokens: token_entities,
            issuers,
            default_entities: self.default_entities.clone(),
        })
    }

    /// Build a single token entity from a validated JWT
    fn build_single_token_entity(
        &self,
        token: &Token,
        built_entities: &BuiltEntities,
    ) -> Result<Entity, MultiIssuerEntityError> {
        // Determine entity type name using the same logic as regular entity builder
        let entity_type = determine_token_entity_type(token);

        // Resolve token_id from the trusted issuer's token_metadata config,
        // falling back to DEFAULT_TKN_ID when the issuer or metadata entry is not found.
        let token_id_claim: &str = token
            .iss
            .as_deref()
            .and_then(|iss| iss.token_metadata.get(&token.name))
            .map_or(DEFAULT_TKN_ID, |m| m.token_id.as_str());

        let entity_id_srcs = [EntityIdSrc::Token {
            token,
            claim: token_id_claim,
        }];
        let entity_id = get_first_valid_entity_id(&entity_id_srcs)
            .map_err(|e| MultiIssuerEntityError::InvalidEntityUid(e.to_string()))?
            .to_string();

        // Get attribute shape from schema if available
        let attrs_shape = self
            .schema
            .as_ref()
            .and_then(|schema| schema.get_entity_shape(&entity_type));

        let claims = token.claims_value();

        let validated_at_ts = chrono::Utc::now().timestamp();

        // Build entity attributes using the same logic as regular entity builder.
        //
        // The closure below is a **read-only data source** — it supplies raw
        // claim values to the schema-driven builder.  It MUST NOT pre-resolve
        // reserved claims (`jti`, `iss`, `exp`); that is the sole responsibility
        // of `add_reserved_claims` below.
        //
        // Schema path: borrow claims from the token and overlay the two
        // synthetic entries (`token_type`, `validated_at`) via the closure,
        // avoiding a full clone of every claim into a temporary HashMap.
        // No-schema path: fall back to the existing iter-all flow.
        let mut attrs = if let Some(shape) = attrs_shape {
            let token_type_val = Value::String(token.name.clone());
            let validated_at_val = Value::Number(validated_at_ts.into());
            // `jti` may be required by the schema but absent from the JWT
            // claims (e.g. when token_id is sourced from `sub`).  The
            // schema-path builder fails hard on missing required attrs,
            // so we fall back to `entity_id` here.  `add_reserved_claims`
            // overwrites the final value anyway.
            let jti_val = Value::String(entity_id.clone());
            super::build_entity_attrs::build_entity_attrs_with_shape_lookup(
                |name| match name {
                    "token_type" => Some(&token_type_val),
                    "validated_at" => Some(&validated_at_val),
                    "jti" => claims.get("jti").or(Some(&jti_val)),
                    other => claims.get(other),
                },
                built_entities,
                shape,
            )?
        } else {
            // Rare path: no schema. Materialize the merged map only here.
            let mut all_claims = HashMap::with_capacity(claims.len() + 2);
            all_claims.extend(claims.iter().map(|(k, v)| (k.clone(), v.clone())));
            all_claims.insert("token_type".to_string(), Value::String(token.name.clone()));
            all_claims.insert(
                "validated_at".to_string(),
                Value::Number(validated_at_ts.into()),
            );
            super::build_entity_attrs::build_entity_attrs(&all_claims, built_entities, None)?
        };

        // ── Reserved-claims overwrite ──────────────────────────────────
        // `add_reserved_claims` is the **single authority** that injects
        // `token_type`, `jti`, `iss`, `exp`, and `validated_at` into `attrs`
        // with their final, correctly-typed values.  Whatever the schema path
        // (or no-schema path) placed in `attrs` for these keys gets overwritten
        // here — this is by design.
        //
        // If you change the closure above to pre-resolve any of these keys
        // you will create dead work and confuse future readers: the value
        // will be computed twice and the second result (here) wins.
        add_reserved_claims(&mut attrs, token, &entity_id, attrs_shape, validated_at_ts)?;

        // Create entity tags for non-reserved JWT claims.
        let mut tags = HashMap::new();
        for (claim_key, claim_value) in token.claims_value() {
            if RESERVED_CLAIMS.contains(&claim_key.as_str()) {
                continue;
            }
            let value = convert_claim_to_string_set(claim_value);
            tags.insert(claim_key.clone(), value);
        }

        // Create the Cedar entity using the existing build_cedar_entity function
        // Note: build_cedar_entity doesn't support tags, so we need to use Entity::new_with_tags directly
        // but we can still reuse the UID creation logic
        let entity_type_name = EntityTypeName::from_str(&entity_type)
            .map_err(|e| MultiIssuerEntityError::InvalidEntityUid(e.to_string()))?;
        // EntityId::from_str returns Result<_, Infallible>, so parsing never fails
        let entity_id = EntityId::from_str(&entity_id).unwrap_or_else(|e| match e {});

        let uid = EntityUid::from_type_name_and_id(entity_type_name, entity_id);

        let entity = Entity::new_with_tags(uid, attrs, HashSet::new(), tags)
            .map_err(|e| MultiIssuerEntityError::EntityCreationFailed(e.to_string()))?;

        Ok(entity)
    }

    /// Generate a unique key for the token entity
    fn generate_entity_key(
        &self,
        token_name: &str,
        token: &Token,
    ) -> Result<String, MultiIssuerEntityError> {
        let issuer = token
            .extract_normalized_issuer()
            .ok_or(MultiIssuerEntityError::MissingIssuer)?;

        let issuer_simplified = self.resolve_issuer_name(&issuer);
        let token_type_simplified = simplify_token_type(token_name);

        Ok(format!("{issuer_simplified}_{token_type_simplified}"))
    }

    /// Resolve issuer name using trusted issuer metadata or fallback to hostname
    fn resolve_issuer_name(&self, issuer: &IssClaim) -> String {
        // First, try to find the issuer in trusted issuer metadata
        if let Some(trusted_issuer) = self.issuers_index.find(issuer) {
            return sanitize_issuer_name(&trusted_issuer.name);
        }

        // Fallback to hostname from JWT iss claim
        let issuer_str = issuer.as_str();
        let hostname = issuer_str
            .replace("https://", "")
            .replace("http://", "")
            .split('/')
            .next()
            .unwrap_or(issuer_str)
            .split(':')
            .next()
            .unwrap_or(issuer_str)
            .to_string();

        sanitize_issuer_name(&hostname)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::default_entities::DefaultEntities;
    use crate::common::policy_store::TrustedIssuer;
    use crate::common::policy_store::token_entity_metadata::TokenEntityMetadata;
    use crate::entity_builder::TrustedIssuerIndex;
    use crate::jwt::{Token, TokenClaims};
    use crate::log::NopLogger;
    use cedar_policy::EvalResult;
    use serde_json::json;
    use std::collections::HashMap;
    use url::Url;

    fn create_test_entity_builder() -> EntityBuilder {
        let mut trusted_issuers = HashMap::new();

        // Add Acme issuer
        let acme_issuer = TrustedIssuer::new(
            "Acme".to_string(),
            "Acme Corporation".to_string(),
            Url::parse("https://idp.acme.com/auth").unwrap(),
            HashMap::new(),
        );
        trusted_issuers.insert("acme".to_string(), acme_issuer);

        // Add Dolphin issuer
        let dolphin_issuer = TrustedIssuer::new(
            "Dolphin".to_string(),
            "Dolphin Sea Services".to_string(),
            Url::parse("https://idp.dolphin.sea/auth").unwrap(),
            HashMap::new(),
        );
        trusted_issuers.insert("dolphin".to_string(), dolphin_issuer);

        // Add Microsoft issuer
        let microsoft_issuer = TrustedIssuer::new(
            "Microsoft".to_string(),
            "Microsoft Azure AD".to_string(),
            Url::parse("https://login.microsoftonline.com/tenant").unwrap(),
            HashMap::new(),
        );
        trusted_issuers.insert("microsoft".to_string(), microsoft_issuer);

        // Add Company issuer
        let company_issuer = TrustedIssuer::new(
            "Company".to_string(),
            "Company Internal Auth".to_string(),
            Url::parse("https://auth.company.internal:8443/oauth").unwrap(),
            HashMap::new(),
        );
        trusted_issuers.insert("company".to_string(), company_issuer);

        EntityBuilder::new(
            TrustedIssuerIndex::new(&trusted_issuers, None),
            None,
            DefaultEntities::default(),
        )
        .unwrap()
    }

    fn create_test_token(
        issuer: &str,
        jti: &str,
        claims: HashMap<String, Value>,
        builder: &EntityBuilder,
    ) -> Token {
        let mut all_claims = claims;
        all_claims.insert("iss".to_string(), json!(issuer));
        all_claims.insert("jti".to_string(), json!(jti));
        all_claims.insert(
            "exp".to_string(),
            json!(chrono::Utc::now().timestamp() + 3600),
        );

        let trusted_issuer = builder.find_trusted_issuer_by_iss(issuer);

        let token_claims = TokenClaims::from(all_claims);
        Token::new("Jans::Access_Token", token_claims, trusted_issuer)
    }

    #[test]
    fn test_issuer_name_resolution_with_trusted_metadata() {
        let builder = create_test_entity_builder();

        // Test with trusted issuer metadata
        let result = builder.resolve_issuer_name(&IssClaim::new("https://idp.acme.com/auth"));
        assert_eq!(result, "acme");

        let result = builder.resolve_issuer_name(&IssClaim::new("https://idp.dolphin.sea/auth"));
        assert_eq!(result, "dolphin");

        let result =
            builder.resolve_issuer_name(&IssClaim::new("https://login.microsoftonline.com/tenant"));
        assert_eq!(result, "microsoft");
    }

    #[test]
    fn test_issuer_name_resolution_fallback_to_hostname() {
        let builder = create_test_entity_builder();

        // Test fallback to hostname for unknown issuer
        let result = builder.resolve_issuer_name(&IssClaim::new("https://unknown.issuer.com/auth"));
        assert_eq!(result, "unknown_issuer_com");
    }

    #[test]
    fn test_token_type_simplification() {
        // Test various token type mappings
        assert_eq!(simplify_token_type("Jans::Access_Token"), "access_token");
        assert_eq!(simplify_token_type("Jans::Id_Token"), "id_token");
        assert_eq!(simplify_token_type("Acme::DolphinToken"), "dolphintoken");
        assert_eq!(
            simplify_token_type("Custom::Employee_Token"),
            "employee_token"
        );
        assert_eq!(simplify_token_type("SimpleToken"), "simpletoken");
    }

    #[test]
    fn test_entity_key_generation() {
        let builder = create_test_entity_builder();

        let mut claims = HashMap::new();
        claims.insert("sub".to_string(), json!("user123"));
        let token = create_test_token("https://idp.acme.com/auth", "token123", claims, &builder);

        let key = builder
            .generate_entity_key("Jans::Access_Token", &token)
            .unwrap();
        assert_eq!(key, "acme_access_token");
    }

    #[test]
    fn test_entity_key_generation_with_unknown_issuer() {
        let builder = create_test_entity_builder();

        let mut claims = HashMap::new();
        claims.insert("sub".to_string(), json!("user123"));
        let token = create_test_token(
            "https://unknown.issuer.com/auth",
            "token123",
            claims,
            &builder,
        );

        let key = builder
            .generate_entity_key("Custom::Employee_Token", &token)
            .unwrap();
        assert_eq!(key, "unknown_issuer_com_employee_token");
    }

    #[test]
    fn test_multiple_tokens_same_issuer_different_names() {
        let builder = create_test_entity_builder();

        let mut tokens = HashMap::new();

        let mut claims1 = HashMap::new();
        claims1.insert("sub".to_string(), json!("user1"));
        let token_one = create_test_token("https://idp.acme.com/auth", "token1", claims1, &builder);
        tokens.insert("Jans::Access_Token".to_string(), token_one);

        let mut claims2 = HashMap::new();
        claims2.insert("sub".to_string(), json!("user2"));
        let token_two = create_test_token("https://idp.acme.com/auth", "token2", claims2, &builder);
        tokens.insert("Jans::Access_Token2".to_string(), token_two);

        let tokens: HashMap<String, Arc<Token>> =
            tokens.into_iter().map(|(k, v)| (k, Arc::new(v))).collect();

        let result = builder.build_multi_issuer_setup_entities(&tokens, &NopLogger);
        assert!(result.is_ok());

        let entities_data = result.unwrap();
        assert_eq!(entities_data.tokens.len(), 2);
        assert!(entities_data.tokens.contains_key("acme_access_token"));
        assert!(entities_data.tokens.contains_key("acme_access_token2"));
    }

    #[test]
    fn test_multiple_tokens_different_issuers_and_types() {
        let builder = create_test_entity_builder();

        let mut tokens = HashMap::new();

        // Token from Acme issuer
        let mut claims1 = HashMap::new();
        claims1.insert("sub".to_string(), json!("user1"));
        let token_one = create_test_token("https://idp.acme.com/auth", "token1", claims1, &builder);
        tokens.insert("Jans::Access_Token".to_string(), token_one);

        // Token from Dolphin issuer
        let mut claims2 = HashMap::new();
        claims2.insert("sub".to_string(), json!("user2"));
        let token_two =
            create_test_token("https://idp.dolphin.sea/auth", "token2", claims2, &builder);
        tokens.insert("Acme::DolphinToken".to_string(), token_two);

        // Token from same issuer but different type
        let mut claims3 = HashMap::new();
        claims3.insert("sub".to_string(), json!("user3"));
        claims3.insert("iss".to_string(), json!("https://idp.acme.com/auth"));
        claims3.insert("jti".to_string(), json!("token3"));
        claims3.insert(
            "exp".to_string(),
            json!(chrono::Utc::now().timestamp() + 3600),
        );
        let token_claims3 = TokenClaims::from(claims3);
        let token_three = Token::new("Jans::Id_Token", token_claims3, None);
        tokens.insert("Jans::Id_Token".to_string(), token_three);

        let tokens: HashMap<String, Arc<Token>> =
            tokens.into_iter().map(|(k, v)| (k, Arc::new(v))).collect();

        let result = builder.build_multi_issuer_setup_entities(&tokens, &NopLogger);
        assert!(result.is_ok());

        let entities_data = result.unwrap();
        assert_eq!(entities_data.tokens.len(), 3);
        assert!(entities_data.tokens.contains_key("acme_access_token"));
        assert!(entities_data.tokens.contains_key("dolphin_dolphintoken"));
        assert!(entities_data.tokens.contains_key("acme_id_token"));
    }

    #[test]
    fn test_token_entity_structure() {
        let builder = create_test_entity_builder();

        let mut claims = HashMap::new();
        claims.insert("sub".to_string(), json!("user123"));
        claims.insert("scope".to_string(), json!(["read:profile", "write:data"]));
        claims.insert("aud".to_string(), json!("my-client"));
        let token = create_test_token("https://idp.acme.com/auth", "token123", claims, &builder);

        let built_entities = BuiltEntities::from(&builder.iss_entities);
        let entity = builder
            .build_single_token_entity(&token, &built_entities)
            .unwrap();

        // Check entity type - should match the token name
        assert_eq!(entity.uid().type_name().to_string(), "Jans::Access_Token");

        // Check core attributes exist
        assert!(entity.attr("token_type").is_some());
        assert!(entity.attr("jti").is_some());
        assert!(entity.attr("iss").is_some());
        assert!(entity.attr("validated_at").is_some());
        assert!(entity.attr("exp").is_some());

        // Check JWT claims are present as tags
        assert!(entity.tag("sub").is_some());
        assert!(entity.tag("scope").is_some());
        assert!(entity.tag("aud").is_some());
    }

    #[test]
    fn test_missing_jti_claim_error() {
        let builder = create_test_entity_builder();

        let mut claims = HashMap::new();
        claims.insert("iss".to_string(), json!("https://test.issuer.com"));
        claims.insert("sub".to_string(), json!("user123"));
        // Note: no "jti" claim
        let token_claims = TokenClaims::from(claims);
        let token = Token::new("Jans::Access_Token", token_claims, None);

        let built_entities = BuiltEntities::from(&builder.iss_entities);
        let result = builder.build_single_token_entity(&token, &built_entities);

        assert!(
            matches!(
                result.unwrap_err(),
                MultiIssuerEntityError::InvalidEntityUid(_)
            ),
            "Should get InvalidEntityUid error due to missing jti claim"
        );
    }

    #[test]
    fn test_schema_based_processing() {
        // Test schema-based processing with a Token entity schema
        let schema_src = r#"
            namespace Jans {
                entity Token = {
                    sub: String,
                    scope: Set<String>,
                    aud: String,
                    custom_claim: Long
                };
                type Url = {"host": String, "path": String, "protocol": String};
                entity TrustedIssuer = {"issuer_entity_id": Url};
            }
        "#;

        let validator_schema = cedar_policy_core::validator::ValidatorSchema::from_str(schema_src)
            .expect("should parse schema");

        let ti = TrustedIssuer::new(
            "Jans".to_string(),
            String::new(),
            Url::parse("https://test.issuer.com").unwrap(),
            HashMap::default(),
        );
        let trusted_issuers = HashMap::from_iter(vec![("Jans".to_string(), ti)]);
        let builder = EntityBuilder::new(
            TrustedIssuerIndex::new(&trusted_issuers, None),
            Some(&validator_schema),
            DefaultEntities::default(),
        )
        .expect("could not build EntityBuilder");

        let iss = "https://test.issuer.com";
        let mut claims = HashMap::new();
        claims.insert("iss".to_string(), json!(iss));
        claims.insert("jti".to_string(), json!("test-jti-123"));
        claims.insert("sub".to_string(), json!("user123"));
        claims.insert("scope".to_string(), json!(["read:profile", "write:data"]));
        claims.insert("aud".to_string(), json!("my-client"));
        claims.insert("custom_claim".to_string(), json!(42));
        claims.insert(
            "exp".to_string(),
            json!(chrono::Utc::now().timestamp() + 3600),
        );
        let token_claims = TokenClaims::from(claims);
        let token = Token::new(
            "Jans::Access_Token",
            token_claims,
            builder.find_trusted_issuer_by_iss(iss),
        );

        let built_entities = BuiltEntities::from(&builder.iss_entities);
        let entity = builder
            .build_single_token_entity(&token, &built_entities)
            .unwrap();

        // Schema-based processing should preserve types according to schema
        // Check that tags exist for schema-defined claims
        assert!(entity.tag("sub").is_some());
        assert!(entity.tag("scope").is_some());
        assert!(entity.tag("aud").is_some());
        assert!(entity.tag("custom_claim").is_some());

        // Core attributes should still be present with correct values
        assert!(matches!(
            entity.attr("token_type").expect("token_type attribute should exist").expect("should be a valid value"),
            EvalResult::String(ref val) if *val == "Jans::Access_Token"
        ));
        assert!(matches!(
            entity.attr("jti").expect("jti attribute should exist").expect("should be a valid value"),
            EvalResult::String(ref val) if *val == "test-jti-123"
        ));
        // exp attribute should be present and be a future timestamp
        let current_time = chrono::Utc::now().timestamp();
        assert!(matches!(
            entity.attr("exp").expect("exp attribute should exist").expect("should be a valid value"),
            EvalResult::Long(exp) if exp > current_time
        ));
        // iss attribute should be present and be an entity reference
        let iss_value = entity
            .attr("iss")
            .expect("iss attribute should exist")
            .expect("should be a valid value");
        // iss should be an entity reference (EntityUid) when token has trusted issuer
        assert!(
            matches!(iss_value, EvalResult::EntityUid(_)),
            "iss should be an entity reference (EntityUid), got {iss_value:?}"
        );

        // Verify schema-defined claims are present as tags
        // (tags exist but values are stored as RestrictedExpression)

        // Reserved claims (iss, jti, exp) should NOT be in tags
        assert!(entity.tag("iss").is_none());
        assert!(entity.tag("jti").is_none());
        assert!(entity.tag("exp").is_none());
    }

    #[test]
    fn test_invalid_tokens_are_skipped() {
        let builder = create_test_entity_builder();

        let mut tokens = HashMap::new();

        // Valid token
        let mut claims1 = HashMap::new();
        claims1.insert("sub".to_string(), json!("user1"));
        let token_one = create_test_token("https://idp.acme.com/auth", "token1", claims1, &builder);
        tokens.insert("Jans::Access_Token".to_string(), token_one);

        // Invalid token (missing issuer)
        let mut claims2 = HashMap::new();
        claims2.insert("sub".to_string(), json!("user2"));
        // Note: no "iss" claim
        let token_claims2 = TokenClaims::from(claims2);
        let token_two = Token::new("Jans::Id_Token", token_claims2, None);
        tokens.insert("Jans::Id_Token".to_string(), token_two);

        // Another valid token
        let mut claims3 = HashMap::new();
        claims3.insert("sub".to_string(), json!("user3"));
        let token_three =
            create_test_token("https://idp.dolphin.sea/auth", "token3", claims3, &builder);
        tokens.insert("Acme::DolphinToken".to_string(), token_three);

        let tokens: HashMap<String, Arc<Token>> =
            tokens.into_iter().map(|(k, v)| (k, Arc::new(v))).collect();

        let result = builder.build_multi_issuer_setup_entities(&tokens, &NopLogger);
        assert!(result.is_ok());

        let entities_data = result.unwrap();
        // Only 2 valid tokens should be processed
        assert_eq!(entities_data.tokens.len(), 2);
        assert!(entities_data.tokens.contains_key("acme_access_token"));
        assert!(entities_data.tokens.contains_key("dolphin_dolphintoken"));
    }

    #[test]
    fn test_all_invalid_tokens_returns_error() {
        let builder = create_test_entity_builder();

        let mut tokens = HashMap::new();

        // Invalid token 1 (missing issuer)
        let mut claims1 = HashMap::new();
        claims1.insert("sub".to_string(), json!("user1"));
        let token_claims1 = TokenClaims::from(claims1);
        let token_one = Token::new("Jans::Access_Token", token_claims1, None);
        tokens.insert("Jans::Access_Token".to_string(), token_one);

        // Invalid token 2 (missing issuer)
        let mut claims2 = HashMap::new();
        claims2.insert("sub".to_string(), json!("user2"));
        let token_claims2 = TokenClaims::from(claims2);
        let token_two = Token::new("Jans::Id_Token", token_claims2, None);
        tokens.insert("Jans::Id_Token".to_string(), token_two);

        let tokens: HashMap<String, Arc<Token>> =
            tokens.into_iter().map(|(k, v)| (k, Arc::new(v))).collect();

        let result = builder.build_multi_issuer_setup_entities(&tokens, &NopLogger);

        assert!(
            matches!(result.unwrap_err(), MultiIssuerEntityError::NoValidTokens),
            "Should return NoValidTokens error when all tokens are invalid"
        );
    }

    #[test]
    fn test_schema_fallback_processing() {
        // Test that claims not in schema fall back to string conversion
        let schema_src = r#"
            namespace Jans {
                entity Token = {
                    sub: String,
                    scope: Set<String>
                };
                type Url = {"host": String, "path": String, "protocol": String};
                entity TrustedIssuer = {"issuer_entity_id": Url};
            }
        "#;

        let validator_schema = cedar_policy_core::validator::ValidatorSchema::from_str(schema_src)
            .expect("should parse schema");

        let ti = TrustedIssuer::new(
            "Jans".to_string(),
            String::new(),
            Url::parse("https://test.issuer.com/.well-known/openid-configuration")
                .expect("url should be parsed"),
            HashMap::new(),
        );

        let trusted_issuers = HashMap::from([("Jans".to_string(), ti)]);
        let builder = EntityBuilder::new(
            TrustedIssuerIndex::new(&trusted_issuers, None),
            Some(&validator_schema),
            DefaultEntities::default(),
        )
        .unwrap();

        let mut claims = HashMap::new();
        claims.insert("sub".to_string(), json!("user123"));
        claims.insert("scope".to_string(), json!(["read:profile", "write:data"]));
        claims.insert("unknown_claim".to_string(), json!("some_value")); // Not in schema
        claims.insert("another_unknown".to_string(), json!(123)); // Not in schema

        let token = create_test_token("https://test.issuer.com", "test-jti-123", claims, &builder);

        let built_entities = BuiltEntities::from(&builder.iss_entities);
        let entity = builder
            .build_single_token_entity(&token, &built_entities)
            .unwrap();

        // Schema-defined claims should be processed according to schema
        assert!(entity.tag("sub").is_some());
        assert!(entity.tag("scope").is_some());

        // Unknown claims should fall back to string conversion and become tags
        assert!(entity.tag("unknown_claim").is_some());
        assert!(entity.tag("another_unknown").is_some());

        // Reserved claims (iss, jti, exp) should NOT be in tags
        assert!(entity.tag("iss").is_none());
        assert!(entity.tag("jti").is_none());
        assert!(entity.tag("exp").is_none());

        // Core attributes should still be present with correct values
        assert!(matches!(
            entity.attr("token_type").expect("token_type attribute should exist").expect("should be a valid value"),
            EvalResult::String(ref val) if *val == "Jans::Access_Token"
        ));
        assert!(matches!(
            entity.attr("jti").expect("jti attribute should exist").expect("should be a valid value"),
            EvalResult::String(ref val) if *val == "test-jti-123"
        ));
        // exp attribute should be present and be a future timestamp
        let current_time = chrono::Utc::now().timestamp();
        assert!(matches!(
            entity.attr("exp").expect("exp attribute should exist").expect("should be a valid value"),
            EvalResult::Long(exp) if exp > current_time
        ));
        // iss attribute should be present and be an entity reference
        let iss_value = entity
            .attr("iss")
            .expect("iss attribute should exist")
            .expect("should be a valid value");
        // iss should be an entity reference (EntityUid)
        assert!(
            matches!(iss_value, EvalResult::EntityUid(_)),
            "iss should be an entity reference (EntityUid), got {iss_value:?}"
        );
    }

    #[test]
    fn test_jti_attr_uses_entity_id_not_raw_claim_without_schema() {
        // ── Setup: TrustedIssuer with token_id = "sub" ─────────────────
        // By default token_id = "jti", making entity_id identical to the
        // raw `jti` claim.  Setting token_id = "sub" decouples them so we
        // can prove that the final `jti` attribute comes from
        // `add_reserved_claims` (entity_id), not from the raw claim.
        let tkn_meta = TokenEntityMetadata::builder()
            .entity_type_name("Jans::Access_Token".into())
            .token_id("sub".into())
            .build();
        let mut token_metadata = HashMap::new();
        token_metadata.insert("Jans::Access_Token".into(), tkn_meta);
        let ti = TrustedIssuer::new(
            "TestIssuer".to_string(),
            String::new(),
            Url::parse("https://test.issuer.com").expect("should parse test issuer URL"),
            token_metadata,
        );
        let trusted_issuers = HashMap::from([("TestIssuer".to_string(), ti)]);
        let builder = EntityBuilder::new(
            TrustedIssuerIndex::new(&trusted_issuers, None),
            None, // no schema — exercises the no-schema path
            DefaultEntities::default(),
        )
        .expect("should create entity builder without schema");

        // ── Token: jti and sub are deliberately different ──────────────
        let iss = "https://test.issuer.com";
        let mut claims = HashMap::new();
        claims.insert("iss".to_string(), json!(iss));
        claims.insert("jti".to_string(), json!("raw-jti-from-jwt"));
        claims.insert("sub".to_string(), json!("entity-sub-uid"));
        claims.insert(
            "exp".to_string(),
            json!(chrono::Utc::now().timestamp() + 3600),
        );
        let token = Token::new(
            "Jans::Access_Token",
            TokenClaims::from(claims),
            builder.find_trusted_issuer_by_iss(iss),
        );
        let built_entities = BuiltEntities::from(&builder.iss_entities);
        let entity = builder
            .build_single_token_entity(&token, &built_entities)
            .expect("should build token entity without schema");

        // ── Verify jti attr = entity_id (NOT the raw jwt jti) ─────────
        // entity_id = "entity-sub-uid" (because token_id = "sub")
        // add_reserved_claims overwrites jti with entity_id.
        let jti_attr = entity
            .attr("jti")
            .expect("jti attribute should exist")
            .expect("jti should be a valid value");
        assert!(
            matches!(jti_attr, EvalResult::String(ref val) if val == "entity-sub-uid"),
            "Expected jti = entity_id ('entity-sub-uid'), proving \
             add_reserved_claims (not the data path) sets jti. \
             Got {jti_attr:?}"
        );

        // Reserved claims must NOT leak into tags
        assert!(
            entity.tag("iss").is_none(),
            "iss should not be a tag (reserved)"
        );
        assert!(
            entity.tag("jti").is_none(),
            "jti should not be a tag (reserved)"
        );
        assert!(
            entity.tag("exp").is_none(),
            "exp should not be a tag (reserved)"
        );

        // Non-reserved claims become tags
        assert!(
            entity.tag("sub").is_some(),
            "sub should be a tag (non-reserved)"
        );
    }

    #[test]
    fn test_reserved_claims_overwrite_schema_path() {
        // ── Schema: entity name MUST match token entity type name ─────
        // `determine_token_entity_type` returns the token's entity type
        // (resolved via trusted-issuer metadata, then fallbacks). For the
        // schema path to activate, that name must exist in the schema's
        // entity map.  We define `entity Access_Token` in `Jans` (full
        // name `Jans::Access_Token`) which matches the token name.
        let schema_src = r#"
            namespace Jans {
                entity Access_Token = {
                    jti: String,
                    sub: String,
                    iss: String,
                    exp: Long
                };
                type Url = {"host": String, "path": String, "protocol": String};
                entity TrustedIssuer = {"issuer_entity_id": Url};
            }
        "#;
        let validator_schema = cedar_policy_core::validator::ValidatorSchema::from_str(schema_src)
            .expect("should parse schema");

        // Same TrustedIssuer with token_id = "sub" as in the no-schema test.
        // The token_metadata entity_type_name must match the schema entity
        // so that `determine_token_entity_type` resolves to something the
        // schema can look up.
        let tkn_meta = TokenEntityMetadata::builder()
            .entity_type_name("Jans::Access_Token".into())
            .token_id("sub".into())
            .build();
        let mut token_metadata = HashMap::new();
        token_metadata.insert("Jans::Access_Token".into(), tkn_meta);
        let ti = TrustedIssuer::new(
            "Jans".to_string(),
            String::new(),
            Url::parse("https://test.issuer.com").expect("should parse test issuer URL"),
            token_metadata,
        );
        let trusted_issuers = HashMap::from([("Jans".to_string(), ti)]);
        let builder = EntityBuilder::new(
            TrustedIssuerIndex::new(&trusted_issuers, None),
            Some(&validator_schema),
            DefaultEntities::default(),
        )
        .expect("should create entity builder with schema");

        let iss = "https://test.issuer.com";
        let mut claims = HashMap::new();
        claims.insert("iss".to_string(), json!(iss));
        claims.insert("jti".to_string(), json!("raw-jti-from-jwt"));
        claims.insert("sub".to_string(), json!("entity-sub-uid"));
        claims.insert(
            "exp".to_string(),
            json!(chrono::Utc::now().timestamp() + 3600),
        );
        // This claim is deliberately NOT in the schema shape.  In the
        // schema path it stays out of `attrs`; in the no-schema path
        // it would become an attribute.  We assert it is absent below.
        claims.insert("extra_test_claim".to_string(), json!("should-not-be-attr"));
        let token = Token::new(
            "Jans::Access_Token",
            TokenClaims::from(claims),
            builder.find_trusted_issuer_by_iss(iss),
        );
        let built_entities = BuiltEntities::from(&builder.iss_entities);
        let entity = builder
            .build_single_token_entity(&token, &built_entities)
            .expect("should build token entity with schema path");

        // ── Prove the schema path was actually taken ──────────────────
        // `extra_test_claim` is NOT in the schema shape → in the schema
        // path it never enters `attrs`.  The no-schema path would add
        // every claim as an attribute.  If this assertion passes, the
        // schema path (not the fallback) was exercised.
        assert!(
            entity.attr("extra_test_claim").is_none(),
            "extra_test_claim should not be an attribute when the schema \
             path is active (it is not in the schema shape)"
        );

        // ── jti attr = entity_id, not the raw claim ───────────────────
        // The schema-path closure returned raw jti = "raw-jti-from-jwt",
        // then add_reserved_claims overwrote it with entity_id.
        let jti_attr = entity
            .attr("jti")
            .expect("jti attribute should exist")
            .expect("jti should be a valid value");
        assert!(
            matches!(jti_attr, EvalResult::String(ref val) if val == "entity-sub-uid"),
            "Expected jti = entity_id ('entity-sub-uid'), proving \
             add_reserved_claims overwrites the schema-built value. \
             Got {jti_attr:?}"
        );

        // ── iss attr = EntityUid, not the String from schema ──────────
        let iss_attr = entity
            .attr("iss")
            .expect("iss attribute should exist")
            .expect("iss should be a valid value");
        assert!(
            matches!(iss_attr, EvalResult::EntityUid(_)),
            "iss should be an EntityUid (overwritten by add_reserved_claims), \
             not a plain String. Got {iss_attr:?}"
        );

        // ── exp attr = Long, not String ───────────────────────────────
        let current_time = chrono::Utc::now().timestamp();
        let exp_attr = entity
            .attr("exp")
            .expect("exp attribute should exist")
            .expect("exp should be a valid value");
        assert!(
            matches!(exp_attr, EvalResult::Long(exp) if exp > current_time),
            "exp should be a future Long (overwritten by add_reserved_claims). \
             Got {exp_attr:?}"
        );

        // Reserved claims must NOT be in tags
        assert!(entity.tag("iss").is_none(), "iss should not be a tag");
        assert!(entity.tag("jti").is_none(), "jti should not be a tag");
        assert!(entity.tag("exp").is_none(), "exp should not be a tag");

        // Non-reserved claims become tags
        assert!(entity.tag("sub").is_some(), "sub should be a tag");
    }

    #[test]
    fn test_schema_path_fallback_to_entity_id_when_jti_missing() {
        // ── Edge case: schema requires `jti` but JWT has none ─────────
        // Previously the schema-path builder would fail with
        // "required claim missing" because `claims.get("jti")` returned
        // `None`.  The closure now falls back to `entity_id` so the
        // builder passes; `add_reserved_claims` then sets the final value.
        let schema_src = r#"
            namespace Jans {
                entity Access_Token = {
                    jti: String,
                    sub: String,
                    iss: String,
                    exp: Long
                };
                type Url = {"host": String, "path": String, "protocol": String};
                entity TrustedIssuer = {"issuer_entity_id": Url};
            }
        "#;
        let validator_schema = cedar_policy_core::validator::ValidatorSchema::from_str(schema_src)
            .expect("should parse schema");

        let tkn_meta = TokenEntityMetadata::builder()
            .entity_type_name("Jans::Access_Token".into())
            .token_id("sub".into())
            .build();
        let mut token_metadata = HashMap::new();
        token_metadata.insert("Jans::Access_Token".into(), tkn_meta);
        let ti = TrustedIssuer::new(
            "Jans".to_string(),
            String::new(),
            Url::parse("https://test.issuer.com").expect("should parse test issuer URL"),
            token_metadata,
        );
        let trusted_issuers = HashMap::from([("Jans".to_string(), ti)]);
        let builder = EntityBuilder::new(
            TrustedIssuerIndex::new(&trusted_issuers, None),
            Some(&validator_schema),
            DefaultEntities::default(),
        )
        .expect("should create entity builder with schema for missing jti test");

        let iss = "https://test.issuer.com";
        let mut claims = HashMap::new();
        // No "jti" claim — deliberately missing to test the fallback.
        // `entity_id` will come from `sub` (token_id = "sub").
        claims.insert("iss".to_string(), json!(iss));
        claims.insert("sub".to_string(), json!("entity-sub-uid"));
        claims.insert(
            "exp".to_string(),
            json!(chrono::Utc::now().timestamp() + 3600),
        );

        let token = Token::new(
            "Jans::Access_Token",
            TokenClaims::from(claims),
            builder.find_trusted_issuer_by_iss(iss),
        );
        let built_entities = BuiltEntities::from(&builder.iss_entities);

        // This must NOT fail — the closure should fall back to entity_id
        // so the schema path doesn't choke on missing required `jti`.
        let entity = builder
            .build_single_token_entity(&token, &built_entities)
            .expect("should succeed even without jti claim — closure falls back to entity_id");

        // jti attribute should be set to entity_id by add_reserved_claims
        let jti_attr = entity
            .attr("jti")
            .expect("jti attribute should exist")
            .expect("jti should be a valid value");
        assert!(
            matches!(jti_attr, EvalResult::String(ref val) if val == "entity-sub-uid"),
            "Expected jti = entity_id ('entity-sub-uid') when raw jti claim \
             is absent. Got {jti_attr:?}"
        );

        // jti must NOT leak into tags (it is a reserved claim)
        assert!(entity.tag("jti").is_none(), "jti should not be a tag");
    }
}