Skip to content

Commit b65b8fb

Browse files
committed
update
1 parent 6860f7a commit b65b8fb

7 files changed

Lines changed: 125 additions & 89 deletions

File tree

src/Config/Token.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
class Token
1111
{
12-
protected $unique_id_key = 'uid';
1312
protected $signer_key = null;
1413
protected $not_before = 0;
1514
protected $expires_at = 3600;
@@ -40,11 +39,6 @@ public function getSigningKey()
4039
return base64_encode($this->signer_key);
4140
}
4241

43-
public function getIdKey(): string
44-
{
45-
return $this->unique_id_key;
46-
}
47-
4842
public function getExpires()
4943
{
5044
return $this->expires_at;

src/JwtAuth.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Service/Jwt.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
namespace xiaodi\JWTAuth\Service;
66

77
use DateTime;
8+
use DateTimeImmutable;
9+
use Exception;
810
use think\App;
911

1012
use Lcobucci\JWT\Token as JwtToken;
13+
use xiaodi\JWTAuth\Exception\JWTException;
14+
use xiaodi\JWTAuth\Exception\TokenAlreadyEexpired;
1115

1216
class Jwt
1317
{
@@ -67,16 +71,9 @@ public function token($identifier, array $claims = []): JwtToken
6771
{
6872
$token = $this->app->get('jwt.token')->make($identifier, $claims);
6973

70-
// $this->app->get('jwt.manager')->login($token);
71-
7274
return $token;
7375
}
7476

75-
public function getToken()
76-
{
77-
return $this->app->get('jwt.token')->getToken();
78-
}
79-
8077
/**
8178
* 验证 Token
8279
*
@@ -90,12 +87,22 @@ public function verify(?string $token): bool
9087
$token = $service->getRequestToken();
9188
}
9289

93-
if (!$service->verify($token)) {
90+
if (!$service->validate($token)) {
91+
$now = new DateTimeImmutable();
9492
$token = $service->getToken();
95-
if ($token->isExpired(new DateTime())) {
96-
// todo 过期
93+
if (!$service->isRefreshExpired($now)) {
94+
$config = $service->getConfig();
95+
if ($config->getAutomaticRenewal()) {
96+
$token = $service->automaticRenewalToken($token);
97+
}
98+
} else {
99+
throw new JWTException('效验失败', 401);
97100
}
101+
}
98102

103+
// 是否存在黑名单
104+
if (true === $this->app->get('jwt.manager')->wasBan($token)) {
105+
throw new TokenAlreadyEexpired('token was ban', $this->config->getReloginCode());
99106
}
100107

101108
return true;

src/Service/Manager.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ protected function resloveConfig()
3636
$this->config = new Config($options);
3737
}
3838

39+
/**
40+
* @var Config
41+
*/
42+
public function getConfig()
43+
{
44+
return $this->config;
45+
}
46+
3947
public function login(Token $token): void
4048
{
4149
if ($this->app->get('jwt.sso')->getEnable()) {
@@ -47,31 +55,30 @@ public function login(Token $token): void
4755

4856
protected function handleSSO(Token $token): void
4957
{
50-
$jti = $token->getClaim('jti');
51-
$store = $token->getClaim('store');
52-
$exp = $token->getClaim('exp') - time();
58+
$jti = $token->claims()->get('jti');
59+
$store = $token->claims()->get()('store');
5360

5461
$this->destroyToken($jti, $store);
5562
}
5663

5764
protected function pushWhitelist(Token $token): void
5865
{
59-
$jti = $token->getClaim('jti');
60-
$store = $token->getClaim('store');
61-
$exp = $token->getClaim('exp') - time();
62-
$tag = $store .'-' . $this->config->getWhitelist();
66+
$jti = $token->claims()->get('jti');
67+
$store = $token->claims()->get('store');
68+
$exp = $token->claims()->get('exp') - time();
69+
$tag = $store . '-' . $this->config->getWhitelist();
6370

6471
$key = $this->formatKey($store, $this->config->getWhitelist(), $jti, (string)$token);
6572
$this->setCache($tag, $key, (string)$token, $exp);
6673
}
6774

6875
protected function pushBlacklist(Token $token): void
6976
{
70-
$jti = $token->getClaim('jti');
71-
$store = $token->getClaim('store');
77+
$jti = $token->claims()->get('jti');
78+
$store = $token->claims()->get('store');
7279

73-
$exp = $token->getClaim('exp') - time();
74-
$tag = $store .'-' . $this->config->getBlacklist();
80+
$exp = $token->claims()->get('exp') - time();
81+
$tag = $store . '-' . $this->config->getBlacklist();
7582
$key = $this->formatKey($store, $this->config->getBlacklist(), $jti, (string)$token);
7683

7784
$this->setCache($tag, $key, (string)$token, $exp);
@@ -84,15 +91,15 @@ public function logout(Token $token): void
8491

8592
public function wasBan(Token $token): bool
8693
{
87-
$jti = $token->getClaim('jti');
88-
$store = $token->getClaim('store');
94+
$jti = $token->claims()->get('jti');
95+
$store = $token->claims()->get('store');
8996

90-
return $this->getBlacklist($store, $jti, (string)$token) === (string) $token ? true : false;
97+
return $this->getBlacklist($store, $jti, $token) === $token->toString();
9198
}
9299

93-
protected function getBlacklist(string $store, string $jti, string $token)
100+
protected function getBlacklist(string $store, string $jti, Token $token)
94101
{
95-
return $this->getCache($store, $this->config->getBlacklist(), $jti, $token);
102+
return $this->getCache($store, $this->config->getBlacklist(), $jti, $token->toString());
96103
}
97104

98105
public function destroyStoreWhitelist($store): void
@@ -108,14 +115,13 @@ public function destroyStoreBlacklist($store): void
108115
public function destroyToken($id, $store): void
109116
{
110117
$type = $this->config->getWhitelist();
111-
$tag = $store .'-' . $type;
118+
$tag = $store . '-' . $type;
112119

113120
$rule = implode(':', [$this->config->getPrefix(), $store, $type, $id]);
114121
$keys = $this->app->cache->getTagItems($tag);
115122
$parser = new Parser();
116123

117-
foreach($keys as $key) {
118-
124+
foreach ($keys as $key) {
119125
if (false !== strpos($key, $rule)) {
120126
$value = $this->app->cache->get($key);
121127

src/Service/SSO.php

Lines changed: 10 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

@@ -18,7 +18,7 @@ class SSO
1818
public function __construct(App $app)
1919
{
2020
$this->app = $app;
21-
21+
2222
$this->init();
2323
}
2424

@@ -29,6 +29,14 @@ protected function init()
2929
$this->config = new Config($options);
3030
}
3131

32+
/**
33+
* @var Config
34+
*/
35+
public function getConfig()
36+
{
37+
return $this->config;
38+
}
39+
3240
protected function getStore(): string
3341
{
3442
return $this->app->get('jwt')->getStore();

src/Service/Token.php

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateTimeZone;
88
use DateTimeImmutable;
9+
use DateTimeInterface;
910
use think\App;
1011
use xiaodi\JWTAuth\Config\Token as Config;
1112
use xiaodi\JWTAuth\Handle\RequestToken;
@@ -72,6 +73,11 @@ protected function getStore()
7273
return $this->app->get('jwt')->getStore();
7374
}
7475

76+
public function getToken()
77+
{
78+
return $this->token;
79+
}
80+
7581
protected function resolveConfig()
7682
{
7783
$store = $this->getStore();
@@ -87,16 +93,21 @@ protected function resolveConfig()
8793
public function make($identifier, array $claims = []): JwtToken
8894
{
8995
$now = new DateTimeImmutable();
90-
return $this->jwtConfiguration->builder()
96+
$builder = $this->jwtConfiguration->builder()
9197
->permittedFor($this->config->getAud())
9298
->issuedBy($this->config->getIss())
9399
->identifiedBy((string)$identifier)
94100
->issuedAt($now)
95101
->canOnlyBeUsedAfter($now)
96102
->expiresAt($this->getExpiryDateTime($now))
97103
->relatedTo((string) $identifier)
98-
->withClaim('scopes', json_encode($claims))
99-
->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
104+
->withClaim('store', $this->getStore());
105+
106+
foreach ($claims as $key => $value) {
107+
$builder->withClaim($key, $value);
108+
}
109+
110+
return $builder->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
100111
}
101112

102113
public function getExpiryDateTime($now): DateTimeImmutable
@@ -117,25 +128,6 @@ public function parse(string $token): JwtToken
117128
return $this->token;
118129
}
119130

120-
/**
121-
*
122-
* @return JWTToken
123-
*/
124-
public function getToken(): ?JwtToken
125-
{
126-
return $this->token;
127-
}
128-
129-
/**
130-
*
131-
* @param string $token
132-
* @return boolean|null
133-
*/
134-
public function verify(string $token): ?bool
135-
{
136-
$this->validate($token);
137-
}
138-
139131
/**
140132
* 效验 Token
141133
* @param string $token
@@ -157,7 +149,7 @@ public function validate(string $token)
157149
public function logout(?string $token): void
158150
{
159151
$token = $token ?: $this->getRequestToken();
160-
$token = $this->parseToken($token);
152+
$token = $this->parse($token);
161153

162154
$this->app->get('jwt.manager')->logout($token);
163155
}
@@ -176,8 +168,53 @@ public function getRequestToken(): string
176168
return $token;
177169
}
178170

179-
public function getType(): string
171+
public function isRefreshExpired(DateTimeInterface $now): bool
172+
{
173+
if (!$this->token->claims()->has('iat')) {
174+
return false;
175+
}
176+
177+
$iat = $this->token->claims()->get('iat');
178+
$refresh_ttl = $this->config->getRefreshTTL();
179+
$refresh_exp = $iat->modify("+{$refresh_ttl} sec");
180+
return $now >= $refresh_exp;
181+
}
182+
183+
/**
184+
* @var Config
185+
*/
186+
public function getConfig()
187+
{
188+
return $this->config;
189+
}
190+
191+
/**
192+
* Token 自动续期
193+
*
194+
* @param Token $token
195+
* @param int|string $ttl 秒数
196+
* @return void
197+
*/
198+
public function automaticRenewalToken(JwtToken $token)
180199
{
181-
return $this->config->getTokenType();
200+
$claims = $token->claims()->all();
201+
202+
$jti = $claims['jti'];
203+
unset($claims['aud']);
204+
unset($claims['iss']);
205+
unset($claims['jti']);
206+
unset($claims['iat']);
207+
unset($claims['nbf']);
208+
unset($claims['exp']);
209+
unset($claims['sub']);
210+
211+
$token = $this->make($jti, $claims);
212+
$refreshAt = $this->config->getRefreshTTL();
213+
214+
header('Access-Control-Expose-Headers:Automatic-Renewal-Token,Automatic-Renewal-Token-RefreshAt');
215+
header("Automatic-Renewal-Token:" . $token->toString());
216+
header("Automatic-Renewal-Token-RefreshAt:$refreshAt");
217+
218+
return $token;
182219
}
183220
}

0 commit comments

Comments
 (0)