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
//! A rust implementation of the verification of Telegram Login requests.
//!
//! Based on the example from the [Telegram docs](https://core.telegram.org/widgets/login#checking-authorization).
//!
//! # Examples:
//!
//! ```
//! extern crate chrono;
//! extern crate telegram_login;
//!
//! use chrono::NaiveDateTime;
//! use telegram_login::{TelegramLogin, TelegramLoginError, check_signature};
//!
//! let t_l = TelegramLogin {
//!   id: 666666666,
//!   username: Some("my_username".to_string()),
//!   first_name: Some("Some".to_string()),
//!   last_name: Some("Guy".to_string()),
//!   photo_url: Some("https://t.me/i/userpic/320/my_username.jpg".to_string()),
//!   auth_date: NaiveDateTime::from_timestamp(1543194375, 0),
//!   hash: "a9cf12636fb07b54b4c95673d017a72364472c41a760b6850bcd5405da769f80".to_string()
//! };
//!
//! let bot_token = "777777777:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string();
//!
//! match check_signature(bot_token, t_l) {
//!   Ok(()) => {
//!     // The login is valid, so we can log the user in.
//!   }
//!   Err(TelegramLoginError::InvalidHash) => {
//!     // The login failed, so we need to return an error to the client.
//!   }
//!   Err(TelegramLoginError::VerificationFailed) => {
//!     // The login failed, so we need to return an error to the client.
//!   }
//! }
//! ```

extern crate chrono;
extern crate hex;
extern crate ring;
extern crate serde;

#[macro_use]
extern crate serde_derive;

use ring::{digest, hmac};

///
/// The Telegram Login data object that is returned from the Telegram Auth endpoint.
///
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TelegramLogin {
    pub id: i32,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub username: Option<String>,
    pub photo_url: Option<String>,
    pub hash: String,
    #[serde(with = "unix_epoch")]
    pub auth_date: chrono::NaiveDateTime,
}

