From 118434de04c4df139792994e2d3c55b58b66ebb5 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 20 Jun 2019 12:36:28 +0200
Subject: [PATCH] FEAT #10954 TIME 0:30 SessionTime + login informations

---
 config/config.xml.default                     |  2 +-
 rest/index.php                                |  4 +--
 src/app/user/controllers/UserController.php   | 32 ++++++++++++++-----
 .../controllers/AuthenticationController.php  | 13 +++++---
 4 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/config/config.xml.default b/config/config.xml.default
index 5573772eba..73b6629858 100755
--- a/config/config.xml.default
+++ b/config/config.xml.default
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <ROOT>
     <config>
-        <CookieTime>1440</CookieTime> <!-- minutes -->
+        <SessionTime>1440</SessionTime> <!-- minutes -->
         <timezone>Europe/Paris</timezone>
         <customLangPathDirectory></customLangPathDirectory>
     </config>
diff --git a/rest/index.php b/rest/index.php
index 5f638c0aea..6ad24a5266 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -26,7 +26,7 @@ $app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response,
     if (!is_file($configPath . '/config.xml')) {
         return $response->withStatus(400)->withJson(['errors' => 'Configuration file is missing']);
     }
-    $routesWithoutAuthentication = ['GET/connection', 'POST/log', 'POST/password', 'PUT/password', 'GET/passwordRules', 'GET/languages/{lang}'];
+    $routesWithoutAuthentication = ['GET/authenticationInformations', 'POST/log', 'POST/password', 'PUT/password', 'GET/passwordRules', 'GET/languages/{lang}'];
     $route = $request->getAttribute('route');
     $currentMethod = empty($route) ? '' : $route->getMethods()[0];
     $currentRoute = empty($route) ? '' : $route->getPattern();
@@ -49,7 +49,7 @@ $app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response,
 });
 
 //Authentication
-$app->get('/connection', \SrcCore\controllers\AuthenticationController::class . ':getConnection');
+$app->get('/authenticationInformations', \SrcCore\controllers\AuthenticationController::class . ':getInformations');
 $app->post('/log', \SrcCore\controllers\AuthenticationController::class . ':log');
 
 //Attachments
diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php
index 3b547a1cf5..6e5648d9be 100755
--- a/src/app/user/controllers/UserController.php
+++ b/src/app/user/controllers/UserController.php
@@ -70,6 +70,10 @@ class UserController
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
+        }
+
         $user = UserController::getUserInformationsById(['id' => $args['id']]);
         if (!empty($user)) {
             $user['groups'] = [];
@@ -103,8 +107,8 @@ class UserController
 
         if (empty($body)) {
             return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']);
-        } elseif (!Validator::stringType()->notEmpty()->validate($body['login'])) {
-            return $response->withStatus(400)->withJson(['errors' => 'Body login is empty or not a string']);
+        } elseif (!Validator::stringType()->notEmpty()->validate($body['login']) || !preg_match("/^[\w.@-]*$/", $body['login'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Body login is empty, not a string or wrong formatted']);
         } elseif (!Validator::stringType()->notEmpty()->validate($body['firstname'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body firstname is empty or not a string']);
         } elseif (!Validator::stringType()->notEmpty()->validate($body['lastname'])) {
@@ -147,7 +151,9 @@ class UserController
 
         $body = $request->getParsedBody();
 
-        if (!Validator::stringType()->notEmpty()->validate($body['firstname'])) {
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
+        } elseif (!Validator::stringType()->notEmpty()->validate($body['firstname'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body firstname is empty or not a string']);
         } elseif (!Validator::stringType()->notEmpty()->validate($body['lastname'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body lastname is empty or not a string']);
@@ -232,6 +238,10 @@ class UserController
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
+        }
+
         $user = UserModel::getById(['id' => $args['id'], 'select' => ['firstname', 'lastname']]);
         if (empty($user)) {
             return $response->withStatus(400)->withJson(['errors' => 'User does not exist']);
@@ -276,6 +286,10 @@ class UserController
 
     public function getPictureById(Request $request, Response $response, array $args)
     {
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
+        }
+
         $user = UserModel::getById(['select' => ['picture'], 'id' => $args['id']]);
         if (empty($user)) {
             return $response->withStatus(400)->withJson(['errors' => 'User does not exist']);
@@ -292,7 +306,9 @@ class UserController
 
         $body = $request->getParsedBody();
 
-        if (!Validator::stringType()->notEmpty()->validate($body['lang'])) {
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
+        } elseif (!Validator::stringType()->notEmpty()->validate($body['lang'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body lang is empty or not a string']);
         } elseif (!Validator::stringType()->notEmpty()->validate($body['writingMode'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body writingMode is empty or not a string']);
@@ -334,6 +350,10 @@ class UserController
 
     public function updatePassword(Request $request, Response $response, array $args)
     {
+        if (!Validator::intVal()->notEmpty()->validate($args['id'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
+        }
+
         $body = $request->getParsedBody();
         if (!Validator::stringType()->notEmpty()->validate($body['newPassword'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
@@ -357,10 +377,6 @@ class UserController
 
         UserModel::updatePassword(['id' => $args['id'], 'password' => $body['newPassword']]);
 
-        if ($user['mode'] == 'standard') {
-            AuthenticationModel::revokeCookie(['userId' => $args['id']]);
-        }
-
         HistoryController::add([
             'code'          => 'OK',
             'objectType'    => 'users',
diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php
index 82aa4c21a5..61f87c9f7a 100755
--- a/src/core/controllers/AuthenticationController.php
+++ b/src/core/controllers/AuthenticationController.php
@@ -136,24 +136,27 @@ class AuthenticationController
         return $response->withJson(['user' => UserController::getUserInformationsById(['id' => $user['id']])]);
     }
 
-    public static function getConnection(Request $request, Response $response)
+    public static function getInformations(Request $request, Response $response)
     {
         $connection = ConfigurationModel::getConnection();
+        $encryptKey = CoreConfigModel::getEncryptKey();
 
-        return $response->withJson(['connection' => $connection]);
+        return $response->withJson(['connection' => $connection, 'changeKey' => $encryptKey == 'Security Key Maarch Parapheur #2008']);
     }
 
     public static function getJWT()
     {
-        $cookieTime = 1;
+        $sessionTime = 1;
 
         $loadedXml = CoreConfigModel::getConfig();
         if ($loadedXml) {
-            $cookieTime = (int)$loadedXml->config->CookieTime;
+            if (!empty($loadedXml->config->sessionTime)) {
+                $sessionTime = (int)$loadedXml->config->sessionTime;
+            }
         }
 
         $token = [
-            'exp'   => time() + 60 * $cookieTime,
+            'exp'   => time() + 60 * $sessionTime,
             'user'  => [
                 'id' => $GLOBALS['id']
             ]
-- 
GitLab