支持 / remote-auth
API认证库
Requires
- php: >=5.4.0
- illuminate/http: ~5.0
- illuminate/support: ~5.0
- namshi/jose: ^5.0 || ^7.0
- nesbot/carbon: ~1.0
Requires (Dev)
- illuminate/auth: ~5.0
- illuminate/console: ~5.0
- illuminate/database: ~5.0
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-09-29 05:21:08 UTC
README
远程认证是一个用于Laravel API认证或Web应用中远程用户管理的库。它是一个基于令牌的认证系统,提供了许多功能,如在不进行任何数据库交互的情况下,阻止用户在特定日期之间登录并获取每个有效用户的登录日期。远程认证以两种方式验证令牌:通常是有效检查或双向认证检查,下面将详细介绍。
入门指南
该库的设计重点是易于设置,任何人都可以轻松使用,无论是初创团队成员还是专家。
先决条件
您需要使用Composer包管理器来安装此包及其依赖项。
安装
按照以下步骤将包安装到Laravel中
composer require support/remote-auth
然后在config/app.php文件中的provider数组中添加以下行
Support\RemoteAuth\JSRServiceProvider::class,
然后在config/app.php文件中的aliash数组中添加
'Remote' => Support\RemoteAuth\Facades\Remote::class,
然后将包发布到Laravel应用中。
php artisan vendor:publish
此命令在config文件夹中创建RemoteAuth.php文件。您可以使用此文件启用或禁用包的功能。建议开发人员也运行以下命令以避免未来出现问题。
php artisan key:generate
php artisan config:cache
然后运行以下命令创建中间件。
php artisan make:middleware RemoteAuth
然后复制以下代码到文件中
<?php
namespace App\Http\Middleware;
use Closure;
use Remote;
class RemoteAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
/*check token is received or not*/
if(empty($request->header('Authorization'))){
return response()->json(array('token is required'));
/*check token is valid or not*/
}else if($user = Remote::verify($request->header('Authorization'))){
/*if valid then user data bind with request*/
$request->r_user = $user;
}else {
/*if token is invalid the return invalid token error*/
return response()->json(array('invalid token'));
}
return $next($request);
}
}
重要
如果您使用上述中间件,则可以在请求类实例中获取认证用户,如下所示
public function get_detail(Request $request){
//get user
$user = $request->r_user;
//token
$token = $request->r_token;
dd($user); //dump $user variable
}
如果config\RemoteAuth.php中的double_verify为true,则返回用户记录。
User {#209
#fillable: array:3 [
0 => "name"
1 => "email"
2 => "password"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [
"id" => 1
"name" => "ravi bhanderi"
"email" => "ravi@gmail.com"
"password" => "$2y$10$hMutgh3AmEdiQbIVosxqS.cdYun4T8f1MK.hjLgrUtFlH3z9axNO6"
"remember_token" => "yS3eixwdIGjYqFBwcAvBy09ZyDiXfAQubojNdlogZoa4rbCSQwxJlXVHyp8v"
"created_at" => "2017-10-08 18:25:16"
"updated_at" => "2017-10-08 18:25:16"
]
#original: array:7 [
"id" => 1
"name" => "ravi bhanderi"
"email" => "ravi@gmail.com"
"password" => "$2y$10$hMutgh3AmEdiQbIVosxqS.cdYun4T8f1MK.hjLgrUtFlH3z9axNO6"
"remember_token" => "yS3eixwdIGjYqFBwcAvBy09ZyDiXfAQubojNdlogZoa4rbCSQwxJlXVHyp8v"
"created_at" => "2017-10-08 18:25:16"
"updated_at" => "2017-10-08 18:25:16"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
如果config\RemoteAuth.php中的double_verify为false,则返回用户标识符(id)和登录日期
array:2 [
"identifire" => 1
"login_date" => Carbon @1524373176 {#186
date: 2018-04-22 04:59:36.389266 UTC (+00:00)
}
]
现在,您的API认证已经准备好了,现在将中间件注册到您的kernal.php文件中,根据您的使用情况进行注册。通常人们使用路由中间件,因此我将它们注册到kernal.php中作为路由中间件。
protected $routeMiddleware = [
...
...
...
remote_auth' => \App\Http\Middleware\RemoteAuth::class
];
使用中间件创建认证路由组,这种类型的路由在登录功能之后考虑。
Route::group(['middleware'=>'remote_auth'],function(){
//route list
});
通用功能
在控制器中使用远程认证之前,您需要使用它。
use Remote;
//get the current user
Remote::user($token);
//login by credential
$token = Remote::attempt(array('email'=>'ravibhanderi14@gmail.com','password'=>'123456'));
//login by id
Remote::byId($id); //id of user
//from user model object
Remote:fromUser($user); //$user is a record of user must be instance of eloquent
//verify token is valid or invalid. Function return user if token is valid and double_verification is on otherwise
return identifire and logindate in array. You can on off double_verification using config\RemoteAuth.php.
Remote::verify($token);
配置文件选项
远程认证配置文件如下。它位于发布供应商后config\RemoteAuth.php中。在较低版本的Laravel中,您可以自己创建。当前Laravel版本是5.5。
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Cache Store
|--------------------------------------------------------------------------
|
this is define token validate time in minute
|
*/
/*if you want to token neverinvalid just comment below line other wise provide time into minute */
'valid' => 60, /* menute */
/*=============================================
= define invlid token beetween two date =
------ date should be format in yyyy/dd/mm------
ex. if you want to stop access of api login or register user between specific date
=============================================*/
'stop_login' => false,
/* date format should be yyyy/mm/dd */
'to_date' => null ,
'from_date' => null,
/*===== End ======*/
/*want to autheticate date with model object*/
'login_date' => true,
/*Verify payload with a data base record. Becuase some time after generating
token user deleted from database. If token is generated and after some time user
deleted from database. What happen user have their authentication token so if
a double_verify is on then user check with also
database otherwise remote-auth verify only token is valid*/
'double_verify' => true,
/**/
];