Files
zyt/server/app/common/service/iam/IamExchangeService.php
T

39 lines
868 B
PHP

<?php
declare(strict_types=1);
namespace app\common\service\iam;
use think\facade\Cache;
class IamExchangeService
{
private int $ttlSeconds;
public function __construct(int $ttlSeconds = 60)
{
$this->ttlSeconds = $ttlSeconds;
}
public function store(array $loginResult): string
{
$code = IamSecurity::base64UrlEncode(random_bytes(32));
Cache::set($this->key($code), $loginResult, max(30, $this->ttlSeconds));
return $code;
}
public function consume(string $code): ?array
{
if (strlen($code) < 20 || strlen($code) > 200) {
return null;
}
$value = Cache::pull($this->key($code));
return is_array($value) ? $value : null;
}
private function key(string $code): string
{
return 'iam_login_exchange_' . hash('sha256', $code);
}
}