///
/// Custom Serde implementation for the auth_date param
///
mod unix_epoch {
    use serde::{self, Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(date: &chrono::NaiveDateTime, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = format!("{}", date.timestamp());
        serializer.serialize_str(&s)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<chrono::NaiveDateTime, D::Error>
    where
        D: Deserializer<'de>,
    {
        match String::deserialize(deserializer) {
            Ok(s) => match s.parse::<i64>() {
                Ok(num_secs) => Ok(chrono::NaiveDateTime::from_timestamp(num_secs, 0)),
                Err(_e) => Err(serde::de::Error::custom(
                    "auth-date param must be a unix epoche (int).",
                )),
            },
            Err(_e) => Err(serde::de::Error::custom("auth-date param malformed.")),
        }
    }
}

///
/// There are 2 ways in which the `check_signature` method can fail.
///
#[derive(Clone, Debug, PartialEq)]
pub enum TelegramLoginError {
    InvalidHash,
    VerificationFailed,
}

///
/// Verifies that the hash in the Telegram auth object is valid.
///
/// The algorithm from the Telegram docs:
///
///  - secret_key = SHA256(<bot_token>)
///  - hex(HMAC_SHA256(data_check_string, secret_key)) == hash
///
/// # Examples:
///
/// ```
/// extern crate chrono;
/// extern crate telegram_login;
///
/// use chrono::NaiveDateTime;
/// use telegram_login::{TelegramLogin, TelegramLoginError, check_signature};
///
/// let t_l = TelegramLogin {
///   id: 666666666,
///   username: Some("my_username".to_string()),
///   first_name: Some("Some".to_string()),
///   last_name: Some("Guy".to_string()),
///   photo_url: Some("https://t.me/i/userpic/320/my_username.jpg".to_string()),
///   auth_date: NaiveDateTime::from_timestamp(1543194375, 0),
///   hash: "a9cf12636fb07b54b4c95673d017a72364472c41a760b6850bcd5405da769f80".to_string()
/// };
///
/// let bot_token = "777777777:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string();
///
/// match check_signature(bot_token, t_l) {
///   Ok(()) => {
///     // The login is valid, so we can log the user in.
///   }
///   Err(TelegramLoginError::InvalidHash) => {
///     // The login failed, so we need to return an error to the client.
///   }
///   Err(TelegramLoginError::VerificationFailed) => {
///     // The login failed, so we need to return an error to the client.
///   }
/// }
/// ```
pub fn check_signature(bot_token: String, user: TelegramLogin) -> Result<(), TelegramLoginError> {
    match hex::decode(&user.hash) {
        Ok(hash) => {
            let data_check_string = gen_check_string(user);
            let secret_key = digest::digest(&digest::SHA256, &bot_token.as_bytes());
            let v_key = hmac::VerificationKey::new(&digest::SHA256, secret_key.as_ref());
            hmac::verify(&v_key, data_check_string.as_bytes(), &hash)
                .map_err(|_e| TelegramLoginError::VerificationFailed)
        }
        Err(_e) => Err(TelegramLoginError::InvalidHash),
    }
}

/// Converts the Struct object received from Telegram into the data_check_string as required
/// in the verification process.
///
/// From the Telegram docs:
///
/// - Data-check-string is a concatenation of all received fields, sorted in alphabetical order,
///  in the format key=<value> with a line feed character ('\n', 0xA0) used as separator
///
fn gen_check_string(data: TelegramLogin) -> String {
    struct Field<A> {
        field: String,
        value: Option<A>,
    }

    // Put the key, value pairs in order
    let fields = vec![
        Field {
            field: "auth_date".to_string(),
            value: Some(data.auth_date.timestamp().to_string()),
        },
        Field {
            field: "first_name".to_string(),
            value: data.first_name,
        },
        Field {
            field: "id".to_string(),
            value: Some(data.id.to_string()),
        },
        Field {
            field: "last_name".to_string(),
            value: data.last_name,
        },
        Field {
            field: "photo_url".to_string(),
            value: data.photo_url,
        },
        Field {
            field: "username".to_string(),
            value: data.username,
        },
    ];

    let mut result = fields
        .into_iter()
        .fold("".to_string(), |acc, f| match f.value {
            Some(val) => format!("{}{}={}\n", acc, f.field, val),
            None => acc,
        });

    // Remove the final "\n" before returning the result
    result.pop();
    result
}

#[cfg(test)]
mod tests {

    use super::*;
    use chrono::NaiveDateTime;

    #[test]
    fn test_gen_check_string() {
        let t_l = TelegramLogin {
            id: 666666666,
            username: Some("my_username".to_string()),
            first_name: Some("Some".to_string()),
            last_name: Some("Guy".to_string()),
            photo_url: Some("https://t.me/i/userpic/320/my_username.jpg".to_string()),
            auth_date: NaiveDateTime::from_timestamp(1543194375, 0),
            hash: "aaaaaaaaaaaaaaaaaeeeeeeeeee4444444444444444444444444444444444444".to_string(),
        };

        assert_eq!(
            gen_check_string(t_l),
            "auth_date=1543194375\nfirst_name=Some\nid=666666666\nlast_name=Guy\nphoto_url=https://t.me/i/userpic/320/my_username.jpg\nusername=my_username"
        );
    }

    #[test]
    fn test_check_signature_success() {
        let t_l = TelegramLogin {
            id: 666666666,
            username: Some("my_username".to_string()),
            first_name: Some("Some".to_string()),
            last_name: Some("Guy".to_string()),
            photo_url: Some("https://t.me/i/userpic/320/my_username.jpg".to_string()),
            auth_date: NaiveDateTime::from_timestamp(1543194375, 0),
            hash: "a9cf12636fb07b54b4c95673d017a72364472c41a760b6850bcd5405da769f80".to_string(),
        };

        assert_eq!(
            check_signature(
                "777777777:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
                t_l
            ),
            Ok(())
        );
    }

    #[test]
    fn test_check_signature_err_verification_failed() {
        let t_l = TelegramLogin {
            id: 666666666,
            username: Some("my_username".to_string()),
            first_name: Some("Some".to_string()),
            last_name: Some("Guy".to_string()),
            photo_url: Some("https://t.me/i/userpic/320/my_username.jpg".to_string()),
            auth_date: NaiveDateTime::from_timestamp(1543194375, 0),
            hash: "aaaaaaaaaaaaaaaaaeeeeeeeeee4444444444444444444444444444444444444".to_string(),
        };

        assert_eq!(
            check_signature(
                "777777777:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
                t_l
            ),
            Err(TelegramLoginError::VerificationFailed)
        );
    }

    #[test]
    fn test_check_signature_err_invalid_hash() {
        let t_l = TelegramLogin {
            id: 666666666,
            username: Some("my_username".to_string()),
            first_name: Some("Some".to_string()),
            last_name: Some("Guy".to_string()),
            photo_url: Some("https://t.me/i/userpic/320/my_username.jpg".to_string()),
            auth_date: NaiveDateTime::from_timestamp(1543194375, 0),
            // Not HEX chars here
            hash: "xxxxxxxaaaaaaaaaaeeeeeeeeee4444444444444444444444444444444444444".to_string(),
        };

        assert_eq!(
            check_signature(
                "777777777:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string(),
                t_l
            ),
            Err(TelegramLoginError::InvalidHash)
        );
    }
}