common-access-token 0.2.7

Implementation of the Common Access Token (CAT) specification
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
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
//! Token implementation for Common Access Token

use crate::claims::{Claims, RegisteredClaims};
use crate::constants::tprint_params;
use crate::error::Error;
use crate::header::{Algorithm, CborValue, Header, HeaderMap, KeyId};
use crate::utils::{compute_hmac_sha256, current_timestamp, verify_hmac_sha256};
use crate::FingerprintType;
use minicbor::{Decoder, Encoder};
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::Path;

/// Common Access Token structure
#[derive(Debug, Clone)]
pub struct Token {
    /// Token header
    pub header: Header,
    /// Token claims
    pub claims: Claims,
    /// Token signature
    pub signature: Vec<u8>,
    /// Original payload bytes (for verification)
    original_payload_bytes: Option<Vec<u8>>,
}

impl Token {
    /// Create a new token with the given header, claims, and signature
    pub fn new(header: Header, claims: Claims, signature: Vec<u8>) -> Self {
        Self {
            header,
            claims,
            signature,
            original_payload_bytes: None,
        }
    }

    /// Encode the token to CBOR bytes
    pub fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        let mut buf = Vec::new();
        let mut enc = Encoder::new(&mut buf);

        // For HMAC algorithms, use COSE_Mac0 format with CWT tag
        if let Some(Algorithm::HmacSha256) = self.header.algorithm() {
            // Apply CWT tag (61)
            enc.tag(minicbor::data::Tag::new(61))?;
            // Apply COSE_Mac0 tag (17)
            enc.tag(minicbor::data::Tag::new(17))?;
        }

        // COSE structure array with 4 items
        enc.array(4)?;

        // 1. Protected header (encoded as CBOR and then as bstr)
        let protected_bytes = encode_map(&self.header.protected)?;
        enc.bytes(&protected_bytes)?;

        // 2. Unprotected header
        encode_map_direct(&self.header.unprotected, &mut enc)?;

        // 3. Payload (encoded as CBOR and then as bstr)
        let claims_map = self.claims.to_map();
        let claims_bytes = encode_map(&claims_map)?;
        enc.bytes(&claims_bytes)?;

        // 4. Signature/MAC
        enc.bytes(&self.signature)?;

