baltic-id 0.0.2

Baltic ID-Card, Smart-ID & Mobile-ID Api client library for Rust
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
use std::error::Error;
use std::str::FromStr;
use anyhow::anyhow;
use base64::Engine;
use base64::engine::general_purpose;
use hex::ToHex;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use strum::Display;
use strum::EnumString;
use thiserror::Error;
use x509_parser::parse_x509_certificate;
use x509_parser::prelude::X509Certificate;

use crate::smart_id::errors::SmartIdError;
use crate::smart_id::errors::SmartIdError::{InvalidParametersException, TechnicalError};
use crate::smart_id::verification_code_calculator::VerificationCodeCalculator;

#[derive(Error, Clone, Debug, Serialize, Deserialize)]
pub enum SmartIdAuthenticationResultError {
    #[error("Response end result verification failed.")]
    InvalidEndResult,
    #[error("Signature verification failed.")]
    SignatureVerificationFailure,
    #[error("Signer's certificate expired.")]
    CertificateExpired,
    #[error("Signer's certificate is not trusted.")]
    CertificateNotTrusted,
    #[error("Signer's certificate level does not match with the requested level.")]
    CertificateLevelMismatch,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartIdAuthenticationResult {
    pub authentication_identity: Option<AuthenticationIdentity>,
    pub valid: bool,
    pub errors: Vec<SmartIdAuthenticationResultError>,
}

impl SmartIdAuthenticationResult {
    pub fn new() -> Self {
        Self {
            authentication_identity: None,
            valid: true,
            errors: Vec::new(),
        }
    }

    pub fn set_authentication_identity(&mut self, authentication_identity: AuthenticationIdentity) {
        self.authentication_identity = Some(authentication_identity);
    }

    pub fn set_valid(&mut self, valid: bool) {
        self.valid = valid;
    }

    pub fn add_error(&mut self, error: SmartIdAuthenticationResultError) {
        self.errors.push(error);
    }
}

pub struct AuthenticationCertificate {
    pub name: String,
    pub subject: AuthenticationCertificateSubject,
    pub hash: String,
    pub issuer: AuthenticationCertificateIssuer,
    pub version: i32,
    pub serial_number: String,
    pub serial_number_hex: String,
    pub valid_from: String,
    pub valid_to: u64,
    pub valid_from_time_t: i32,
    pub valid_to_time_t: i32,
    pub signature_type_sn: String,
    pub signature_type_ln: String,
    pub signature_type_nid: i32,
    pub purposes: Vec<String>,
    pub extensions: Option<AuthenticationCertificateExtensions>,
}


pub struct AuthenticationCertificateExtensions {
    basic_constraints: String,
    key_usage: String,
    certificate_policies: String,
    subject_key_identifier: String,
    qc_statements: String,
    authority_key_identifier: String,
    authority_info_access: String,
    extended_key_usage: String,
    subject_alt_name: String,
}

pub struct AuthenticationCertificateIssuer {
    pub c: String,
    pub o: String,
    pub undef: String,
    pub cn: String,
}

pub struct AuthenticationCertificateSubject {
    //Country code
    pub c: String,
    //Country name
    pub o: String,
    //Organizational unit name
    pub ou: String,
    //Common name
    pub cn: String,
    //Surname
    pub sn: String,
    //Given name
    pub gn: String,
    //Serial number
    pub serial_number: String,
}

pub struct AuthenticationHash {
    pub data_to_sign: String,
    pub hash: String,
    pub hash_type: HashType,
}

impl AuthenticationHash {
    pub fn generate_random_hash(hash_type: HashType) -> Self {
        let random_bytes = Self::get_random_bytes();
        let mut authentication_hash = AuthenticationHash {
            data_to_sign: random_bytes.encode_hex::<String>(),
            hash: String::new(),
            hash_type,
        };
        authentication_hash.set_hash(&authentication_hash.calculate_hash());
        authentication_hash
    }

    fn calculate_hash(&self) -> String {
        return DigestCalculator::calculate_digest(&self.data_to_sign, self.hash_type.clone());
    }

    pub fn generate() -> Self {
        Self::generate_random_hash(HashType::Sha512)
    }

    fn get_random_bytes() -> Vec<u8> {
        let mut random_bytes = vec![0u8; 64];
        rand::thread_rng().fill_bytes(&mut random_bytes);
        random_bytes
    }

    pub fn get_hash_type(&self) -> HashType {
        self.hash_type.clone()
    }

