apisix_admin_client/models/
common.rs

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
use serde::{Deserialize, Serialize};
use anyhow::Result;
use crate::models::requests::InteractionFlow;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum Capability {
    QUALIFIED,
    ADVANCED,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum CertificateLevel {
    QUALIFIED,
    ADVANCED,
    QSCD
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum ResultState {
    OK,
    USER_REFUSED,
    TIMEOUT,
    DOCUMENT_UNUSABLE,
    WRONG_VC,
    REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP,
    USER_REFUSED_CERT_CHOICE,
    USER_REFUSED_DISPLAYTEXTANDPIN,
    USER_REFUSED_VC_CHOICE,
    USER_REFUSED_CONFIRMATIONMESSAGE,
    USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE,
    UNKNOWN,
}

impl From<String> for ResultState {
    fn from(value: String) -> Self {
        match value.to_uppercase().as_str() {
            "OK" => ResultState::OK,
            "USER_REFUSED" => ResultState::USER_REFUSED,
            "TIMEOUT" => ResultState::TIMEOUT,
            "DOCUMENT_UNUSABLE" => ResultState::DOCUMENT_UNUSABLE,
            "WRONG_VC" => ResultState::WRONG_VC,
            "REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP" => ResultState::REQUIRED_INTERACTION_NOT_SUPPORTED_BY_APP,
            "USER_REFUSED_CERT_CHOICE" => ResultState::USER_REFUSED_CERT_CHOICE,
            "USER_REFUSED_DISPLAYTEXTANDPIN" => ResultState::USER_REFUSED_DISPLAYTEXTANDPIN,
            "USER_REFUSED_VC_CHOICE" => ResultState::USER_REFUSED_VC_CHOICE,
            "USER_REFUSED_CONFIRMATIONMESSAGE" => ResultState::USER_REFUSED_CONFIRMATIONMESSAGE,
            "USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE" => ResultState::USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE,
            _ => ResultState::UNKNOWN,
        }
    }
}


#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum HashType {
    SHA256,
    SHA384,
    SHA512
}

impl From<String> for CertificateLevel {
    fn from(value: String) -> Self {
        match value.to_uppercase().as_str() {
            "QUALIFIED" => CertificateLevel::QUALIFIED,
            "ADVANCED" => CertificateLevel::ADVANCED,
            "QSCD" => CertificateLevel::QSCD,
            _ => CertificateLevel::QUALIFIED
        }
    }
}

impl From<CertificateLevel> for String {
    fn from(value: CertificateLevel) -> Self {
        format!("{:?}", value).to_uppercase()
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum IdentityType {
    PAS,
    IDC,
    PNO
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
#[non_exhaustive]
pub enum CountryCode {
    EE, LT, LV, BE
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SemanticsIdentifier {
    pub identifier: String,
}

impl SemanticsIdentifier {
    pub fn new_from_string(identity_type: impl Into<String>, country_code: impl Into<String>, identity_number: impl Into<String>) -> Self {
        SemanticsIdentifier { identifier: format!("{}{}-{}", identity_type.into(), country_code.into(), identity_number.into()) }
    }

    pub fn new_from_enum(identity_type: IdentityType, country_code: CountryCode, identity_number: impl Into<String>) -> Self {
        SemanticsIdentifier { identifier: format!("{:?}{:?}-{}", identity_type, country_code, identity_number.into()) }
    }

    pub fn new_from_string_mock(identity_type: impl Into<String>, country_code: impl Into<String>) -> Self {
        SemanticsIdentifier { identifier: format!("{}{}-{}-MOCK-Q", identity_type.into(), country_code.into(), "00010299944") }
    }

    pub fn new_from_enum_mock(identity_type: IdentityType, country_code: CountryCode) -> Self {
        SemanticsIdentifier { identifier: format!("{:?}{:?}-{}-MOCK-Q", identity_type, country_code, "00010299944") }
    }
}


#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Interaction {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "type")]
    pub interaction_flow: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "displayText60")]
    pub display_text_60: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "displayText200")]
    pub display_text_200: Option<String>,
}

impl Interaction {
    pub fn new(interaction_flow: Option<InteractionFlow>, display_text_60: Option<String>, display_text_200: Option<String>) -> Self {
        Interaction {
            interaction_flow: interaction_flow.map(|x| x.into()),
            display_text_60,
            display_text_200,
        }
    }

