1 | <?php |
||
12 | final class TemporaryAccessService |
||
13 | { |
||
14 | /** |
||
15 | * The access token repository implementation. |
||
16 | * |
||
17 | * @var AccessTokenRepositoryInterface |
||
18 | */ |
||
19 | private $repository; |
||
20 | |||
21 | /** |
||
22 | * The token generator implementation. |
||
23 | * |
||
24 | * @var TokenGeneratorInterface |
||
25 | */ |
||
26 | private $tokenGenerator; |
||
27 | |||
28 | /** |
||
29 | * TemporaryAccessService constructor. |
||
30 | * |
||
31 | * @param AccessTokenRepositoryInterface $repository The access token repository implementation. |
||
32 | * @param TokenGeneratorInterface $tokenGenerator The token generator implementation. |
||
33 | */ |
||
34 | 25 | public function __construct(AccessTokenRepositoryInterface $repository, TokenGeneratorInterface $tokenGenerator) |
|
39 | |||
40 | /** |
||
41 | * Retrieve an access token from the storage by the actual token. |
||
42 | * |
||
43 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
44 | * @param string|TokenInterface $encryptedText The token of the authenticatable. |
||
45 | * |
||
46 | * @return null|AccessTokenInterface |
||
47 | */ |
||
48 | 16 | public function retrieve(AuthenticatableContract $authenticatable, $encryptedText) |
|
54 | |||
55 | /** |
||
56 | * Retrieve an access token from the storage by the plain token. |
||
57 | * |
||
58 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
59 | * @param string $plainText The token of the authenticatable. |
||
60 | * |
||
61 | * @return null|AccessTokenInterface |
||
62 | */ |
||
63 | 1 | public function retrieveUsingPlainText(AuthenticatableContract $authenticatable, $plainText) |
|
71 | |||
72 | /** |
||
73 | * Determine if an access token exists and is valid. |
||
74 | * |
||
75 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
76 | * @param string|TokenInterface $encryptedText The encrypted token of the authenticatable. |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 6 | public function check(AuthenticatableContract $authenticatable, $encryptedText) |
|
84 | |||
85 | /** |
||
86 | * Determine if an access token exists and is valid. |
||
87 | * |
||
88 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
89 | * @param string $plainText The plain token of the authenticatable. |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | 2 | public function checkUsingPlainText(AuthenticatableContract $authenticatable, $plainText) |
|
99 | |||
100 | /** |
||
101 | * Determine if an access token record exists and prolong the expire date if so. |
||
102 | * If no prolong time given, we will reset the original expire time. |
||
103 | * |
||
104 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
105 | * @param string|TokenInterface $encryptedText The token of the authenticatable. |
||
106 | * @param int|null $prolong The prolong time in minutes. |
||
107 | * |
||
108 | * @return bool|AccessTokenInterface |
||
109 | */ |
||
110 | 6 | public function checkAndProlong(AuthenticatableContract $authenticatable, $encryptedText, $prolong = null) |
|
118 | |||
119 | /** |
||
120 | * Determine if an access token record exists and prolong the expire date if so. |
||
121 | * If no prolong time given, we will reset the original expire time. |
||
122 | * |
||
123 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
124 | * @param string $plainText The token of the authenticatable. |
||
125 | * @param int|null $prolong The prolong time in minutes. |
||
126 | * |
||
127 | * @return bool|AccessTokenInterface |
||
128 | */ |
||
129 | 1 | public function checkUsingPlainTextAndProlong(AuthenticatableContract $authenticatable, $plainText, $prolong = null) |
|
135 | |||
136 | /** |
||
137 | * Generate a new access token in the storage and get the token. |
||
138 | * |
||
139 | * @param AuthenticatableContract $authenticatable The authenticatable who owns the token. |
||
140 | * @param Carbon|null $expiresAt The optional expire date of the access token. |
||
141 | * |
||
142 | * @return AccessTokenInterface |
||
143 | */ |
||
144 | 2 | public function generate(AuthenticatableContract $authenticatable, Carbon $expiresAt = null) |
|
154 | |||
155 | /** |
||
156 | * Update an access token in the storage. |
||
157 | * |
||
158 | * @param AccessTokenInterface $accessToken The access token to be updated. |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | 6 | public function update(AccessTokenInterface $accessToken) |
|
170 | |||
171 | /** |
||
172 | * Revive an token from the given plain text. |
||
173 | * |
||
174 | * @param string $plainText The plain text to be converted back to token instance. |
||
175 | * |
||
176 | * @return TokenInterface |
||
177 | */ |
||
178 | 5 | public function makeTokenFromPlainText($plainText) |
|
182 | |||
183 | /** |
||
184 | * Revive an token from the given plain text. |
||
185 | * |
||
186 | * @param string $encryptedText The encrypted token to be converted back to token instance. |
||
187 | * |
||
188 | * @return TokenInterface |
||
189 | */ |
||
190 | 12 | public function makeTokenFromEncryptedText($encryptedText) |
|
194 | |||
195 | /** |
||
196 | * Retrieve the first resource by the given attributes. |
||
197 | * |
||
198 | * @param array $queryParams The key - value pairs to match. |
||
199 | * @param array $attributes The attributes to be returned from the storage. |
||
200 | * |
||
201 | * @return AccessTokenInterface|null |
||
202 | */ |
||
203 | 2 | public function retrieveByAttributes(array $queryParams, array $attributes = ['*']) |
|
211 | |||
212 | /** |
||
213 | * Delete the given access token from the storage. |
||
214 | * |
||
215 | * @param AccessTokenInterface|string $accessToken The access token or the encrypted text to be deleted. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | 2 | public function delete($accessToken) |
|
223 | |||
224 | /** |
||
225 | * Delete the expired access tokens from the storage. |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | 1 | public function deleteExpired() |
|
233 | |||
234 | /** |
||
235 | * Retrieve an access token from the storage. |
||
236 | * |
||
237 | * @param int $authenticatableId |
||
238 | * @param string $encryptedText |
||
239 | * |
||
240 | * @return GenericAccessToken|null |
||
241 | */ |
||
242 | 16 | private function retrieveFromRepository($authenticatableId, $encryptedText) |
|
243 | { |
||
244 | 16 | if (! $attributes = $this->repository->retrieve($authenticatableId, $encryptedText)) { |
|
245 | 5 | return; |
|
246 | } |
||
247 | |||
248 | 11 | return $this->makeAccessToken((array) $attributes); |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Prolong the access token then update it in the storage. |
||
253 | * |
||
254 | * @param AccessTokenInterface $accessToken |
||
255 | * @param int|null $prolong |
||
256 | * |
||
257 | * @return bool|AccessTokenInterface |
||
258 | */ |
||
259 | 5 | private function prolongAndUpdateAccessToken(AccessTokenInterface $accessToken, $prolong = null) |
|
269 | |||
270 | /** |
||
271 | * Prolong an access token. |
||
272 | * |
||
273 | * @param AccessTokenInterface $accessToken |
||
274 | * @param int|null $prolong |
||
275 | * |
||
276 | * @return AccessTokenInterface |
||
277 | */ |
||
278 | 5 | private function prolongAccessToken(AccessTokenInterface $accessToken, $prolong = null) |
|
284 | |||
285 | /** |
||
286 | * Get a new access token instance with the given attributes. |
||
287 | * |
||
288 | * @param array|TokenInterface $token |
||
289 | * @param array $attributes |
||
290 | * |
||
291 | * @return GenericAccessToken |
||
292 | */ |
||
293 | 14 | private function makeAccessToken($token, array $attributes = []) |
|
303 | |||
304 | /** |
||
305 | * Get the current UNIX timestamp. |
||
306 | * |
||
307 | * @return Carbon |
||
308 | */ |
||
309 | 2 | private function getNow() |
|
313 | } |
||
314 |