Repository: alexjustesen/speedtest-tracker (language: php)

Issue:
Allowed IPs
> Hello guys, just an idea in addition to the 2FA request. Would there also be a possibility of some IP based auto-lock in case of failed logins in a certain (short) period of time, to lock-out any brute-force attempts? 

 _Originally posted by @KossikCZ in [#344](https://github.com/alexjustesen/speedtest-tracker/issues/344#issuecomment-1500902532)_


Discussion / hints:

Fix patch:
diff --git a/app/Http/Middleware/AllowedIpAddressesMiddleware.php b/app/Http/Middleware/AllowedIpAddressesMiddleware.php
new file mode 100644
index 000000000..f3f6a338d
--- /dev/null
+++ b/app/Http/Middleware/AllowedIpAddressesMiddleware.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Illuminate\Http\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+class AllowedIpAddressesMiddleware
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
+     */
+    public function handle(Request $request, Closure $next): Response
+    {
+        if (blank(config('speedtest.allowed_ips'))) {
+            return $next($request);
+        }
+
+        $allowedIps = explode(',', config('speedtest.allowed_ips'));
+
+        return in_array($request->ip(), $allowedIps)
+            ? $next($request)
+            : abort(403);
+    }
+}
diff --git a/bootstrap/app.php b/bootstrap/app.php
index bc634e905..3a71da348 100644
--- a/bootstrap/app.php
+++ b/bootstrap/app.php
@@ -19,6 +19,14 @@
             'public-dashboard' => App\Http\Middleware\PublicDashboard::class,
         ]);
 
+        $middleware->prependToGroup('api', [
+            App\Http\Middleware\AllowedIpAddressesMiddleware::class,
+        ]);
+
+        $middleware->prependToGroup('web', [
+            App\Http\Middleware\AllowedIpAddressesMiddleware::class,
+        ]);
+
         $middleware->redirectGuestsTo(fn () => route('filament.admin.auth.login'));
         $middleware->redirectUsersTo(AppServiceProvider::HOME);
 


Test patch:
diff --git a/config/speedtest.php b/config/speedtest.php
index f18055c87..ba3dc003d 100644
--- a/config/speedtest.php
+++ b/config/speedtest.php
@@ -11,6 +11,8 @@
     /**
      * General settings.
      */
+    'allowed_ips' => env('ALLOWED_IPS'),
+
     'content_width' => env('CONTENT_WIDTH', '7xl'),
 
     'prune_results_older_than' => (int) env('PRUNE_RESULTS_OLDER_THAN', 0),