Skip to content

Commit

Permalink
token 有效期
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwu123 committed May 27, 2021
1 parent 5ab0e5e commit e3b8752
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Config
*/
protected $handler;

/**
* @var int token 有效期
*/
public $tokenExpired = 3600;


public function __construct(string $appID = '', string $appCertificate = '', string $customerKey = '', string $customerSecret = '')
{
Expand Down Expand Up @@ -240,4 +245,22 @@ public function getAppIdLen()
{
return strlen($this->getAppID());
}

/**
* @return int
*/
public function getTokenExpired(): int
{
return $this->tokenExpired;
}

/**
* @param int $tokenExpired
* @return Config
*/
public function setTokenExpired(int $tokenExpired): Config
{
$this->tokenExpired = $tokenExpired;
return $this;
}
}
2 changes: 1 addition & 1 deletion src/Token/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function extract(Config $config, $token)
*/
public function build()
{
$msg = $this->message->packContent();
$msg = $this->message->packContent($this->config->getTokenExpired());
$val = array_merge(unpack("C*", $this->config->getAppID()), unpack("C*", $this->channelName), unpack("C*", $this->uid), $msg);

$sig = hash_hmac('sha256', implode(array_map("chr", $val)), $this->config->getAppCertificate(), true);
Expand Down
4 changes: 2 additions & 2 deletions src/Token/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function __construct()
$this->privileges = array();
}

public function packContent()
public function packContent($tokenExpired = 0)
{
$this->salt = mt_rand(1, 99999999);
$this->ts = Tool::time()->timestamp;
$this->ts = Tool::time()->timestamp + $tokenExpired;
$buffer = unpack("C*", pack("V", $this->salt));
$buffer = array_merge($buffer, unpack("C*", pack("V", $this->ts)));
$buffer = array_merge($buffer, unpack("C*", pack("v", sizeof($this->privileges))));
Expand Down

0 comments on commit e3b8752

Please sign in to comment.