Horizon: remove basic auth, add allowed emails list to env

This commit is contained in:
Víctor Falcón 2025-12-17 09:10:15 +01:00
parent 46c5b13739
commit 095f9ea94e
3 changed files with 11 additions and 54 deletions

View File

@ -78,6 +78,5 @@ SUBSCRIPTIONS_ENABLED=false
STRIPE_PRO_MONTHLY_PRICE_ID=
STRIPE_PRO_YEARLY_PRICE_ID=
# Horizon Basic Auth (optional - if not set, Horizon will be accessible without authentication)
HORIZON_BASIC_AUTH_USERNAME=
HORIZON_BASIC_AUTH_PASSWORD=
# Horizon Allowed Emails (comma-separated list of emails allowed to access Horizon in production)
HORIZON_ALLOWED_EMAILS=

View File

@ -1,36 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class HorizonBasicAuth
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$username = config('horizon.basic_auth_username');
$password = config('horizon.basic_auth_password');
if (empty($username) || empty($password)) {
return $next($request);
}
$authUser = $request->getUser();
$authPassword = $request->getPassword();
if ($authUser !== $username || $authPassword !== $password) {
return response('Unauthorized', 401, [
'WWW-Authenticate' => 'Basic realm="Horizon"',
]);
}
return $next($request);
}
}

View File

@ -2,10 +2,7 @@
namespace App\Providers;
use App\Http\Middleware\HorizonBasicAuth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider;
class HorizonServiceProvider extends HorizonApplicationServiceProvider
@ -15,15 +12,6 @@ class HorizonServiceProvider extends HorizonApplicationServiceProvider
*/
public function boot(): void
{
$username = config('horizon.basic_auth_username');
$password = config('horizon.basic_auth_password');
if (! empty($username) && ! empty($password)) {
$middleware = config('horizon.middleware', ['web']);
$middleware[] = HorizonBasicAuth::class;
Config::set('horizon.middleware', $middleware);
}
parent::boot();
// Horizon::routeSmsNotificationsTo('15556667777');
@ -39,9 +27,15 @@ class HorizonServiceProvider extends HorizonApplicationServiceProvider
protected function gate(): void
{
Gate::define('viewHorizon', function ($user = null) {
return in_array(optional($user)->email, [
//
]);
$allowedEmails = array_filter(
explode(',', env('HORIZON_ALLOWED_EMAILS', ''))
);
if (empty($allowedEmails)) {
return false;
}
return in_array(optional($user)->email, $allowedEmails);
});
}
}