ldc-gen-game-web/app/Providers/RouteServiceProvider.php

91 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2020-10-21 15:26:21 +02:00
<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
2020-10-21 15:26:21 +02:00
namespace App\Providers;
use App\Models\User;
2026-10-02 12:09:46 +02:00
use App\Http\Middleware\VerifyCsrfToken;
2023-08-21 10:22:07 +02:00
use Illuminate\Cache\RateLimiting\Limit;
2020-10-21 15:26:21 +02:00
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
2023-08-21 10:22:07 +02:00
use Illuminate\Http\Request;
2026-10-02 12:09:46 +02:00
use Illuminate\Support\Facades\Log;
2023-08-21 10:22:07 +02:00
use Illuminate\Support\Facades\RateLimiter;
2020-10-21 15:26:21 +02:00
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* @var string
*/
2020-10-29 15:40:23 +01:00
public const HOME = '/';
2020-10-21 15:26:21 +02:00
/**
* Define your route model bindings, pattern filters, etc.
2020-10-21 15:26:21 +02:00
*/
2023-08-21 10:22:07 +02:00
public function boot(): void
2020-10-21 15:26:21 +02:00
{
2023-08-21 10:22:07 +02:00
$this->configureRateLimiting();
$this->cspReportRoute();
2026-10-02 12:09:46 +02:00
$this->routes(function (): void {
Route::middleware('web')
->namespace($this->namespace)
2023-08-21 10:22:07 +02:00
->group(base_path('routes/web-fo.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web-bo.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web-re7.php'));
Route::model('user', User::class);
});
2020-10-21 15:26:21 +02:00
}
2023-08-21 10:22:07 +02:00
/**
* Configure the rate limiters for the application.
*/
protected function configureRateLimiting(): void
{
2026-10-02 12:09:46 +02:00
RateLimiter::for('api', fn (Request $request) => Limit::perMinute(150)->by($request->user()?->id ?: resolve('clientIp')));
RateLimiter::for('web', fn (Request $request) => Limit::perMinute(150)->by($request->user()?->id ?: resolve('clientIp')));
RateLimiter::for('credentials', fn (Request $request) => Limit::perMinute(30)->by($request->user()?->id ?: resolve('clientIp')));
2023-08-21 10:22:07 +02:00
}
/**
* * Register CSP report URI
*
* Put this function inside RouteServiceProvider,
* and call it in RouteServiceProvider::boot
*/
2026-10-02 12:09:46 +02:00
private function cspReportRoute(): void
2023-08-21 10:22:07 +02:00
{
// * CSP REPORT URI, Please add /csp-report to VerifyCsrfToken $except middleware
2026-10-02 12:09:46 +02:00
Route::post('/csp-report', function (): void {
2023-08-21 10:22:07 +02:00
$isCspRequest = request()->header('Content-Type') === 'application/csp-report';
2026-10-02 12:09:46 +02:00
abort_if(! $isCspRequest || ! request()->json('csp-report') || ! is_array(request()->json('csp-report')), 401);
2023-08-21 10:22:07 +02:00
// * Flatten if needed
$cspReport = collect(request()->json('csp-report'))
2026-10-02 12:09:46 +02:00
->flatMap(fn ($v, $k): array => [$k => is_array($v) ? collect($v)->flatten()->implode(', ') : $v])->all();
2023-08-21 10:22:07 +02:00
// * Filter array keys
$cspReport = collect(array_intersect_key($cspReport, [
'blocked-uri' => '', 'document-uri' => '', 'original-policy' => '',
2026-10-02 12:09:46 +02:00
'referrer' => '', 'violated-directive' => '',
2023-08-21 10:22:07 +02:00
]));
2026-10-02 12:09:46 +02:00
Log::warning(sprintf(
2023-08-21 10:22:07 +02:00
"CSP REPORT for %s : \n %s",
$cspReport->get('document-uri'),
$cspReport->implode("\n")
));
})->middleware('throttle:5,2')
2026-10-02 12:09:46 +02:00
->withoutMiddleware(VerifyCsrfToken::class)
2023-08-21 10:22:07 +02:00
->name('cspReportUri');
}
2020-10-21 15:26:21 +02:00
}