123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace lemo\api;
- use lemo\api\Send;
- use think\Exception;
- use think\facade\Request;
- use think\facade\Cache;
- class Oauth
- {
- use Send;
-
-
- public static $accessTokenPrefix = 'accessToken_';
-
- public static $expires = 7200;
-
- final function authenticate()
- {
- return self::certification(self::getClient());
- }
-
- public static function getClient()
- {
-
- try {
- $authorization = Request::header('authentication');
- $authorization = explode(" ", $authorization);
- $authorizationInfo = explode(":", base64_decode($authorization[1]));
- $clientInfo['uid'] = $authorizationInfo[2];
- $clientInfo['appid'] = $authorizationInfo[0];
- $clientInfo['access_token'] = $authorizationInfo[1];
- return $clientInfo;
- } catch (Exception $e) {
- return self::returnMsg(401,'Invalid authorization credentials',Request::header(''));
- }
- }
-
- public static function certification($data = []){
- $getCacheAccessToken = Cache::get(self::$accessTokenPrefix . $data['access_token']);
- if(!$getCacheAccessToken){
- return self::returnMsg(401,'fail',"access_token不存在或为空");
- }
- if($getCacheAccessToken['client']['appid'] !== $data['appid']){
- return self::returnMsg(401,'fail',"appid错误");
- }
- return $data;
- }
-
- 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 = '')
- {
- unset($data['version']);
- unset($data['sign']);
- return self::_getOrderMd5($data,$app_secret);
- }
-
- private static function _getOrderMd5($params = [] , $app_secret = '') {
- ksort($params);
- $params['key'] = $app_secret;
- return strtolower(md5(urldecode(http_build_query($params))));
- }
- }
|