Oauth.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace lemo\api;
  3. use lemo\api\Send;
  4. use think\Exception;
  5. use think\facade\Request;
  6. use think\facade\Cache;
  7. /**
  8. * API鉴权验证
  9. */
  10. class Oauth
  11. {
  12. use Send;
  13. /**
  14. * accessToken存储前缀
  15. *
  16. * @var string
  17. */
  18. public static $accessTokenPrefix = 'accessToken_';
  19. /**
  20. * 过期时间秒数
  21. *
  22. * @var int
  23. */
  24. public static $expires = 7200;
  25. /**
  26. * 认证授权 通过用户信息和路由
  27. * @param Request $request
  28. * @return \Exception|UnauthorizedException|mixed|Exception
  29. * @throws UnauthorizedException
  30. */
  31. final function authenticate()
  32. {
  33. return self::certification(self::getClient());
  34. }
  35. /**
  36. * 获取用户信息
  37. * @param Request $request
  38. * @return $this
  39. * @throws UnauthorizedException
  40. */
  41. public static function getClient()
  42. {
  43. //获取头部信息
  44. try {
  45. $authorization = Request::header('authentication'); //获取请求中的authentication字段,值形式为USERID asdsajh..这种形式
  46. $authorization = explode(" ", $authorization);//explode分割,获取后面一窜base64加密数据
  47. $authorizationInfo = explode(":", base64_decode($authorization[1])); //对base_64解密,获取到用:拼接的自字符串,然后分割,可获取appid、accesstoken、uid这三个参数
  48. $clientInfo['uid'] = $authorizationInfo[2];
  49. $clientInfo['appid'] = $authorizationInfo[0];
  50. $clientInfo['access_token'] = $authorizationInfo[1];
  51. return $clientInfo;
  52. } catch (Exception $e) {
  53. return self::returnMsg(401,'Invalid authorization credentials',Request::header(''));
  54. }
  55. }
  56. /**
  57. * 获取用户信息后 验证权限
  58. * @return mixed
  59. */
  60. public static function certification($data = []){
  61. $getCacheAccessToken = Cache::get(self::$accessTokenPrefix . $data['access_token']); //获取缓存access_token
  62. if(!$getCacheAccessToken){
  63. return self::returnMsg(401,'fail',"access_token不存在或为空");
  64. }
  65. if($getCacheAccessToken['client']['appid'] !== $data['appid']){
  66. return self::returnMsg(401,'fail',"appid错误"); //appid与缓存中的appid不匹配
  67. }
  68. return $data;
  69. }
  70. /**
  71. * 检测当前控制器和方法是否匹配传递的数组
  72. *
  73. * @param array $arr 需要验证权限的数组
  74. * @return boolean
  75. */
  76. public static function match($arr = [])
  77. {
  78. $request = Request::instance();
  79. $arr = is_array($arr) ? $arr : explode(',', $arr);
  80. if (!$arr)
  81. {
  82. return false;
  83. }
  84. $arr = array_map('strtolower', $arr);
  85. // 是否存在
  86. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
  87. {
  88. return true;
  89. }
  90. // 没找到匹配
  91. return false;
  92. }
  93. /**
  94. * 生成签名
  95. * _字符开头的变量不参与签名
  96. */
  97. public static function makeSign ($data = [],$app_secret = '')
  98. {
  99. unset($data['version']);
  100. unset($data['sign']);
  101. return self::_getOrderMd5($data,$app_secret);
  102. }
  103. /**
  104. * 计算ORDER的MD5签名
  105. */
  106. private static function _getOrderMd5($params = [] , $app_secret = '') {
  107. ksort($params);
  108. $params['key'] = $app_secret;
  109. return strtolower(md5(urldecode(http_build_query($params))));
  110. }
  111. }