CompanyAuth.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace General\Middleware;
  3. use App\Consts\SysConsts;
  4. use Closure;
  5. use General\Helpers\CommonHelper;
  6. use General\Requests\CompanyAuth\CompanyAuthRequest;
  7. use General\Services\BaseAuthConfig;
  8. use General\Services\Config\ConfigService;
  9. use Illuminate\Http\Request;
  10. class CompanyAuth
  11. {
  12. use BaseAuthConfig;
  13. public function handle(Request $request, Closure $next)
  14. {
  15. $app_id = $request->get('app_id', '');
  16. $service = new ConfigService;
  17. $validate = new CompanyAuthRequest($request);
  18. $validate->validate();
  19. $client_ip = CommonHelper::GetClientIp();
  20. if (in_array($client_ip, $service->CompanyAuthIps($app_id))) {
  21. $params = $request->except('_url');
  22. $timestamp = (int) $params['timestamp'];
  23. $diff = time() - $timestamp;
  24. if ($diff < SysConsts::ONE_MINUTE_SECONDS * 60) {
  25. $config = $service->findCompanyAuthConfig($params['app_id']);
  26. if (isset($params['sign']) && strcasecmp(CommonHelper::sign($params, $config->app_secret), $params['sign']) == 0) {
  27. $this->setGlobalConfig($config);
  28. return $next($request);
  29. } else {
  30. CommonHelper::myLog('test')->info(CommonHelper::sign($params, $config->app_secret));
  31. return response()->error('COMPANY_AUTH_SIGN_ERROR');
  32. }
  33. } else {
  34. return response()->error('COMPANY_AUTH_EXPIRED');
  35. }
  36. } else {
  37. // return response()->error('COMPANY_AUTH_IP_NOT_EXISTS', [
  38. // 'ip' => $client_ip
  39. // ]);
  40. }
  41. }
  42. }