    pub fn get_data_to_sign(&self) -> &str {
        &self.data_to_sign
    }

    pub fn set_hash(&mut self, hash: &str) {
        self.hash = hash.to_string();
    }

    pub fn get_hash(&self) -> &str {
        &self.hash
    }

    pub fn calculate_verification_code(&self) -> String {
        VerificationCodeCalculator::calculate(&self.hash)
    }

    pub fn calculate_hash_in_base64(&self) -> String {
        let hash = self.calculate_hash();
        general_purpose::STANDARD
            .encode(hash.as_bytes())
    }
}


#[cfg(test)]
mod authentication_hash_tests {
    use base64::engine::general_purpose::STANDARD;

    use super::*;

    #[test]
    fn generate_random_hash_of_type_sha512() {
        let authentication_hash = AuthenticationHash::generate_random_hash(HashType::Sha512);
        assert_eq!(HashType::Sha512, authentication_hash.get_hash_type());
        assert_eq!(
            STANDARD.decode(&authentication_hash.calculate_hash_in_base64()).unwrap(),
            authentication_hash.get_hash().as_bytes().to_vec()
        );
    }

    #[test]
    fn generate_random_hash_of_type_sha384() {
        let authentication_hash = AuthenticationHash::generate_random_hash(HashType::Sha384);
        assert_eq!(HashType::Sha384, authentication_hash.get_hash_type());
        assert_eq!(
            STANDARD.decode(&authentication_hash.calculate_hash_in_base64()).unwrap(),
            authentication_hash.get_hash().as_bytes().to_vec()
        );
    }

    #[test]
    fn generate_random_hash_of_type_sha256() {
        let authentication_hash = AuthenticationHash::generate_random_hash(HashType::Sha256);
        assert_eq!(HashType::Sha256, authentication_hash.get_hash_type());
        assert_eq!(
            STANDARD.decode(&authentication_hash.calculate_hash_in_base64()).unwrap(),
            authentication_hash.get_hash().as_bytes().to_vec()
        );
    }
}


#[derive(Debug,Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthenticationSessionRequest {
    #[serde(rename = "relyingPartyUUID")]
    relying_party_uuid: String,
    relying_party_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    network_interface: Option<String>,
    certificate_level: CertificateLevel,
    hash: String,
    hash_type: HashType,
    #[serde(skip_serializing_if = "Option::is_none")]
    nonce: Option<String>,
    allowed_interactions_order: Vec<Interaction>,
}

#[derive(Debug,Clone,Default,PartialEq,EnumString, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum CertificateLevel {
    #[strum(serialize = "QUALIFIED")]
    #[default]
    Qualified,
    #[strum(serialize = "ADVANCED")]
    Advanced,
    #[strum(serialize = "QSCD")]
    Qscd,
}

impl CertificateLevel {
    pub fn is_equal_or_above(&self, certificate_level: &str) -> bool {
        if self == &CertificateLevel::from_str(certificate_level).unwrap() {
            true
        } else {
            match (certificate_level.parse().unwrap(), &self) {
                (CertificateLevel::Advanced, CertificateLevel::Advanced)
                | (CertificateLevel::Qualified, CertificateLevel::Qualified)
                | (CertificateLevel::Qualified, CertificateLevel::Advanced) => true,
                _ => false,
            }
        }
    }
}

impl AuthenticationSessionRequest {
    pub fn new(relying_party_uuid: String, relying_party_name: String, hash: String, hash_type: HashType) -> Self {
        AuthenticationSessionRequest {
            relying_party_uuid,
            relying_party_name,
            network_interface: None,
            certificate_level: CertificateLevel::Qualified,
            hash: hash,
            hash_type,
            nonce: None,
            allowed_interactions_order: Vec::new(),
        }
    }

    pub fn set_relying_party_uuid(&mut self, relying_party_uuid: &str) {
        self.relying_party_uuid = relying_party_uuid.to_string();
    }

    pub fn get_relying_party_uuid(&self) -> &str {
        &self.relying_party_uuid
    }

    pub fn set_relying_party_name(&mut self, relying_party_name: &str) {
        self.relying_party_name = relying_party_name.to_string();
    }

    pub fn get_relying_party_name(&self) -> &str {
        &self.relying_party_name
    }

    pub fn set_network_interface(&mut self, network_interface: String) {
        self.network_interface = Some(network_interface);
    }

    pub fn get_network_interface(&self) -> Option<&str> {
        self.network_interface.as_deref()
    }

