#![forbid(unsafe_code)]
#![forbid(clippy::exit)]
#![deny(clippy::pattern_type_mismatch)]
#![warn(
clippy::future_not_send,
clippy::exhaustive_enums,
clippy::exhaustive_structs,
clippy::must_use_unit,
clippy::missing_inline_in_public_items,
clippy::must_use_candidate
)]
use hmac::{Hmac, Mac};
use serde::Deserialize;
use sha2::Sha256;
use url::Url;
use std::{
collections::HashMap,
time::{Duration, SystemTime, SystemTimeError, UNIX_EPOCH},
};
type HmacSha256 = Hmac<Sha256>;
#[derive(Debug, PartialEq, Deserialize)]
pub struct InitData {
pub auth_date: u64,
pub can_send_after: Option<u64>,
pub chat: Option<Chat>,
pub chat_type: Option<String>,
pub chat_instance: Option<String>,
pub hash: String,
pub query_id: String,
pub receiver: Option<User>,
pub start_param: Option<String>,
pub user: Option<User>,
}
#[derive(Debug, PartialEq, Deserialize)]
pub struct User {
pub added_to_attachment_menu: Option<bool>,
pub allows_write_to_pm: Option<bool>,
pub is_premium: Option<bool>,
pub first_name: String,
pub id: i64,
pub is_bot: Option<bool>,
pub last_name: Option<String>,
pub language_code: Option<String>,
pub photo_url: Option<String>,
pub username: Option<String>,
}
#[derive(Debug, PartialEq, Deserialize)]
pub struct Chat {
pub id: i64,
pub r#type: String,
pub title: String,
pub photo_url: Option<String>,
pub username: Option<String>,
}
#[derive(Debug)]
pub enum ParseDataError {
InvalidSignature(serde_json::Error),
InvalidQueryString(url::ParseError),
}
pub fn parse(init_data: String) -> Result<InitData, ParseDataError> {
let url = Url::parse(&format!("http://dummy.com?{}", init_data))
.map_err(ParseDataError::InvalidQueryString)?;
static STRING_PROPS: phf::Set<&'static str> = phf::phf_set! {
"start_param",
};
let mut pairs = Vec::new();
for (key, value) in url.query_pairs() {
let val = value.to_string();
let formatted_pair = if STRING_PROPS.contains(key.as_ref()) {
format!("\"{}\":\"{}\"", key, val)
} else {
if serde_json::from_str::<serde_json::Value>(&val).is_ok() {
format!("\"{}\":{}", key, val)
} else {
format!("\"{}\":\"{}\"", key, val)
}
};
pairs.push(formatted_pair);
}
let json_str = format!("{{{}}}", pairs.join(","));
serde_json::from_str(&json_str).map_err(ParseDataError::InvalidSignature)
}
#[derive(Debug)]
pub enum SignError {
CouldNotProcessSignature,
CouldNotProcessAuthTime(SystemTimeError),
InvalidQueryString(url::ParseError),
}
pub fn sign(
payload: HashMap<String, String>,
bot_token: String,
auth_time: SystemTime,
) -> Result<String, SignError> {
let mut pairs = payload
.iter()
.filter_map(|(k, v)| {
if k == "hash" || k == "auth_date" {
None
} else {
Some(format!("{}={}", k, v))
}
})
.collect::<Vec<String>>();
let auth_date = auth_time
.duration_since(UNIX_EPOCH)
.map_err(SignError::CouldNotProcessAuthTime)?
.as_secs();
pairs.push(format!("auth_date={}", auth_date));
pairs.sort();
let payload = pairs.join("\n");
let mut sk_hmac = HmacSha256::new_from_slice("WebAppData".as_bytes())
.map_err(|_| SignError::CouldNotProcessSignature)?;
sk_hmac.update(bot_token.as_bytes());
let secret_key = sk_hmac.finalize().into_bytes();
let mut imp_hmac =
HmacSha256::new_from_slice(&secret_key).map_err(|_| SignError::CouldNotProcessSignature)?;
imp_hmac.update(payload.as_bytes());
let result = imp_hmac.finalize().into_bytes();
Ok(hex::encode(result))
}
pub fn sign_query_string(
qs: String,
bot_token: String,
auth_time: SystemTime,
) -> Result<String, SignError> {
let url =
Url::parse(&format!("http://dummy.com?{}", qs)).map_err(SignError::InvalidQueryString)?;
let mut params: HashMap<String, String> = HashMap::new();
for (key, value) in url.query_pairs() {
params.insert(key.to_string(), value.to_string());
}
sign(params, bot_token, auth_time)
}
#[derive(Debug)]
pub enum ValidationError {
InvalidQueryString(url::ParseError),
UnexpectedFormat,
SignMissing,
AuthDateMissing,
Expired,
SignInvalid,
}
pub fn validate(
init_data: String,
bot_token: String,
exp_in: Duration,
) -> Result<bool, ValidationError> {
let url = Url::parse(&format!("http://dummy.com?{}", init_data))
.map_err(ValidationError::InvalidQueryString)?;
let mut auth_date: Option<SystemTime> = None;
let mut hash: Option<String> = None;
let mut pairs = Vec::new();
for (key, value) in url.query_pairs() {
if key == "hash" {
hash = Some(value.to_string());
continue;
}
if key == "auth_date" {
if let Ok(timestamp) = value.parse::<u64>() {
auth_date = Some(UNIX_EPOCH + Duration::from_secs(timestamp));
}
}
pairs.push(format!("{}={}", key, value));
}
let hash = hash.ok_or(ValidationError::SignMissing)?;
if exp_in != Duration::from_secs(0) {
let auth_date = auth_date.ok_or(ValidationError::AuthDateMissing)?;
if auth_date + exp_in < SystemTime::now() {
return Err(ValidationError::Expired);
}
}
pairs.sort();
let calculated_hash = sign_query_string(init_data, bot_token, auth_date.unwrap_or(UNIX_EPOCH))
.map_err(|_| ValidationError::UnexpectedFormat)?;
if calculated_hash != hash {
return Err(ValidationError::SignInvalid);
}
Ok(true)
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn test_parse_valid_data() {
let init_data = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2&start_param=abc";
let result = parse(init_data.to_string());
assert!(result.is_ok());
let data = result.unwrap();
assert_eq!(
data,
InitData {
auth_date: 1662771648,
can_send_after: None,
chat: None,
chat_type: None,
chat_instance: None,
hash: "c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2"
.to_string(),
query_id: "AAHdF6IQAAAAAN0XohDhrOrc".to_string(),
receiver: None,
start_param: Some("abc".to_string()),
user: Some(User {
added_to_attachment_menu: None,
allows_write_to_pm: None,
is_premium: Some(true),
first_name: "Vladislav".to_string(),
id: 279058397,
is_bot: None,
last_name: Some("Kibenko".to_string()),
language_code: Some("ru".to_string()),
photo_url: None,
username: Some("vdkfrost".to_string())
})
}
);
}
#[test]
fn test_parse_invalid_data() {
let init_data = "invalid data";
let result = parse(init_data.to_string());
assert!(result.is_err());
}
#[test]
fn test_sign_query_string() {
let qs = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2".to_string();
let test_bot_token = "5768337691:AAH5YkoiEuPk8-FZa32hStHTqXiLPtAEhx8".to_string();
let test_sign_hash =
"c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2".to_string();
let auth_time = SystemTime::UNIX_EPOCH + Duration::from_secs(1662771648);
let result = sign_query_string(qs, test_bot_token, auth_time).unwrap();
assert_eq!(result, test_sign_hash);
}
#[test]
fn test_sign_query_string_no_date() {
let qs = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D".to_string();
let test_bot_token = "5768337691:AAH5YkoiEuPk8-FZa32hStHTqXiLPtAEhx8".to_string();
let test_sign_hash =
"c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2".to_string();
let auth_time = SystemTime::UNIX_EPOCH + Duration::from_secs(1662771648);
let result = sign_query_string(qs, test_bot_token, auth_time).unwrap();
assert_eq!(result, test_sign_hash);
}
#[test]
fn test_validate_success() {
let init_data = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2".to_string();
let token = "5768337691:AAH5YkoiEuPk8-FZa32hStHTqXiLPtAEhx8".to_string();
let exp_in = Duration::from_secs(1662771648);
assert!(validate(init_data, token, exp_in).is_ok());
}
#[test]
fn test_validate_expired() {
let init_data =
"query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2.".to_string();
let token = "your_bot_token".to_string();
let exp_in = Duration::from_secs(86400);
assert!(matches!(
validate(init_data, token, exp_in),
Err(ValidationError::Expired)
));
}
#[test]
fn test_validate_missing_hash() {
let init_data = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648".to_string();
let token = "your_bot_token".to_string();
let exp_in = Duration::from_secs(86400);
assert!(matches!(
validate(init_data, token, exp_in),
Err(ValidationError::SignMissing)
));
}
}