定制 tg/tgwebvalid 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tg/tgwebvalid

Composer 安装命令:

composer require tg/tgwebvalid

包简介

An easy way to validate Telegram Login Widget and Telegram Mini App users on your website using PHP

README 文档

README

Testing Status Minimum PHP Version Latest version Downloads License

Documentation StandWithUkraine StandWithUkraine

StandWithUkraine

About TgWebValid

They say we are cool 😎

⭐️ Support us, give us a star on GitHub and become our sponsor 😊

🙏 Please let us know on GitHub if something isn't working for you or if you need additional functionality

User authentication occurs by encrypting the received, raw, user data and comparing it with the hash provided by the telegram. A failed check can be equated with a possible attempt to bypass or hack the system.

The library verifies users Telegram Login Widget and Telegram Web App

WARNING: Use user data only after successful authentication

To quickly and safely verify a user, we recommend following a few simple steps

Installation

You can install the TgWebValid library through the composer package manager by executing the command

composer require tg/tgwebvalid

Add the --no-dev flag to install only the dependencies needed to run your project in a production environment.

Using

The first thing you need to do is to set in the constructor of the TgWebValid class the token of the Telegram bot on behalf of which authentication is performed by default. And store the result in a variable.

Also, if you want to throw an exception in case of a validation error, set the second parameter to true. But be sure to use the try catch structure

<?php

use TgWebValid\TgWebValid;

include './vendor/autoload.php';

$tgWebValid = new TgWebValid('TELEGRAM_BOT_TOKEN', false);

If your project uses multiple bots, you can easily interact with them, just add them all

<?php

$tgWebValid->addBot('secondary', 'TELEGRAM_BOT_TOKEN_2');
$tgWebValid->addBot('minor', 'TELEGRAM_BOT_TOKEN_3');

Getting a bot to work is easy. Specify the name of the bot to work with, or leave the argument empty to get the default bot

$bot = $tgWebValid->bot('minor');

Next, you need to decide on the type of authentication you need to do.

Telegram Web App authentication

To perform this type of verification, you should use the validateInitData method. Which argument accepts data for processing. If the validation is successful, you will be returned an InitData object with the data, or false if the validation fails.

Use the second argument to enable or disable an exception on failed validation

$initData = $bot->validateInitData('query_id=...');

if (!$initData) {
    // validation fails
}

/**
 * The initData object can contain the following data:
 */

// Time opening a web application
$initData->authDate;

// An object containing data about the current user
$initData->user;

// May contain a chat partner data object
$initData->receiver;

// May contain an object with chat data
$initData->chat;

// and other data

Note. Certain data is present depending on the situation, so sometimes it can be null instead of data or a data object. More details in the Telegram official documentation

Telegram Login Widget authentication

To perform this type of check, you should use the validateLoginWidget method. Which argument accepts an array with raw user data. You will be returned a LoginWidget object with the data, or false if the validation fails

Use the second argument to enable or disable an exception on failed validation

$loginWidget = $bot->validateLoginWidget([
    'auth_date' => 1679130118,
    'first_name' => 'Сергій',
    // other fields
]);

if (!$loginWidget) {
    // validation fails
}

/**
 * The LoginWidget object can contain the following data:
 */

// User token
$loginWidget->id;

// User first name
$loginWidget->firstName;

// Username
$loginWidget->username;

// Link to profile photo
$loginWidget->photoUrl;

// Authorization time
$loginWidget->authDate;

// and other data

Note. Certain data is present depending on the situation, so sometimes it can be null instead of data or a data object.

Full example

<?php

use TgWebValid\TgWebValid;
use TgWebValid\Exceptions\BotException;
use TgWebValid\Exceptions\ValidationException;
use Exception;

include './vendor/autoload.php';

try {
    $tgWebValid = new TgWebValid('TELEGRAM_BOT_TOKEN', true);

    // Add bots only when needed
    $tgWebValid->addBot('secondary', 'TELEGRAM_BOT_TOKEN_2');
    $tgWebValid->addBot('minor', 'TELEGRAM_BOT_TOKEN_3');

    $initData = $tgWebValid->bot()->validateInitData('query_id=...');

    var_dump($initData);

} catch (ValidationException $e) {
    // Verification failed
} catch (BotException $e) {
    // The bot name is incorrect
} catch (Exception $e) {
    // Other exceptions
}

Additionally

Our library is autonomous, so it can be used in any frameworks, or without them.

Security

If you discover a security vulnerability in TgWebValid, please create an issue with a detailed description. All security vulnerabilities will be fixed immediately. Pull requests are also welcome.

Assistance

We will be glad if you join the development and improvement of the project. You can create an issue and/or a pull request

License

TgWebValid - is open source software available under the MIT. See the license file for more information.

tg/tgwebvalid 适用场景与选型建议

tg/tgwebvalid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.6k 次下载、GitHub Stars 达 68, 最近一次更新时间为 2023 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「authorization」 「Authentication」 「validation」 「login」 「auth」 「verification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tg/tgwebvalid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tg/tgwebvalid 我们能提供哪些服务?
定制开发 / 二次开发

基于 tg/tgwebvalid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 68
  • 点击次数: 30
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 68
  • Watchers: 2
  • Forks: 21
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-05