Skip to content

Commit 285ff39

Browse files
committed
支持 RSA
1 parent f3885e2 commit 285ff39

4 files changed

Lines changed: 85 additions & 27 deletions

File tree

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
],
1010
'token' => [
1111
'signer_key' => 'tant',
12+
'public_key' => 'file://path/public.key',
13+
'private_key' => 'file://path/private.key',
1214
'not_before' => 0,
1315
'expires_at' => 3600,
1416
'refresh_ttL' => 7200,

src/Config/Token.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use Lcobucci\JWT\Signer;
88
use xiaodi\JWTAuth\Exception\JWTException;
9+
use Lcobucci\JWT\Signer\Key\InMemory;
10+
use Lcobucci\JWT\Signer\Key;
11+
use Lcobucci\JWT\Signer\Key\LocalFileReference;
12+
use Lcobucci\JWT\Signer\Hmac;
13+
use Lcobucci\JWT\Signer\Rsa;
914

1015
class Token
1116
{
@@ -20,6 +25,8 @@ class Token
2025
protected $iss = 'client.xiaodim.com';
2126
protected $aud = 'server.xiaodim.com';
2227
protected $automatic_renewal = false;
28+
protected $public_key = '';
29+
protected $private_key = '';
2330

2431
public function __construct(array $options)
2532
{
@@ -30,13 +37,43 @@ public function __construct(array $options)
3037
}
3138
}
3239

33-
public function getSigningKey()
40+
public function getHamcKey(): Key
3441
{
3542
if (empty($this->signer_key)) {
3643
throw new JWTException('config signer_key required.', 500);
3744
}
3845

39-
return base64_encode($this->signer_key);
46+
return InMemory::base64Encoded((string)$this->signer_key);
47+
}
48+
49+
public function RSASigner()
50+
{
51+
$signer = $this->getSigner();
52+
53+
return $signer instanceof Rsa;
54+
}
55+
56+
public function getSignerKey(): Key
57+
{
58+
$signer = $this->getSigner();
59+
60+
if ($this->RSASigner()) {
61+
return $this->getPrivateKey();
62+
} else if ($signer instanceof Hmac) {
63+
return $this->getHamcKey();
64+
} else {
65+
throw new JWTException('not support.', 500);
66+
}
67+
}
68+
69+
public function getPublicKey(): Key
70+
{
71+
return LocalFileReference::file($this->public_key);
72+
}
73+
74+
public function getPrivateKey(): Key
75+
{
76+
return LocalFileReference::file($this->private_key);
4077
}
4178

4279
public function getExpires()
@@ -73,7 +110,7 @@ public function getReloginCode()
73110
{
74111
return $this->relogin_code;
75112
}
76-
113+
77114
public function getRefreshCode()
78115
{
79116
return $this->refresh_code;

src/Service/JwtAuth.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,12 @@ public function verify(string $token = null): bool
8383
// 是否存在黑名单
8484
$this->wasBan($token);
8585

86-
if (!$service->validate($token)) {
87-
$now = new DateTimeImmutable();
88-
89-
$token = $service->getToken();
90-
if (!$service->isRefreshExpired($now)) {
91-
$config = $service->getConfig();
92-
if ($config->getAutomaticRenewal()) {
93-
$token = $service->automaticRenewalToken($token);
94-
}
95-
} else {
96-
throw new JWTException('效验失败', 401);
97-
}
98-
} else {
99-
$token = $this->app->get('jwt.token')->getToken();
86+
// 检测合法性
87+
if ($service->validate($token)) {
88+
return $service->validateExp($token);
10089
}
10190

102-
return true;
91+
return false;
10392
}
10493

10594
protected function wasBan($token)
@@ -122,10 +111,9 @@ public function logout(string $token = null)
122111
$token = $service->parse($token);
123112
$this->app->get('jwt.manager')->logout($token);
124113
}
125-
114+
126115
public function user()
127116
{
128117
return $this->app->get('jwt.user')->find();
129118
}
130-
131119
}

src/Service/Token.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
use xiaodi\JWTAuth\Handle\RequestToken;
1313
use Lcobucci\JWT\Configuration;
1414
use Lcobucci\JWT\Token as JwtToken;
15-
use Lcobucci\JWT\Signer\Key\InMemory;
1615
use Lcobucci\JWT\Validation\Constraint\SignedWith;
1716
use Lcobucci\JWT\Validation\Constraint\ValidAt;
1817
use Lcobucci\Clock\SystemClock;
1918
use xiaodi\JWTAuth\Exception\JWTException;
19+
use Lcobucci\JWT\Signer\Key\InMemory;
2020

2121
class Token
2222
{
@@ -59,7 +59,7 @@ public function initJwtConfiguration()
5959
{
6060
$this->jwtConfiguration = Configuration::forSymmetricSigner(
6161
$this->config->getSigner(),
62-
InMemory::base64Encoded($this->config->getSigningKey())
62+
$this->config->getSignerKey()
6363
);
6464
}
6565

@@ -123,24 +123,55 @@ public function parse(string $token): JwtToken
123123
return $this->token;
124124
}
125125

126+
protected function getValidateConfig()
127+
{
128+
return Configuration::forSymmetricSigner(
129+
$this->config->getSigner(),
130+
$this->config->RSASigner() ? $this->config->getPublicKey() : $this->config->getHamcKey()
131+
);
132+
}
133+
126134
/**
127-
* 效验 Token
135+
* 效验合法性 Token
128136
* @param string $token
129137
* @return boolean
130138
*/
131139
public function validate(string $token)
132140
{
133141
$token = $this->parse($token);
134-
$this->jwtConfiguration->setValidationConstraints(
142+
143+
$jwtConfiguration = $this->getValidateConfig();
144+
145+
$jwtConfiguration->setValidationConstraints(
146+
new SignedWith($jwtConfiguration->signer(), $jwtConfiguration->signingKey())
147+
);
148+
149+
$constraints = $jwtConfiguration->validationConstraints();
150+
151+
return $jwtConfiguration->validator()->validate($token, ...$constraints);
152+
}
153+
154+
/**
155+
* 效验是否过期 Token
156+
* @param string $token
157+
* @return boolean
158+
*/
159+
public function validateExp(string $token)
160+
{
161+
$token = $this->parse($token);
162+
163+
$jwtConfiguration = $this->getValidateConfig();
164+
165+
$jwtConfiguration->setValidationConstraints(
135166
new ValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get()))),
136-
new SignedWith($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey())
137167
);
138168

139-
$constraints = $this->jwtConfiguration->validationConstraints();
169+
$constraints = $jwtConfiguration->validationConstraints();
140170

141-
return $this->jwtConfiguration->validator()->validate($token, ...$constraints);
171+
return $jwtConfiguration->validator()->validate($token, ...$constraints);
142172
}
143173

174+
144175
public function login(JwtToken $token)
145176
{
146177
$this->app->get('jwt.manange')->login($token);

0 commit comments

Comments
 (0)