fix(auth): 修复鉴权

This commit is contained in:
李东云
2022-08-18 10:51:25 +08:00
parent 0300311b74
commit 4ca20324de

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Singularity\HyperfSaml\Services\Sp; namespace Singularity\HyperfSaml\Services\Sp;
use Hyperf\Utils\Codec\Json;
use Swoole\Exception; use Swoole\Exception;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@@ -38,22 +39,12 @@ class Sso
private string $acsUrl; private string $acsUrl;
private string $issuer; private string $issuer;
/**
* @Inject
* @var \Hyperf\Redis\Redis
*/
private Redis $redis;
/**
* @Inject()
* @var \Hyperf\Contract\StdoutLoggerInterface
*/
private StdoutLoggerInterface $stdoutLogger;
public function __construct( public function __construct(
private Base $base, private Base $base,
private RequestInterface $request, private RequestInterface $request,
private ResponseInterface $response, private ResponseInterface $response,
private Redis $redis,
private StdoutLoggerInterface $stdoutLogger
) { ) {
$this->idpId = config('saml.server.idp_id'); $this->idpId = config('saml.server.idp_id');
$this->idpAssertionUrl = config('saml.server.idp_assertion_url'); $this->idpAssertionUrl = config('saml.server.idp_assertion_url');
@@ -123,6 +114,7 @@ class Sso
$type = $this->redis->type($key); $type = $this->redis->type($key);
try { try {
$this->stdoutLogger->info('user_token redis type: ' . $type);
switch ($type) { switch ($type) {
case SWOOLE_REDIS_TYPE_STRING: case SWOOLE_REDIS_TYPE_STRING:
// 旧的值 // 旧的值
@@ -134,10 +126,13 @@ class Sso
break; break;
case SWOOLE_REDIS_TYPE_HASH: case SWOOLE_REDIS_TYPE_HASH:
$origin_token = $this->redis->hKeys($key); $origin_token = $this->redis->hKeys($key);
$this->stdoutLogger->info('user_token redis keys: ' . Json::encode($origin_token));
$origin_token = array_pop($origin_token); $origin_token = array_pop($origin_token);
$latest_token = $this->redis->exists($key . ':' . $origin_token); $this->stdoutLogger->info('user_token redis key: ' . $origin_token);
$latest_token = $this->redis->hGet($key, $origin_token);
$this->stdoutLogger->info('user_token redis value isset: ' . $latest_token);
if (empty($latest_token)) { if (empty($latest_token) || $latest_token !== $token) {
throw new Exception(code: CommonErrorCode::AUTH_SESSION_CREATED_AT_ERROR); throw new Exception(code: CommonErrorCode::AUTH_SESSION_CREATED_AT_ERROR);
} }
break; break;