Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(Session): Add Session Format Docs
Browse files Browse the repository at this point in the history
record login ip information in session string without hit redis cache
(Which may lost).
  • Loading branch information
Rhilip committed Jun 5, 2019
1 parent c556644 commit a2a1ce1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
6 changes: 4 additions & 2 deletions apps/middleware/AuthByCookiesMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public function handle($callable, \Closure $next)
*/
$userSessionId = app()->request->cookie(Constant::cookie_name);
if (substr($userSessionId, 0, 1) === '1') {
$record_ip = app()->redis->hGet('Site:Sessions:secure', $userSessionId);
if (app()->request->getClientIp() !== $record_ip) { // The Ip isn't matched
$record_ip_crc = substr($userSessionId, 2, 8);
$this_ip_crc = sprintf('%08x',crc32(app()->request->getClientIp()));

if (strcasecmp($record_ip_crc,$this_ip_crc) !== 0) { // The Ip isn't matched
app()->cookie->delete(Constant::cookie_name);
return app()->response->redirect('/auth/login');
}
Expand Down
24 changes: 16 additions & 8 deletions apps/models/form/UserLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,23 @@ public function createUserSession()

$exist_session_count = app()->redis->zCount($this->sessionSaveKey, $userId, $userId);
if ($exist_session_count < app()->config->get('base.max_per_user_session')) {
/**
* SessionId Format:
* /^(?P<secure_login_flag>[01])\$(?P<ip_or_random_crc>[a-z0-9]{8})\$\w+$/
* The first character of sessionId is the Flag of secure login,
* if secure login, The second param is the sprintf('%08x',crc32($id))
* else, Another random string with length 8
* The prefix of sessionId is in lowercase
*
*/
if ($this->securelogin === 'yes') {
$sid_prefix = '1$' . sprintf('%08x',crc32(app()->request->getClientIp())) . '$';
} else {
$sid_prefix = '0$' . StringHelper::getRandomString(8) . '$';
}
$sid_prefix = strtolower($sid_prefix);
do { // To make sure this session is unique !
// The first character of sessionId is the Flag of secure login
$userSessionId = StringHelper::getRandomString($this->sessionLength - 1);
$userSessionId = ($this->securelogin === 'yes' ? '1' : '0') . $userSessionId;
$userSessionId = $sid_prefix . StringHelper::getRandomString($this->sessionLength - strlen($sid_prefix));

$count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `user_session_log` WHERE sid = :sid')->bindParams([
'sid' => $userSessionId
Expand All @@ -151,11 +164,6 @@ public function createUserSession()
// Add this session id in Redis Cache
app()->redis->zAdd($this->sessionSaveKey, $userId, $userSessionId);

// Add IP linked
if ($this->securelogin === 'yes') {
app()->redis->hSet('Site:Sessions:secure', $userSessionId, app()->request->getClientIp());
}

// Set User Cookie
$cookieExpire = $this->cookieExpires;
if ($this->logout === 'yes') {
Expand Down

0 comments on commit a2a1ce1

Please sign in to comment.