Skip to content

Commit 2cf8b03

Browse files
author
qbhy
committed
feat:完善sso登录测试用例
1 parent 036fe17 commit 2cf8b03

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require": {
2626
"php": ">=7.2",
2727
"ext-swoole": ">=4.4",
28-
"96qbhy/simple-jwt": "^v1.2",
28+
"96qbhy/simple-jwt": "^v1.3",
2929
"hyperf/cache": "^2.0",
3030
"hyperf/di": "^2.0",
3131
"ext-json": "*",
@@ -53,7 +53,7 @@
5353
"sort-packages": true
5454
},
5555
"scripts": {
56-
"test": "co-phpunit -c phpunit.xml --colors=always",
56+
"test": "phpunit -c phpunit.xml --colors=always",
5757
"analyse": "phpstan analyse --memory-limit 300M -l 0 ./src",
5858
"cs-fix": "php-cs-fixer fix $1",
5959
"post-install-cmd": "cghooks add --ignore-lock",
@@ -71,5 +71,11 @@
7171
"composer test"
7272
]
7373
}
74+
},
75+
"repositories": {
76+
"packagist": {
77+
"type": "composer",
78+
"url": "https://mirrors.aliyun.com/composer/"
79+
}
7480
}
7581
}

src/Guard/JwtGuard.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public function parseToken()
6666

6767
public function login(Authenticatable $user)
6868
{
69-
$token = $this->jwtManager->make(['uid' => $user->getId()])->token();
69+
$token = $this->jwtManager->make([
70+
'uid' => $user->getId(),
71+
's' => str_random(),
72+
])->token();
7073

7174
Context::set($this->resultKey($token), $user);
7275

@@ -81,15 +84,15 @@ public function login(Authenticatable $user)
8184
*/
8285
public function resultKey($token)
8386
{
84-
return $this->name . '.auth.result.' . $this->getJti($token);
87+
return $this->name . '.auth.result' . $this->getJti($token);
8588
}
8689

8790
public function user(?string $token = null): ?Authenticatable
8891
{
8992
$token = $token ?? $this->parseToken();
9093
if (Context::has($key = $this->resultKey($token))) {
9194
$result = Context::get($key);
92-
if ($result instanceof \Throwable) {
95+
if ($result instanceof UnauthorizedException) {
9396
throw $result;
9497
}
9598
return $result ?: null;
@@ -141,7 +144,7 @@ public function guest(?string $token = null): bool
141144
*/
142145
public function refresh(?string $token = null): ?string
143146
{
144-
$token = $token ?? $this->parseToken();
147+
$token = $token ?: $this->parseToken();
145148

146149
if ($token) {
147150
Context::set($this->resultKey($token), null);
@@ -180,13 +183,13 @@ public function getJwtManager(): JWTManager
180183
/**
181184
* 获取 token 标识.
182185
*
183-
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
184186
* @throws \Qbhy\SimpleJwt\Exceptions\InvalidTokenException
185187
* @throws \Qbhy\SimpleJwt\Exceptions\SignatureException
188+
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
186189
* @return mixed|string
187190
*/
188191
protected function getJti(string $token): string
189192
{
190-
return $this->getJwtManager()->parse($token)->getPayload()['jti'] ?? md5($token);
193+
return $this->getJwtManager()->justParse($token)->getPayload()['jti'] ?? md5($token);
191194
}
192195
}

src/Guard/SsoGuard.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ public function getClients(): array
4747

4848
public function login(Authenticatable $user, string $client = null)
4949
{
50-
$client = $client = $this->getClients()[0]; // 需要至少配置一个客户端
50+
$client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端
5151
$token = parent::login($user);
5252
$redisKey = str_replace('{uid}', (string) $user->getId(), $this->config['redis_key'] ?? 'u:token:{uid}');
5353

54-
if (! empty($previousToken = $this->redis->hGet($redisKey, $client))) {
54+
if (! empty($previousToken = $this->redis->hGet($redisKey, $client)) && $previousToken != $token) {
5555
// 如果存在上一个 token,就给他拉黑,也就是强制下线
56-
Context::set($this->resultKey($previousToken), 0);
57-
$this->getJwtManager()->addBlacklist($previousToken);
58-
$this->redis->hDel($redisKey, $client);
56+
Context::set($this->resultKey($previousToken), null);
57+
$this->getJwtManager()->addBlacklist($this->getJwtManager()->justParse($previousToken));
5958
$this->eventDispatcher->dispatch(new ForcedOfflineEvent($user, $client));
6059
}
6160

src/helper.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@ function auth(?string $guard = null)
3030
return $auth->guard($guard);
3131
}
3232
}
33+
34+
if (! function_exists('str_random')) {
35+
function str_random($num = 6): string
36+
{
37+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
38+
$randomString = '';
39+
for ($i = 0; $i < $num; ++$i) {
40+
$index = rand(0, strlen($characters) - 1);
41+
$randomString .= $characters[$index];
42+
}
43+
return $randomString;
44+
}
45+
}

tests/Cases/ExampleTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,20 @@ public function testSsoGuard()
101101
$this->assertTrue(is_string($token));
102102
$this->assertTrue($guard->check($token));
103103

104-
// // 抢线登录
105-
// $newToken = $guard->login($user, 'pc');
106-
// var_dump('抢线的token', $newToken);
107-
// $this->assertTrue($newToken != $token);
108-
// $this->assertTrue($guard->check($newToken));
109-
//
110-
// // 测试旧 token 还能不能用
111-
// $this->assertTrue($guard->guest($token));
112-
//
113-
// // 第二个设备登录
114-
// $weappToken = $guard->login($user, 'weapp');
115-
// $this->assertTrue($guard->check($weappToken));
104+
// 抢线登录
105+
$newToken = $guard->login($user, 'pc');
106+
$this->assertTrue($newToken != $token);
107+
$this->assertTrue($guard->check($newToken));
108+
109+
// 测试掉线的 token 还能不能用
110+
$this->assertTrue($guard->guest($token));
111+
112+
// 第二个设备登录
113+
$weappToken = $guard->login($user, 'weapp');
114+
$this->assertTrue($guard->check($weappToken));
115+
116+
$this->assertTrue((1 ?: 2) == 1);
117+
$this->assertTrue((1 ?? 2) == 1);
116118
}
117119

118120
protected function auth()

0 commit comments

Comments
 (0)