diff --git a/api-payroll/composer.json b/api-payroll/composer.json index 5ed5d2e..86aff1a 100644 --- a/api-payroll/composer.json +++ b/api-payroll/composer.json @@ -25,6 +25,12 @@ "Tests\\": "tests/" } }, + "autoload": { + "psr-4": { + "App\\Service\\": "src/service", + "App\\Application\\": "src/application" + } + }, "config": { "process-timeout" : 0 }, diff --git a/api-payroll/composer.lock b/api-payroll/composer.lock index 87831fe..65a8bff 100644 --- a/api-payroll/composer.lock +++ b/api-payroll/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "bea55e49da1d79bf5a4874824904525d", + "hash": "9f4397e11cb2603e7754216c4f59c7ad", "content-hash": "5e16cb7781829836a704bd8767830833", "packages": [ { diff --git a/api-payroll/src/application/SessionApplication.php b/api-payroll/src/application/SessionApplication.php new file mode 100644 index 0000000..bfb94e2 --- /dev/null +++ b/api-payroll/src/application/SessionApplication.php @@ -0,0 +1,43 @@ +cryptographyService = $cryptographyService; + + // The database parameters + $this->host = $mysqlSettings['host']; + $this->database = $mysqlSettings['database']; + $this->user = $mysqlSettings['user']; + $this->password = $mysqlSettings['password']; + $this->charset = $mysqlSettings['charset']; + $this->pdoConnectionOptions = $mysqlSettings['pdoConnectionOptions']; + + // Generic error messages + $this->databaseConnectionErrorMessage = $mysqlSettings['databaseConnectionErrorMessage']; + $this->databaseSelectQueryErrorMessage = $mysqlSettings['databaseSelectQueryErrorMessage']; + $this->databaseInsertQueryErrorMessage = $mysqlSettings['databaseInsertQueryErrorMessage']; + + // Initiate the connection + $dsn = "mysql:host=$this->host;dbname=$this->database;charset=$this->charset"; + try { + $this->pdo = new PDO($dsn, $this->user, $this->password, $this->pdoConnectionOptions); + } catch (Exception $e) { + error_log($e->getMessage()); + exit($this->databaseConnectionErrorMessage); + } + } + + function newSession($userName, $password){ + $real = 'slothness'; + + if($this->cryptographyService->decryptPassword($real, $password)){ + + } + } +} +?> \ No newline at end of file diff --git a/api-payroll/src/dependencies.php b/api-payroll/src/dependencies.php index a2c576e..61f49fd 100644 --- a/api-payroll/src/dependencies.php +++ b/api-payroll/src/dependencies.php @@ -21,7 +21,16 @@ $container['logger'] = function ($c) { // Cryto functions $container['cryptographyService'] = function ($c) { $cryptographySettings = $c->get('settings')['cryptography']; - require dirname(__FILE__) . "/../src/service/cryptography.php"; - $cryptographyService = new cryptographyService($cryptographySettings); + $cryptographyService = new App\Service\CryptographyService($cryptographySettings); return $cryptographyService; }; + +// The session application +$container['sessionApplication'] = function ($c) { + $cryptographySettings = $c->get('settings')['cryptography']; + $cryptographyService = new App\Service\CryptographyService($cryptographySettings); + + $mysqlSettings = $c->get('settings')['mysql']; + $sessionApplication = new App\Application\SessionApplication($mysqlSettings, $cryptographyService); + return $sessionApplication; +}; \ No newline at end of file diff --git a/api-payroll/src/routes.php b/api-payroll/src/routes.php index 00d1592..d588aed 100644 --- a/api-payroll/src/routes.php +++ b/api-payroll/src/routes.php @@ -11,4 +11,35 @@ $app->get('/[{name}]', function (Request $request, Response $response, array $ar // Render index view return $this->renderer->render($response, 'index.phtml', $args); +}); + + +$app->post('/api/session/login', function ($request, $response) { + $RequestData = $request->getParsedBody(); + + $data = $this->sessionApplication->newSession($RequestData['userName'], $RequestData['password']); + + return $response->withStatus(200) + ->withHeader('Content-Type', 'application/json') + ->write(json_encode($data)); +}); + + +$app->get('/api/encrypt/{string}', function (Request $request, Response $response, array $args) { + return $this->cryptographyService->encryptString($args['string']); +}); + +$app->get('/api/decrypt/{string}', function (Request $request, Response $response, array $args) { + return $this->cryptographyService->decryptString($args['string']); +}); + +$app->get('/api/encrypt/password/{string}', function (Request $request, Response $response, array $args) { + return $this->cryptographyService->encryptPassword($args['string']); +}); + +$app->get('/api/decrypt/password/{string}', function (Request $request, Response $response, array $args) { + $cosa = $this->cryptographyService->decryptPassword("pablso", "$2y$12$4T.gxWkQNPPFQau7ghfiQegdJQOm1yLTlbOTvcI3AizyqF/JSHr06"); + if ($cosa){ + return "yea"; + } }); \ No newline at end of file diff --git a/api-payroll/src/service/cryptography.php b/api-payroll/src/service/CryptographyService.php similarity index 97% rename from api-payroll/src/service/cryptography.php rename to api-payroll/src/service/CryptographyService.php index 6183ddd..9e3dcca 100644 --- a/api-payroll/src/service/cryptography.php +++ b/api-payroll/src/service/CryptographyService.php @@ -1,4 +1,6 @@ settings = $cryptographySettings; @@ -19,7 +21,7 @@ class cryptographyService{ * * @param $text string * @return string - * @throws Exception + * @throws \Exception */ function encryptString($text){ try { diff --git a/api-payroll/src/settings.php b/api-payroll/src/settings.php index e8c6719..54893f4 100644 --- a/api-payroll/src/settings.php +++ b/api-payroll/src/settings.php @@ -23,5 +23,22 @@ return [ 'passwordHashCost' => '12', 'ivSize' => 16, // 128 bits ], + + // Datanase settings + 'mysql' => [ + 'host' => 'localhost', + 'database' => 'payroll', + 'user' => 'root', + 'password' => '12345678', + 'charset' => 'utf8', + 'pdoConnectionOptions' => [ + PDO::ATTR_EMULATE_PREPARES => true, // The querys will be prepared by pdo instead of the dbms + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Errors will be returned as exceptions + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Data will be returned in associative arrays + ], + 'databaseConnectionErrorMessage' => 'Unable to connect to the database.', + 'databaseSelectQueryErrorMessage' => 'There was an error fetching the data.', + 'databaseInsertQueryErrorMessage' => 'There was an error inserting the record.', + ], ], ];