Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ValidatesWebData trait #562

Merged
merged 15 commits into from
Sep 8, 2023
Merged

Add ValidatesWebData trait #562

merged 15 commits into from
Sep 8, 2023

Conversation

Lukasss93
Copy link
Member
@Lukasss93 Lukasss93 commented Sep 6, 2023

TODO

  • validateWebAppData
  • validateLoginData
  • Exception
  • Entities
  • real tests
    • webapp
    • login
  • pest tests

How to use

// $initData MUST BE a query string value like this: 
// "user=%7B%22id%22%3A12345678%2C%22first_name%22%3A%22Mario%22%2C%22last_name%22%3A%22Super%22%2C%22username%22%3A%22SuperMario%22%2C%22language_code%22%3A%22en%22%2C%22is_premium%22%3Atrue%2C%22allows_write_to_pm%22%3Atrue%7D&chat_instance=-123456789&chat_type=private&start_param=foo&auth_date=1693264973&hash=1a2b3c4d5e6f"

// how to get $initData from frontend (example): 
// $initData = $_GET['initData'] or $_POST['initData']; //initData is an example key

try {
	$webappData = $bot->validateWebAppData($initData);
	//$webappData->user->id
	//$webappData->toArray()['user']['id']
} catch (InvalidDataException) {
	echo 'Invalid data!';
}
/* 
$initData MUST BE a query string value like this: 
"id=12345678&first_name=Mario&last_name=Super&username=SuperMario&photo_url=photourl&auth_date=1693264973&hash=1a2b3c4d5e6f"

how to get $initData from frontend: 
$initData = http_build_query([
    'id'=>$_POST['id'],
    'first_name'=>$_POST['first_name'],
    'last_name'=>$_POST['last_name'],
    'username'=>$_POST['username'],
    'photo_url'=>$_POST['photo_url'],
    'auth_date'=>$_POST['auth_date'],
    'hash'=>$_POST['hash'],
]);
*/

try {
	$loginData = $bot->validateLoginData($initData);
	//$loginData->id
	//$loginData->toArray()['id']
} catch (InvalidDataException) {
	echo 'Invalid data!';
}

Question

How to implement this PR?

Variant 1 (boolean) ❌

  • isWebAppDataValid and isLoginDataValid will return a boolean value
  • you need to call getWebAppDataAsArray and getIsLoginDataAsArray after validation to read parsed data

Variant 2 (exception) ✅

  • rename isWebAppDataValid and isLoginDataValid to validateWebAppData and validateLoginData
  • validateWebAppData and validateLoginData will return the parsed data otherwise an InvalidDataException

@Lukasss93 Lukasss93 added the enhancement New feature or request label Sep 6, 2023
@Lukasss93 Lukasss93 self-assigned this Sep 6, 2023
@Lukasss93 Lukasss93 added help wanted Extra attention is needed question Further information is requested wip Work in progress labels Sep 6, 2023
@CrazyTapok-bit
Copy link

Hi @Lukasss93, could we discuss connecting our tgWebValid library to provide the user validation functionality you need? We have support for working with several bots and all necessary tests

@Lukasss93
Copy link
Member Author

Hi @Lukasss93, could we discuss connecting our tgWebValid library to provide the user validation functionality you need? We have support for working with several bots and all necessary tests

Hello, thanks for the support (I use your library too).
We have a small rule: to have as few dependencies as possible.
Anyway, I'd like some feedback from @sergix44 to understand what they think.

@CrazyTapok-bit
Copy link

Hello, thanks for the support (I use your library too). We have a small rule: to have as few dependencies as possible. Anyway, I'd like some feedback from @sergix44 to understand what they think.

Of course, everyone's opinion is important. And I will accept any decision you make, but could I somehow influence it so that it would be positive, so that you could focus most of all on the development of the functionality of the bot, and I would take care of the rest. If you have any conditions, please tell me, let's talk

@sergix44
Copy link
Member
sergix44 commented Sep 7, 2023

TODO

  • isWebAppDataValid

  • isLoginDataValid

  • getWebAppDataAsArray

  • getIsLoginDataAsArray

  • real tests

    • webapp
    • login
  • pest tests?

How to use