        Ok(buf)
    }

    /// Decode a token from CBOR bytes
    ///
    /// This function supports both COSE_Sign1 (tag 18) and COSE_Mac0 (tag 17) structures,
    /// as well as custom tags. It will automatically skip any tags and process the underlying
    /// CBOR array.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
        let mut dec = Decoder::new(bytes);

        // Check if the token starts with a tag (COSE_Sign1 tag = 18, COSE_Mac0 tag = 17, or custom tag = 61)
        if dec.datatype()? == minicbor::data::Type::Tag {
            // Skip the tag
            let _ = dec.tag()?;

            // Check for a second tag
            if dec.datatype()? == minicbor::data::Type::Tag {
                let _ = dec.tag()?;
            }
        }

        // Expect array with 4 items
        let array_len = dec.array()?.unwrap_or(0);
        if array_len != 4 {
            return Err(Error::InvalidFormat(format!(
                "Expected array of length 4, got {array_len}"
            )));
        }

        // 1. Protected header
        let protected_bytes = dec.bytes()?;
        let protected = decode_map(protected_bytes)?;

        // 2. Unprotected header
        let unprotected = decode_map_direct(&mut dec)?;

        // Create header
        let header = Header {
            protected,
            unprotected,
        };

        // 3. Payload
        let claims_bytes = dec.bytes()?;
        let claims_map = decode_map(claims_bytes)?;
        let claims = Claims::from_map(&claims_map);

        // 4. Signature
        let signature = dec.bytes()?.to_vec();

        Ok(Self {
            header,
            claims,
            signature,
            original_payload_bytes: Some(claims_bytes.to_vec()),
        })
    }

    /// Verify the token signature
    ///
    /// This function supports both COSE_Sign1 and COSE_Mac0 structures.
    /// It will first try to verify the signature using the COSE_Sign1 structure,
    /// and if that fails, it will try the COSE_Mac0 structure.
    pub fn verify(&self, key: &[u8]) -> Result<(), Error> {
        let alg = self.header.algorithm().ok_or_else(|| {
            Error::InvalidFormat("Missing algorithm in protected header".to_string())
        })?;

        match alg {
            Algorithm::HmacSha256 => {
                // Try with COSE_Sign1 structure first
                let sign1_input = self.sign1_input()?;
                let sign1_result = verify_hmac_sha256(key, &sign1_input, &self.signature);

                if sign1_result.is_ok() {
                    return Ok(());
                }

                // If COSE_Sign1 verification fails, try COSE_Mac0 structure
                let mac0_input = self.mac0_input()?;
                verify_hmac_sha256(key, &mac0_input, &self.signature)
            }
        }
    }

    /// Verify the token claims
    pub fn verify_claims(&self, options: &VerificationOptions) -> Result<(), Error> {
        let now = current_timestamp();

        // Check expiration
        if options.verify_exp {
            if let Some(exp) = self.claims.registered.exp {
                if now >= exp {
                    return Err(Error::Expired);
                }
            } else if options.require_exp {
                return Err(Error::MissingClaim("exp".to_string()));
            }
        }

        // Check not before
        if options.verify_nbf {
            if let Some(nbf) = self.claims.registered.nbf {
                if now < nbf {
                    return Err(Error::NotYetValid);
                }
            }
        }

        // Check issuer
        if let Some(expected_iss) = &options.expected_issuer {
            if let Some(iss) = &self.claims.registered.iss {
                if iss != expected_iss {
                    return Err(Error::InvalidIssuer);
                }
            } else if options.require_iss {
                return Err(Error::MissingClaim("iss".to_string()));
            }
        }

        // Check audience
        if let Some(expected_aud) = &options.expected_audience {
            if let Some(aud) = &self.claims.registered.aud {
                if aud != expected_aud {
                    return Err(Error::InvalidAudience);
                }
            } else if options.require_aud {
                return Err(Error::MissingClaim("aud".to_string()));
            }
        }

        // Check CAT-specific claims
        if options.verify_catu {
            self.verify_catu_claim(options)?;
        }

        if options.verify_catm {
            self.verify_catm_claim(options)?;
        }

        if options.verify_catreplay {
            self.verify_catreplay_claim(options)?;
        }

        if options.verify_cattprint {
            self.verify_cattprint_claim(options)?;
        }

        Ok(())
    }

    /// Verify the CATU (URI) claim against the provided URI
    fn verify_catu_claim(&self, options: &VerificationOptions) -> Result<(), Error> {
        use crate::constants::{cat_keys, uri_components};
        use url::Url;

        // Get the URI to verify against
        let uri = match &options.uri {
            Some(uri) => uri,
            None => {
                return Err(Error::InvalidClaimValue(
                    "No URI provided for CATU verification".to_string(),
                ))
            }
        };

        // Parse the URI
        let parsed_uri = match Url::parse(uri) {
            Ok(url) => url,
            Err(_) => {
                return Err(Error::InvalidClaimValue(format!(
                    "Invalid URI format: {uri}"
                )))
            }
        };

        // Parse the Path from the URI
        let parsed_path = Path::new(parsed_uri.path());

        // Check if token has CATU claim
        let catu_claim = match self.claims.custom.get(&cat_keys::CATU) {
            Some(claim) => claim,
            None => return Ok(()), // No CATU claim, so nothing to verify
        };

        // CATU claim should be a map
        let component_map = match catu_claim {
            CborValue::Map(map) => map,
            _ => {
                return Err(Error::InvalidUriClaim(
                    "CATU claim is not a map".to_string(),
                ))
            }
        };

        // Verify each component in the CATU claim
        for (component_key, component_value) in component_map {
            match *component_key {
                uri_components::SCHEME => {
                    self.verify_uri_component(
                        &parsed_uri.scheme().to_string(),
                        component_value,
                        "scheme",
                    )?;
                }
                uri_components::HOST => {
                    self.verify_uri_component(
                        &parsed_uri.host_str().unwrap_or("").to_string(),
                        component_value,
                        "host",
                    )?;
                }
                uri_components::PORT => {
                    let port = parsed_uri.port().map(|p| p.to_string()).unwrap_or_default();
                    self.verify_uri_component(&port, component_value, "port")?;
                }
                uri_components::PATH => {
                    self.verify_uri_component(
                        &parsed_uri.path().to_string(),
                        component_value,
                        "path",
                    )?;
                }
                uri_components::QUERY => {
                    let query = parsed_uri.query().unwrap_or("").to_string();
                    self.verify_uri_component(&query, component_value, "query")?;
                }
                uri_components::PARENT_PATH => {
                    // Extract parent directory path from URI path.
                    // For URI "https://example.com/a/b/file.txt", this extracts "/a/b".
                    // For root-level files, this returns an empty string.
                    // Non-UTF8 paths are converted to empty strings.
                    let parent_path = parsed_path.parent().unwrap_or(Path::new("")).to_str().unwrap_or("").to_string();
                    self.verify_uri_component(&parent_path, component_value, "parent_path")?;
                }
                uri_components::FILENAME => {
                    // Extract complete filename (with extension) from URI path.
                    // For URI "https://example.com/path/video.mp4", this extracts "video.mp4".
                    // For paths without a filename, this returns an empty string.
                    // Non-UTF8 filenames are converted to empty strings.
                    let filename = parsed_path.file_name().unwrap_or(OsStr::new("")).to_str().unwrap_or("").to_string();
                    self.verify_uri_component(&filename, component_value, "filename")?;
                }
                uri_components::STEM => {
                    // Extract filename without extension from URI path.
                    // For URI "https://example.com/path/video.mp4", this extracts "video".
                    // For "archive.tar.gz", this extracts "archive.tar" (only last extension removed).
                    // For files without extension, returns the entire filename.
                    // Non-UTF8 stems are converted to empty strings.
                    let stem = parsed_path.file_stem().unwrap_or(OsStr::new("")).to_str().unwrap_or("").to_string();
                    self.verify_uri_component(&stem, component_value, "stem")?;
                }
                uri_components::EXTENSION => {
                    // Extract file extension from path
                    let path = parsed_uri.path();
                    let extension = path.split('.').next_back().unwrap_or("").to_string();
                    if !path.contains('.') || path.ends_with('.') {
                        // No extension or ends with dot
                        self.verify_uri_component(&"".to_string(), component_value, "extension")?;
                    } else {
                        self.verify_uri_component(
                            &format!(".{extension}"),
                            component_value,
                            "extension",
                        )?;
                    }
                }
                _ => {
                    // Ignore unsupported components
                }
            }
        }

        Ok(())
    }

    /// Verify a URI component against match conditions
    fn verify_uri_component(
        &self,
        component: &String,
        match_conditions: &CborValue,
        component_name: &str,
    ) -> Result<(), Error> {
        use crate::constants::match_types;
        use hmac_sha256::Hash as Sha256Hash;
        use hmac_sha512::Hash as Sha512Hash;
        use regex::Regex;

        // Match conditions should be a map
        let match_map = match match_conditions {
            CborValue::Map(map) => map,
            _ => {
                return Err(Error::InvalidUriClaim(format!(
                    "Match conditions for {component_name} is not a map"
                )))
            }
        };

        for (match_type, match_value) in match_map {
            match *match_type {
                match_types::EXACT => {
                    if let CborValue::Text(text) = match_value {
                        if component != text {
                            return Err(Error::InvalidUriClaim(format!(
                                "URI component {component_name} '{component}' does not exactly match required value '{text}'"
                            )));
                        }
                    }
                }
                match_types::PREFIX => {
                    if let CborValue::Text(prefix) = match_value {
                        if !component.starts_with(prefix) {
                            return Err(Error::InvalidUriClaim(format!(
                                "URI component {component_name} '{component}' does not start with required prefix '{prefix}'"
                            )));
                        }
                    }
                }
                match_types::SUFFIX => {
                    if let CborValue::Text(suffix) = match_value {
                        if !component.ends_with(suffix) {
                            return Err(Error::InvalidUriClaim(format!(
                                "URI component {component_name} '{component}' does not end with required suffix '{suffix}'"
                            )));
                        }
                    }
                }
                match_types::CONTAINS => {
                    if let CborValue::Text(contained) = match_value {
                        if !component.contains(contained) {
                            return Err(Error::InvalidUriClaim(format!(
                                "URI component {component_name} '{component}' does not contain required text '{contained}'"
                            )));
                        }
                    }
                }
                match_types::REGEX => {
                    if let CborValue::Array(array) = match_value {
                        if let Some(CborValue::Text(pattern)) = array.first() {
                            match Regex::new(pattern) {
                                Ok(regex) => {
                                    if !regex.is_match(component) {
                                        return Err(Error::InvalidUriClaim(format!(
                                            "URI component {component_name} '{component}' does not match required regex pattern '{pattern}'"
                                        )));
                                    }
                                }
                                Err(_) => {
                                    return Err(Error::InvalidUriClaim(format!(
                                        "Invalid regex pattern: {pattern}"
                                    )))
                                }
                            }
                        }
                    }
                }
                match_types::SHA256 => {
                    if let CborValue::Bytes(expected_hash) = match_value {
                        let hash = Sha256Hash::hash(component.as_bytes());

                        if !ct_codecs::verify(&hash, expected_hash.as_slice()) {
                            return Err(Error::InvalidUriClaim(format!(
                                "URI component {component_name} '{component}' SHA-256 hash does not match expected value"
                            )));
                        }
                    }
                }
                match_types::SHA512_256 => {
                    if let CborValue::Bytes(expected_hash) = match_value {
                        let hash = Sha512Hash::hash(component.as_bytes());
                        let truncated_hash = &hash[0..32]; // Take first 256 bits (32 bytes)

                        if !ct_codecs::verify(truncated_hash, &expected_hash[..]) {
                            return Err(Error::InvalidUriClaim(format!(
                                "URI component {component_name} '{component}' SHA-512/256 hash does not match expected value"
                            )));
                        }
                    }
                }
                _ => {
                    // Ignore unsupported match types
                }
            }
        }

        Ok(())
    }

    /// Verify the CATM (HTTP method) claim against the provided method
    fn verify_catm_claim(&self, options: &VerificationOptions) -> Result<(), Error> {
        use crate::constants::cat_keys;

        // Get the HTTP method to verify against
        let method = match &options.http_method {
            Some(method) => method,
            None => {
                return Err(Error::InvalidClaimValue(
                    "No HTTP method provided for CATM verification".to_string(),
                ))
            }
        };

        // Check if token has CATM claim
        let catm_claim = match self.claims.custom.get(&cat_keys::CATM) {
            Some(claim) => claim,
            None => return Ok(()), // No CATM claim, so nothing to verify
        };

        // CATM claim should be an array of allowed methods
        let allowed_methods = match catm_claim {
            CborValue::Array(methods) => methods,
            _ => {
                return Err(Error::InvalidMethodClaim(
                    "CATM claim is not an array".to_string(),
                ))
            }
        };

        // Check if the provided method is in the allowed methods list
        let method_upper = method.to_uppercase();
        let method_allowed = allowed_methods.iter().any(|m| {
            if let CborValue::Text(allowed) = m {
                allowed.to_uppercase() == method_upper
            } else {
                false
            }
        });

        if !method_allowed {
            return Err(Error::InvalidMethodClaim(format!(
                "HTTP method '{}' is not allowed. Permitted methods: {:?}",
                method,
                allowed_methods
                    .iter()
                    .filter_map(|m| if let CborValue::Text(t) = m {
                        Some(t.as_str())
                    } else {
                        None
                    })
                    .collect::<Vec<&str>>()
            )));
        }

        Ok(())
    }

    /// Verify the CATREPLAY claim for token replay protection
    fn verify_catreplay_claim(&self, options: &VerificationOptions) -> Result<(), Error> {
        use crate::constants::{cat_keys, replay_values};

        // Check if token has CATREPLAY claim
        let catreplay_claim = match self.claims.custom.get(&cat_keys::CATREPLAY) {
            Some(claim) => claim,
            None => return Ok(()), // No CATREPLAY claim, so nothing to verify
        };

        // Get the replay protection value
        let replay_value = match catreplay_claim {
            CborValue::Integer(value) => *value as i32,
            _ => {
                return Err(Error::InvalidClaimValue(
                    "CATREPLAY claim is not an integer".to_string(),
                ))
            }
        };

        match replay_value {
            replay_values::PERMITTED => {
                // Replay is permitted, no verification needed
                Ok(())
            }
            replay_values::PROHIBITED => {
                // Replay is prohibited, check if token has been seen before
                if options.token_seen_before {
                    Err(Error::ReplayViolation(
                        "Token replay is prohibited".to_string(),
                    ))
                } else {
                    Ok(())
                }
            }
            replay_values::REUSE_DETECTION => {
                // Reuse is detected but allowed, no error returned
                // Implementations should log or notify about reuse
                Ok(())
            }
            _ => Err(Error::InvalidClaimValue(format!(
                "Invalid CATREPLAY value: {replay_value}"
            ))),
        }
    }

    /// Verify the CATTPRINT (TLS Fingerprint) claim against the provided fingerprint type and value
    fn verify_cattprint_claim(&self, options: &VerificationOptions) -> Result<(), Error> {
        use crate::constants::cat_keys;

        // Get the Fingerprint type to verify against
        let fingerprint_type = match &options.fingerprint_type {
            Some(fingerprint_type) => fingerprint_type,
            None => {
                return Err(Error::InvalidClaimValue(
                    "No Fingerprint Type provided for CATTPRINT verification".to_string(),
                ))
            }
        };

        // Get the Fingerprint value to verify against
        let fingerprint_value = match &options.fingerprint_value {
            Some(fingerprint_value) => fingerprint_value,
            None => {
                return Err(Error::InvalidClaimValue(
                    "No Fingerprint Value provided for CATTPRINT verification".to_string(),
                ))
            }
        };

        // Check if token has CATTPRINT claim
        let cattprint_claim = match self.claims.custom.get(&cat_keys::CATTPRINT) {
            Some(claim) => claim,
            None => return Ok(()), // No CATTPRINT claim, so nothing to verify
        };

        // CATTPRINT claim should be a map of 2 values
        let cattprint_map = match cattprint_claim {
            CborValue::Map(cattprint_map) => cattprint_map,
            _ => {
                return Err(Error::InvalidTLSFingerprintClaim(
                    "CATTPRINT claim is not a map".to_string(),
                ))
            }
        };

        // Check if the provided Fingerprint Type matches
        let claim_fingerprint_type = cattprint_map.get(&tprint_params::FINGERPRINT_TYPE);
        if let Some(CborValue::Integer(claim_type)) = claim_fingerprint_type {
            if *claim_type != (*fingerprint_type as i64) {
                // Convert claim_type (i64) to FingerprintType for human-readable name
                let claim_type_name = FingerprintType::from_i64(*claim_type)
                    .map(|ft| ft.as_str())
                    .unwrap_or("<unknown>");
                return Err(Error::InvalidTLSFingerprintClaim(format!(
                    "TLS Fingerprint Type '{}' does not match required value '{}'",
                    claim_type_name, fingerprint_type.as_str()
                )));
            }
        } else {
            return Err(Error::InvalidTLSFingerprintClaim(
                "Missing or invalid Fingerprint Type in CATTPRINT claim".to_string(),
            ));
        }

        // Check if the provided Fingerprint Value matches
        let fingerprint_value_upper = fingerprint_value.to_lowercase();
        let claim_fingerprint_value = cattprint_map.get(&tprint_params::FINGERPRINT_VALUE);
        if let Some(CborValue::Text(claim_value)) = claim_fingerprint_value {
            if claim_value.to_lowercase() != fingerprint_value_upper {
                return Err(Error::InvalidTLSFingerprintClaim(format!(
                    "TLS Fingerprint Value '{}' does not match required value '{}'",
                    claim_value, fingerprint_value
                )));
            }
        } else {
            return Err(Error::InvalidTLSFingerprintClaim(
                "Missing or invalid Fingerprint Value in CATTPRINT claim".to_string(),
            ));
        }

        Ok(())
    }

    // Note: signature_input method removed as we now use mac0_input for HMAC algorithms

    /// Get the encoded payload bytes, using original bytes if available
    fn get_payload_bytes(&self) -> Result<Vec<u8>, Error> {
        if let Some(ref original) = self.original_payload_bytes {
            // Use original bytes for verification
            Ok(original.clone())
        } else {
            // Encode claims for newly created tokens
            let claims_map = self.claims.to_map();
            encode_map(&claims_map)
        }
    }

    /// Get the COSE_Sign1 signature input
    fn sign1_input(&self) -> Result<Vec<u8>, Error> {
        // Sig_structure = [
        //   context : "Signature1",
        //   protected : bstr .cbor header_map,
        //   external_aad : bstr,
        //   payload : bstr .cbor claims
        // ]

        let mut buf = Vec::new();
        let mut enc = Encoder::new(&mut buf);

        // Start array with 4 items
        enc.array(4)?;

        // 1. Context
        enc.str("Signature1")?;

        // 2. Protected header
        let protected_bytes = encode_map(&self.header.protected)?;
        enc.bytes(&protected_bytes)?;

        // 3. External AAD (empty in our case)
        enc.bytes(&[])?;

        // 4. Payload
        let claims_bytes = self.get_payload_bytes()?;
        enc.bytes(&claims_bytes)?;

        Ok(buf)
    }

    /// Get the COSE_Mac0 signature input
    fn mac0_input(&self) -> Result<Vec<u8>, Error> {
        // Mac_structure = [
        //   context : "MAC0",
        //   protected : bstr .cbor header_map,
        //   external_aad : bstr,
        //   payload : bstr .cbor claims
        // ]

        let mut buf = Vec::new();
        let mut enc = Encoder::new(&mut buf);

        // Start array with 4 items
        enc.array(4)?;

        // 1. Context
        enc.str("MAC0")?;

        // 2. Protected header
        let protected_bytes = encode_map(&self.header.protected)?;
        enc.bytes(&protected_bytes)?;

        // 3. External AAD (empty in our case)
        enc.bytes(&[])?;

        // 4. Payload
        let claims_bytes = self.get_payload_bytes()?;
        enc.bytes(&claims_bytes)?;

        Ok(buf)
    }

    // Convenience methods for common token operations

    /// Check if the token has expired
    ///
    /// Returns `true` if the token has an expiration claim and the current time is at or after it.
    /// Returns `false` if the token has no expiration claim or if it hasn't expired yet.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, RegisteredClaims, current_timestamp};
    ///
    /// let key = b"my-secret-key";
    /// let now = current_timestamp();
    ///
    /// // Token that expires in 1 hour
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .registered_claims(RegisteredClaims::new().with_expiration(now + 3600))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(!token.is_expired());
    /// ```
    pub fn is_expired(&self) -> bool {
        if let Some(exp) = self.claims.registered.exp {
            current_timestamp() >= exp
        } else {
            false
        }
    }

    /// Get the duration until token expiration
    ///
    /// Returns `Some(Duration)` if the token has an expiration claim and hasn't expired yet.
    /// Returns `None` if the token has no expiration claim or has already expired.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, RegisteredClaims, current_timestamp};
    ///
    /// let key = b"my-secret-key";
    /// let now = current_timestamp();
    ///
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .registered_claims(RegisteredClaims::new().with_expiration(now + 3600))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// if let Some(duration) = token.expires_in() {
    ///     println!("Token expires in {} seconds", duration.as_secs());
    /// }
    /// ```
    pub fn expires_in(&self) -> Option<std::time::Duration> {
        if let Some(exp) = self.claims.registered.exp {
            let now = current_timestamp();
            if now < exp {
                Some(std::time::Duration::from_secs(exp - now))
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Check if the token is valid based on the not-before (nbf) claim
    ///
    /// Returns `true` if the token has no nbf claim or if the current time is at or after it.
    /// Returns `false` if the token has an nbf claim and the current time is before it.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, RegisteredClaims, current_timestamp};
    ///
    /// let key = b"my-secret-key";
    /// let now = current_timestamp();
    ///
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .registered_claims(RegisteredClaims::new().with_not_before(now))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(token.is_valid_yet());
    /// ```
    pub fn is_valid_yet(&self) -> bool {
        if let Some(nbf) = self.claims.registered.nbf {
            current_timestamp() >= nbf
        } else {
            true
        }
    }

    /// Get the issuer claim value
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, RegisteredClaims};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .registered_claims(RegisteredClaims::new().with_issuer("example-issuer"))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert_eq!(token.issuer(), Some("example-issuer"));
    /// ```
    pub fn issuer(&self) -> Option<&str> {
        self.claims.registered.iss.as_deref()
    }

    /// Get the subject claim value
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, RegisteredClaims};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .registered_claims(RegisteredClaims::new().with_subject("user-123"))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert_eq!(token.subject(), Some("user-123"));
    /// ```
    pub fn subject(&self) -> Option<&str> {
        self.claims.registered.sub.as_deref()
    }

    /// Get the audience claim value
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, RegisteredClaims};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .registered_claims(RegisteredClaims::new().with_audience("api-service"))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert_eq!(token.audience(), Some("api-service"));
    /// ```
    pub fn audience(&self) -> Option<&str> {
        self.claims.registered.aud.as_deref()
    }

    /// Get the expiration timestamp
    pub fn expiration(&self) -> Option<u64> {
        self.claims.registered.exp
    }

    /// Get the not-before timestamp
    pub fn not_before(&self) -> Option<u64> {
        self.claims.registered.nbf
    }

    /// Get the issued-at timestamp
    pub fn issued_at(&self) -> Option<u64> {
        self.claims.registered.iat
    }

    /// Get a custom claim as a string
    ///
    /// Returns `Some(&str)` if the claim exists and is a text value, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .custom_string(100, "custom-value")
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert_eq!(token.get_custom_string(100), Some("custom-value"));
    /// assert_eq!(token.get_custom_string(999), None);
    /// ```
    pub fn get_custom_string(&self, key: i32) -> Option<&str> {
        match self.claims.custom.get(&key) {
            Some(CborValue::Text(s)) => Some(s.as_str()),
            _ => None,
        }
    }

    /// Get a custom claim as an integer
    ///
    /// Returns `Some(i64)` if the claim exists and is an integer value, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .custom_int(100, 42)
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert_eq!(token.get_custom_int(100), Some(42));
    /// assert_eq!(token.get_custom_int(999), None);
    /// ```
    pub fn get_custom_int(&self, key: i32) -> Option<i64> {
        match self.claims.custom.get(&key) {
            Some(CborValue::Integer(i)) => Some(*i),
            _ => None,
        }
    }

    /// Get a custom claim as binary data
    ///
    /// Returns `Some(&[u8])` if the claim exists and is a bytes value, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    ///
    /// let key = b"my-secret-key";
    /// let data = vec![1, 2, 3, 4];
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .custom_binary(100, data.clone())
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert_eq!(token.get_custom_binary(100), Some(data.as_slice()));
    /// assert_eq!(token.get_custom_binary(999), None);
    /// ```
    pub fn get_custom_binary(&self, key: i32) -> Option<&[u8]> {
        match self.claims.custom.get(&key) {
            Some(CborValue::Bytes(b)) => Some(b.as_slice()),
            _ => None,
        }
    }

    /// Get a reference to a custom claim value
    ///
    /// Returns `Some(&CborValue)` if the claim exists, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, CborValue};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .custom_string(100, "value")
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// if let Some(CborValue::Text(s)) = token.get_custom_claim(100) {
    ///     assert_eq!(s, "value");
    /// }
    /// ```
    pub fn get_custom_claim(&self, key: i32) -> Option<&CborValue> {
        self.claims.custom.get(&key)
    }

    /// Check if a custom claim exists
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    ///
    /// let key = b"my-secret-key";
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .custom_string(100, "value")
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(token.has_custom_claim(100));
    /// assert!(!token.has_custom_claim(999));
    /// ```
    pub fn has_custom_claim(&self, key: i32) -> bool {
        self.claims.custom.contains_key(&key)
    }
}

