Skip to content

Commit 727fea0

Browse files
committed
Added session based login
1 parent 58056a7 commit 727fea0

File tree

8 files changed

+83
-46
lines changed

8 files changed

+83
-46
lines changed

web/includes/CUserManager.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ class CUserManager
3838
* @param $password the current user's password
3939
* @return noreturn.
4040
*/
41-
public function __construct($aid, $password)
41+
public function __construct($aid)
4242
{
4343
$this->dbh = new Database(DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS, DB_PREFIX);
4444

45-
if ($this->CheckLogin($password, $aid)) {
46-
$this->aid = $aid;
47-
$this->GetUserArray($aid);
48-
}
45+
$this->aid = $aid;
46+
$this->GetUserArray($aid);
4947
}
5048

5149

@@ -63,7 +61,7 @@ public function GetUserArray($aid = null)
6361
}
6462
// Invalid aid
6563
if ($aid < 0 || empty($aid)) {
66-
return 0;
64+
return false;
6765
}
6866

6967
// We already got the data from the DB, and its saved in the manager
@@ -82,7 +80,7 @@ public function GetUserArray($aid = null)
8280
$res = $this->dbh->single();
8381

8482
if (!$res) {
85-
return 0; // ohnoes some type of db error
83+
return false; // ohnoes some type of db error
8684
}
8785

8886
$user = array();
@@ -212,30 +210,24 @@ public function CheckLogin($password, $aid)
212210

