Skip to content

Commit e3b8752

Browse files
committed
token 有效期
1 parent 5ab0e5e commit e3b8752

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Config.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Config
4747
*/
4848
protected $handler;
4949

50+
/**
51+
* @var int token 有效期
52+
*/
53+
public $tokenExpired = 3600;
54+
5055

5156
public function __construct(string $appID = '', string $appCertificate = '', string $customerKey = '', string $customerSecret = '')
5257
{
@@ -240,4 +245,22 @@ public function getAppIdLen()
240245
{
241246
return strlen($this->getAppID());
242247
}
248+
249+
/**
250+
* @return int
251+
*/
252+
public function getTokenExpired(): int
253+
{
254+
return $this->tokenExpired;
255+
}
256+
257+
/**
258+
* @param int $tokenExpired
259+
* @return Config
260+
*/
261+
public function setTokenExpired(int $tokenExpired): Config
262+
{
263+
$this->tokenExpired = $tokenExpired;
264+
return $this;
265+
}
243266
}

src/Token/AccessToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected function extract(Config $config, $token)
157157
*/
158158
public function build()
159159
{
160-
$msg = $this->message->packContent();
160+
$msg = $this->message->packContent($this->config->getTokenExpired());
161161
$val = array_merge(unpack("C*", $this->config->getAppID()), unpack("C*", $this->channelName), unpack("C*", $this->uid), $msg);
162162

163163
$sig = hash_hmac('sha256', implode(array_map("chr", $val)), $this->config->getAppCertificate(), true);

src/Token/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public function __construct()
2020
$this->privileges = array();
2121
}
2222

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

0 commit comments

Comments
 (0)