/// Options for token verification
#[derive(Debug, Clone, Default)]
pub struct VerificationOptions {
    /// Verify expiration claim
    pub verify_exp: bool,
    /// Require expiration claim
    pub require_exp: bool,
    /// Verify not before claim
    pub verify_nbf: bool,
    /// Expected issuer
    pub expected_issuer: Option<String>,
    /// Require issuer claim
    pub require_iss: bool,
    /// Expected audience
    pub expected_audience: Option<String>,
    /// Require audience claim
    pub require_aud: bool,
    /// Verify CAT-specific URI claim (CATU) against provided URI
    pub verify_catu: bool,
    /// URI to verify against CATU claim
    pub uri: Option<String>,
    /// Verify CAT-specific HTTP methods claim (CATM) against provided method
    pub verify_catm: bool,
    /// HTTP method to verify against CATM claim
    pub http_method: Option<String>,
    /// Verify CAT-specific replay protection (CATREPLAY)
    pub verify_catreplay: bool,
    /// Whether the token has been seen before (for replay protection)
    pub token_seen_before: bool,
    /// Verify CAT-specific TLS Fingerprint claim (CATTPRINT) against provided Fingerprint Type and Value
    pub verify_cattprint: bool,
    /// Fingerprint Type to verify against CATTPRINT claim
    pub fingerprint_type: Option<FingerprintType>,
    /// Fingerprint Value to verify against CATTPRINT claim
    pub fingerprint_value: Option<String>,
}

