WebApp
in package
HMAC-SHA256-based standard WebApp signature validation and init data parsing.
Port of upstream aiogram/utils/web_app.py.
This variant uses the bot token to derive the HMAC secret and verify the
hash field in the init data. Use this on the bot server where the token
is available.
Table of Contents
Methods
- checkSignature() : bool
- Verify the HMAC-SHA256 signature of WebApp init data.
- parseInitData() : WebAppInitData
- Parse a WebApp init data query string into a {@see WebAppInitData} DTO.
- safeParseInitData() : WebAppInitData
- Parse and validate WebApp init data in one step.
- __construct() : mixed
- parseQuery() : array<string, string>
- Parse a URL-encoded query string into a string-keyed assoc array, preserving the literal key names (no `.` or space mangling). Mirrors Python's `urllib.parse.parse_qsl(strict_parsing=True)`.
Methods
checkSignature()
Verify the HMAC-SHA256 signature of WebApp init data.
public
static checkSignature(string $token, string $initData) : bool
The algorithm:
- Parse the query string.
- Remove
hash(the signature to verify) andsignature(Ed25519). - Sort remaining fields alphabetically.
- Build data-check string as "key=value" pairs joined by newlines.
- Derive HMAC key: HMAC-SHA256("WebAppData", token).
- Compute HMAC-SHA256(secret, data_check_string).
- Compare with the received hash using constant-time comparison.
Parameters
- $token : string
-
the bot token (e.g. "123456:ABC-DEF...")
- $initData : string
-
the raw WebApp init data query string
Return values
bool —true if the signature is valid, false otherwise
parseInitData()
Parse a WebApp init data query string into a {@see WebAppInitData} DTO.
public
static parseInitData(string $initData) : WebAppInitData
Any value that starts with [ or { is auto-decoded as JSON before DTO
construction, mirroring upstream web_app.py:parse_webapp_init_data:
if value.startswith(('[', '{')):
value = json.loads(value)
Known structured fields (user, receiver, chat) are additionally
converted to typed DTOs.
Parameters
- $initData : string
-
the raw WebApp init data query string
Tags
Return values
WebAppInitDatasafeParseInitData()
Parse and validate WebApp init data in one step.
public
static safeParseInitData(string $token, string $initData) : WebAppInitData
Calls self::checkSignature() first and throws if the signature is invalid, then delegates to self::parseInitData().
Parameters
- $token : string
-
the bot token
- $initData : string
-
the raw WebApp init data query string
Tags
Return values
WebAppInitData__construct()
private
__construct() : mixed
parseQuery()
Parse a URL-encoded query string into a string-keyed assoc array, preserving the literal key names (no `.` or space mangling). Mirrors Python's `urllib.parse.parse_qsl(strict_parsing=True)`.
private
static parseQuery(string $input) : array<string, string>
Parameters
- $input : string