Mit BASIC_AUTH_ENABLED=true (Staging) bekam Stripe für jeden Webhook-Call ein 401 — Zahlungen wurden nie erfüllt. Der Endpoint ist durch die Signaturprüfung (STRIPE_WEBHOOK_SECRET) geschützt. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class BasicAuthMiddleware
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param Closure(Request): (Response) $next
|
|
*/
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
// Gate global deaktiviert (z. B. Produktion/Tests)
|
|
if (! config('auth.basic.enabled', true)) {
|
|
return $next($request);
|
|
}
|
|
|
|
// Skip Basic Auth für Livewire-Requests komplett
|
|
// Diese sind bereits durch Laravel Session/CSRF geschützt
|
|
$path = $request->path();
|
|
|
|
if (
|
|
str_starts_with($path, 'livewire/') ||
|
|
str_contains($path, '/livewire/') ||
|
|
$request->is('livewire/*') ||
|
|
$request->is('*/livewire/*')
|
|
) {
|
|
return $next($request);
|
|
}
|
|
|
|
// Skip Basic Auth für Flux UI Assets (flux.js, flux.min.js, editor.js, etc.)
|
|
if (str_starts_with($path, 'flux/')) {
|
|
return $next($request);
|
|
}
|
|
|
|
// Skip Basic Auth für API und Short-Links; API-Zugriff wird per Sanctum geschützt.
|
|
if ($request->is('api/*') || $request->is('_cabinet/*')) {
|
|
return $next($request);
|
|
}
|
|
|
|
// Skip Basic Auth für den Stripe-Webhook — Stripe sendet keine
|
|
// Zugangsdaten mit; der Endpoint ist durch die Signaturprüfung
|
|
// (STRIPE_WEBHOOK_SECRET) geschützt.
|
|
if ($request->is('stripe/webhook')) {
|
|
return $next($request);
|
|
}
|
|
|
|
// Zugangsdaten aus der Config (gespeist aus .env). Ist nichts
|
|
// konfiguriert, wird der Gate nicht erzwungen.
|
|
$user = config('auth.basic.user');
|
|
$pass = config('auth.basic.password');
|
|
|
|
if (($user === null || $user === '') && ($pass === null || $pass === '')) {
|
|
return $next($request);
|
|
}
|
|
|
|
// Timing-sicherer Vergleich beider Felder (kein Short-Circuit).
|
|
$userValid = hash_equals((string) $user, (string) $request->getUser());
|
|
$passValid = hash_equals((string) $pass, (string) $request->getPassword());
|
|
|
|
if (! $userValid || ! $passValid) {
|
|
return response('Unauthorized.', 401, ['WWW-Authenticate' => 'Basic']);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|