impl VerificationOptions {
    /// Create new default verification options
    pub fn new() -> Self {
        Self {
            verify_exp: true,
            require_exp: false,
            verify_nbf: true,
            expected_issuer: None,
            require_iss: false,
            expected_audience: None,
            require_aud: false,
            verify_catu: false,
            uri: None,
            verify_catm: false,
            http_method: None,
            verify_catreplay: false,
            token_seen_before: false,
            verify_cattprint: false,
            fingerprint_type: None,
            fingerprint_value: None,
        }
    }

    /// Set whether to verify expiration
    pub fn verify_exp(mut self, verify: bool) -> Self {
        self.verify_exp = verify;
        self
    }

    /// Set whether to require expiration
    pub fn require_exp(mut self, require: bool) -> Self {
        self.require_exp = require;
        self
    }

    /// Set whether to verify not before
    pub fn verify_nbf(mut self, verify: bool) -> Self {
        self.verify_nbf = verify;
        self
    }

    /// Set expected issuer
    pub fn expected_issuer<S: Into<String>>(mut self, issuer: S) -> Self {
        self.expected_issuer = Some(issuer.into());
        self
    }

    /// Set whether to require issuer
    pub fn require_iss(mut self, require: bool) -> Self {
        self.require_iss = require;
        self
    }

