Function validate

Source
pub fn validate(
    init_data: &str,
    token: &str,
    expires_in: Option<u64>,
) -> Result<InitData, InitDataError>
Expand description

Validates the authenticity and integrity of Telegram Mini Apps init data.

This function performs several checks:

  1. Validates the format of the init data
  2. Extracts and verifies the hash
  3. Checks the data hasn’t expired
  4. Parses the data into a strongly-typed structure

§Arguments

  • init_data - Raw init data string from Telegram Mini App
  • token - Bot token used for validation
  • expires_in - Optional expiration time in seconds (defaults to 24 hours), set to 0 to disable expiration check

§Returns

  • Ok(InitData) - Parsed and validated init data
  • Err(InitDataError) - Various validation or parsing errors

§Example

use init_data_rs::validate;

let init_data = "query_id=123&auth_date=1662771648&hash=...";
let result = validate(init_data, "BOT_TOKEN", None);

§Errors

See init_data_rs::parse for possible errors

§Panics

This function panics if SystemTime::now returns a date less than UNIX_EPOCH. Meaning the function should panic only if the device time is really, REALLY bad.