Skip to content

Commit 6860f7a

Browse files
committed
update
1 parent cd05fb8 commit 6860f7a

3 files changed

Lines changed: 40 additions & 54 deletions

File tree

src/Config/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getSigningKey()
3737
throw new JWTException('config signer_key required.', 500);
3838
}
3939

40-
return $this->signer_key;
40+
return base64_encode($this->signer_key);
4141
}
4242

4343
public function getIdKey(): string

src/Service/Jwt.php

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace xiaodi\JWTAuth\Service;
66

7+
use DateTime;
78
use think\App;
89

910
use Lcobucci\JWT\Token as JwtToken;
@@ -38,7 +39,7 @@ public function store(string $store = null): self
3839
if ($store) {
3940
$this->store = $store;
4041
}
41-
42+
4243
return $this;
4344
}
4445

@@ -82,52 +83,21 @@ public function getToken()
8283
* @param string $token
8384
* @return boolean
8485
*/
85-
public function verify(?string $token = null): bool
86+
public function verify(?string $token): bool
8687
{
88+
$service = $this->app->get('jwt.token');
8789
if (!$token) {
88-
$token = $this->app->get('jwt.token')->getRequestToken();
90+
$token = $service->getRequestToken();
8991
}
9092

91-
return $this->app->get('jwt.token')->verify($token);
92-
}
93-
94-
public function destroyStoreWhitelist($store)
95-
{
96-
return $this->app->get('jwt.manager')->destroyStoreWhitelist($store);
97-
}
98-
99-
public function user()
100-
{
101-
return $this->app->get('jwt.user');
102-
}
103-
104-
public function type()
105-
{
106-
return $this->app->get('jwt.token')->getType();
107-
}
108-
109-
public function refreshTTL()
110-
{
111-
return $this->app->get('jwt.token')->getRefreshTTL();
112-
}
113-
114-
public function ttl()
115-
{
116-
return $this->app->get('jwt.token')->getRefreshTTL();
117-
}
118-
119-
public function refresh(?string $token = null)
120-
{
121-
return $this->app->get('jwt.token')->refresh($token);
122-
}
93+
if (!$service->verify($token)) {
94+
$token = $service->getToken();
95+
if ($token->isExpired(new DateTime())) {
96+
// todo 过期
97+
}
12398

124-
public function logout(?string $token = null)
125-
{
126-
return $this->app->get('jwt.token')->logout($token);
127-
}
99+
}
128100

129-
public function destroyToken($jti, $store)
130-
{
131-
return $this->app->get('jwt.manager')->destroyToken($jti, $store);
101+
return true;
132102
}
133103
}

src/Service/Token.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Lcobucci\JWT\Validation\Constraint\SignedWith;
1616
use Lcobucci\JWT\Validation\Constraint\ValidAt;
1717
use Lcobucci\Clock\SystemClock;
18+
use xiaodi\JWTAuth\Exception\JWTException;
1819

1920
/**
2021
*
@@ -58,7 +59,7 @@ protected function init()
5859
$this->initJwtConfiguration();
5960
}
6061

61-
protected function initJwtConfiguration()
62+
public function initJwtConfiguration()
6263
{
6364
$this->jwtConfiguration = Configuration::forSymmetricSigner(
6465
$this->config->getSigner(),
@@ -104,14 +105,19 @@ public function getExpiryDateTime($now): DateTimeImmutable
104105
return $now->modify("+{$ttl} sec");
105106
}
106107

107-
public function parseToken(string $token): JwtToken
108+
/**
109+
*
110+
* @param string $token
111+
* @return JwtToken
112+
*/
113+
public function parse(string $token): JwtToken
108114
{
109-
$token = $this->jwtConfiguration->parser()->parse($token);
110-
return $token;
115+
$this->token = $this->jwtConfiguration->parser()->parse($token);
116+
117+
return $this->token;
111118
}
112119

113120
/**
114-
* 验证成功的Token
115121
*
116122
* @return JWTToken
117123
*/
@@ -120,22 +126,32 @@ public function getToken(): ?JwtToken
120126
return $this->token;
121127
}
122128

129+
/**
130+
*
131+
* @param string $token
132+
* @return boolean|null
133+
*/
123134
public function verify(string $token): ?bool
124135
{
125-
$this->token = $this->parseToken($token);
136+
$this->validate($token);
137+
}
126138

139+
/**
140+
* 效验 Token
141+
* @param string $token
142+
* @return boolean
143+
*/
144+
public function validate(string $token)
145+
{
146+
$token = $this->parse($token);
127147
$this->jwtConfiguration->setValidationConstraints(
128148
new ValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get()))),
129149
new SignedWith($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey())
130150
);
131151

132152
$constraints = $this->jwtConfiguration->validationConstraints();
133153

134-
if (!$this->jwtConfiguration->validator()->validate($this->token, ...$constraints)) {
135-
throw new JWTException('效验失败', 401);
136-
}
137-
138-
return true;
154+
return $this->jwtConfiguration->validator()->validate($token, ...$constraints);
139155
}
140156

141157
public function logout(?string $token): void

0 commit comments

Comments
 (0)