Skip to content

Commit 8a5f544

Browse files
committed
update
1 parent c0d64b6 commit 8a5f544

4 files changed

Lines changed: 56 additions & 9 deletions

File tree

src/JwtService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use xiaodi\JWTAuth\Service\Manager;
99
use xiaodi\JWTAuth\Service\Token;
1010
use xiaodi\JWTAuth\Service\SSO;
11+
use xiaodi\JWTAuth\Service\User;
1112

1213
class JwtService extends \think\Service
1314
{
@@ -17,6 +18,7 @@ public function register()
1718
$this->app->bind('jwt.manager', Manager::class);
1819
$this->app->bind('jwt.token', Token::class);
1920
$this->app->bind('jwt.sso', SSO::class);
21+
$this->app->bind('jwt.user', User::class);
2022
}
2123

2224
public function boot()

src/Service/Jwt.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public function token(array $claims): JwtToken
6767
return $token;
6868
}
6969

70-
public function user()
70+
public function getToken()
7171
{
72+
return $this->app->get('jwt.token')->getToken();
7273
}
7374

7475
/**
@@ -86,4 +87,9 @@ public function destroyStoreWhitelist($store)
8687
{
8788
return $this->app->get('jwt.manager')->destroyStoreWhitelist($store);
8889
}
90+
91+
public function user()
92+
{
93+
return $this->app->get('jwt.user');
94+
}
8995
}

src/Service/Token.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class Token
3333
*/
3434
protected $claims;
3535

36+
/**
37+
*
38+
* @var JwtToken
39+
*/
40+
protected $token;
41+
3642
public function __construct(App $app)
3743
{
3844
$this->app = $app;
@@ -141,27 +147,37 @@ protected function parseToken(string $token): JwtToken
141147
return $token;
142148
}
143149

150+
/**
151+
* 验证成功的Token
152+
*
153+
* @return JWTToken
154+
*/
155+
public function getToken(): ?JwtToken
156+
{
157+
return $this->token;
158+
}
159+
144160
public function verify(string $token): ?bool
145161
{
146-
$token = $this->parseToken($token);
162+
$this->token = $this->parseToken($token);
147163

148-
if (false === $token->verify($this->config->getSigner(), $this->config->makeSignerKey())) {
164+
if (false === $this->token->verify($this->config->getSigner(), $this->config->makeSignerKey())) {
149165
throw new JWTException('此 Token 与 密钥不匹配', $this->config->getReloginCode());
150166
}
151167

152168
// Token 是否已可用
153169
$now = time();
154-
$exp = $token->getClaim('nbf');
170+
$exp = $this->token->getClaim('nbf');
155171
if ($now < $exp) {
156172
throw new JWTException('此 Token 暂未可用', 500);
157173
}
158174

159175
// 是否已过期
160-
if (true === $token->isExpired()) {
161-
if ($now <= $token->getClaim('refreshAt')) {
176+
if (true === $this->token->isExpired()) {
177+
if ($now <= $this->token->getClaim('refreshAt')) {
162178
// 是否开启自动续签
163179
if ($this->config->getAutomaticRenewal()) {
164-
$token = $this->automaticRenewalToken($token);
180+
$this->token = $this->automaticRenewalToken($this->token);
165181
} else {
166182
throw new TokenAlreadyEexpired('Token 已过期,请重新刷新', $this->config->getReloginCode());
167183
}
@@ -172,12 +188,12 @@ public function verify(string $token): ?bool
172188

173189
$data = new ValidationData();
174190

175-
$jwt_id = $token->getHeader('jti');
191+
$jwt_id = $this->token->getHeader('jti');
176192
$data->setIssuer($this->config->getIss());
177193
$data->setAudience($this->config->getAud());
178194
$data->setId($jwt_id);
179195

180-
if (!$token->validate($data)) {
196+
if (!$this->token->validate($data)) {
181197
throw new JWTException('此 Token 效验不通过', $this->config->getReloginCode());
182198
}
183199

src/Service/User.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace xiaodi\JWTAuth\Service;
66

7+
use Exception;
78
use think\App;
89
use think\Model;
910
use xiaodi\JWTAuth\Config\User as Config;
11+
use xiaodi\JWTAuth\Exception\JWTException;
1012

1113
class User
1214
{
@@ -39,4 +41,25 @@ protected function resolveConfig(): array
3941

4042
return $options;
4143
}
44+
45+
protected function getClass(): string
46+
{
47+
$store = $this->getStore();
48+
$class = $this->config->getClass();
49+
if (!$class) {
50+
throw new JWTException("{$store}应用未配置用户模型文件");
51+
}
52+
53+
return $class;
54+
}
55+
56+
public function get()
57+
{
58+
$class = $this->getClass();
59+
$token = $this->app->get('jwt')->getToken();
60+
$uid = $token->getHeader('jti');
61+
62+
$model = new $class();
63+
return $model->find($uid);
64+
}
4265
}

0 commit comments

Comments
 (0)