    pub fn set_certificate_level(&mut self, certificate_level:CertificateLevel) {
        self.certificate_level = certificate_level;
    }

    pub fn get_certificate_level(&self) -> CertificateLevel {
        self.certificate_level.clone()
    }

    pub fn set_hash(&mut self, hash: &str) {
        self.hash = hash.to_string();
    }

    pub fn get_hash(&self) -> &str {
        &self.hash
    }

    pub fn set_hash_type(&mut self, hash_type: HashType) {
        self.hash_type = hash_type;
    }

    pub fn get_hash_type(&self) -> HashType {
        self.hash_type.clone()
    }

    pub fn set_nonce(&mut self, nonce: String) {
        self.nonce = Some(nonce.to_string());
    }

    pub fn get_nonce(&self) -> Option<&str> {
        self.nonce.as_deref()
    }

    pub fn set_allowed_interactions_order(
        &mut self,
        allowed_interactions_order: Vec<Interaction>,
    ) {
        self.allowed_interactions_order = allowed_interactions_order;
    }

    pub fn get_allowed_interactions_order(&self) -> &Vec<Interaction> {
        &self.allowed_interactions_order
    }

}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthenticationSessionResponse {
    #[serde(rename = "sessionID")]
    pub session_id: String,
}

pub struct CertificateParser;

impl CertificateParser {
    //let certificate_bytes = CertificateParser::get_der_certificate(certificate_value).unwrap();

    pub fn parse_x509_certificate(certificate: &[u8]) -> Result<X509Certificate, anyhow::Error> {
        let (rest, parsed_cert) = parse_x509_certificate(certificate).unwrap();
        if !rest.is_empty() {
            return Err(anyhow!("Failed to parse the entire X.509 certificate"));
        }
        Ok(parsed_cert)
    }

    pub fn get_der_certificate(certificate_value: String) -> Result<Vec<u8>, Box<dyn Error>> {
        let certificate_value = certificate_value.trim();
        let begin_cert = "-----BEGIN CERTIFICATE-----";
        let end_cert = "-----END CERTIFICATE-----";

        if certificate_value.starts_with(begin_cert) && certificate_value.ends_with(end_cert) {
            let base64_cert = &certificate_value[begin_cert.len()..certificate_value.len() - end_cert.len()];
            let base64_decoded = general_purpose::STANDARD
                .decode(base64_cert).unwrap();
            Ok(base64_decoded)
        } else {
            Err("Invalid certificate format: missing BEGIN_CERT or END_CERT".into())
        }
    }
}

pub struct DigestCalculator;

impl DigestCalculator {
    pub fn calculate_digest(data_to_digest: &str, hash_type: HashType) -> String {
        use openssl::hash::{hash, MessageDigest};
        let message_digest = match hash_type {
            HashType::Md5 => MessageDigest::md5(),
            HashType::Sha1 => MessageDigest::sha1(),
            HashType::Sha256 => MessageDigest::sha256(),
            HashType::Sha384 => MessageDigest::sha384(),
            HashType::Sha512 => MessageDigest::sha512(),
            _ => panic!("Unsupported hash type: {}", hash_type),
        };
        hash(message_digest, data_to_digest.as_bytes())
            .unwrap()
            .encode_hex()
    }
}

#[derive(Display,Default, Debug, Clone, PartialEq, EnumString, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
#[strum(serialize_all = "UPPERCASE")]
pub enum HashType {
    Md5,
    Sha1,
    Sha256,
    Sha384,
    #[default]
    Sha512,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Interaction {
    #[serde(rename = "type")]
    interaction_type: InteractionType,
    #[serde(skip_serializing_if = "Option::is_none")]
    display_text60: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    display_text200: Option<String>,
}

impl Interaction {
    pub fn of_type_display_text_and_pin(display_text60: String) -> Interaction {
        Interaction {
            interaction_type: InteractionType::DisplayTextAndPIN,
            display_text60: Some(display_text60),
            display_text200: None,
        }
    }

    pub fn of_type_verification_code_choice(display_text60: String) -> Interaction {
        Interaction {
            interaction_type: InteractionType::VerificationCodeChoice,
            display_text60: Some(display_text60),
            display_text200: None,
        }
    }

    pub fn of_type_confirmation_message(display_text200: String) -> Interaction {
        Interaction {
            interaction_type: InteractionType::ConfirmationMessage,
            display_text60: None,
            display_text200: Some(display_text200),
        }
    }