    pub fn diplay_text_and_pin(display_text_60: impl Into<String>) -> Self {
        Interaction {
            interaction_flow: Some(InteractionFlow::DISPLAY_TEXT_AND_PIN.into()),
            display_text_60: Some(display_text_60.into()),
            display_text_200: None,
        }
    }

    pub fn verification_code_choice(display_text_60: impl Into<String>) -> Self {
        Interaction {
            interaction_flow: Some(InteractionFlow::VERIFICATION_CODE_CHOICE.into()),
            display_text_60: Some(display_text_60.into()),
            display_text_200: None,
        }
    }

    pub fn confirmation_message(display_text_200: impl Into<String>) -> Self {
        Interaction {
            interaction_flow: Some(InteractionFlow::CONFIRMATION_MESSAGE.into()),
            display_text_60: None,
            display_text_200: Some(display_text_200.into()),
        }
    }

    pub fn confirmation_message_and_verification_code_choice(display_text_200: impl Into<String>) -> Self {
        Interaction {
            interaction_flow: Some(InteractionFlow::CONFIRMATION_MESSAGE_AND_VERIFICATION_CODE_CHOICE.into()),
            display_text_60: None,
            display_text_200: Some(display_text_200.into()),
        }
    }

    pub fn validate_display_text60(&self) -> Result<()> {

        match &self.interaction_flow {
            None => Ok(()),
            Some(inter_f) => {
                let inter_f_typed = InteractionFlow::from(inter_f.clone());
                if inter_f_typed.eq(&InteractionFlow::VERIFICATION_CODE_CHOICE) || inter_f_typed.eq(&InteractionFlow::DISPLAY_TEXT_AND_PIN) {
                    let display_text_60 = self.display_text_60.clone();
                    let display_text_200 = self.display_text_200.clone();
                    if display_text_60.is_none() {
                        return Err(anyhow::anyhow!(format!("displayText60 cannot be null for AllowedInteractionOrder of type {:?}", inter_f_typed.clone())));
                    };
                    if display_text_60.is_some() && display_text_60.unwrap().clone().len() > 60 {
                        return Err(anyhow::anyhow!("displayText60 must not be longer than 60 characters"));
                    };
                    if display_text_200.is_some() {
                        return Err(anyhow::anyhow!(format!("displayText200 must be null for AllowedInteractionOrder of type {:?}", inter_f_typed.clone())));
                    };
                }
                Ok(())
            }
        }
    }

    pub fn validate_display_text200(&self) -> Result<()> {
        match &self.interaction_flow {
            None => Ok(()),
            Some(inter_f) => {
                let inter_f_typed = InteractionFlow::from(inter_f.clone());
                if inter_f_typed.eq(&InteractionFlow::CONFIRMATION_MESSAGE) || inter_f_typed.eq(&InteractionFlow::CONFIRMATION_MESSAGE_AND_VERIFICATION_CODE_CHOICE) {
                    let display_text_60 = self.display_text_60.clone();
                    let display_text_200 = self.display_text_200.clone();
                    if display_text_200.is_none() {
                        return Err(anyhow::anyhow!(format!("displayText200 cannot be null for AllowedInteractionOrder of type {:?}", inter_f_typed.clone())));
                    };
                    if display_text_200.is_some() && display_text_200.unwrap().clone().len() > 200 {
                        return Err(anyhow::anyhow!("displayText200 must not be longer than 200 characters"));
                    };
                    if display_text_60.is_some() {
                        return Err(anyhow::anyhow!(format!("displayText60 must be null for AllowedInteractionOrder of type {:?}", inter_f_typed.clone())));
                    };
                }
                Ok(())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tracing::{error, info};
    use tracing_test::traced_test;
    use crate::models::requests::InteractionFlow;

    #[traced_test]
    #[tokio::test]
    async fn test_semantics_id_construct_by_string() {
        let sem_id = SemanticsIdentifier::new_from_string("PNO", "BE", "123456789");
        assert_eq!(sem_id.identifier, "PNOBE-123456789");
    }

    #[traced_test]
    #[tokio::test]
    async fn test_semantics_id_construct_by_enum() {
        let sem_id = SemanticsIdentifier::new_from_enum(IdentityType::PNO, CountryCode::BE, "123456789");
        assert_eq!(sem_id.identifier, "PNOBE-123456789");
    }

    #[traced_test]
    #[tokio::test]
    async fn test_resolve_certificate_level_string_value() {
        let cert_level: CertificateLevel = CertificateLevel::QUALIFIED;
        assert_eq!(String::from(cert_level), "QUALIFIED");
    }
}