jwtSecret = $_ENV['JWT_SECRET'] ?? 'default-secret-fallback'; // Use um fallback seguro em dev } public function __invoke(ServerRequestInterface $request, callable $next) { $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); } $token = $matches[1]; try { $decoded = JWT::decode($token, new Key($this->jwtSecret, 'HS256')); $userId = $decoded->sub ?? null; $apiUser = $decoded->username ?? null; if (empty($userId) || empty($apiUser)) { return ResponseLib::sendFail("Unauthorized: Invalid JWT claims", [], "E_VALIDATE")->withStatus(401); } $dbFile = $_ENV['DB_FILE'] ?? 'bridge.db'; $dbPath = __DIR__ . '/../' . $dbFile; $pdo = new \PDO("sqlite:" . $dbPath); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare("SELECT user_id FROM user WHERE user_id = :user_id AND user_name = :user_name AND user_flag = 'a'"); $stmt->execute(['user_id' => $userId, 'user_name' => $apiUser]); $user = $stmt->fetch(\PDO::FETCH_ASSOC); if (!$user) { return ResponseLib::sendFail("Unauthorized: Invalid or inactive user", [], "E_VALIDATE")->withStatus(401); } $request = $request ->withAttribute('api_user', $apiUser) ->withAttribute('api_user_id', $userId); return $next($request); } catch (\Exception $e) { return ResponseLib::sendFail("Unauthorized: " . $e->getMessage(), [], "E_VALIDATE")->withStatus(401); } } }