    /// Set expected audience
    pub fn expected_audience<S: Into<String>>(mut self, audience: S) -> Self {
        self.expected_audience = Some(audience.into());
        self
    }

    /// Set whether to require audience
    pub fn require_aud(mut self, require: bool) -> Self {
        self.require_aud = require;
        self
    }

    /// Set whether to verify CAT-specific URI claim (CATU)
    pub fn verify_catu(mut self, verify: bool) -> Self {
        self.verify_catu = verify;
        self
    }

    /// Set URI to verify against CATU claim
    pub fn uri<S: Into<String>>(mut self, uri: S) -> Self {
        self.uri = Some(uri.into());
        self
    }

    /// Set whether to verify CAT-specific HTTP methods claim (CATM)
    pub fn verify_catm(mut self, verify: bool) -> Self {
        self.verify_catm = verify;
        self
    }

    /// Set HTTP method to verify against CATM claim
    pub fn http_method<S: Into<String>>(mut self, method: S) -> Self {
        self.http_method = Some(method.into());
        self
    }

    /// Set whether to verify CAT-specific replay protection (CATREPLAY)
    pub fn verify_catreplay(mut self, verify: bool) -> Self {
        self.verify_catreplay = verify;
        self
    }

    /// Set whether the token has been seen before (for replay protection)
    pub fn token_seen_before(mut self, seen: bool) -> Self {
        self.token_seen_before = seen;
        self
    }

