浏览代码

stmt variable with no close on jwtauth midleware

ljoaquim 5 月之前
父节点
当前提交
4d9833d1ed
共有 2 个文件被更改,包括 14 次插入19 次删除
  1. 2 0
      middlewares/JWTAuthMiddleware.php
  2. 12 19
      public/index.php

+ 2 - 0
middlewares/JWTAuthMiddleware.php

@@ -45,10 +45,12 @@ class JwtAuthMiddleware
             $dbPath = __DIR__ . '/../' . $dbFile;
             $pdo = new \PDO("sqlite:" . $dbPath);
             $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+            $pdo->setAttribute(\PDO::ATTR_TIMEOUT, 5000);
 
             $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);
+            $stmt->closeCursor();
 
             if (!$user) {
                 return ResponseLib::sendFail("Unauthorized: Invalid or inactive user", [], "E_VALIDATE")->withStatus(401);

+ 12 - 19
public/index.php

@@ -34,34 +34,27 @@ $authHmac = new HmacAuthMiddleware();
 $authJwt = new JWTAuthMiddleware();
 $cors = new CorsControl();
 
-// Função para envolver rotas com CORS
-$withCors = function ($handler) use ($cors) {
-    return function (ServerRequestInterface $request) use ($handler, $cors) {
-        return $cors($request, $handler);
-    };
-};
-
 
 // Rotas com CORS aplicado
-$app->get('/hmachelloworld', $withCors($authHmac), \Controllers\HelloController::class);
+$app->get('/hmachelloworld', $cors, $authHmac, \Controllers\HelloController::class);
 
-$app->get('/jwthelloworld', $withCors($authJwt), \Controllers\HelloController::class);
+$app->get('/jwthelloworld', $cors, $authJwt,  \Controllers\HelloController::class);
 
 
 //Rotas User
-$app->post('/login', $withCors(\Controllers\LoginController::class));
-$app->post('/register', $withCors(\Controllers\RegisterController::class));
+$app->post('/login', $cors, \Controllers\LoginController::class);
+$app->post('/register', $cors, $authJwt, \Controllers\RegisterController::class);
 
 //Rotas Category
-$app->post('/category/get', $withCors(\Controllers\CategoryGetController::class) );
-$app->post('/category/create', $withCors(\Controllers\CategoryCreateController::class) );
-$app->post('/category/delete', $withCors(\Controllers\CategoryDeleteController::class) );
-$app->post('/category/add-product', $withCors(\Controllers\CategoryAddProductController::class) );
+$app->post('/category/get', $cors, \Controllers\CategoryGetController::class);
+$app->post('/category/create', $cors, $authJwt, \Controllers\CategoryCreateController::class);
+$app->post('/category/delete', $cors, $authJwt, \Controllers\CategoryDeleteController::class);
+$app->post('/category/add-product', $cors, $authJwt, \Controllers\CategoryAddProductController::class);
 
 //Rotas Product 
-$app->post('/product/get', $withCors(\Controllers\ProductGetController::class));
-$app->post('/product/create', $withCors(\Controllers\ProductCreateController::class));
-$app->post('/product/update', $withCors(\Controllers\ProductUpdateController::class));
-$app->post('/product/delete', $withCors(\Controllers\ProductDeleteController::class));
+$app->post('/product/get', $cors, $authJwt, \Controllers\ProductGetController::class);
+$app->post('/product/create', $cors, $authJwt, \Controllers\ProductCreateController::class);
+$app->post('/product/update', $cors, $authJwt, \Controllers\ProductUpdateController::class);
+$app->post('/product/delete', $cors, $authJwt, \Controllers\ProductDeleteController::class);
 
 $app->run();