定制 smskin/laravel-tgwebapp-auth 二次开发

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

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

smskin/laravel-tgwebapp-auth

最新稳定版本:1.0.5

Composer 安装命令:

composer require smskin/laravel-tgwebapp-auth

包简介

Authentication guard for Telegram web app user integration

README 文档

README

Telegram WebApp Documentation

Use Case: When developing an API for a Telegram WebApp, it is necessary to verify that the user who sent the request to the API is indeed the one they claim to be (i.e., the request actually came from the Telegram WebApp).

How It Works

  1. The Telegram WebApp JS script retrieves the WebAppUser object from the API and sends it in every request to the API in the request header (the header name is configurable).
  2. The Guard receives the request and extracts the WebAppUser object from it.
  3. The Guard verifies the data signature using the BOT_TOKEN.
  4. The Guard looks for the user in the database:
    1. If the user is found, they are authenticated.
    2. If the user is not found:
      1. If automatic user creation is allowed, the user will be created and authenticated.
      2. If automatic user creation is disabled, a 403 error is returned.

Configuration

In the config/auth.php file, the tgwebapp guard must be registered.

Example of the file content after registering the guard:

...
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'tgwebapp' => [
        'driver' => 'tgwebapp', // the name of the guard
        'token' => env('TELEGRAM_BOT_TOKEN'), // bot token
        'autoCreation' => true, // flag allowing automatic user creation
        'userDataHeaderName' => 'X-TELEGRAM-USER-DATA', // header name from which the guard retrieves the WebAppUser object
        'userModel' => \App\Models\User::class, // user model class
    ]
],
...

Usage

Include the guard in the routing file routes/web.php.

...
Route::middleware('auth:tgwebapp')->group(function(){
    Route::get('/me', function(){
        return 'Hello!';
    });
});
...

A GET request to /me will go through authentication via the Telegram WebApp guard.

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固