49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Middleware;
|
||
|
|
|
||
|
|
use App\Models\Project;
|
||
|
|
use Closure;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
|
||
|
|
class AuthenticateDsn
|
||
|
|
{
|
||
|
|
public function handle(Request $request, Closure $next): Response
|
||
|
|
{
|
||
|
|
$project = $request->route('project');
|
||
|
|
|
||
|
|
if (! $project instanceof Project) {
|
||
|
|
abort(404, 'Project not found.');
|
||
|
|
}
|
||
|
|
|
||
|
|
$sentryKey = $this->extractSentryKey($request);
|
||
|
|
|
||
|
|
if (! $sentryKey || $sentryKey !== $project->dsn_public_key) {
|
||
|
|
abort(401, 'Invalid DSN key.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $next($request);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Extract the sentry_key from the query string or X-Sentry-Auth header.
|
||
|
|
*
|
||
|
|
* Header format: Sentry sentry_version=7, sentry_client=..., sentry_key=<key>
|
||
|
|
*/
|
||
|
|
private function extractSentryKey(Request $request): ?string
|
||
|
|
{
|
||
|
|
if ($key = $request->query('sentry_key')) {
|
||
|
|
return $key;
|
||
|
|
}
|
||
|
|
|
||
|
|
$header = $request->header('X-Sentry-Auth', '');
|
||
|
|
|
||
|
|
if (preg_match('/sentry_key=([a-f0-9\-]+)/i', $header, $matches)) {
|
||
|
|
return $matches[1];
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|