quynhnguyenthe / laraliff
Laravel的LIFF认证
v1.0.0
2023-12-25 10:35 UTC
Requires
- php: >=7.0.0
- guzzlehttp/guzzle: ^5|^6|^7
- illuminate/support: ^5.5|^6|^7|^8
- tymon/jwt-auth: ^1.0
README
概述
- LIFF应用程序 文档 LIFF
- tymondesigns/jwt-auth 仓库 jwt
laraliff能做什么
- 使用LIFF应用程序和服务器中的用户数据 [ID令牌] (https://developers.line.biz/ja/docs/liff/using-user-profile/#%E3%83%A6%E3%83%BC%E3%82%B5%E3%82%99%E3%83%BC%E6%83%85%E5%A0%B1%E3%82%92%E3%82%B5%E3%83%BC%E3%83%8F%E3%82%99%E3%83%BC%E3%81%A6%E3%82%99%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B)Using
- 一旦认证成功,过渡到使用JWT进行认证。
如何使用
composer require quynhnguyenthe/laraliff
tymondesigns/jwt-auth 创建jwt配置文件
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
创建laraliff配置文件
php artisan vendor:publish --provider="QuynhNguyenThe\Laraliff\Providers\LaraliffServiceProvider"
JWT密钥生成
php artisan jwt:secret
将LIFF_CHANNEL_ID添加到.env文件中
...
LIFF_CHANNEL_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
将以下字段添加到用于认证的表的模式中
liff_id
- LIFF ID
name
- Line应用中的名称
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('liff_id')->unique(); $table->string('name'); $table->timestamps(); }); } ... }
※模式名称可从配置中更改
<?php return [ 'liff_channel_id' => env('LIFF_CHANNEL_ID', 'liff_channel_id'), 'fields' => [ 'liff_id' => 'liff_id', 'name' => 'name', ], ];
将以下方法添加到用于认证的模型中
namespace App; use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements JWTSubject { use Notifiable; // Rest omitted for brevity /** * Get the identifier that will be stored in the subject claim of the JWT. * * @return mixed */ public function getJWTIdentifier() { return $this->getKey(); } /** * Return a key value array, containing any custom claims to be added to the JWT. * * @return array */ public function getJWTCustomClaims() { return []; } }
更改 config/auth.php
'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ], ... 'guards' => [ 'api' => [ 'driver' => 'laraliff', 'provider' => 'users', ], ],
添加认证路由
Route::group([ 'middleware' => 'api', 'prefix' => 'auth' ], function ($router) { Route::post('login', 'AuthController@login'); Route::post('logout', 'AuthController@logout'); Route::post('refresh', 'AuthController@refresh'); Route::post('me', 'AuthController@me'); });
创建认证控制器
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\User; use QuynhNguyenThe\Laraliff\Services\Exceptions\LiffUnverfiedException; use QuynhNguyenThe\Laraliff\Services\LiffVerificationService; class AuthController extends Controller { public function __construct() { $this->middleware('auth:api', ['except' => ['login']]); } public function register(LiffVerificationService $verificationService) { try { $liff = $verificationService->verify(request('token')); } catch (LiffUnverfiedException $e) { return response()->json(['error' => 'LIFF ID Token is unauthorized'], 401); } $user = User::create([ 'liff_id' => $liff['sub'], 'name' => $liff['name'], 'picture' => $liff['picture'], ]); return response()->json(auth('api')->login($user)); } public function login() { try { $jwt = auth('api')->attempt(request(['liff_id_token'])); } catch (LiffUnverfiedException $e) { return response()->json(['error' => 'LIFF ID Token is unauthorized'], 401); } if (!$jwt) { return response()->json(['error' => 'User not found'], 404); } return response()->json($jwt); } public function me() { return response()->json(auth('api')->user()); } public function logout() { auth()->logout(); return response()->json(['message' => 'Successfully logged out']); } }