Skip to content

Commit

Permalink
GH-150 Move cookie login handling to a separate unit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Feb 9, 2021
1 parent 3727c2a commit 81c7a6c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 34 deletions.
53 changes: 19 additions & 34 deletions login.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,29 @@
$Search['error'] = 1;
}
} else if (!empty($_COOKIE[$sessionCookieKey])) {
$Search['mode'] = 2;

$verificationResult = Session\Utils\Cookie\verifySessionCookie([
'userEntityFetcher' => function ($fetcherParams) {
$userId = $fetcherParams['userId'];

$Query_GetUser = '';
$Query_GetUser .= "SELECT `id`, `username`, `password`, `isAI` ";
$Query_GetUser .= "FROM {{table}} ";
$Query_GetUser .= "WHERE `id` = {$userId} LIMIT 1;";

return doquery($Query_GetUser, 'users');
},
]);

if (!$verificationResult['isSuccess']) {
switch ($verificationResult['error']['code']) {
case 'INVALID_USER_ID':
$Search['error'] = 2;
break;
case 'USER_NOT_FOUND':
$Search['error'] = 3;
break;
case 'INVALID_PASSWORD':
$Search['error'] = 4;
break;
}
$loginAttemptResult = Session\Input\CookieLogin\handleCookieLogin([]);

setcookie($sessionCookieKey, false, 0, '/', '');
} else {
include_once($_EnginePath . '/includes/functions/IPandUA_Logger.php');
if ($loginAttemptResult['isSuccess']) {
Session\Utils\Redirects\redirectToOverview();

$UserData = $verificationResult['payload']['userEntity'];
die();
}

IPandUA_Logger($UserData);
$Search['mode'] = 2;

header("Location: ./overview.php");
die();
switch ($loginAttemptResult['error']['code']) {
case 'NO_COOKIE':
$Search['error'] = 2;
break;
case 'INVALID_USER_ID':
$Search['error'] = 2;
break;
case 'USER_NOT_FOUND':
$Search['error'] = 3;
break;
case 'INVALID_PASSWORD':
$Search['error'] = 4;
break;
}
}

Expand Down
3 changes: 3 additions & 0 deletions modules/session/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

$includePath = $_EnginePath . 'modules/session/';

include($includePath . './input/cookieLogin.inputHandler.php');

include($includePath . './screens/LoginView/LoginView.component.php');
include($includePath . './screens/LoginView/components/LoginForm/LoginForm.component.php');

include($includePath . './utils/cookie.utils.php');
include($includePath . './utils/rateLimiter.utils.php');
include($includePath . './utils/redirects.utils.php');

});

Expand Down
65 changes: 65 additions & 0 deletions modules/session/input/cookieLogin.inputHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace UniEngine\Engine\Modules\Session\Input\CookieLogin;

use UniEngine\Engine\Modules\Session;

// Arguments:
// - $params
//
function handleCookieLogin($params) {
global $_EnginePath;

$createSuccess = function ($payload) {
return [
'isSuccess' => true,
'payload' => $payload,
];
};
$createFailure = function ($error) {
return [
'isSuccess' => false,
'error' => $error,
];
};

if (!(Session\Utils\Cookie\hasSessionCookie())) {
return $createFailure([
'code' => 'NO_COOKIE',
]);
}

$verificationResult = Session\Utils\Cookie\verifySessionCookie([
'userEntityFetcher' => function ($fetcherParams) {
$userId = $fetcherParams['userId'];

$Query_GetUser = '';
$Query_GetUser .= "SELECT `id`, `username`, `password`, `isAI` ";
$Query_GetUser .= "FROM {{table}} ";
$Query_GetUser .= "WHERE `id` = {$userId} LIMIT 1;";

return doquery($Query_GetUser, 'users');
},
]);

if (!$verificationResult['isSuccess']) {
$sessionCookieKey = getSessionCookieKey();

// TODO: Side effect, move elsewhere (?)
setcookie($sessionCookieKey, false, 0, '/', '');

return $createFailure([
'code' => $verificationResult['error']['code'],
]);
}

include_once($_EnginePath . '/includes/functions/IPandUA_Logger.php');

$UserData = $verificationResult['payload']['userEntity'];

IPandUA_Logger($UserData);

return $createSuccess([]);
}

?>
5 changes: 5 additions & 0 deletions modules/session/input/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
9 changes: 9 additions & 0 deletions modules/session/utils/redirects.utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace UniEngine\Engine\Modules\Session\Utils\Redirects;

function redirectToOverview() {
header("Location: ./overview.php");
}

?>

0 comments on commit 81c7a6c

Please sign in to comment.