Skip to content

Commit 4e15dde

Browse files
committed
完善同一帐号只有登录一个地方
1 parent 6fb70d9 commit 4e15dde

File tree

8 files changed

+382
-109
lines changed

8 files changed

+382
-109
lines changed

config/ltool.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
'rsa_public_key' => env('LTOOL_SIGN_RSA_PUBLIC', __DIR__.'/../pems/rsa_public_key.pem'),
1212
],
1313

14-
// jwt验证
15-
'signer' => [
16-
'nbf' => 0,
17-
'exp' => 3600
14+
// token-cache验证
15+
'token-cache' => [
16+
// 缓存key前缀
17+
'key_prefix' => 'token:cache:',
18+
// 有效时间
19+
'exp' => 10,
20+
// 刷新有效期
21+
'ttl' => 86400
1822
]
1923
];
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* Time: 21:42
77
*/
88

9-
namespace LTools\Auths\Signer;
9+
namespace LTools\Auths\Cache;
1010

1111

1212
use Illuminate\Auth\GuardHelpers;
1313
use Illuminate\Contracts\Auth\Authenticatable;
1414
use Illuminate\Contracts\Auth\Guard;
1515
use Illuminate\Contracts\Auth\UserProvider;
1616

17-
class SignerGuard implements Guard
17+
class CacheGuard implements Guard
1818
{
1919
use GuardHelpers;
2020
/**
@@ -26,7 +26,7 @@ class SignerGuard implements Guard
2626
* SignerGuard constructor.
2727
*
2828
* @param UserProvider $provider
29-
* @param TokenHandle $handle
29+
* @param TokenHandle $handle
3030
*/
3131
public function __construct(UserProvider $provider, TokenHandle $handle)
3232
{
@@ -38,15 +38,24 @@ public function __construct(UserProvider $provider, TokenHandle $handle)
3838
* Get the currently authenticated user.
3939
*
4040
* @return \Illuminate\Contracts\Auth\Authenticatable|null
41+
* @throws \LTools\Exceptions\TokenException
4142
*/
4243
public function user()
4344
{
45+
if ($this->user !== null) {
46+
return $this->user;
47+
}
48+
49+
if ($token = $this->handle->check()) {
50+
$this->user = $this->provider->retrieveById($token->getId());
51+
}
52+
4453
return $this->user;
4554
}
4655

4756
/**
4857
* @param array $credentials
49-
* @param bool $login
58+
* @param bool $login
5059
*
5160
* @return bool|string
5261
*/
@@ -60,6 +69,27 @@ public function attempt(array $credentials = [], $login = true)
6069
return false;
6170
}
6271

72+
/**
73+
* logout
74+
* @author luffyzhao@vip.126.com
75+
* @return bool
76+
* @throws \LTools\Exceptions\TokenException
77+
*/
78+
public function logout(): bool
79+
{
80+
return $this->handle->delete();
81+
}
82+
83+
84+
/**
85+
* refresh
86+
* @author luffyzhao@vip.126.com
87+
* @return mixed
88+
* @throws \LTools\Exceptions\TokenException
89+
*/
90+
public function refresh(){
91+
return $this->handle->refresh();
92+
}
6393

6494
/**
6595
*
@@ -98,7 +128,6 @@ public function login(Authenticatable $user)
98128
{
99129
$token = $this->handle->generate($user);
100130
$this->setUser($user);
101-
102131
return $token;
103132
}
104133
}

src/Auths/Cache/Token.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: luffy
5+
* Date: 2019/4/8
6+
* Time: 16:57
7+
*/
8+
9+
namespace LTools\Auths\Cache;
10+
11+
use Illuminate\Support\Facades\Crypt;
12+
use Illuminate\Support\Str;
13+
14+
class Token
15+
{
16+
/**
17+
* @var mixed
18+
*/
19+
protected $id;
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $code;
25+
26+
/**
27+
* @var int
28+
*/
29+
protected $time;
30+
31+
/**
32+
* Token constructor.
33+
* @param $authId
34+
*/
35+
public function __construct($authId)
36+
{
37+
$this->id = $authId;
38+
39+
$this->code = $this->getRedisString();
40+
41+
$this->time = time();
42+
}
43+
44+
/**
45+
* generateTokenString
46+
* @author luffyzhao@vip.126.com
47+
* @return string
48+
*/
49+
private function getRedisString(): string
50+
{
51+
return Str::random(16);
52+
}
53+
54+
/**
55+
* __toString
56+
* @author luffyzhao@vip.126.com
57+
* @return string
58+
*/
59+
public function __toString() : string
60+
{
61+
return Crypt::encrypt($this);
62+
}
63+
64+
/**
65+
* @return mixed
66+
*/
67+
public function getId()
68+
{
69+
return $this->id;
70+
}
71+
72+
/**
73+
* @return string
74+
*/
75+
public function getCode(): string
76+
{
77+
return $this->code;
78+
}
79+
80+
/**
81+
* @return int
82+
*/
83+
public function getTime(): int
84+
{
85+
return $this->time;
86+
}
87+
}

0 commit comments

Comments
 (0)