213211
public function login($aid, $password, $save = true)
214212
{
215-
if ($this->CheckLogin($this->encrypt_password($password), $aid)) {
213+
if ($this->CheckLogin($this->encrypt_password($password), $aid) || $this->CheckLogin($this->hash($password), $aid)) {
216214
//Old password hash detected update it.
217215
$this->dbh->query('UPDATE `:prefix_admins` SET password = :password WHERE aid = :aid');
218-
$this->dbh->bind(':password', $this->hash($password));
216+
$this->dbh->bind(':password', password_hash($password, PASSWORD_BCRYPT));
219217
$this->dbh->bind(':aid', $aid);
220218
$this->dbh->execute();
221219

222-
setcookie("aid", $aid);
223-
setcookie("password", $this->hash($password));
224-
setcookie("user", $_SESSION['user']['user']);
220+
\SessionManager::sessionStart('login', 604800, 0);
221+
$_SESSION['aid'] = $aid;
225222
return true;
226223
}
227224

228-
if ($this->CheckLogin($this->hash($password), $aid)) {
229-
if ($save) {
230-
//Sets cookies
231-
setcookie("aid", $aid, time()+LOGIN_COOKIE_LIFETIME);
232-
setcookie("password", $this->hash($password), time()+LOGIN_COOKIE_LIFETIME);
233-
setcookie("user", isset($_SESSION['user']['user'])?$_SESSION['user']['user']:null, time()+LOGIN_COOKIE_LIFETIME);
234-
return true;
235-
}
236-
setcookie("aid", $aid);
237-
setcookie("password", $this->hash($password));
238-
setcookie("user", $_SESSION['user']['user']);
225+
$this->dbh->query('SELECT password FROM `:prefix_admins` WHERE aid = :aid');
226+
$this->dbh->bind(':aid', $aid);
227+
$hash = $this->dbh->single();
228+
if (password_verify($password, $hash['password'])) {
229+
\SessionManager::sessionStart('login', 604800, 0);
230+
$_SESSION['aid'] = $aid;
239231
return true;
240232
}
241233
return false;

web/includes/SessionManager.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
class SessionManager
3+
{
4+
public static function sessionStart($name, $expires = 86400, $limit = 0, $path = '/', $domain = null)
5+
{
6+
$secure = false;
7+
session_name($name.'_Session');
8+
$domain = isset($domain) ? $domain : $_SERVER['SERVER_NAME'];
9+
if ($_SERVER['SERVER_PORT'] == 443) {
10+
$secure = true;
11+
}
12+
session_set_cookie_params($limit, $path, $domain, $secure, true);
13+
session_start();
14+
15+
$_SESSION['userAgent'] = hash('sha256', $_SERVER['HTTP_USER_AGENT']);
16+
$_SESSION['EXPIRES'] = time()+$expires;
17+
}
18+
public static function checkSession()
19+
{
20+
if (!isset($_SESSION['userAgent'])) {
21+
return false;
22+
}
23+
if (!self::validateSession() || !self::preventHijacking()) {
24+
session_destroy();
25+
session_start();
26+
return false;
27+
} elseif (rand(1, 100) <= 10) {
28+
self::regenerateSession();
29+
}
30+
return true;
31+
}
32+
protected static function preventHijacking()
33+
{
34+
if (!isset($_SESSION['userAgent'])) {
35+
return false;
36+
}
37+
if ($_SESSION['userAgent'] !== hash('sha256', $_SERVER['HTTP_USER_AGENT'])) {
38+
return false;
39+
}
40+
return true;
41+
}
42+
protected static function regenerateSession()
43+
{
44+
$_SESSION['EXPIRES'] = time() + 10;
45+
session_regenerate_id(false);
46+
$newSession = session_id();
47+
session_write_close();
48+
session_id($newSession);
49+
session_start();
50+
unset($_SESSION['EXPIRES']);
51+
}
52+
protected static function validateSession()
53+
{
54+
if (isset($_SESSION['EXPIRES']) && $_SESSION['EXPIRES'] < time()) {
55+
return false;
56+
}
57+
return true;
58+
}
59+
}

web/includes/sb-callback.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
require_once('xajax.inc.php');
2929
include_once('system-functions.php');
3030
include_once('user-functions.php');
31+
session_start();
3132
$xajax = new xajax();
3233
//$xajax->debugOn();
3334
$xajax->setRequestURI(XAJAX_REQUEST_URI);
3435
global $userbank;
3536

36-
if(isset($_COOKIE['aid'], $_COOKIE['password']) && $userbank->CheckLogin($_COOKIE['password'], $_COOKIE['aid']))
37-
{
37+
if (\SessionManager::checkSession()) {
3838
$xajax->registerFunction("AddMod");
3939
$xajax->registerFunction("RemoveMod");
4040
$xajax->registerFunction("AddGroup");
@@ -94,7 +94,6 @@
9494
global $userbank;
9595
$username = $userbank->GetProperty("user");
9696

97-
9897
function Plogin($username, $password, $remember, $redirect, $nopass)
9998
{
10099
global $userbank;

web/includes/user-functions.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ function generate_salt($length = 5)
6363
*/
6464
function logout()
6565
{
66-
setcookie('aid', '', time()-86400);
67-
setcookie('password', '', time()-86400);
6866
$_SESSION = array();
6967
session_destroy();
7068
return true;

web/init.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
Licensed under CC BY-NC-SA 3.0
2525
Page: <http://www.sourcebans.net/> - <http://www.gameconnect.net/>
2626
*************************************************************************/
27+
session_start();
28+
2729
//Hotfix for dash_intro_text
2830
if (isset($_POST['dash_intro_text'])) {
2931
$dash_intro_text = $_POST['dash_intro_text'];
@@ -55,6 +57,7 @@
5557
define('SB_AID', isset($_COOKIE['aid'])?$_COOKIE['aid']:null);
5658
define('XAJAX_REQUEST_URI', './index.php');
5759

60+
require_once(INCLUDES_PATH.'/SessionManager.php');
5861
include_once(INCLUDES_PATH . "/CSystemLog.php");
5962
include_once(INCLUDES_PATH . "/CUserManager.php");
6063
include_once(INCLUDES_PATH . "/CUI.php");
@@ -361,4 +364,4 @@ function sbError($errno, $errstr, $errfile, $errline)
361364
// ---------------------------------------------------
362365
// Setup our user manager
363366
// ---------------------------------------------------
364-
$userbank = new CUserManager(isset($_COOKIE['aid'])?$_COOKIE['aid']:'', isset($_COOKIE['password'])?$_COOKIE['password']:'');
367+
$userbank = new CUserManager(isset($_SESSION['aid']) ? $_SESSION['aid'] : -1);

web/install/index.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
Page: <http://www.sourcebans.net/> - <http://www.gameconnect.net/>
2626
*************************************************************************/
2727

28-
session_start();
2928
include_once 'init.php';
3029
include_once(INCLUDES_PATH . "/user-functions.php");
3130
include_once(INCLUDES_PATH . "/system-functions.php");

web/install/template/page.5.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
define('STEAMAPIKEY', '{steamapikey}'); // Steam API Key for Shizz
2828
define('SB_WP_URL', '{sbwpurl}'); //URL of SourceBans Site
2929
define('SB_EMAIL', '{sbwpemail}');
30-
define('SB_NEW_SALT', '{sbsalt}');
3130
3231
//define('DEVELOPER_MODE', true); // Use if you want to show debugmessages
3332
//define('SB_MEM', '128M'); // Override php memory limit, if isn't enough (Banlist is just a blank page)
@@ -47,14 +46,6 @@
4746
}
4847
';
4948

50-
/* Generate random salt for each SourceBans installation
51-
* Note: this is not good you should assign every user a unique salt
52-
* and because of the codebase we can only assign one salt without fucking parts
53-
* of SoureBans up. (it's safer than the currently used salt which is the same on every instance of SourceBans)
54-
*/
55-
require_once('../includes/random_compat/lib/random.php');
56-
$salt = "$5$".strtr(substr(base64_encode(random_bytes(16)), 0, 16), '+/', '-_');
57-
5849
$web_cfg = str_replace("{server}", $_POST['server'], $web_cfg);
5950
$web_cfg = str_replace("{user}", $_POST['username'], $web_cfg);
6051
$web_cfg = str_replace("{pass}", $_POST['password'], $web_cfg);
@@ -65,7 +56,6 @@
6556
$web_cfg = str_replace("{steamapikey}", $_POST['apikey'], $web_cfg);
6657
$web_cfg = str_replace("{sbwpurl}", $_POST['sb-wp-url'], $web_cfg);
6758
$web_cfg = str_replace("{sbwpemail}", $_POST['sb-email'], $web_cfg);
68-
$web_cfg = str_replace("{sbsalt}", $salt, $web_cfg);
6959

7060
$srv_cfg = str_replace("{server}", $_POST['server'], $srv_cfg);
7161
$srv_cfg = str_replace("{user}", $_POST['username'], $srv_cfg);
@@ -92,8 +82,7 @@
9282
$db->query('INSERT INTO `:prefix_admins` (user, authid, password, gid, email, extraflags, immunity) VALUES (:user, :authid, :password, :gid, :email, :extraflags, :immunity)');
9383
$db->bind(':user', $_POST['uname']);
9484
$db->bind(':authid', $_POST['steam']);
95-
//$db->bind(':password', sha1(sha1(SB_SALT . $_POST['pass1'])));
96-
$db->bind(':password', crypt($_POST['pass1'], $salt));
85+
$db->bind(':password', password_hash($_POST['pass1'], PASSWORD_BCRYPT));
9786
$db->bind(':gid', -1);
9887
$db->bind(':email', $_POST['email']);
9988
$db->bind(':extraflags', (1<<24));
@@ -223,7 +212,6 @@
223212
<input type="hidden" name="sb-wp-url" value="<?php echo $_POST['sb-wp-url']?>">
224213
<input type="hidden" name="sb-email" value="<?php echo $_POST['sb-email']?>">
225214
<input type="hidden" name="charset" value="<?php echo $_POST['charset']?>">
226-
<input type="hidden" name="sbsalt" value="<?php echo $salt?>">
227215
</div>
228216
</form>
229217

web/steamopenid.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*************************************************************************/
2727
// Steam Login by @duhowpi 2015
2828

29-
session_start();
3029
include_once 'init.php';
3130
include_once 'config.php';
3231
require_once 'includes/openid.php';
@@ -83,8 +82,8 @@ function convert64to32(Database $dbs, $communityID)
8382
header("Location: " . SB_URL . "/index.php?p=login&m=empty_pwd");
8483
die;
8584
} else {
86-
setcookie("aid", $result['aid'], time() + LOGIN_COOKIE_LIFETIME);
87-
setcookie("password", $result['password'], time() + LOGIN_COOKIE_LIFETIME);
85+
\SessionManager::sessionStart('login', 604800, 0);
86+
$_SESSION['aid'] = $result['aid'];
8887
}
8988
}
9089
} else {

0 commit comments

Comments
 (0)