    pub fn of_type_confirmation_message_and_verification_code_choice(
        display_text200: String,
    ) -> Interaction {
        Interaction {
            interaction_type: InteractionType::ConfirmationMessageAndVerificationCodeChoice,
            display_text60: None,
            display_text200: Some(display_text200),
        }
    }

    pub fn to_array(&self) -> serde_json::Value {
        let mut interaction = serde_json::json!({
            "type": self.interaction_type.as_str(),
        });

        if let Some(display_text60) = &self.display_text60 {
            interaction["displayText60"] = serde_json::Value::String(display_text60.clone());
        } else if let Some(display_text200) = &self.display_text200 {
            interaction["displayText200"] = serde_json::Value::String(display_text200.clone());
        }

        interaction
    }

    pub fn validate(&self) -> Result<(), SmartIdError> {
        match self.interaction_type {
            InteractionType::DisplayTextAndPIN | InteractionType::VerificationCodeChoice => {
                if let Some(display_text60) = &self.display_text60 {
                    if display_text60.len() > 60 {
                        return Err(InvalidParametersException(
                            "Interactions of type displayTextAndPIN and verificationCodeChoice require displayTexts with length 60 or less".to_string(),
                        ));
                    }
                }
            }
            InteractionType::ConfirmationMessage
            | InteractionType::ConfirmationMessageAndVerificationCodeChoice => {
                if let Some(display_text200) = &self.display_text200 {
                    if display_text200.len() > 200 {
                        return Err(InvalidParametersException(
                            "Interactions of type confirmationMessage and confirmationMessageAndVerificationCodeChoice require displayTexts with length 200 or less".to_string(),
                        ));
                    }
                }
            }
        }

        Ok(())
    }
}

#[derive(Debug, Clone,EnumString, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum InteractionType {
    DisplayTextAndPIN,
    VerificationCodeChoice,
    ConfirmationMessage,
    ConfirmationMessageAndVerificationCodeChoice,
}

impl InteractionType {
    pub fn as_str(&self) -> &'static str {
        match self {
            InteractionType::DisplayTextAndPIN => "displayTextAndPIN",
            InteractionType::VerificationCodeChoice => "verificationCodeChoice",
            InteractionType::ConfirmationMessage => "confirmationMessage",
            InteractionType::ConfirmationMessageAndVerificationCodeChoice => {
                "confirmationMessageAndVerificationCodeChoice"
            }
        }
    }
}

pub struct SemanticsIdentifier {
    semantics_identifier: String, // https://www.etsi.org/deliver/etsi_en/319400_319499/31941201/01.01.01_60/en_31941201v010101p.pdf in chapter 5.1.3
}

impl SemanticsIdentifier {
    pub fn from_string(semantics_identifier: String) -> SemanticsIdentifier {
        SemanticsIdentifier {
            semantics_identifier,
        }
    }

    pub fn builder() -> SemanticsIdentifierBuilder {
        SemanticsIdentifierBuilder::new()
    }

    pub fn as_string(&self) -> &str {
        &self.semantics_identifier
    }

    pub fn validate(&self) -> Result<(), SmartIdError> {
        let regex = regex::Regex::new(r"^[A-Z\:]{5}-[a-zA-Z\d\-]{5,30}$").unwrap();
        if !regex.is_match(&self.semantics_identifier) {
            return Err(InvalidParametersException(format!(
                "The semantics identifier '{}' has an invalid format",
                &self.semantics_identifier
            )));
        }
        Ok(())
    }
}

pub struct SemanticsIdentifierBuilder {
    semantics_identifier_type: Option<String>,
    country_code: Option<String>,
    identifier: Option<String>,
}

impl SemanticsIdentifierBuilder {
    pub fn new() -> SemanticsIdentifierBuilder {
        SemanticsIdentifierBuilder {
            semantics_identifier_type: None,
            country_code: None,
            identifier: None,
        }
    }

    pub fn with_semantics_identifier_type(
        mut self,
        semantics_identifier_type: String,
    ) -> SemanticsIdentifierBuilder {
        self.semantics_identifier_type = Some(semantics_identifier_type);
        self
    }

    pub fn with_country_code(mut self, country_code: String) -> SemanticsIdentifierBuilder {
        self.country_code = Some(country_code);
        self
    }

    pub fn with_identifier(mut self, identifier: String) -> SemanticsIdentifierBuilder {
        self.identifier = Some(identifier);
        self
    }

