Skip to content

Commit

Permalink
Fixed cache key repetition (swoft-cloud/swoft-component#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
aprchen authored and huangzhhui committed Sep 7, 2018
1 parent 5aba246 commit 4d3efab
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function isLoggedIn()
* @param array $data
* @return AuthSession
*/
public function login(string $accountTypeName, array $data):AuthSession
public function login(string $accountTypeName, array $data): AuthSession
{
if (!$account = $this->getAccountType($accountTypeName)) {
throw new AuthException(ErrorCode::AUTH_INVALID_ACCOUNT_TYPE);
Expand All @@ -121,7 +121,7 @@ public function login(string $accountTypeName, array $data):AuthSession
if ($this->cacheEnable === true) {
try {
$this->getCacheClient()->set(
$this->getCacheKey($result->getIdentity()),
$this->getCacheKey($result->getIdentity(), $result->getExtendedData()),
$session->getToken(),
$session->getExpirationTime()
);
Expand All @@ -133,9 +133,12 @@ public function login(string $accountTypeName, array $data):AuthSession
return $session;
}

protected function getCacheKey($identity)
protected function getCacheKey(string $identity, array $extendedData)
{
return $this->prefix . $identity;
if (empty($extendedData)) {
return $this->prefix . $identity;
}
return $this->prefix . $identity . (string)$extendedData[0];
}

/**
Expand Down Expand Up @@ -218,7 +221,7 @@ public function getCacheClient()
* @return bool
* @throws AuthException
*/
public function authenticateToken(string $token):bool
public function authenticateToken(string $token): bool
{
try {
/** @var AuthSession $session */
Expand All @@ -245,7 +248,7 @@ public function authenticateToken(string $token):bool

if ($this->cacheEnable === true) {
try {
$cache = $this->getCacheClient()->get($this->getCacheKey($session->getIdentity()));
$cache = $this->getCacheClient()->get($this->getCacheKey($session->getIdentity(), $session->getExtendedData()));
if (!$cache || $cache !== $token) {
throw new AuthException(ErrorCode::AUTH_TOKEN_INVALID);
}
Expand Down

0 comments on commit 4d3efab

Please sign in to comment.