    /// Set whether to verify CAT-specific TLS Fingerprint claim (CATTPRINT)
    pub fn verify_cattprint(mut self, verify: bool) -> Self {
        self.verify_cattprint = verify;
        self
    }

    /// Set fingerprint type to verify for the CATTPRINT claim
    pub fn fingerprint_type(mut self, fingerprint_type: FingerprintType) -> Self {
        self.fingerprint_type = Some(fingerprint_type);
        self
    }

    /// Set fingerprint value to verify for the CATTPRINT claim
    pub fn fingerprint_value<S: Into<String>>(mut self, fingerprint_value: S) -> Self {
        self.fingerprint_value = Some(fingerprint_value.into());
        self
    }
}

/// Builder for creating tokens
#[derive(Debug, Clone, Default)]
pub struct TokenBuilder {
    header: Header,
    claims: Claims,
}

impl TokenBuilder {
    /// Create a new token builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the algorithm
    pub fn algorithm(mut self, alg: Algorithm) -> Self {
        self.header = self.header.with_algorithm(alg);
        self
    }

    /// Set the key identifier in the protected header
    pub fn protected_key_id(mut self, kid: KeyId) -> Self {
        self.header = self.header.with_protected_key_id(kid);
        self
    }

    /// Set the key identifier in the unprotected header
    pub fn unprotected_key_id(mut self, kid: KeyId) -> Self {
        self.header = self.header.with_unprotected_key_id(kid);
        self
    }

