Skip to content

Commit 291c39c

Browse files
authored
Merge pull request #8 from friendsofthinkphp/dev
支持 多应用配置 #7
2 parents 86f5877 + 6f7afbe commit 291c39c

7 files changed

Lines changed: 147 additions & 92 deletions

File tree

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $ php think jwt:make
2929
* `model` 用户模型
3030
* `refresh` Token过期抛异常code = 50001
3131
* `relogin` Token失效异常code = 50002
32-
* `automaticRenewal` [开启过期自动续签](#过期自动续签)
32+
* `automaticRenewal` [开启过期自动续签](过期自动续签)
3333

3434
以下两个异常都会抛一个HTTP异常 StatusCode = 401
3535
* `xiaodi\Exception\HasLoggedException`
@@ -142,10 +142,6 @@ return [
142142

143143
`automaticRenewal => true`
144144

145-
满足以下条件 就可以实现过期自动续签
146-
147-
* Token 刷新时间 必须是在有效时间内
148-
149145
系统检测到 Token 已过期, 会自动续期并返回以下 header 信息。
150146

151147
* Automatic-Renewal-Token

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"thinkphp"
88
],
99
"description": "ThinkPHP Jwt Component",
10-
"minimum-stability": "dev",
10+
"minimum-stability": "stable",
1111
"authors": [
1212
{
1313
"name": "xiaodi",

config/config.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
<?php
22

33
return [
4-
'default' => [
5-
'uniqidKey' => 'uid',
6-
'signerKey' => '',
7-
'notBefore' => 0,
8-
'expiresAt' => 3600,
9-
'refreshTTL' => 7200,
10-
'signer' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
11-
'type' => 'Header',
12-
'refresh' => 50001,
13-
'relogin' => 50002,
14-
'iss' => '',
15-
'aud' => '',
16-
'automaticRenewal' => false,
17-
],
18-
'user' => [
19-
'inject' => false,
20-
'model' => '',
21-
],
22-
'blacklist' => [
23-
'cacheName' => 'blacklist',
24-
],
4+
'default' => 'admin',
5+
'apps' => [
6+
'admin' => [
7+
'token' => [
8+
'uniqidKey' => 'uid',
9+
'signerKey' => '',
10+
'notBefore' => 0,
11+
'expiresAt' => 3600,
12+
'refreshTTL' => 7200,
13+
'signer' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
14+
'type' => 'Header',
15+
'refresh' => 50001,
16+
'relogin' => 50002,
17+
'iss' => '',
18+
'aud' => '',
19+
'automaticRenewal' => false,
20+
],
21+
'user' => [
22+
'bind' => false,
23+
'model' => '',
24+
],
25+
'blacklist' => [
26+
'cacheKey' => 'admin',
27+
],
28+
]
29+
]
2530
];

src/Blacklist.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,35 @@ class Blacklist
1414
{
1515
private $app;
1616

17-
private $store;
17+
private $cacheKey;
1818

19-
protected $cacheName = 'blacklist';
19+
protected $cache;
2020

2121
public function __construct(App $app)
2222
{
2323
$this->app = $app;
2424

25-
$this->store = $this->getStore();
25+
$this->setStoreConfig();
2626

27-
$configs = $this->getConfig();
28-
29-
foreach ($configs as $key => $config) {
30-
$this->$key = $config;
31-
}
27+
$this->cache = $this->getCache();
3228
}
3329

34-
public function getConfig()
30+
public function setStoreConfig()
3531
{
36-
return $this->app->config->get('jwt.blacklist', []);
32+
$store = $this->app->jwt->getStore();
33+
$configs = $this->app->config->get("jwt.apps.{$store}.blacklist", []);
34+
35+
foreach ($configs as $key => $v) {
36+
$this->$key = $v;
37+
}
3738
}
3839

3940
/**
4041
* 获取 缓存驱动.
4142
*
4243
* @return void
4344
*/
44-
public function getStore()
45+
public function getCache()
4546
{
4647
return $this->app->cache;
4748
}
@@ -58,7 +59,7 @@ public function add(Token $token)
5859
if (false === $this->has($token)) {
5960
$claims = $token->getClaims();
6061
$exp = $claims['exp']->getValue() - time();
61-
$this->store->push($this->cacheName, (string) $token, $exp);
62+
$this->cache->push($this->cacheKey, (string) $token, $exp);
6263
}
6364
}
6465

@@ -83,6 +84,6 @@ public function has(Token $token): bool
8384
*/
8485
public function getAll(): array
8586
{
86-
return $this->store->get($this->cacheName, []);
87+
return $this->cache->get($this->cacheKey, []);
8788
}
8889
}

src/Jwt.php

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,83 @@
1717

1818
class Jwt
1919
{
20-
/**
21-
* @var User
22-
*/
20+
use \xiaodi\JWTAuth\Traits\Jwt;
21+
22+
private $config;
23+
24+
private $store;
25+
2326
private $user;
2427

25-
/**
26-
* @var Token
27-
*/
2828
private $token;
2929

30-
/**
31-
* @var Manager
32-
*/
33-
private $manager;
30+
public function __construct(App $app, $store = null)
31+
{
32+
$this->app = $app;
33+
34+
if ($store === null) {
35+
$store = $this->getDefaultStore();
36+
}
3437

35-
use \xiaodi\JWTAuth\Traits\Jwt;
38+
$this->store = $store;
39+
$this->make();
40+
}
3641

37-
public function __construct(App $app, Manager $manager, User $user)
42+
public function store(string $name = '')
3843
{
39-
$this->app = $app;
40-
$this->manager = $manager;
41-
$this->user = $user;
44+
$jwt = app('jwt', ['store' => $name], true);
45+
$this->app->bind('jwt', $jwt);
46+
return $jwt;
47+
}
4248

43-
$config = $this->getConfig();
44-
foreach ($config as $key => $v) {
45-
$this->$key = $v;
49+
protected function make()
50+
{
51+
$this->setStoreConfig();
52+
53+
return $this;
54+
}
55+
56+
public function getStore()
57+
{
58+
return $this->store;
59+
}
60+
61+
/**
62+
* 获取默认 app
63+
*
64+
* @return void
65+
*/
66+
public function getDefaultStore()
67+
{
68+
$store = $this->app->config->get("jwt.default", '');
69+
if (!$store) {
70+
throw new JWTException('默认应用 未配置.', 500);
4671
}
72+
73+
return $store;
4774
}
4875

4976
/**
50-
* 获取jwt配置.
77+
* 获取 app jwt 配置
5178
*
52-
* @return array
79+
* @return void
5380
*/
54-
public function getConfig(): array
81+
public function getStoreConfig()
5582
{
56-
return $this->app->config->get('jwt.default', []);
83+
$config = $this->app->config->get("jwt.apps.{$this->store}.token", []);
84+
if (empty($config)) {
85+
throw new JWTException("{$this->store} 应用 未配置完整.", 500);
86+
}
87+
88+
return $config;
89+
}
90+
91+
protected function setStoreConfig()
92+
{
93+
$config = $this->getStoreConfig();
94+
foreach ($config as $key => $v) {
95+
$this->$key = $v;
96+
}
5797
}
5898

5999
/**
@@ -85,7 +125,7 @@ public function token(array $claims): Token
85125

86126
$token = $builder->getToken($this->getSigner(), $this->makeSignerKey());
87127

88-
$this->manager->login($token);
128+
$this->app['jwt.manager']->login($token);
89129

90130
return $token;
91131
}
@@ -100,7 +140,7 @@ public function token(array $claims): Token
100140
private function makeTokenId(array $claims): string
101141
{
102142
if (empty($claims[$this->getUniqidKey()])) {
103-
throw new JWTException('用户唯一值·uniqidKey·未配置', 500);
143+
throw new JWTException('uniqidKey 未配置', 500);
104144
}
105145

106146
return (string) $claims[$this->getUniqidKey()];
@@ -113,7 +153,12 @@ private function makeTokenId(array $claims): string
113153
*/
114154
public function user(): Model
115155
{
116-
return $this->user->get();
156+
return $this->app['jwt.user']->get();
157+
}
158+
159+
public function getToken()
160+
{
161+
return $this->token;
117162
}
118163

119164
/**
@@ -137,7 +182,7 @@ public function refresh(Token $token = null): Token
137182
unset($claims['aud']);
138183

139184
// 加入黑名单
140-
$this->manager->refresh($token);
185+
$this->app['jwt.manager']->refresh($token);
141186

142187
return $this->token($claims);
143188
}
@@ -189,7 +234,7 @@ public function logout(Token $token = null)
189234
{
190235
$token = $token ?: $this->getRequestToken();
191236

192-
$this->manager->logout($token);
237+
$this->app['jwt.manager']->logout($token);
193238
}
194239

195240
/**
@@ -252,7 +297,7 @@ protected function automaticRenewalToken(Token $token)
252297
protected function validateToken(Token $token)
253298
{
254299
// 是否在黑名单
255-
if ($this->manager->hasBlacklist($token)) {
300+
if ($this->app['jwt.manager']->hasBlacklist($token)) {
256301
throw new TokenAlreadyEexpired('此 Token 已注销,请重新登录', $this->getReloginCode());
257302
}
258303

src/Middleware/Jwt.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
class Jwt
1515
{
1616
private $app;
17-
private $user;
1817

19-
public function __construct(App $app, User $user)
18+
public function __construct(App $app)
2019
{
2120
$this->app = $app;
22-
$this->user = $user;
2321
}
2422

25-
public function handle($request, \Closure $next)
23+
public function handle($request, \Closure $next, $store = 'admin')
2624
{
27-
if (true === $this->app->jwt->verify()) {
28-
// 自动注入用户模型
29-
if ($this->user->hasInject()) {
30-
$user = $this->user->get();
25+
if (true === $this->app->jwt->store($store)->verify()) {
26+
27+
$jwt_user = $this->app['jwt.user'];
28+
29+
if ($jwt_user->bind()) {
30+
$user = $jwt_user->get();
3131
// 路由注入
3232
$request->user = $user;
3333

3434
// 绑定当前用户模型
35-
$model = $this->user->getModel();
35+
$model = $jwt_user->getClass();
3636
$this->app->bind($model, $user);
3737
}
3838

0 commit comments

Comments
 (0)