lthn.io/app/Http/Middleware/DomainScope.php

46 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
/**
* Octane-compatible domain scoping middleware.
*
* Checks the Host header per-request and returns 404 if the
* request domain doesn't match the allowed patterns. This
* replaces $_SERVER['HTTP_HOST'] checks at route registration
* time, which don't work with Octane (routes registered once).
*
* Usage in route files:
* Route::middleware('domain:explorer.lthn.io')->group(...)
* Route::middleware('domain:docs.lthn.io,docs.test')->group(...)
*/
class DomainScope
{
public function handle(Request $request, Closure $next, string ...$domains): mixed
{
$host = $request->getHost();
foreach ($domains as $domain) {
// Exact match
if ($host === $domain) {
return $next($request);
}
// Wildcard match (e.g., *.lthn.io)
if (str_starts_with($domain, '*.')) {
$suffix = substr($domain, 1); // .lthn.io
if (str_ends_with($host, $suffix)) {
return $next($request);
}
}
}
abort(404);
}
}