[add] Login endpoint
This commit is contained in:
parent
3fe49d894d
commit
8b09f75d3a
@ -25,6 +25,12 @@
|
||||
"Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\Service\\": "src/service",
|
||||
"App\\Application\\": "src/application"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"process-timeout" : 0
|
||||
},
|
||||
|
2
api-payroll/composer.lock
generated
2
api-payroll/composer.lock
generated
@ -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": [
|
||||
{
|
||||
|
43
api-payroll/src/application/SessionApplication.php
Normal file
43
api-payroll/src/application/SessionApplication.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace App\Application;
|
||||
|
||||
class SessionApplication{
|
||||
// The to be connection
|
||||
private $pdo = '';
|
||||
|
||||
function __construct($mysqlSettings, $cryptographyService){
|
||||
// Services
|
||||
$this->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)){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@ -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;
|
||||
};
|
@ -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";
|
||||
}
|
||||
});
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
/**
|
||||
* A collection of functions to securely handling sensitive data,
|
||||
* passwords as well as making use of other crypto needs within
|
||||
@ -7,7 +9,7 @@
|
||||
* @property settings
|
||||
*/
|
||||
|
||||
class cryptographyService{
|
||||
class CryptographyService{
|
||||
|
||||
function __construct($cryptographySettings) {
|
||||
$this->settings = $cryptographySettings;
|
||||
@ -19,7 +21,7 @@ class cryptographyService{
|
||||
*
|
||||
* @param $text string
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
function encryptString($text){
|
||||
try {
|
@ -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.',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user