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

Commit

Permalink
perf(JWT): Short JWT payload key
Browse files Browse the repository at this point in the history
1. Use official key `aud` to store user_id.
2. Short `secure_login_ip` to `ip`
  • Loading branch information
Rhilip committed Aug 15, 2019
1 parent 6f11931 commit 7895158
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

### Fix
- **Auth/Login:** Fix user can't login after commit `6009dc8` (d509127)
- **Component:** Fix parent::onRequest{Before,After} miss (200926f)
- **User:** Fix User Class miss in string format (3680444)

### Perf
Expand Down
10 changes: 5 additions & 5 deletions apps/components/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ protected function loadCurUserIdFromCookies()

$payload = JWTHelper::decode($user_session);
if ($payload === false) return false;
if (!isset($payload['jti']) || !isset($payload['user_id'])) return false;
if (!isset($payload['jti']) || !isset($payload['aud'])) return false;

// Check if user lock access ip ?
if (isset($payload['secure_login_ip'])) {
if (isset($payload['ip'])) {
$now_ip_crc = sprintf('%08x', crc32(app()->request->getClientIp()));
if (strcasecmp($payload['secure_login_ip'], $now_ip_crc) !== 0) return false;
if (strcasecmp($payload['ip'], $now_ip_crc) !== 0) return false;
}

// Verity $jti is force expired or not by checking mapUserSessionToId
Expand All @@ -101,7 +101,7 @@ protected function loadCurUserIdFromCookies()
])->queryScalar();
app()->redis->zAdd(Constant::mapUserSessionToId, $uid ?: 0, $payload['jti']); // Store 0 if session -> uid is invalid
if ($uid === false) return false; // this session is not exist or marked as expired
} elseif ($expired_check != $payload['user_id']) return false; // may return (double) 0 , which means already make invalid ; or it check if user obtain this session (may Overdesign)
} elseif ($expired_check != $payload['aud']) return false; // may return (double) 0 , which means already make invalid ; or it check if user obtain this session (may Overdesign)

$this->cur_user_jit = $payload['jti'];

Expand All @@ -114,7 +114,7 @@ protected function loadCurUserIdFromCookies()
app()->response->setHeader('Strict-Transport-Security', 'max-age=1296000; includeSubDomains');
}

return $payload['user_id'];
return $payload['aud'];
}

protected function loadCurUserIdFromPasskey()
Expand Down
10 changes: 6 additions & 4 deletions apps/models/form/Auth/UserLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public function flush()
private function createUserSession()
{
$timenow = time();
$login_ip = app()->request->getClientIp();

do { // Generate unique JWT ID
$jti = StringHelper::getRandomString(64);
Expand All @@ -155,6 +154,7 @@ private function createUserSession()
$payload = [
'iss' => config('base.site_url'),
'sub' => config('base.site_generator'),
'aud' => $this->self['id'], // Store User Id so we can quick check session status and load their information
'iat' => $timenow,
'jti' => $jti,
];
Expand All @@ -166,9 +166,11 @@ private function createUserSession()
$payload['exp'] = $cookieExpire;

// Custom Payload key
$payload['user_id'] = $this->self['id']; // Store User Id so we can quick load their information
if ($this->securelogin === 'yes' || config('security.secure_login') > 1)
$payload['secure_login_ip'] = sprintf('%08x', crc32($login_ip)); // Store User Login IP ( in CRC32 format )
if ($this->securelogin === 'yes' || config('security.secure_login') > 1) {
$login_ip = app()->request->getClientIp();
$payload['ip'] = sprintf('%08x', crc32($login_ip)); // Store User Login IP ( in CRC32 format )
}

if ($this->ssl || config('security.ssl_login') > 1)
$payload['ssl'] = true; // Store User want full ssl protect

Expand Down
2 changes: 1 addition & 1 deletion framework/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Session extends Component
{
// 保存的Key前缀
public $saveKeyPrefix = 'SESSION:';
public $saveKeyPrefix = 'Session:';

// 生存时间
public $maxLifetime = 7200;
Expand Down

0 comments on commit 7895158

Please sign in to comment.