69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Filament\Forms\Components\Field;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
URL::macro(
|
|
'alternateHasCorrectSignature',
|
|
function (Request $request, $absolute = true, array $ignoreQuery = []) {
|
|
$ignoreQuery[] = 'signature';
|
|
|
|
$absoluteUrl = url($request->path());
|
|
$url = $absolute ? $absoluteUrl : '/'.$request->path();
|
|
|
|
$queryString = collect(explode('&', (string) $request
|
|
->server->get('QUERY_STRING')))
|
|
->reject(fn ($parameter) => in_array(str()->before($parameter, '='), $ignoreQuery))
|
|
->join('&');
|
|
|
|
$original = rtrim($url.'?'.$queryString, '?');
|
|
|
|
// Use the application key as the HMAC key
|
|
$key = config('app.key'); // Ensure app.key is properly set in .env
|
|
|
|
if (empty($key)) {
|
|
throw new \RuntimeException('Application key is not set.');
|
|
}
|
|
|
|
$signature = hash_hmac('sha256', $original, $key);
|
|
|
|
return hash_equals($signature, (string) $request->query('signature', ''));
|
|
}
|
|
);
|
|
|
|
URL::macro('alternateHasValidSignature', function (Request $request, $absolute = true, array $ignoreQuery = []) {
|
|
return URL::alternateHasCorrectSignature($request, $absolute, $ignoreQuery)
|
|
&& URL::signatureHasNotExpired($request);
|
|
});
|
|
|
|
Request::macro('hasValidSignature', function ($absolute = true, array $ignoreQuery = []) {
|
|
return URL::alternateHasValidSignature($this, $absolute, $ignoreQuery);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
if (! app()->environment('local')) {
|
|
URL::forceScheme('https');
|
|
}
|
|
|
|
Field::configureUsing(function (Field $field): void {
|
|
$field->validationAttribute(fn (Field $component) => $component->getLabel());
|
|
});
|
|
}
|
|
}
|