1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Garbuzivan\Laraveltokens; |
6
|
|
|
|
7
|
|
|
use Garbuzivan\Laraveltokens\Exceptions\TokenIsNotNalidException; |
8
|
|
|
use Garbuzivan\Laraveltokens\Interfaces\AccessTokenRepositoryInterface; |
9
|
|
|
use Garbuzivan\Laraveltokens\Interfaces\GlobalTokenRepositoryInterface; |
10
|
|
|
use Garbuzivan\Laraveltokens\Models\AccessToken; |
11
|
|
|
use Garbuzivan\Laraveltokens\Traits\ManagerAccessTokenTrait; |
12
|
|
|
use Garbuzivan\Laraveltokens\Traits\ManagerGlobalTokenTrait; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
class TokenManager |
16
|
|
|
{ |
17
|
|
|
use ManagerAccessTokenTrait, ManagerGlobalTokenTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Config $config |
21
|
|
|
*/ |
22
|
|
|
protected Config $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AccessTokenRepositoryInterface |
26
|
|
|
*/ |
27
|
|
|
protected AccessTokenRepositoryInterface $accessTokenRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var GlobalTokenRepositoryInterface |
31
|
|
|
*/ |
32
|
|
|
protected GlobalTokenRepositoryInterface $globalTokenRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Configuration constructor. |
36
|
|
|
* |
37
|
|
|
* @param Config $config |
38
|
|
|
* @param AccessTokenRepositoryInterface $TokenRepository |
39
|
|
|
* @param GlobalTokenRepositoryInterface $globalTokenRepository |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
Config $config, |
43
|
|
|
AccessTokenRepositoryInterface $TokenRepository, |
44
|
|
|
GlobalTokenRepositoryInterface $globalTokenRepository |
45
|
|
|
) { |
46
|
|
|
$this->config = $config; |
47
|
|
|
$this->accessTokenRepository = $TokenRepository; |
48
|
|
|
$this->globalTokenRepository = $globalTokenRepository; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Авторизация по токену |
53
|
|
|
* |
54
|
|
|
* @param string $token |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
* @throws TokenIsNotNalidException |
58
|
|
|
*/ |
59
|
|
|
public function auth(string $token): AccessToken |
60
|
|
|
{ |
61
|
|
|
$token = $this->config->isEncryption() ? $this->getHash($token) : $token; |
62
|
|
|
$tokenDb = $this->accessTokenRepository->getAccessToken($token); |
63
|
|
|
if (!is_null($tokenDb) || $tokenDb->isValid()) { |
64
|
|
|
if ($this->config->isLastUse()) { |
65
|
|
|
$this->accessTokenRepository->setLastUseAccessToken($token); |
66
|
|
|
} |
67
|
|
|
return $tokenDb; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
$tokenDb = $this->globalTokenRepository->getGlobalToken($token); |
70
|
|
|
if (!is_null($tokenDb) || $tokenDb->isValid()) { |
71
|
|
|
if ($this->config->isLastUse()) { |
72
|
|
|
$this->globalTokenRepository->setLastUseGlobalToken($token); |
73
|
|
|
} |
74
|
|
|
return $tokenDb; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
throw new TokenIsNotNalidException; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Очистить таблицу токенов |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function deleteAllTokens(): void |
85
|
|
|
{ |
86
|
|
|
$this->accessTokenRepository->deleteAllAccessToken(); |
87
|
|
|
$this->globalTokenRepository->deleteAllGlobalToken(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Проверить актуальность токена (наличие токена и дата активности) |
92
|
|
|
* |
93
|
|
|
* @param string $token |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isValid(string $token): bool |
98
|
|
|
{ |
99
|
|
|
$token = $this->config->isEncryption() ? $this->getHash($token) : $token; |
100
|
|
|
$tokenInfo = $this->accessTokenRepository->getAccessToken($token); |
101
|
|
|
if (is_null($tokenInfo) || !$tokenInfo->isValid()) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Генерация случайного токена на основе даты и случайной строки |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function generateAccessToken(): string |
113
|
|
|
{ |
114
|
|
|
return sha1(time() . Str::random()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Преобразование токена для БД в зависимости от настройки Encryption |
119
|
|
|
* |
120
|
|
|
* @param string $token |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getAccessTokenDb(string $token): string |
125
|
|
|
{ |
126
|
|
|
return $this->config->isEncryption() ? $this->getHash($token) : $token; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Получение хэша |
131
|
|
|
* |
132
|
|
|
* @param string $string |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getHash(string $string): string |
137
|
|
|
{ |
138
|
|
|
return hash('sha256', $string); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Сравнение токена |
143
|
|
|
* |
144
|
|
|
* @param string $token |
145
|
|
|
* @param string $hash |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function isVerify(string $token, string $hash): bool |
150
|
|
|
{ |
151
|
|
|
return strcmp($this->getAccessTokenDb($token), $hash) !== 0; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Получить deault Morph |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getDefaultMorph(): string |
160
|
|
|
{ |
161
|
|
|
return 'App\Models\User'; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|