126 lines
3.4 KiB
PHP
126 lines
3.4 KiB
PHP
<?php
|
||
namespace app\api\controller;
|
||
|
||
use think\Exception;
|
||
use think\facade\Request;
|
||
use think\facade\Cache;
|
||
|
||
/**
|
||
* API鉴权验证
|
||
*/
|
||
class OauthController
|
||
{
|
||
use SendController;
|
||
|
||
/**
|
||
* accessToken存储前缀
|
||
*
|
||
* @var string
|
||
*/
|
||
public static $accessTokenPrefix = 'accessToken_';
|
||
|
||
/**
|
||
* 过期时间秒数
|
||
*
|
||
* @var int
|
||
*/
|
||
public static $expires = 7200;
|
||
|
||
/**
|
||
* 认证授权 通过用户信息和路由
|
||
*/
|
||
final function authenticate()
|
||
{
|
||
return self::certification(self::getClient());
|
||
}
|
||
|
||
/**
|
||
* 获取用户信息
|
||
* @param Request $request
|
||
* @return $this
|
||
* @throws Exception
|
||
*/
|
||
public static function getClient()
|
||
{
|
||
//获取头部信息
|
||
try {
|
||
$request = Request::instance();
|
||
$authorization = $request->header('authentication'); //获取请求中的authentication字段,值形式为USERID asdsajh..这种形式
|
||
$authorization = explode(" ", $authorization); //explode分割,获取后面一窜base64加密数据
|
||
$authorizationInfo = explode("-", base64_decode($authorization[1])); //对base_64解密,获取到用:拼接的自字符串,然后分割,可获取appid、accesstoken、uid这三个参数
|
||
$clientInfo['appid'] = $authorizationInfo[0];
|
||
$clientInfo['uid'] = $authorizationInfo[1];
|
||
$clientInfo['access_token'] = $authorizationInfo[2];
|
||
$clientInfo['refresh_token'] = $authorizationInfo[3];
|
||
return $clientInfo;
|
||
} catch (Exception $e) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户信息后 验证权限
|
||
* @return mixed
|
||
*/
|
||
public static function certification($data = []){
|
||
$getCacheAccessToken = Cache::get(TokenController::$accessTokenPrefix . $data['access_token']); //获取缓存access_token
|
||
return $getCacheAccessToken;
|
||
}
|
||
|
||
/**
|
||
* 获取用户信息后 验证权限
|
||
* @return mixed
|
||
*/
|
||
public static function certificationToken($data = []){
|
||
$token = Cache::get(TokenController::$refreshAccessTokenPrefix.$data['refresh_token']);
|
||
return $token ? $token : '';
|
||
}
|
||
|
||
/**
|
||
* 检测当前控制器和方法是否匹配传递的数组
|
||
*
|
||
* @param array $arr 需要验证权限的数组
|
||
* @return boolean
|
||
*/
|
||
public static function match($arr = [])
|
||
{
|
||
$request = Request::instance();
|
||
$arr = is_array($arr) ? $arr : explode(',', $arr);
|
||
if (!$arr)
|
||
{
|
||
return false;
|
||
}
|
||
$arr = array_map('strtolower', $arr);
|
||
// 是否存在
|
||
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// 没找到匹配
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 生成签名
|
||
* _字符开头的变量不参与签名
|
||
*/
|
||
public static function makeSign ($data = [],$app_secret = '')
|
||
{
|
||
$clientInfo['appid'] = $data['appid'];
|
||
$clientInfo['phone'] = $data['phone'];
|
||
$clientInfo['password'] = $data['password'];
|
||
$clientInfo['timestamp'] = $data['timestamp'];
|
||
return self::_getOrderMd5($clientInfo,$app_secret);
|
||
}
|
||
|
||
/**
|
||
* 计算ORDER的MD5签名
|
||
*/
|
||
private static function _getOrderMd5($params = [] , $app_secret = '') {
|
||
$str = $app_secret . $params['appid'] . $params['phone'] . md5($params['password']). $params['timestamp'];
|
||
return strtolower(md5($str));
|
||
}
|
||
|
||
}
|