    pub fn build(&self) -> Result<SemanticsIdentifier, String> {
        let semantics_identifier_type = self
            .semantics_identifier_type
            .clone()
            .ok_or("Semantics identifier type is missing")?;
        let country_code = self.country_code.clone().ok_or("Country code is missing")?;
        let identifier = self.identifier.clone().ok_or("Identifier is missing")?;
        let semantics_identifier_string = format!(
            "{}{}-{}",
            semantics_identifier_type, country_code, identifier
        );
        Ok(SemanticsIdentifier::from_string(
            semantics_identifier_string,
        ))
    }
}

pub struct SemanticsIdentifierTypes;

impl SemanticsIdentifierTypes {
    pub const PNO: &'static str = "PNO";
    pub const PAS: &'static str = "PAS";
    pub const IDC: &'static str = "IDC";
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionCertificate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) certificate_level: Option<String>,
}

impl SessionCertificate {
    pub fn new() -> SessionCertificate {
        SessionCertificate {
            value: None,
            certificate_level: None,
        }
    }

    pub fn set_value(&mut self, value: String) {
        self.value = Some(value);
    }

    pub fn get_value(&self) -> Option<String> {
        self.value.clone()
    }

    pub fn set_certificate_level(&mut self, certificate_level: String) {
        self.certificate_level = Some(certificate_level);
    }

    pub fn get_certificate_level(&self) -> Option<String> {
        self.certificate_level.clone()
    }
}

#[derive(Debug, PartialEq, EnumString, Display, Clone, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SessionEndResultCode {
    #[strum(serialize = "OK")]
    #[serde(rename = "OK")]
    Ok,
    #[strum(serialize = "USER_REFUSED")]
    #[serde(rename = "USER_REFUSED")]
    UserRefused,
    #[strum(serialize = "TIMEOUT")]
    #[serde(rename = "TIMEOUT")]
    Timeout,
    #[strum(serialize = "DOCUMENT_UNUSABLE")]
    #[serde(rename = "DOCUMENT_UNUSABLE")]
    DocumentUnusable,
    #[strum(serialize = "REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP")]
    #[serde(rename = "REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP")]
    RequiredInteractionNotSupportedByApp,
    #[strum(serialize = "USER_REFUSED_DISPLAYTEXTANDPIN")]
    #[serde(rename = "USER_REFUSED_DISPLAYTEXTANDPIN")]
    UserRefusedDisplayTextAndPIN,
    #[strum(serialize = "USER_REFUSED_VC_CHOICE")]
    #[serde(rename = "USER_REFUSED_VC_CHOICE")]
    UserRefusedVCChoice,
    #[strum(serialize = "USER_REFUSED_CONFIRMATIONMESSAGE")]
    #[serde(rename = "USER_REFUSED_CONFIRMATIONMESSAGE")]
    UserRefusedConfirmationMessage,
    #[strum(serialize = "USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE")]
    #[serde(rename = "USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE")]
    UserRefusedConfirmationMessageWithVCChoice,
    #[strum(serialize = "USER_REFUSED_CERT_CHOICE")]
    #[serde(rename = "USER_REFUSED_CERT_CHOICE")]
    UserRefusedCertChoice,
    #[strum(serialize = "WRONG_VC")]
    #[serde(rename = "WRONG_VC")]
    WrongVC,
}

// impl SessionEndResultCode {
//     pub const OK: &'static str = "OK";
//     pub const USER_REFUSED: &'static str = "USER_REFUSED";
//     pub const TIMEOUT: &'static str = "TIMEOUT";
//     pub const DOCUMENT_UNUSABLE: &'static str = "DOCUMENT_UNUSABLE";
//     pub const REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP: &'static str =
//         "REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP";
//     pub const USER_REFUSED_DISPLAYTEXTANDPIN: &'static str = "USER_REFUSED_DISPLAYTEXTANDPIN";
//     pub const USER_REFUSED_VC_CHOICE: &'static str = "USER_REFUSED_VC_CHOICE";
//     pub const USER_REFUSED_CONFIRMATIONMESSAGE: &'static str = "USER_REFUSED_CONFIRMATIONMESSAGE";
//     pub const USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE: &'static str =
//         "USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE";
//     pub const USER_REFUSED_CERT_CHOICE: &'static str = "USER_REFUSED_CERT_CHOICE";
//     pub const WRONG_VC: &'static str = "WRONG_VC";
// }

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionResult {
    pub end_result: SessionEndResultCode,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_number: Option<String>,
}

