[add] Setting up the middleware

This commit is contained in:
2018-08-15 23:41:01 -06:00
parent 86a0540da7
commit df33525a4b
7 changed files with 189 additions and 12 deletions

View File

@@ -4,11 +4,13 @@ namespace App\Application;
use Exception;
class SessionApplication{
private $session;
private $pdo;
private $cryptographyService;
private $asserts;
function __construct($mysql, $cryptographyService, $asserts){
function __construct($session, $mysql, $cryptographyService, $asserts){
$this->session = $session;
$this->cryptographyService = $cryptographyService;
$this->pdo = $mysql;
$this->asserts = $asserts;
@@ -18,7 +20,8 @@ class SessionApplication{
* @return bool
*/
function verifySession(){
return isset($_SESSION['userName']);
$userName = $this->session->get('userName');
return isset($userName);
}
/**
@@ -30,7 +33,7 @@ class SessionApplication{
$session['loggedIn'] = $this->verifySession();
if($this->verifySession()){
$session['userName'] = $_SESSION['userName'];
$session['userName'] = $this->session->get('userName');
}
return $session;
@@ -83,7 +86,7 @@ class SessionApplication{
}
if($this->cryptographyService->decryptPassword($password, $storedPassword)){
$_SESSION['userName'] = $userName;
$this->session->set('userName', $userName);
return true;
}
else{
@@ -118,7 +121,7 @@ class SessionApplication{
* @return array
*/
function destroySession(){
session_destroy();
$this->session->clear();
return array('status' => 'success', 'message' => 'Successfully logged out.');
}

View File

@@ -18,6 +18,13 @@ $container['logger'] = function ($c) {
return $logger;
};
// Session handler
$container['session'] = function ($container) {
return new \Adbar\Session(
$container->get('settings')['session']['namespace']
);
};
// Mysql connection
$container['mysql'] = function ($c) {
$mysqlSettings = $c->get('settings')['mysql'];
@@ -60,7 +67,8 @@ $container['asserts'] = function ($c) {
// The session application
$container['sessionApplication'] = function ($c) {
$sessionApplication = new App\Application\SessionApplication($c['mysql'], $c['cryptographyService'], $c['asserts']);
$sessionApplication = new App\Application\SessionApplication($c['session'], $c['mysql'],
$c['cryptographyService'], $c['asserts']);
return $sessionApplication;
};

View File

@@ -19,4 +19,6 @@ $app->add(new \Tuupola\Middleware\Cors([
->withHeader("Content-Type", "application/json")
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
]));
]));
$app->add(new \Adbar\SessionMiddleware($app->getContainer()->get('settings')['session']));

View File

@@ -16,6 +16,35 @@ return [
'level' => \Monolog\Logger::DEBUG,
],
// Session handle settings
'session' => [
// Session cookie settings
'name' => 'payroll-laziness-rocks',
'lifetime' => 1,
'path' => '/',
'domain' => "laziness.rocks",
'secure' => false,
'httponly' => true,
// Set session cookie path, domain and secure automatically
'cookie_autoset' => true,
// Path where session files are stored, PHP's default path will be used if set null
'save_path' => null,
// Session cache limiter
'cache_limiter' => 'nocache',
// Extend session lifetime after each user activity
'autorefresh' => false,
// Encrypt session data if string is set
'encryption_key' => '7de431684c34cf2c898268cff71392f38c4175dde050c9ee69502b81571484e0',
// Session namespace
'namespace' => 'slim'
],
// Cryptography settings
'cryptography' => [
'encryptionAlgorithm' => 'AES-256-CBC',