garphild/auth-telegram

Telegram认证

1.0.5 2020-11-07 11:37 UTC

This package is auto-updated.

Last update: 2024-09-07 20:42:21 UTC


README

PHP用于Telegram登录小部件的包装器。Telegram登录小部件是授权用户访问您网站的一种简单方法。

有用的链接

功能

  • 异步/同步加载
  • 不同按钮大小
  • 用户的照片
  • 可配置按钮圆角
  • 认证结果可以发送到回调或重定向URL
  • 获取写入用户权限

构造器参数

  • $botName: 字符串 - 机器人名称
  • $botKey: 字符串 - 机器人认证密钥
  • $cookieName: 字符串 - 存储认证数据的cookie名称
  • $config: 字符串 - 小部件的配置数组

配置选项

  • size: 字符串 enum('large', 'medium', 'small') - 按钮大小
  • userPhoto: 布尔值 - 在认证结果中获取用户的照片
  • cornerRadius: 数字 - 按钮圆角半径
  • resultActionType: 字符串 enum('callback', 'url') - 按钮圆角半径
  • resultAction: 字符串 - 回调JavaScript函数名称或重定向URL
  • requestWrite: 布尔值 - 允许获取写入消息到用户权限
  • async: 布尔值 - 允许异步脚本加载
  • maxAge: 数字 - 认证数据的有效最大年龄

公共属性

  • user TelegramUserModel - 认证用户数据或null

公共方法

  • isAuthentificated(): boolean - 检查用户是否已认证(基于cookie)
  • getWidget(): string - 获取小部件HTML代码
  • logOut(): void - 清除cookie和用户数据
  • setUser(TelegramUserModel $user): void - 设置当前用户
  • clearUser(): void - 清除当前用户
  • checkTelegramAuthorization($auth_data): void - 检查来自Telegram的数据

用法

基本用法

require_once "./vendor/autoload.php";

use Garphild\AuthTelegram\TelegramAuthentificator;

define('BOT_USERNAME', 'XXXXXXX'); // place username of your bot here
define('BOT_KEY', 'XXXXX:XXXXXXXXX'); // place @botFather key of your bot here
define('BOT_COOKIE_NAME', 'XXXXXX'); // place cookie name to store data
$config = [
  ...
];
$tgAuth = new TelegramAuthentificator(BOT_USERNAME, BOT_KEY, BOT_COOKIE_NAME, $config);

重定向

查看示例文件夹。这是简单的示例。不要在生产环境中使用。使用会话来存储数据或JWT密钥或任何安全方法。

login_example.php

require_once "./vendor/autoload.php";

use Garphild\AuthTelegram\TelegramAuthentificator;

define('BOT_USERNAME', 'XXXXXXX'); // place username of your bot here
define('BOT_KEY', 'XXXXX:XXXXXXXXX'); // place @botFather key of your bot here
define('BOT_COOKIE_NAME', 'XXXXXX'); // place cookie name to store data
$config = [
  'resultActionType' => 'url',
  'resultAction' => 'check_authorization.php',
  ...
];
$tgAuth = new TelegramAuthentificator(BOT_USERNAME, BOT_KEY, BOT_COOKIE_NAME, $config);
if (isset($_COOKIE["tg_user"])) {
    $tgAuth->setUser(new TelegramUserModel(json_decode($_COOKIE["tg_user"], true)));
}

if ($_GET['logout']) {
  $tgAuth->logOut();
  setcookie("tg_user", null);
}

if ($tgAuth->isAuthentificated()) {
    ... user authentificated ...
} else {
    ... not authentificated ...
    echo $tgAuth->getWidget();
}

check_authorization.php

require_once "./vendor/autoload.php";

use Garphild\AuthTelegram\TelegramAuthentificator;

define('BOT_USERNAME', 'XXXXXXX'); // place username of your bot here
define('BOT_KEY', 'XXXXX:XXXXXXXXX'); // place @botFather key of your bot here
define('BOT_COOKIE_NAME', 'XXXXXX'); // place cookie name to store data
$config = [
  ...
];
$tgAuth = new TelegramAuthentificator(BOT_USERNAME, BOT_KEY, BOT_COOKIE_NAME, $config);

try {
  $tgAuth->logIn($_GET);
  setcookie("tg_user", json_encode($tgAuth->user));
} catch (Exception $e) {
  die ($e->getMessage());
}

header('Location: login_example.php');

登录成功后,用户信息作为 $tgAuth->user 可用。