|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Auth; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
9
|
|
|
use Spiral\Auth\Exception\TokenStorageException; |
|
10
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
11
|
|
|
use Spiral\Core\Exception\ScopeException; |
|
12
|
|
|
|
|
13
|
|
|
final class TokenStorageScope implements TokenStorageInterface, SingletonInterface |
|
14
|
|
|
{ |
|
15
|
4 |
|
public function __construct( |
|
16
|
|
|
private readonly ContainerInterface $container |
|
17
|
|
|
) { |
|
18
|
4 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Load token by id, must return null if token not found. |
|
22
|
|
|
* |
|
23
|
|
|
* @throws TokenStorageException |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function load(string $id): ?TokenInterface |
|
26
|
|
|
{ |
|
27
|
1 |
|
return $this->getTokenStorage()->load($id); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create token based on the payload provided by actor provider. |
|
32
|
|
|
* |
|
33
|
|
|
* @throws TokenStorageException |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
|
36
|
|
|
{ |
|
37
|
1 |
|
return $this->getTokenStorage()->create($payload, $expiresAt); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Delete token from the persistent storage. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws TokenStorageException |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function delete(TokenInterface $token): void |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->getTokenStorage()->delete($token); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @throws ScopeException |
|
52
|
|
|
*/ |
|
53
|
4 |
|
private function getTokenStorage(): TokenStorageInterface |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
4 |
|
return $this->container->get(TokenStorageInterface::class); |
|
57
|
|
|
} catch (NotFoundExceptionInterface $e) { |
|
58
|
|
|
throw new ScopeException('Unable to resolve token storage, invalid scope', $e->getCode(), $e); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|