diff --git a/src/Config.php b/src/Config.php index a7c0e4b..36f2724 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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 = '') { @@ -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; + } } diff --git a/src/Token/AccessToken.php b/src/Token/AccessToken.php index 95bccd2..b4089eb 100644 --- a/src/Token/AccessToken.php +++ b/src/Token/AccessToken.php @@ -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); diff --git a/src/Token/Message.php b/src/Token/Message.php index 495385d..7a49fe9 100644 --- a/src/Token/Message.php +++ b/src/Token/Message.php @@ -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))));