|
|
@@ -20,7 +20,6 @@ class JwtAuthMiddleware
|
|
|
|
|
|
public function __invoke(ServerRequestInterface $request, callable $next)
|
|
|
{
|
|
|
- // 1. Extrai o token do header Authorization
|
|
|
$authHeader = $request->getHeaderLine('Authorization');
|
|
|
if (empty($authHeader) || !preg_match('/Bearer\s+(.*)/', $authHeader, $matches)) {
|
|
|
return ResponseLib::sendFail("Unauthorized: Missing or invalid Authorization header", [], "E_VALIDATE")->withStatus(401);
|
|
|
@@ -29,10 +28,7 @@ class JwtAuthMiddleware
|
|
|
$token = $matches[1];
|
|
|
|
|
|
try {
|
|
|
- // 2. Decodifica e valida o JWT
|
|
|
- $decoded = JWT::decode($token, new Key($this->jwtSecret, 'HS256')); // Use HS256 ou algoritmo desejado
|
|
|
-
|
|
|
- // 3. Extrai claims (assuma que o JWT tem 'sub' como user_id e 'username')
|
|
|
+ $decoded = JWT::decode($token, new Key($this->jwtSecret, 'HS256'));
|
|
|
$userId = $decoded->sub ?? null;
|
|
|
$apiUser = $decoded->username ?? null;
|
|
|
|
|
|
@@ -40,7 +36,6 @@ class JwtAuthMiddleware
|
|
|
return ResponseLib::sendFail("Unauthorized: Invalid JWT claims", [], "E_VALIDATE")->withStatus(401);
|
|
|
}
|
|
|
|
|
|
- // 4. Verifica se o usuário existe e está ativo no banco (similar ao HMAC)
|
|
|
$dbFile = $_ENV['DB_FILE'] ?? 'bridge.db';
|
|
|
$dbPath = __DIR__ . '/../' . $dbFile;
|
|
|
$pdo = new \PDO("sqlite:" . $dbPath);
|
|
|
@@ -54,7 +49,6 @@ class JwtAuthMiddleware
|
|
|
return ResponseLib::sendFail("Unauthorized: Invalid or inactive user", [], "E_VALIDATE")->withStatus(401);
|
|
|
}
|
|
|
|
|
|
- // 5. Tudo certo, adiciona atributos ao request (compatível com HMAC)
|
|
|
$request = $request
|
|
|
->withAttribute('api_user', $apiUser)
|
|
|
->withAttribute('api_user_id', $userId);
|
|
|
@@ -62,7 +56,6 @@ class JwtAuthMiddleware
|
|
|
return $next($request);
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
- // Captura erros de JWT (ex: expirado, inválido)
|
|
|
return ResponseLib::sendFail("Unauthorized: " . $e->getMessage(), [], "E_VALIDATE")->withStatus(401);
|
|
|
}
|
|
|
}
|