authenticatedUser($request); $token = $user->currentAccessToken(); // If this is a stateful request just push the request through to the next // middleware in the stack, there is nothing we need to explicitly check. If // this is a valid API Key, but there is no allowed IP restriction, also pass // the request through. if (! $token instanceof HasAbilities || $token instanceof TransientToken || empty($token->allowed_ips)) { return $next($request); } $requestIp = $request->ip(); throw_if($requestIp === null, AccessDeniedHttpException::class, __('exceptions.api.invalid_ip')); try { $find = new IP($requestIp); } catch (IpException) { throw new AccessDeniedHttpException(__('exceptions.api.invalid_ip')); } foreach ($token->allowed_ips as $ip) { try { if (Range::parse($ip)->contains($find)) { return $next($request); } } catch (IpException|NetworkException|InvalidArgumentException) { // Ignore the error entirely continue; } } Activity::event('auth:ip-blocked') ->actor($user) ->subject($user, $token) ->property('identifier', $token->identifier) ->log(); throw new AccessDeniedHttpException(__('exceptions.api.ip_forbidden', ['ip' => $request->ip()])); } }