    /// Set the registered claims
    pub fn registered_claims(mut self, claims: RegisteredClaims) -> Self {
        self.claims = self.claims.with_registered_claims(claims);
        self
    }

    /// Add a custom claim with a string value
    pub fn custom_string<S: Into<String>>(mut self, key: i32, value: S) -> Self {
        self.claims = self.claims.with_custom_string(key, value);
        self
    }

    /// Add a custom claim with a binary value
    pub fn custom_binary<B: Into<Vec<u8>>>(mut self, key: i32, value: B) -> Self {
        self.claims = self.claims.with_custom_binary(key, value);
        self
    }

    /// Add a custom claim with an integer value
    pub fn custom_int(mut self, key: i32, value: i64) -> Self {
        self.claims = self.claims.with_custom_int(key, value);
        self
    }

    /// Add a custom claim with a nested map value
    pub fn custom_map(mut self, key: i32, value: BTreeMap<i32, CborValue>) -> Self {
        self.claims = self.claims.with_custom_map(key, value);
        self
    }

    /// Add a custom claim with a CborValue directly
    pub fn custom_cbor(mut self, key: i32, value: CborValue) -> Self {
        self.claims.custom.insert(key, value);
        self
    }

    /// Add a custom claim with an array value
    pub fn custom_array(mut self, key: i32, value: Vec<CborValue>) -> Self {
        self.claims.custom.insert(key, CborValue::Array(value));
        self
    }

    /// Set expiration time relative to now (in seconds)
    ///
    /// This is a convenience method that sets the expiration claim to the current time plus the specified number of seconds.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm, current_timestamp};
    ///
    /// let key = b"my-secret-key";
    ///
    /// // Token expires in 1 hour
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .expires_in_secs(3600)
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(!token.is_expired());
    /// ```
    pub fn expires_in_secs(mut self, seconds: u64) -> Self {
        let exp = current_timestamp() + seconds;
        self.claims.registered.exp = Some(exp);
        self
    }

    /// Set expiration time relative to now using a Duration
    ///
    /// This is a convenience method that sets the expiration claim to the current time plus the specified duration.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    /// use std::time::Duration;
    ///
    /// let key = b"my-secret-key";
    ///
    /// // Token expires in 1 hour
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .expires_in(Duration::from_secs(3600))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(!token.is_expired());
    /// ```
    pub fn expires_in(self, duration: std::time::Duration) -> Self {
        self.expires_in_secs(duration.as_secs())
    }

    /// Set token lifetime with issued-at and expiration claims
    ///
    /// This convenience method sets both the `iat` (issued at) and `exp` (expiration) claims.
    /// The issued-at is set to the current time, and expiration is set to current time plus the specified seconds.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    ///
    /// let key = b"my-secret-key";
    ///
    /// // Token valid for 1 hour
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .valid_for_secs(3600)
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(token.issued_at().is_some());
    /// assert!(token.expiration().is_some());
    /// ```
    pub fn valid_for_secs(mut self, seconds: u64) -> Self {
        let now = current_timestamp();
        self.claims.registered.iat = Some(now);
        self.claims.registered.exp = Some(now + seconds);
        self
    }

    /// Set token lifetime with issued-at and expiration claims using a Duration
    ///
    /// This convenience method sets both the `iat` (issued at) and `exp` (expiration) claims.
    /// The issued-at is set to the current time, and expiration is set to current time plus the specified duration.
    ///
    /// # Example
    ///
    /// ```
    /// use common_access_token::{TokenBuilder, Algorithm};
    /// use std::time::Duration;
    ///
    /// let key = b"my-secret-key";
    ///
    /// // Token valid for 1 hour
    /// let token = TokenBuilder::new()
    ///     .algorithm(Algorithm::HmacSha256)
    ///     .valid_for(Duration::from_secs(3600))
    ///     .sign(key)
    ///     .unwrap();
    ///
    /// assert!(token.issued_at().is_some());
    /// assert!(token.expiration().is_some());
    /// ```
    pub fn valid_for(self, duration: std::time::Duration) -> Self {
        self.valid_for_secs(duration.as_secs())
    }

    /// Build and sign the token
    pub fn sign(self, key: &[u8]) -> Result<Token, Error> {
        // Ensure we have an algorithm
        let alg = self.header.algorithm().ok_or_else(|| {
            Error::InvalidFormat("Missing algorithm in protected header".to_string())
        })?;

        // Create token without signature
        let token = Token {
            header: self.header,
            claims: self.claims,
            signature: Vec::new(),
            original_payload_bytes: None,
        };

        // Compute signature input based on algorithm
        // HMAC algorithms use COSE_Mac0 structure, others use COSE_Sign1
        let (_signature_input, signature) = match alg {
            Algorithm::HmacSha256 => {
                let mac_input = token.mac0_input()?;
                let mac = compute_hmac_sha256(key, &mac_input);
                (mac_input, mac)
            }
        };

        // Create final token with signature
        Ok(Token {
            header: token.header,
            claims: token.claims,
            signature,
            original_payload_bytes: None,
        })
    }
}

