CompanyAuth.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. return $next($request);
  16. $app_id = $request->get('app_id', '');
  17. $service = new ConfigService;
  18. $validate = new CompanyAuthRequest($request);
  19. $validate->validate();
  20. $client_ip = CommonHelper::GetClientIp();
  21. if (in_array($client_ip, $service->CompanyAuthIps($app_id))) {
  22. $params = $request->except('_url');
  23. $timestamp = (int) $params['timestamp'];
  24. $diff = time() - $timestamp;
  25. if ($diff < SysConsts::ONE_MINUTE_SECONDS * 60) {
  26. $config = $service->findCompanyAuthConfig($params['app_id']);
  27. if (isset($params['sign']) && strcasecmp(CommonHelper::sign($params, $config->app_secret), $params['sign']) == 0) {
  28. $this->setGlobalConfig($config);
  29. return $next($request);
  30. } else {
  31. CommonHelper::myLog('test')->info(CommonHelper::sign($params, $config->app_secret));
  32. return response()->error('COMPANY_AUTH_SIGN_ERROR');
  33. }
  34. } else {
  35. return response()->error('COMPANY_AUTH_EXPIRED');
  36. }
  37. } else {
  38. return response()->error('COMPANY_AUTH_IP_NOT_EXISTS', [
  39. 'ip' => $client_ip
  40. ]);
  41. }
  42. }
  43. }