impl SessionResult {
    pub fn new(end_result: SessionEndResultCode) -> SessionResult {
        SessionResult {
            end_result,
            document_number: None,
        }
    }

    pub fn set_document_number(&mut self, document_number: String) {
        self.document_number = Some(document_number);
    }

    pub fn get_document_number(&self) -> Option<&String> {
        self.document_number.as_ref()
    }

    pub fn get_end_result(&self) -> SessionEndResultCode {
        self.end_result.clone()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionSignature {
    pub algorithm: Option<String>,
    pub value: Option<String>,
}

impl SessionSignature {
    pub fn new() -> SessionSignature {
        SessionSignature {
            algorithm: None,
            value: None,
        }
    }

    pub fn set_algorithm(&mut self, algorithm: String) {
        self.algorithm = Some(algorithm);
    }

    pub fn get_algorithm(&self) -> Option<String> {
        self.algorithm.clone()
    }

    pub fn set_value(&mut self, value: String) {
        self.value = Some(value);
    }

    pub fn get_value(&self) -> Option<String> {
        self.value.clone()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStatus {
    pub state: SessionStatusCode,
    pub result: Option<SessionResult>,
    pub signature: Option<SessionSignature>,
    pub cert: Option<SessionCertificate>,
    pub ignored_properties: Option<Vec<String>>,
    pub interaction_flow_used: Option<String>,
}

impl SessionStatus {
    pub fn new() -> SessionStatus {
        SessionStatus {
            state: SessionStatusCode::RUNNING,
            result: None,
            signature: None,
            cert: None,
            ignored_properties: None,
            interaction_flow_used: None,
        }
    }

    pub fn set_state(&mut self, state: SessionStatusCode) {
        self.state = state;
    }

    pub fn set_result(&mut self, result: Option<SessionResult>) {
        self.result = result;
    }

    pub fn set_signature(&mut self, signature: Option<SessionSignature>) {
        self.signature = signature;
    }

    pub fn set_cert(&mut self, cert: Option<SessionCertificate>) {
        self.cert = cert;
    }

    pub fn set_ignored_properties(&mut self, ignored_properties: Option<Vec<String>>) {
        self.ignored_properties = ignored_properties;
    }

    pub fn set_interaction_flow_used(&mut self, interaction_flow_used: Option<String>) {
        self.interaction_flow_used = interaction_flow_used;
    }

    pub fn get_state(&self) -> SessionStatusCode {
        self.state.clone()
    }

    pub fn get_result(&self) -> Option<SessionResult> {
        self.result.clone()
    }

    pub fn get_signature(&self) -> Option<&SessionSignature> {
        self.signature.as_ref()
    }

    pub fn get_cert(&self) -> Option<&SessionCertificate> {
        self.cert.as_ref()
    }

    pub fn get_ignored_properties(&self) -> Option<&Vec<String>> {
        self.ignored_properties.as_ref()
    }

    pub fn get_interaction_flow_used(&self) -> Option<&str> {
        self.interaction_flow_used.as_deref()
    }

    pub fn is_running_state(&self) -> bool {
        self.state == SessionStatusCode::RUNNING
    }
}

#[derive(Display, Clone, Debug, PartialEq, EnumString, Serialize, Deserialize)]
pub enum SessionStatusCode {
    RUNNING,
    COMPLETE,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SessionStatusRequest {
    pub session_id: String,
    pub session_status_response_socket_timeout_ms: u64,
    pub network_interface: String,
}

impl SessionStatusRequest {
    pub fn new(session_id: String) -> SessionStatusRequest {
        SessionStatusRequest {
            session_id,
            session_status_response_socket_timeout_ms: 1000,
            network_interface: String::new(),
        }
    }

    pub fn set_session_status_response_socket_timeout_ms(
        &mut self,
        session_status_response_socket_timeout_ms: u64,
    ) {
        self.session_status_response_socket_timeout_ms = session_status_response_socket_timeout_ms;
    }

    pub fn is_session_status_response_socket_timeout_set(&self) -> bool {
        self.session_status_response_socket_timeout_ms > 0
    }

    pub fn set_network_interface(&mut self, network_interface: String) {
        self.network_interface = network_interface;
    }

    pub fn to_json(&self) -> serde_json::Value {
        let mut json_obj = serde_json::json!({});

        let timeout_ms = self.session_status_response_socket_timeout_ms;
        json_obj["timeoutMs"] = serde_json::Value::Number(serde_json::Number::from(timeout_ms));

        let network_interface = &self.network_interface;
        json_obj["networkInterface"] = serde_json::Value::String(network_interface.clone());

        json_obj
    }
}

pub struct SignableData {
    pub data_to_sign: String,
    pub hash_type: HashType,
}

impl SignableData {
    pub fn new(data_to_sign: String) -> SignableData {
        SignableData {
            data_to_sign,
            hash_type: HashType::Sha512,
        }
    }

    pub fn calculate_hash_in_base64(&self) -> String {
        let digest = self.calculate_hash();
        general_purpose::STANDARD.encode(&digest)
    }

    pub fn calculate_hash(&self) -> Vec<u8> {
        DigestCalculator::calculate_digest(&self.data_to_sign, self.hash_type.clone()).into_bytes()
    }

    pub fn set_hash_type(&mut self, hash_type: HashType) {
        self.hash_type = hash_type;
    }

    pub fn get_hash_type(&self) -> &HashType {
        &self.hash_type
    }

    pub fn are_fields_filled(&self) -> bool {
        !self.data_to_sign.is_empty()
    }

    pub fn get_data_to_sign(&self) -> &str {
        &self.data_to_sign
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartIdAuthenticationResponse {
    pub end_result: SessionEndResultCode,
    pub signed_data: String,
    pub value_in_base64: String,
    pub algorithm_name: Option<String>,
    pub certificate: String,
    pub requested_certificate_level: Option<String>,
    pub certificate_level: String,
    pub state: SessionStatusCode,
    pub ignored_properties: Option<Vec<String>>,
    pub interaction_flow_used: Option<String>,
    pub document_number: Option<String>,
}

impl SmartIdAuthenticationResponse {
    pub fn new() -> SmartIdAuthenticationResponse {
        SmartIdAuthenticationResponse {
            end_result: SessionEndResultCode::Ok,
            signed_data: String::new(),
            value_in_base64: String::new(),
            algorithm_name: None,
            certificate: String::new(),
            requested_certificate_level: None,
            certificate_level: String::new(),
            state: SessionStatusCode::RUNNING,
            ignored_properties: None,
            interaction_flow_used: None,
            document_number: None,
        }
    }

    pub fn get_end_result(&self) -> SessionEndResultCode {
        self.end_result.clone()
    }

    pub fn set_end_result(&mut self, end_result: SessionEndResultCode) {
        self.end_result = end_result;
    }

    pub fn get_signed_data(&self) -> &str {
        &self.signed_data
    }

    pub fn set_signed_data(&mut self, signed_data: String) {
        self.signed_data = signed_data;
    }

    pub fn get_value_in_base64(&self) -> String {
        self.value_in_base64.clone()
    }

    pub fn set_value_in_base64(&mut self, value_in_base64: String) {
        self.value_in_base64 = value_in_base64;
    }

    pub fn get_algorithm_name(&self) -> &str {
        self.algorithm_name.as_deref().unwrap_or("")
    }

    pub fn set_algorithm_name(&mut self, algorithm_name: String) {
        self.algorithm_name = Some(algorithm_name.clone());
    }

    pub fn get_certificate(&self) -> &str {
        self.certificate.as_str()
    }

    // pub fn get_parsed_certificate<'a>(&self) -> Result<X509Certificate<'a>, anyhow::Error> {
    //     let certificate = CertificateParser::get_der_certificate(self.certificate.clone()).unwrap();
    //     CertificateParser::parse_x509_certificate(certificate.as_slice())
    // }

    // pub fn get_certificate_instance(&self) -> Option<X509Certificate> {
    //     let certificate = CertificateParser::get_der_certificate(self.certificate.clone()).unwrap();
    //     let parsed = CertificateParser::parse_x509_certificate(certificate.as_slice()).unwrap();
    //     Some(parsed)
    // }

    pub fn set_certificate(&mut self, certificate: String) {
        self.certificate = certificate;
    }

    pub fn get_certificate_level(&self) -> &str {
        self.certificate_level.as_str()
    }

    pub fn set_certificate_level(&mut self, certificate_level: String) {
        self.certificate_level = certificate_level;
    }

    pub fn get_requested_certificate_level(&self) -> Option<&str> {
        self.requested_certificate_level.as_deref()
    }

    pub fn set_requested_certificate_level(&mut self, requested_certificate_level: Option<String>) {
        self.requested_certificate_level = requested_certificate_level;
    }

    pub fn get_value(&self) -> Result<Vec<u8>, SmartIdError> {
        match self.value_in_base64.is_empty() {
            true => Err(TechnicalError(
                "No value in base64 format".to_string(),
            )),
            false => {
                let decoded =
                    general_purpose::STANDARD.decode(self.value_in_base64.as_str()).map_err(|_| {
                        TechnicalError(format!(
                            "Failed to decode base64: {}",
                            self.value_in_base64
                        ))
                    })?;
                Ok(decoded)
            }
        }
    }

    pub fn set_state(&mut self, state: SessionStatusCode) {
        self.state = state;
    }

    pub fn get_state(&self) -> SessionStatusCode {
        self.state.clone()
    }

    pub fn set_ignored_properties(&mut self, ignored_properties: Option<Vec<String>>) {
        self.ignored_properties = ignored_properties;
    }

    pub fn get_interaction_flow_used(&self) -> &str {
        self.interaction_flow_used.as_deref().unwrap_or("")
    }

    pub fn set_interaction_flow_used(&mut self, interaction_flow_used: Option<String>) {
        self.interaction_flow_used = interaction_flow_used;
    }

    pub fn is_running_state(&self) -> bool {
        self.state == SessionStatusCode::RUNNING
    }

    pub fn set_document_number(&mut self, document_number: Option<String>) {
        self.document_number = document_number;
    }

    pub fn get_document_number(&self) -> Option<&str> {
        self.document_number.as_deref()
    }
}


#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthenticationIdentity {
    pub given_name: String,
    pub sur_name: String,
    pub identity_code: String,
    pub identity_number: String,
    pub country: String,
    pub auth_certificate: String,
    pub date_of_birth: Option<chrono::DateTime<chrono::Utc>>,
}

impl AuthenticationIdentity {
    pub fn new() -> AuthenticationIdentity {
        AuthenticationIdentity {
            given_name: String::new(),
            sur_name: String::new(),
            identity_code: String::new(),
            identity_number: String::new(),
            country: String::new(),
            auth_certificate: String::new(),
            date_of_birth: None,
        }
    }

    pub fn set_given_name(&mut self, given_name: String) -> &mut AuthenticationIdentity {
        self.given_name = given_name;
        self
    }

    pub fn get_given_name(&self) -> &str {
        &self.given_name
    }

    pub fn set_sur_name(&mut self, sur_name: String) -> &mut AuthenticationIdentity {
        self.sur_name = sur_name;
        self
    }

    pub fn get_sur_name(&self) -> &str {
        &self.sur_name
    }

    pub fn set_identity_code(&mut self, identity_code: String) -> &mut AuthenticationIdentity {
        self.identity_code = identity_code;
        self
    }

    pub fn get_identity_code(&self) -> &str {
        &self.identity_code
    }

    pub fn set_identity_number(&mut self, identity_number: String) -> &mut AuthenticationIdentity {
        self.identity_number = identity_number;
        self
    }

    pub fn get_identity_number(&self) -> &str {
        &self.identity_number
    }

    pub fn set_country(&mut self, country: String) -> &mut AuthenticationIdentity {
        self.country = country;
        self
    }

    pub fn get_country(&self) -> &str {
        &self.country
    }

    pub fn set_auth_certificate(
        &mut self,
        auth_certificate: String,
    ) -> &mut AuthenticationIdentity {
        self.auth_certificate = auth_certificate;
        self
    }

    pub fn get_auth_certificate(&self) -> &str {
        &self.auth_certificate
    }

    pub fn set_date_of_birth(
        &mut self,
        date_of_birth: Option<chrono::DateTime<chrono::Utc>>,
    ) -> &mut AuthenticationIdentity {
        self.date_of_birth = date_of_birth;
        self
    }

    pub fn get_date_of_birth(&self) -> Option<&chrono::DateTime<chrono::Utc>> {
        self.date_of_birth.as_ref()
    }
}


#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartIdErrorResponse {
    #[serde(rename = "type")]
    pub r#type: String,
    #[serde(rename = "title")]
    pub title: String,
    #[serde(rename = "status")]
    pub status: i32,
    #[serde(rename = "detail")]
    pub detail: String,
    #[serde(rename = "instance", skip_serializing_if = "Option::is_none")]
    pub instance: Option<String>,
    #[serde(rename = "properties", skip_serializing_if = "Option::is_none")]
    pub properties: Option<String>,
    #[serde(rename = "code")]
    pub code: i32,
    #[serde(rename = "message")]
    pub message: String,
}