// $initData MUST BE a query string value like this: 
// "user=%7B%22id%22%3A12345678%2C%22first_name%22%3A%22Mario%22%2C%22last_name%22%3A%22Super%22%2C%22username%22%3A%22SuperMario%22%2C%22language_code%22%3A%22en%22%2C%22is_premium%22%3Atrue%2C%22allows_write_to_pm%22%3Atrue%7D&chat_instance=-123456789&chat_type=private&start_param=foo&auth_date=1693264973&hash=1a2b3c4d5e6f"

// how to get $initData from frontend (example): 
// $initData = $_GET['initData'] or $_POST['initData']; //initData is an example key

$bot->isWebAppDataValid($initData); // true or false
/* 
$initData MUST BE a query string value like this: 
"id=12345678&first_name=Mario&last_name=Super&username=SuperMario&photo_url=photourl&auth_date=1693264973&hash=1a2b3c4d5e6f"

how to get $initData from frontend: 
$initData = http_build_query([
    'id'=>$_POST['id'],
    'first_name'=>$_POST['first_name'],
    'last_name'=>$_POST['last_name'],
    'username'=>$_POST['username'],
    'photo_url'=>$_POST['photo_url'],
    'auth_date'=>$_POST['auth_date'],
    'hash'=>$_POST['hash'],
]);
*/

$bot->isLoginDataValid($initData); // true or false

Question

How to implement this PR?

Variant 1 (boolean)

  • isWebAppDataValid and isLoginDataValid will return a boolean value
  • you need to call getWebAppDataAsArray and getIsLoginDataAsArray after validation to read parsed data

Variant 2 (exception)

  • rename isWebAppDataValid and isLoginDataValid to validateWebAppData and validateLoginData
  • validateWebAppData and validateLoginData will return the parsed data otherwise an InvalidDataException

2️⃣

Hello, thanks for the support (I use your library too). We have a small rule: to have as few dependencies as possible. Anyway, I'd like some feedback from @sergix44 to understand what they think.

Of course, everyone's opinion is important. And I will accept any decision you make, but could I somehow influence it so that it would be positive, so that you could focus most of all on the development of the functionality of the bot, and I would take care of the rest. If you have any conditions, please tell me, let's talk

For now I would keep the implementation as small as possible, if in the future there is a need for additional features, such as multi bot, the introduction of third-party libraries may be considered.

@Lukasss93 Lukasss93 changed the title Add InteractWithWeb trait Add ValidatesWebData trait Sep 7, 2023
@CrazyTapok-bit
Copy link
CrazyTapok-bit commented Sep 7, 2023

For now I would keep the implementation as small as possible, if in the future there is a need for additional features, such as multi bot, the introduction of third-party libraries may be considered.

Please note that this is not only about multibot. The library allows you to test both types of users (Telegram Login Widget and Telegram Web App) out of the box. Also, after parsing and checking the data, entities are created so that the user can get all the necessary data and later manipulate, store, and so on. And these entities have 100% autocomplete, which is very convenient for other developers. Another advantage is the ability to retrieve data as an array (which is what @Lukasss93 is trying to achieve in this enhancement). Your library can get all this in a matter of minutes.

Of course, it's up to you to decide, but if you let me show you how it would look and feel...

@Lukasss93 Lukasss93 removed help wanted Extra attention is needed question Further information is requested wip Work in progress labels Sep 8, 2023
@Lukasss93 Lukasss93 marked this pull request as ready for review September 8, 2023 19:39
@Lukasss93 Lukasss93 requested a review from sergix44 as a code owner September 8, 2023 19:39
@codeclimate
Copy link
codeclimate bot commented Sep 8, 2023

Code Climate has analyzed commit d6b7150 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2

The test coverage on the diff in this pull request is 100.0% (60% is the threshold).

This pull request will bring the total coverage in the repository to 96.9% (0.1% change).

View more on Code Climate.

@Lukasss93 Lukasss93 linked an issue Sep 8, 2023 that may be closed by this pull request
@Lukasss93 Lukasss93 merged commit 55943aa into master Sep 8, 2023
@Lukasss93 Lukasss93 deleted the interact-with-web branch September 8, 2023 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User validation
3 participants