OpenApiAuthMiddleware.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\middleware;
  12. use app\common\repositories\openapi\OpenAuthRepository;
  13. use app\common\repositories\system\merchant\MerchantRepository;
  14. use app\Request;
  15. use crmeb\exceptions\AuthException;
  16. use crmeb\services\JwtTokenService;
  17. use Firebase\JWT\ExpiredException;
  18. use think\exception\ValidateException;
  19. use think\Response;
  20. use Throwable;
  21. class OpenApiAuthMiddleware extends BaseMiddleware
  22. {
  23. public function before(Request $request)
  24. {
  25. $unique = $request->param('unique');
  26. $expiration = $request->param('expiration');
  27. $access_key = $request->param('access_key');
  28. $signature = $request->param('signature');
  29. if (!$unique || !$expiration || !$access_key || !$signature) throw new AuthException('验证失败,请完善参数');
  30. if ((time() - $expiration) > 300) throw new AuthException('验证已过期');
  31. $openAuthRepository = app()->make(OpenAuthRepository::class);
  32. $auth = $openAuthRepository->getSearch(['access_key' => $access_key])->find();
  33. $secret_key = $auth->secret_key;
  34. $credential = ['mer','openapi'];
  35. $policy = [
  36. 'access_key' => $access_key,
  37. 'conditions' => $access_key.'/'.implode('/', $credential),
  38. 'expiration' => date('YmdHis',$expiration),
  39. 'unique' => $unique
  40. ];
  41. ksort($policy);
  42. $policy = json_encode($policy);
  43. $jsonPolicy64 = base64_encode($policy);
  44. $_signature = bin2hex(hash_hmac('sha256', $jsonPolicy64, $secret_key, true));
  45. if ($signature !== $_signature) throw new AuthException('验证失败');
  46. if ($auth->status != 1 || $auth->is_del) throw new AuthException('账号已被禁用');
  47. $request->macro('openAuthInfo', function () use($auth) {
  48. unset($auth['secret_key']);
  49. return $auth;
  50. });
  51. $request->macro('isAuth', function () {
  52. return true;
  53. });
  54. $request->macro('openMerId', function () use($auth) {
  55. return $auth->mer_id;
  56. });
  57. $request->macro('openRoule', function () use($auth) {
  58. return $auth->auth;
  59. });
  60. $auth->last_time = date('Y-m-d H:i:s', time());
  61. $auth->last_ip = request()->ip();
  62. $auth->save();
  63. }
  64. public function after(Response $response)
  65. {
  66. // TODO: Implement after() method.
  67. }
  68. }