This Python module securely validates and extracts user data from a URL-encoded string, using HMAC and SHA-256 for data integrity. It features a User class to structure user information and a Data class for validation and data parsing. Ideal for web applications needing secure user authentication and data management, it's easy to integrate and use.
Project description
Data Validation and User Extraction Module
This Python module is designed to handle the validation and extraction of user data from a URL-encoded string. It uses HMAC and SHA-256 hashing to ensure data integrity and security. The module provides a simple way to validate input data and parse it into a structured User object.
Requirements
This module requires Python 3.6+ and uses the following standard libraries:
hmachashliburllibjsondataclasses
Installation
No external libraries are required for this module. Simply include the code in your Python project.
Usage
User Class
The User class is a data class that holds the following information about a user:
id: The user's unique identifier (integer).first_name: The user's first name (string).last_name: The user's last name (string).username: The user's username (string).language_code: The language code of the user (string).is_premium: A boolean indicating if the user has a premium account.allows_write_to_pm: A boolean indicating if the user allows writing to private messages.
Data Class
The Data class is responsible for validating input data and extracting user information from a URL-encoded string.
Initialization
To create an instance of the Data class, you need to provide a bot token:
data_instance = Data(BOT_TOKEN="your_bot_token_here")
is_valid_data Method
This method checks if the provided data is valid by comparing the generated hash with the initial data hash.
is_valid = data_instance.is_valid_data(INIT_DATA_HASH="your_init_data_hash", DATA_CHECK_STRING="your_data_check_string")
GetData Method
This static method extracts user data from the data check string and returns a User object.
user = Data.GetData(DATA_CHECK_STRING="your_data_check_string")
Example
Here's an example of how to use the module:
BOT_TOKEN = "1231212:sdsadasdsadasd231ec"
INIT_DATA_HASH = "90677c2f2e1326cdb4d5cd781905c375f5397b8f6dbee501487385aa3faf3e0e"
DATA_CHECK_STRING = "auth_date=1721578504&chat_instance=-5432073769796079823&chat_type=private&user=%7B%22id%22%3A6195030789%2C%22first_name%22%3A%22._.%22%2C%22last_name%22%3A%22%22%2C%22username%22%3A%22ksdaskdnc2w%22%2C%22language_code%22%3A%22en%22%2C%22allows_write_to_pm%22%3Atrue%7D"
data_instance = Data(BOT_TOKEN)
if data_instance.is_valid_data(INIT_DATA_HASH, DATA_CHECK_STRING):
user = Data.GetData(DATA_CHECK_STRING)
print(f"User {user.first_name} ({user.username}) is authenticated.")
else:
print("Invalid data provided.")
License
This module is licensed under the MIT License. Feel free to use and modify it as needed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file verifytelegram-1.0.tar.gz.
File metadata
- Download URL: verifytelegram-1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae973306f89064e25fc25a5906af5ae9d8b683713520e9ba9e55817ea071193
|
|
| MD5 |
86d8cb523d5dc330413b700e8dace31c
|
|
| BLAKE2b-256 |
939f99ef4b46477cd2730eca78667ffd2b9208ff95f4da0f8b957c2a585749e2
|
File details
Details for the file verifyTelegram-1.0-py3-none-any.whl.
File metadata
- Download URL: verifyTelegram-1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50b14d26203d2b605963d49d6b6cda268c6cc592afd18ecc845cf32f3d0e8a5c
|
|
| MD5 |
4c1c406d09b2d31f62ae47b8d48b38f7
|
|
| BLAKE2b-256 |
1b66a7ec6e883356f01c496dea9d0c1bf3078622c8c6cada6a0d15288ce93c38
|