Skip to content

Commit

Permalink
升级token管理机制
Browse files Browse the repository at this point in the history
  • Loading branch information
ilovintit committed May 17, 2020
1 parent c973de7 commit e59baa9
Showing 1 changed file with 121 additions and 95 deletions.
216 changes: 121 additions & 95 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,143 @@

namespace ZhiEq\ApiTokenAuth;

use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;
use Psr\SimpleCache\InvalidArgumentException;

class Token
{
protected $headerKey;
protected $headerKey;

protected $cacheKey;
protected $cacheKey;

protected $expired;
protected $expired;

protected $autoRefresh;
protected $autoRefresh;

private $_token;
protected $redisCon;

/**
* ApiToken constructor.
* @param $config
*/
private $_token;

public function __construct($config)
{
if (!isset($config['cache']) || empty($config['cache'])) {
throw new \RuntimeException('config key "cache" can\'t empty.');
/**
* ApiToken constructor.
* @param $config
*/

public function __construct($config)
{
if (!isset($config['cache']) || empty($config['cache'])) {
throw new \RuntimeException('config key "cache" can\'t empty.');
}
if (!isset($config['header']) || empty($config['header'])) {
throw new \RuntimeException('config header "cache" can\'t empty.');
}
$this->headerKey = $config['header'];
$this->cacheKey = $config['cache'];
$this->expired = isset($config['expired']) ? $config['expired'] : 120;
$this->autoRefresh = isset($config['refresh']) ? $config['refresh'] : true;
$this->redisCon = isset($config['redis']) ? $config['redis'] : 'cache';
}

/**
* @return array|string
*/

public function getToken()
{
if ($this->_token === null) {
$this->_token = Request::instance()->header($this->headerKey);
}
return $this->_token;
}
if (!isset($config['header']) || empty($config['header'])) {
throw new \RuntimeException('config header "cache" can\'t empty.');

/**
* @return null
*/

protected function getCode()
{
try {
$cacheCode = Redis::connection($this->redisCon)->get($this->redisCacheKey());
return empty($cacheCode) ? null : unserialize($cacheCode);
} catch (\Exception $exception) {
return null;
}
}

/**
* @return null
*/

public function getUserCode()
{
try {
$userCode = $this->getCode();
$this->autoRefresh === true && $userCode !== null && $this->cacheUserCode($userCode);
return $userCode;
} catch (\Exception $exception) {
logs()->error($exception);
return null;
}
}
$this->headerKey = $config['header'];
$this->cacheKey = $config['cache'];
$this->expired = isset($config['expired']) ? $config['expired'] : 120;
$this->autoRefresh = isset($config['refresh']) ? $config['refresh'] : true;
}

/**
* @return array|string
*/

public function getToken()
{
if ($this->_token === null) {
$this->_token = Request::instance()->header($this->headerKey);

/**
*
*/

public function clearUserCode()
{
try {
$this->getCode() && Redis::connection($this->redisCon)->forget($this->redisCacheKey());
} catch (\Exception $exception) {
logs()->error($exception);
}
}
return $this->_token;
}

/**
* @return null
*/

protected function getCode()
{
try {
return cache()->tags($this->cacheKey)->get($this->getToken());
} catch (\Exception $exception) {
return null;

/**
*
*/

protected function generateToken()
{
$this->_token = Str::random(40);
return $this;
}
}

/**
* @return null
*/

public function getUserCode()
{
try {
$userCode = $this->getCode();
if ($this->autoRefresh === true && $userCode !== null) {
cache()->tags($this->cacheKey)->put($this->getToken(), $userCode, Carbon::now()->addMinutes($this->expired));
}
return $userCode;
} catch (\Exception $exception) {
logs()->error($exception);
return null;

/**
* @param $userCode
*/

public function setUserCode($userCode)
{
try {
$this->generateToken()->cacheUserCode($userCode);
} catch (\Exception $exception) {
logs()->error($exception);
}
}
}

/**
*
*/

public function clearUserCode()
{
try {
$this->getCode() && cache()->tags($this->cacheKey)->forget($this->getToken());
} catch (\Exception $exception) {
logs()->error($exception);

/**
* @param $userCode
* @return bool
*/

protected function cacheUserCode($userCode)
{
try {
Redis::connection($this->redisCon)->set($this->redisCacheKey(), serialize($userCode), 'EX', $this->expired * 60);
} catch (InvalidArgumentException $e) {
return false;
}
}
}

/**
*
*/

protected function generateToken()
{
$this->_token = Str::random(40);
}

/**
* @param $userCode
*/

public function setUserCode($userCode)
{
try {
$this->generateToken();
cache()->tags($this->cacheKey)->add($this->getToken(), $userCode, $this->expired);
} catch (\Exception $exception) {
logs()->error($exception);

/**
* @return string
*/

protected function redisCacheKey()
{
return config('cache.prefix') . ':' . $this->cacheKey . $this->getToken();
}
}
}

0 comments on commit e59baa9

Please sign in to comment.