Skip to content

Commit c90b009

Browse files
committed
update
1 parent 8a5f544 commit c90b009

5 files changed

Lines changed: 77 additions & 74 deletions

File tree

README.md

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ $ php think jwt:make
3030
<?php
3131

3232
return [
33-
'default' => 'admin',
34-
'apps' => [
33+
'stores' => [
3534
'admin' => [
35+
'sso' => [
36+
'enable' => false,
37+
],
3638
'token' => [
37-
'uniqidKey' => 'uid',
38-
'signerKey' => '',
39-
'notBefore' => 0,
40-
'expiresAt' => 3600,
41-
'refreshTTL' => 7200,
39+
'unique_id_key' => 'uid',
40+
'signer_key' => 'tant',
41+
'not_before' => 0,
42+
'expires_at' => 3600,
43+
'refresh_ttL' => 7200,
4244
'signer' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
4345
'type' => 'Header',
44-
'refresh' => 50001,
45-
'relogin' => 50002,
46-
'iss' => '',
47-
'aud' => '',
48-
'automaticRenewal' => false,
46+
'relogin_code' => 50001,
47+
'refresh_code' => 50002,
48+
'iss' => 'client.tant',
49+
'aud' => 'server.tant',
50+
'automatic_renewal' => false,
4951
],
5052
'user' => [
5153
'bind' => false,
52-
'model' => '',
54+
'class' => null,
5355
]
5456
]
5557
],
@@ -65,49 +67,42 @@ return [
6567

6668
```
6769
## token
68-
* `uniqidKey` 用户唯一标识
69-
* `signerKey` 密钥
70-
* `notBefore` 时间前不能使用 默认生成后直接使用
71-
* `expiresAt` Token有效期(秒)
70+
* `unique_id_key` 用户唯一标识
71+
* `signer_key` 密钥
72+
* `not_before` 时间前不能使用 默认生成后直接使用
73+
* `refresh_ttL` Token有效期(秒)
7274
* `signer` 加密算法
7375
* `type` 获取 Token 途径
74-
* `refresh` Token过期抛异常code = 50001
75-
* `relogin` Token失效异常code = 50002
76-
* `automaticRenewal` [开启过期自动续签](#过期自动续签)
76+
* `relogin_code` Token过期抛异常code = 50001
77+
* `refresh_code` Token失效异常code = 50002
78+
* `automatic_renewal` [开启过期自动续签](#过期自动续签)
7779

7880
## user
7981
* `bind` 是否注入用户模型(中间件有效)
80-
* `model` 用户模型文件
82+
* `class` 用户模型类文件
8183

82-
## blacklist
83-
* `cacheKey` 黑名单缓存key
84+
## manager
85+
* `prefix` 缓存前缀
86+
* `blacklist` 黑名单缓存名
87+
* `whitelist` 白名单缓存名
8488

8589
以下两个异常都会抛一个HTTP异常 StatusCode = 401
8690
* `xiaodi\Exception\HasLoggedException`
8791
* `xiaodi\Exception\TokenAlreadyEexpired`
8892

8993
## Token 生成
9094
```php
95+
namespace app\home\controller\Auth;
96+
9197
use xiaodi\JWTAuth\Facade\Jwt;
9298

9399
public function login()
94100
{
95101
//...登录判断逻辑
96102

97-
// 默认应用
103+
// 自动获取当前应用下的jwt配置
98104
return json([
99105
'token' => Jwt::token(['uid' => 1]),
100-
'token_type' => Jwt::type(),
101-
'expires_in' => Jwt::ttl(),
102-
'refresh_in' => Jwt::refreshTTL()
103-
]);
104-
105-
// 指定应用
106-
return json([
107-
'token' => Jwt::store('wechat')->token(['uid' => 1]),
108-
'token_type' => Jwt::type(),
109-
'expires_in' => Jwt::ttl(),
110-
'refresh_in' => Jwt::refreshTTL()
111106
]);
112107
}
113108
```
@@ -124,20 +119,12 @@ class User {
124119

125120
public function test()
126121
{
127-
try {
128-
// 默认应用
129-
Jwt::verify($token);
130-
131-
// 指定应用
132-
// Jwt::store('wechat')->verify($token);
133-
} catch (HasLoggedException $e) {
134-
// 已在其它终端登录
135-
} catch (TokenAlreadyEexpired $e) {
136-
// Token已过期
122+
if (true === Jwt::verify($token)) {
123+
// 验证成功
137124
}
138125

139126
// 验证成功
140-
// 如 开启用户注入功能 可获取当前用户信息
127+
// 如配置用户模型文件 可获取当前用户信息
141128
dump(Jwt::user());
142129
}
143130
}
@@ -148,11 +135,7 @@ class User {
148135
```php
149136
use xiaodi\JWTAuth\Middleware\Jwt;
150137

151-
// 默认应用
152138
Route::get('/hello', 'index/index')->middleware(Jwt::class);
153-
154-
// 指定应用
155-
Route::get('/hello', 'index/index')->middleware(Jwt::class, 'wechat');
156139
```
157140

158141
## Token 自动获取

src/Config/Token.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare (strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace xiaodi\JWTAuth\Config;
66

@@ -88,4 +88,8 @@ public function getAutomaticRenewal()
8888
return $this->automatic_renewal;
8989
}
9090

91+
public function getTokenType()
92+
{
93+
return $this->type;
94+
}
9195
}

src/Middleware/Jwt.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,39 @@
66

77
use think\App;
88
use xiaodi\JWTAuth\Exception\JWTException;
9-
use xiaodi\JWTAuth\User;
109

1110
/**
1211
* 中间件.
1312
*/
1413
class Jwt
1514
{
16-
private $app;
15+
protected $app;
1716

1817
public function __construct(App $app)
1918
{
2019
$this->app = $app;
2120
}
2221

23-
public function handle($request, \Closure $next, $store = 'admin')
22+
public function handle($request, \Closure $next)
2423
{
2524
// 暂时修复 6.0.3 options 问题
2625
if ($request->isOptions()) {
2726
return $next($request);
2827
}
29-
30-
if (true === $this->app->jwt->store($store)->verify()) {
31-
32-
$user = $this->app['jwt.user'];
33-
34-
if ($user->bind()) {
35-
$info = $user->get();
36-
if (!$info){
37-
throw new JWTException('没有此用户', 401);
38-
}
39-
40-
// 路由注入
41-
$request->user = $info;
42-
43-
// 绑定当前用户模型
44-
$model = $user->getClass();
45-
$this->app->bind($model, $info);
28+
29+
if (true === $this->app->get('jwt')->verify()) {
30+
31+
$user = $this->app->get('jwt.user');
32+
33+
if ($user->getBind()) {
34+
if ($info = $user->get()) {
35+
// 路由注入
36+
$request->user = $info;
37+
38+
// 绑定当前用户模型
39+
$model = $user->getClass();
40+
$this->app->bind($model, $info);
41+
}
4642
}
4743

4844
return $next($request);

src/Service/Jwt.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare (strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace xiaodi\JWTAuth\Service;
66

@@ -78,8 +78,12 @@ public function getToken()
7878
* @param string $token
7979
* @return boolean
8080
*/
81-
public function verify(string $token): bool
81+
public function verify(?string $token = null): bool
8282
{
83+
if (!$token) {
84+
$token = $this->app->get('jwt.token')->getRequestToken();
85+
}
86+
8387
return $this->app->get('jwt.token')->verify($token);
8488
}
8589

src/Service/Token.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare (strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace xiaodi\JWTAuth\Service;
66

@@ -11,7 +11,9 @@
1111
use Lcobucci\JWT\Parser;
1212
use xiaodi\JWTAuth\Config\Token as Config;
1313
use xiaodi\JWTAuth\Exception\JWTException;
14+
use xiaodi\JWTAuth\Exception\JWTInvalidArgumentException;
1415
use xiaodi\JWTAuth\Exception\TokenAlreadyEexpired;
16+
use xiaodi\JWTAuth\Handle\RequestToken;
1517

1618
/**
1719
* Undocumented class
@@ -199,4 +201,18 @@ public function verify(string $token): ?bool
199201

200202
return true;
201203
}
204+
205+
/**
206+
* 自动获取请求下的Token.
207+
*
208+
* @return string
209+
*/
210+
protected function getRequestToken(): string
211+
{
212+
$requestToken = new RequestToken($this->app);
213+
214+
$token = $requestToken->get($this->config->getTokenType());
215+
216+
return $token;
217+
}
202218
}

0 commit comments

Comments
 (0)