ldc-web/app/Providers/RouteServiceProvider.php

102 lines
3.4 KiB
PHP
Raw Normal View History

2020-04-09 13:05:23 +02:00
<?php
namespace App\Providers;
2022-09-21 11:17:58 +00:00
use Illuminate\Cache\RateLimiting\Limit;
2020-04-09 13:05:23 +02:00
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
2022-09-21 11:17:58 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
2020-04-09 13:05:23 +02:00
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
2023-09-22 09:59:11 +02:00
* The path to your application's "home" route.
2020-04-09 13:05:23 +02:00
*
2023-09-22 09:59:11 +02:00
* Typically, users are redirected here after authentication.
2022-09-21 11:17:58 +00:00
*
2020-04-09 13:05:23 +02:00
* @var string
*/
public const HOME = '/';
2020-04-09 13:05:23 +02:00
/**
2023-09-22 09:59:11 +02:00
* Define your route model bindings, pattern filters, and other route configuration.
2020-04-09 13:05:23 +02:00
*
* @return void
*/
2023-09-22 09:59:11 +02:00
public function boot(): void
2020-04-09 13:05:23 +02:00
{
2022-09-21 11:17:58 +00:00
$this->configureRateLimiting();
2020-04-09 13:05:23 +02:00
2023-09-22 09:59:11 +02:00
$this->cspReportRoute();
2022-09-21 11:17:58 +00:00
$this->routes(function () {
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web-bo.php'));
2020-04-09 13:05:23 +02:00
2022-09-21 11:17:58 +00:00
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web-fo.php'));
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
2022-09-21 11:17:58 +00:00
});
2020-04-09 13:05:23 +02:00
}
/**
2022-09-21 11:17:58 +00:00
* Configure the rate limiters for the application.
2020-04-09 13:05:23 +02:00
*
* @return void
*/
2023-09-22 09:59:11 +02:00
protected function configureRateLimiting(): void
2020-04-09 13:05:23 +02:00
{
2023-09-22 09:59:11 +02:00
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(150)->by($request->user()?->id ?: app('clientIp'));
});
2022-09-21 11:17:58 +00:00
RateLimiter::for('web', function (Request $request) {
2023-09-22 09:59:11 +02:00
return Limit::perMinute(150)->by($request->user()?->id ?: app('clientIp'));
2022-09-21 11:17:58 +00:00
});
2023-09-22 09:59:11 +02:00
RateLimiter::for('credentials', function (Request $request) {
return Limit::perMinute(30)->by($request->user()?->id ?: app('clientIp'));
2022-09-21 11:17:58 +00:00
});
2020-04-09 13:05:23 +02:00
}
2023-09-22 09:59:11 +02:00
/**
* * Register CSP report URI
*
* Put this function inside RouteServiceProvider,
* and call it in RouteServiceProvider::boot
*
* @return void
*/
private function cspReportRoute()
{
// * CSP REPORT URI, Please add /csp-report to VerifyCsrfToken $except middleware
\Illuminate\Support\Facades\Route::post('/csp-report', function () {
$isCspRequest = request()->header('Content-Type') === 'application/csp-report';
if (!$isCspRequest or !request()->json('csp-report') or !is_array(request()->json('csp-report'))) {
abort(401);
}
// * Flatten if needed
$cspReport = collect(request()->json('csp-report'))
->flatMap(function ($v, $k) {
return [$k => is_array($v) ? collect($v)->flatten()->implode(', ') : $v];
})->all();
2023-09-22 09:59:11 +02:00
// * Filter array keys
$cspReport = collect(array_intersect_key($cspReport, [
'blocked-uri' => '', 'document-uri' => '', 'original-policy' => '',
'referrer' => '', 'violated-directive' => ''
]));
\Illuminate\Support\Facades\Log::warning(sprintf(
"CSP REPORT for %s : \n %s",
$cspReport->get('document-uri'),
$cspReport->implode("\n")
));
})->middleware('throttle:5,2')
->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class)
->name('cspReportUri');
}
2020-04-09 13:05:23 +02:00
}