ldc-cocotte/app/Providers/RouteServiceProvider.php

224 lines
8.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Providers;
use App\Contracts\Executor;
use App\Cqs\Queries\Products\GetPublishedProductWithPricingQuery;
use App\Cqs\Queries\Products\GetPublishedProductWithPricingQueryInput;
use App\Dto\ProductWithPricingDto;
use App\Lib\Helpers\RequestHelper;
use App\Models\Discount;
use App\Models\Faq;
use App\Models\Product;
use App\Models\ProductInco;
use App\Models\Range;
use App\Models\Slide;
use App\Models\Tag;
use Domain\Models\ValueObjects\Slug;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Shared\Pagination\Pagination;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class RouteServiceProvider extends ServiceProvider
{
public const string ROUTE_GROUP_WEB = 'web';
public const string ROUTE_GROUP_WEB_CREDENTIALS = 'credentials';
public const string ROUTE_GROUP_WEB_AJAX = 'web-ajax';
public const string ROUTE_GROUP_API = 'api';
private const int RATE_LIMIT_PER_MINUTE_FOR_CREDENTIALS = 6;
private const int RATE_LIMIT_PER_MINUTE_FOR_WEB = 60;
private const int RATE_LIMIT_PER_MINUTE_FOR_AJAX = 60;
private const int RATE_LIMIT_PER_MINUTE_FOR_API = 60;
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
#[\Override]
public function boot()
{
$this->configureRateLimiting();
Request::macro('pagination', function (int $defaultPageSize = 25): Pagination {
/** @var \Illuminate\Http\Request $this */
return RequestHelper::pagination($this, $defaultPageSize);
});
$this->routes(function (): void {
Route::middleware([
self::ROUTE_GROUP_WEB,
'admin-secure',
'throttle:' . self::ROUTE_GROUP_WEB,
])->name('bo.')->prefix('bo')
->group(base_path('routes/web-bo.php'));
Route::middleware([
self::ROUTE_GROUP_WEB,
'throttle:' . self::ROUTE_GROUP_WEB,
])->name('fo.')
->group(base_path('routes/web-fo.php'));
Route::middleware([
self::ROUTE_GROUP_WEB_AJAX,
'throttle:' . self::ROUTE_GROUP_WEB_AJAX,
])->name('ajax.')->prefix('ajax')
->group(base_path('routes/ajax-fo.php'));
Route::middleware([
self::ROUTE_GROUP_API,
'throttle:' . self::ROUTE_GROUP_API,
])->name('api.')->prefix('api')
->group(base_path('routes/api.php'));
});
Route::model('inco', ProductInco::class);
Route::model('faq', Faq::class);
Route::model('tag', Tag::class);
Route::model('slide', Slide::class);
Route::model('range', Range::class);
Route::model('discount', Discount::class);
Route::model('product', Product::class);
Route::bind(
'productWithPricingPublished',
fn (string $value) => $this->resolvePublishedProductWithPricing($value)
);
$this->cspReportRoute();
}
/**
* Configure the rate limiters for the application.
*/
protected function configureRateLimiting(): void
{
RateLimiter::for(
self::ROUTE_GROUP_WEB_CREDENTIALS,
fn (Request $request) => Limit::perMinute(self::RATE_LIMIT_PER_MINUTE_FOR_CREDENTIALS)
->by($request->user()?->id ?: resolve('clientIp'))
);
RateLimiter::for(
self::ROUTE_GROUP_WEB,
fn (Request $request) => Limit::perMinute(self::RATE_LIMIT_PER_MINUTE_FOR_WEB)
->by($request->user()?->id ?: resolve('clientIp'))
);
RateLimiter::for(
self::ROUTE_GROUP_WEB_AJAX,
fn (Request $request) => Limit::perMinute(self::RATE_LIMIT_PER_MINUTE_FOR_AJAX)
->by($request->user()?->id ?: resolve('clientIp'))
);
RateLimiter::for(
self::ROUTE_GROUP_API,
fn (Request $request) => Limit::perMinute(self::RATE_LIMIT_PER_MINUTE_FOR_API)
->by($request->user()?->id ?: resolve('clientIp'))
);
}
private function resolvePublishedProductWithPricing(string $value): ProductWithPricingDto
{
try {
$input = new GetPublishedProductWithPricingQueryInput(new Slug($value));
} catch (\InvalidArgumentException) {
throw new NotFoundHttpException();
}
return $this->app->make(Executor::class)->execute(
$this->app->make(GetPublishedProductWithPricingQuery::class),
$input,
) ?? throw new NotFoundHttpException();
}
/**
* * Register CSP report URI
*
* Put this function inside RouteServiceProvider,
* and call it in RouteServiceProvider::boot
*/
private function cspReportRoute(): void
{
// * CSP REPORT URI, Please add /csp-report to VerifyCsrfToken $except middleware
Route::post('/csp-report', function (Request $request): void {
$isCspRequest = in_array(
request()->header('Content-Type'),
[
'application/reports+json',
'application/csp-report',
]
);
abort_if(! $isCspRequest, 401);
$cspReport = (function (array $data): array {
// Format Reporting API moderne
if (isset($data[0]) && is_array($data[0]) && isset($data[0]['type']) && $data[0]['type'] === 'csp-violation') {
return (array) ($data[0]['body'] ?? []);
}
// Format CSP classique
if (isset($data['csp-report'])) {
return (array) $data['csp-report'];
}
return [];
})((array) $request->json()->all());
// * Filter array keys
$cspReport = [
'document-uri' => Arr::get($cspReport, 'documentURL', Arr::get($cspReport, 'document-uri', 'unknown')),
'referrer' => Arr::get($cspReport, 'referrer', ''),
'blocked-uri' => Arr::get($cspReport, 'blockedURL', Arr::get($cspReport, 'blocked-uri', '')),
'effective-directive' => Arr::get($cspReport, 'effectiveDirective', Arr::get($cspReport, 'effective-directive', 'unknown')),
'original-policy' => Arr::get($cspReport, 'originalPolicy', Arr::get($cspReport, 'original-policy', 'unknown')),
'disposition' => Arr::get($cspReport, 'disposition', 'enforce'),
'status-code' => Arr::get(
$cspReport,
'statusCode',
Arr::get($cspReport, 'status-code', 200)
),
'line-number' => Arr::get($cspReport, 'lineNumber'),
'column-number' => Arr::get($cspReport, 'columnNumber'),
'source-file' => Arr::get($cspReport, 'sourceFile'),
];
/** @var array<string, string> $cspReport */
$cspReport = array_filter($cspReport, fn ($value) => ! is_string($value) || trim($value) !== '');
$cspReportLines = array_map(
fn ($key, $value) => "\t{$key} -> {$value}",
array_keys($cspReport),
$cspReport
);
Log::warning(sprintf(
"CSP REPORT : \n %s",
implode("\n", $cspReportLines)
));
})->middleware('throttle:5,2')
->withoutMiddleware(PreventRequestForgery::class)
->name('cspReportUri');
}
}