Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
33 lines
1000 B
PHP
33 lines
1000 B
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\AgentToken;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Вход агента (MCP): Bearer-токен агента → пользователь.
|
|
* Кладёт user в тот же атрибут, что RemoteUser, — дальше код не различает, кто пришёл.
|
|
*/
|
|
class AuthenticateAgent
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$token = $request->bearerToken();
|
|
if ($token === null || $token === '') {
|
|
return response()->json(['message' => 'Нужен Bearer-токен агента'], 401);
|
|
}
|
|
|
|
$agent = AgentToken::findByToken($token);
|
|
if ($agent === null) {
|
|
return response()->json(['message' => 'Неизвестный токен агента'], 401);
|
|
}
|
|
|
|
$request->attributes->set('user', $agent->user);
|
|
|
|
return $next($request);
|
|
}
|
|
}
|