Skip to content

Commit 6fb70d9

Browse files
committed
auth
1 parent 6046439 commit 6fb70d9

File tree

4 files changed

+33
-108
lines changed

4 files changed

+33
-108
lines changed

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"illuminate/contracts": "^5.6.0",
77
"illuminate/http": "^5.6.0",
88
"illuminate/support": "^5.6.0",
9+
"illuminate/redis": "^5.6.0",
910
"lcobucci/jwt": "^3.2",
1011
"namshi/jose": "^7.0",
1112
"nesbot/carbon": "^1.0",
@@ -32,5 +33,11 @@
3233
"name": "luffyzhao",
3334
"email": "luffyzhao@vip.126.com"
3435
}
35-
]
36+
],
37+
"repositories": {
38+
"packagist": {
39+
"type": "composer",
40+
"url": "https://packagist.phpcomposer.com"
41+
}
42+
}
3643
}

src/Auths/Signer/SignerGuard.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111

1212
use Illuminate\Auth\GuardHelpers;
13+
use Illuminate\Contracts\Auth\Authenticatable;
1314
use Illuminate\Contracts\Auth\Guard;
1415
use Illuminate\Contracts\Auth\UserProvider;
15-
use LTools\Contracts\Signer\SignerInterface;
1616

1717
class SignerGuard implements Guard
1818
{
@@ -90,13 +90,13 @@ public function validate(array $credentials = [])
9090
}
9191

9292
/**
93-
* @param SignerInterface $user
93+
* @param Authenticatable $user
9494
*
9595
* @return bool|string
9696
*/
97-
public function login(SignerInterface $user)
97+
public function login(Authenticatable $user)
9898
{
99-
$token = $this->handle->fromUser($user);
99+
$token = $this->handle->generate($user);
100100
$this->setUser($user);
101101

102102
return $token;

src/Auths/Signer/TokenHandle.php

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111

1212
use Illuminate\Http\Request;
13-
use Illuminate\Support\Facades\Config;
13+
use Illuminate\Support\Facades\Redis;
1414
use Illuminate\Support\Str;
15-
use Lcobucci\JWT\Builder;
1615
use LTools\Contracts\Signer\SignerInterface;
16+
use Illuminate\Support\Facades\Crypt;
1717

1818
class TokenHandle
1919
{
@@ -24,37 +24,16 @@ class TokenHandle
2424
*/
2525
protected $header = 'authorization';
2626

27-
/**
28-
* 过期时间
29-
*
30-
* @var int
31-
* @author luffyzhao@vip.126.com
32-
*/
33-
protected $expired = 3600;
34-
35-
/**
36-
* 生命周期
37-
*
38-
* @var int
39-
* @author luffyzhao@vip.126.com
40-
*/
41-
protected $ttl = 0;
42-
4327
/**
4428
* @var Request
4529
* @author luffyzhao@vip.126.com
4630
*/
4731
protected $request;
48-
/**
49-
* @var Builder
50-
*/
51-
private $builder;
5232

5333

54-
public function __construct(Request $request, Builder $builder)
34+
public function __construct(Request $request)
5535
{
5636
$this->request = $request;
57-
$this->builder = $builder;
5837
}
5938

6039
/**
@@ -66,99 +45,40 @@ public function __construct(Request $request, Builder $builder)
6645
* @return bool|string
6746
* @author luffyzhao@vip.126.com
6847
*/
69-
public function fromUser(SignerInterface $user)
48+
public function generate(SignerInterface $user): string
7049
{
71-
$code = Str::random(5);
72-
$now = time();
73-
74-
if($user->saveSignerCode($code)){
75-
return $this->builder
76-
->setIssuer(Config::get('app.url'))
77-
->setId(Str::random(12), true)
78-
->setIssuedAt($now)
79-
->setNotBefore($now + Config::get('ltool.signer.nbf'))
80-
->setExpiration($now + Config::get('ltool.signer.exp'))
81-
->set('id', $user->getAuthIdentifier())
82-
->set('code', $code)
83-
->getToken();
84-
}
50+
return $this->tokenString($user);
8551
}
8652

87-
/**
88-
* 尝试从请求头解析token
89-
* @method parse
90-
*
91-
* @return mixed
92-
* @author luffyzhao@vip.126.com
93-
*/
94-
protected function parse()
95-
{
96-
return $this->request->headers->get($this->header)
97-
?: $this->fromAltHeaders();
98-
}
9953

10054
/**
101-
* 试图从某些其他可能的报头解析 token
102-
* @method fromAltHeaders
103-
*
104-
* @return mixed
55+
* tokenString
56+
* @param SignerInterface $user
10557
* @author luffyzhao@vip.126.com
106-
*/
107-
protected function fromAltHeaders()
108-
{
109-
return $this->request->server->get('HTTP_AUTHORIZATION')
110-
?: $this->request->server->get(
111-
'REDIRECT_HTTP_AUTHORIZATION'
112-
);
113-
}
114-
115-
/**
11658
* @return string
11759
*/
118-
public function getHeader(): string
60+
protected function tokenString(SignerInterface $user): string
11961
{
120-
return $this->header;
121-
}
12262

123-
/**
124-
* @param string $header
125-
*/
126-
public function setHeader(string $header)
127-
{
128-
$this->header = $header;
129-
}
63+
$code = $this->getRedisString();
13064

131-
/**
132-
* @return int
133-
*/
134-
public function getExpired(): int
135-
{
136-
return $this->expired;
137-
}
65+
Redis::hset('token:user', $user->getAuthIdentifier(), $code);
13866

139-
/**
140-
* @param int $expired
141-
*/
142-
public function setExpired(int $expired)
143-
{
144-
$this->expired = $expired;
67+
return Crypt::encrypt([
68+
'id' => $user->getAuthIdentifier(),
69+
'code' => $code,
70+
'time' => time()
71+
]);
14572
}
14673

14774
/**
148-
* @return int
149-
*/
150-
public function getTtl(): int
151-
{
152-
return $this->ttl;
153-
}
154-
155-
/**
156-
* @param int $ttl
75+
* generateTokenString
76+
* @author luffyzhao@vip.126.com
77+
* @return string
15778
*/
158-
public function setTtl(int $ttl)
79+
private function getRedisString(): string
15980
{
160-
$this->ttl = $ttl;
81+
return Str::random(16);
16182
}
16283

163-
16484
}

src/Contracts/Signer/SignerInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@
1111

1212
interface SignerInterface
1313
{
14-
public function getSignerIdentifier();
15-
16-
public function saveSignerCode($code);
14+
public function getAuthIdentifier();
1715
}

0 commit comments

Comments
 (0)