// Helper functions for CBOR encoding/decoding

fn encode_map(map: &HeaderMap) -> Result<Vec<u8>, Error> {
    let mut buf = Vec::new();
    let mut enc = Encoder::new(&mut buf);

    encode_map_direct(map, &mut enc)?;

    Ok(buf)
}

/// Encode a CBOR value directly to the encoder
fn encode_cbor_value(value: &CborValue, enc: &mut Encoder<&mut Vec<u8>>) -> Result<(), Error> {
    match value {
        CborValue::Integer(i) => {
            enc.i64(*i)?;
        }
        CborValue::Bytes(b) => {
            enc.bytes(b)?;
        }
        CborValue::Text(s) => {
            enc.str(s)?;
        }
        CborValue::Map(nested_map) => {
            // Create a nested encoder for the map
            encode_map_direct(nested_map, enc)?;
        }
        CborValue::Array(arr) => {
            // Create a nested encoder for the array
            enc.array(arr.len() as u64)?;
            for item in arr {
                encode_cbor_value(item, enc)?;
            }
        }
        CborValue::Null => {
            enc.null()?;
        }
    }
    Ok(())
}

fn encode_map_direct(map: &HeaderMap, enc: &mut Encoder<&mut Vec<u8>>) -> Result<(), Error> {
    enc.map(map.len() as u64)?;

    for (key, value) in map {
        enc.i32(*key)?;
        encode_cbor_value(value, enc)?;
    }

    Ok(())
}

fn decode_map(bytes: &[u8]) -> Result<HeaderMap, Error> {
    let mut dec = Decoder::new(bytes);
    decode_map_direct(&mut dec)
}

/// Decode a CBOR array
fn decode_array(dec: &mut Decoder<'_>) -> Result<Vec<CborValue>, Error> {
    let array_len = dec.array()?.unwrap_or(0);
    let mut array = Vec::with_capacity(array_len as usize);

    for _ in 0..array_len {
        // Try to decode based on the datatype
        let datatype = dec.datatype()?;

        // Handle each type separately
        let value = if datatype == minicbor::data::Type::Int {
            // Integer value
            let i = dec.i64()?;
            CborValue::Integer(i)
        } else if datatype == minicbor::data::Type::U8
            || datatype == minicbor::data::Type::U16
            || datatype == minicbor::data::Type::U32
            || datatype == minicbor::data::Type::U64
        {
            // Unsigned integer value
            let i = dec.u64()? as i64;
            CborValue::Integer(i)
        } else if datatype == minicbor::data::Type::Bytes {
            // Byte string
            let b = dec.bytes()?;
            CborValue::Bytes(b.to_vec())
        } else if datatype == minicbor::data::Type::String {
            // Text string
            let s = dec.str()?;
            CborValue::Text(s.to_string())
        } else if datatype == minicbor::data::Type::Map {
            // Nested map
            let nested_map = decode_map_direct(dec)?;
            CborValue::Map(nested_map)
        } else if datatype == minicbor::data::Type::Array {
            // Nested array
            let nested_array = decode_array(dec)?;
            CborValue::Array(nested_array)
        } else if datatype == minicbor::data::Type::Null {
            // Null value
            dec.null()?;
            CborValue::Null
        } else {
            // Unsupported type
            return Err(Error::InvalidFormat(format!(
                "Unsupported CBOR type in array: {datatype:?}"
            )));
        };

        array.push(value);
    }

    Ok(array)
}

fn decode_map_direct(dec: &mut Decoder<'_>) -> Result<HeaderMap, Error> {
    let map_len = dec.map()?.unwrap_or(0);
    let mut map = HeaderMap::new();

    for _ in 0..map_len {
        let key = dec.i32()?;

        // Try to decode based on the datatype
        let datatype = dec.datatype()?;

        // Handle each type separately
        let value = if datatype == minicbor::data::Type::Int {
            // Integer value
            let i = dec.i64()?;
            CborValue::Integer(i)
        } else if datatype == minicbor::data::Type::U8
            || datatype == minicbor::data::Type::U16
            || datatype == minicbor::data::Type::U32
            || datatype == minicbor::data::Type::U64
        {
            // Unsigned integer value
            let i = dec.u64()? as i64;
            CborValue::Integer(i)
        } else if datatype == minicbor::data::Type::Bytes {
            // Byte string
            let b = dec.bytes()?;
            CborValue::Bytes(b.to_vec())
        } else if datatype == minicbor::data::Type::String {
            // Text string
            let s = dec.str()?;
            CborValue::Text(s.to_string())
        } else if datatype == minicbor::data::Type::Map {
            // Nested map
            let nested_map = decode_map_direct(dec)?;
            CborValue::Map(nested_map)
        } else if datatype == minicbor::data::Type::Array {
            // Array
            let array = decode_array(dec)?;
            CborValue::Array(array)
        } else if datatype == minicbor::data::Type::Null {
            // Null value
            dec.null()?;
            CborValue::Null
        } else {
            // Unsupported type
            return Err(Error::InvalidFormat(format!(
                "Unsupported CBOR type: {datatype:?}"
            )));
        };

        map.insert(key, value);
    }

    Ok(map)
}