tg_flows/types/
encrypted_credentials.rs

1use serde::{Deserialize, Serialize};
2
3/// Contains data required for decrypting and authenticating
4/// [`EncryptedPassportElement`].
5///
6/// See the [Telegram Passport Documentation] for a complete description of the
7/// data decryption and authentication processes.
8///
9/// [The official docs](https://core.telegram.org/bots/api#encryptedcredentials).
10///
11/// [`EncryptedPassportElement`]:
12/// crate::types::EncryptedPassportElement
13/// [Telegram Passport Documentation]: https://core.telegram.org/passport#receiving-information
14#[serde_with_macros::skip_serializing_none]
15#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
16pub struct EncryptedCredentials {
17    /// Base64-encoded encrypted JSON-serialized data with unique user's
18    /// payload, data hashes and secrets required for
19    /// [`EncryptedPassportElement`] decryption and authentication.
20    ///
21    /// [`EncryptedPassportElement`]:
22    /// crate::types::EncryptedPassportElement
23    pub data: String, // TODO: check base64 type
24
25    /// Base64-encoded data hash for data authentication.
26    pub hash: String,
27
28    /// A base64-encoded secret, encrypted with the bot's public RSA key,
29    /// required for data decryption.
30    pub secret: String,
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn must_serialize_encrypted_credentials_to_json() {
39        // given
40        let expected_json = r#"
41        {
42            "data":"someData",
43            "hash":"1122",
44            "secret":"secret"
45        }"#
46        .replace('\n', "")
47        .replace(' ', "");
48        let encrypted_credentials = EncryptedCredentials {
49            data: "someData".to_string(),
50            hash: "1122".to_string(),
51            secret: "secret".to_string(),
52        };
53        // when
54        let actual_json = serde_json::to_string(&encrypted_credentials).unwrap();
55        //then
56        assert_eq!(actual_json, expected_json)
57    }
58}