url_signer — HMAC-signed URLs for Mini Apps
UrlSignerService
UrlSignerService
Service for securing Telegram Mini App URLs via HMAC-SHA256 signatures.
This service generates tamper-proof URLs by appending a cryptographic signature based on a request identifier and a timestamp. It is essential for protecting backend endpoints from unauthorized access outside the intended user session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret_key
|
str
|
The cryptographic key used for HMAC signature generation. |
required |
Source code in src/codex_bot/url_signer/service.py
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 | |
Functions
generate_signed_url(base_url, request_id, action='reply')
Generate a cryptographically signed URL for Telegram Mini Apps.
Constructs a URL including a request identifier, current timestamp, and an HMAC-SHA256 signature calculated over the combined payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
The root domain of the Mini App host. |
required |
request_id
|
str | int
|
A unique identifier for the specific request or entity. |
required |
action
|
str
|
The logical path component identifying the target action. |
'reply'
|
Returns:
| Type | Description |
|---|---|
str
|
A fully qualified URL with integrated security parameters. |
Source code in src/codex_bot/url_signer/service.py
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 | |
verify_signed_url(req_id, timestamp, signature, max_age=300)
Verifies the URL signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
req_id
|
str
|
Request ID from the |
required |
timestamp
|
str
|
Timestamp from the |
required |
signature
|
str
|
Signature from the |
required |
max_age
|
int
|
Maximum signature age in seconds (default is 300). |
300
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
is_valid = signer.verify_signed_url(
req_id=request.query_params["req_id"],
timestamp=request.query_params["ts"],
signature=request.query_params["sig"],
)
Source code in src/codex_bot/url_signer/service.py
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 | |