1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\RefreshToken\Storage; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManager; |
17
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
18
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
19
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractRefreshToken; |
20
|
|
|
use Silverback\ApiComponentsBundle\RefreshToken\RefreshToken; |
21
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\RefreshTokenRepository; |
22
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Vincent Chalamon <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class DoctrineRefreshTokenStorage implements RefreshTokenStorageInterface |
28
|
|
|
{ |
29
|
|
|
private ManagerRegistry $registry; |
30
|
|
|
private int $ttl; |
31
|
|
|
private string $className; |
32
|
|
|
|
33
|
7 |
|
public function __construct(ManagerRegistry $registry, int $ttl, array $options) |
34
|
|
|
{ |
35
|
7 |
|
$this->registry = $registry; |
36
|
7 |
|
$this->ttl = $ttl; |
37
|
|
|
|
38
|
7 |
|
if (!isset($options['class'])) { |
39
|
|
|
throw new \InvalidArgumentException('You must specify silverback_api_components.refresh.token.options.class option.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
$this->className = $options['class']; |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
public function findOneByUser(UserInterface $user): ?RefreshToken |
46
|
|
|
{ |
47
|
|
|
$em = $this->getEntityManager(); |
48
|
|
|
$repository = $em->getRepository($this->className); |
49
|
|
|
if (!$repository instanceof RefreshTokenRepository) { |
50
|
|
|
throw new \InvalidArgumentException('RefreshToken entity repository must be instance of ' . RefreshTokenRepository::class); |
51
|
|
|
} |
52
|
|
|
$refreshTokens = $repository->findByUser($user); |
53
|
|
|
|
54
|
|
|
// Concurrent requests will result in multiple refresh tokens having been expired and re-created |
55
|
|
|
if (\count($refreshTokens) > 1) { |
56
|
|
|
foreach ($refreshTokens as $i => $refreshToken) { |
57
|
|
|
if ($i > 0) { |
58
|
|
|
$em->remove($refreshToken); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$em->flush(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $refreshTokens[0]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function create(UserInterface $user): RefreshToken |
68
|
|
|
{ |
69
|
|
|
$className = $this->className; |
70
|
|
|
/** @var RefreshToken $refreshToken */ |
71
|
|
|
$refreshToken = new $className(); |
72
|
|
|
$refreshToken->setCreatedAt(new \DateTimeImmutable()); |
73
|
|
|
$refreshToken->setExpiresAt(new \DateTimeImmutable("$this->ttl seconds")); |
74
|
|
|
$refreshToken->setUser($user); |
75
|
|
|
|
76
|
|
|
$em = $this->getEntityManager(); |
77
|
|
|
$em->persist($refreshToken); |
78
|
|
|
$em->flush($refreshToken); |
79
|
|
|
|
80
|
|
|
return $refreshToken; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function expireAll(?UserInterface $user): void |
84
|
|
|
{ |
85
|
|
|
$em = $this->getEntityManager(); |
86
|
|
|
$repository = $em->getRepository($this->className); |
87
|
|
|
$refreshTokens = $user ? $repository->findBy(['user' => $user]) : $repository->findAll(); |
88
|
|
|
|
89
|
|
|
foreach ($refreshTokens as $refreshToken) { |
90
|
|
|
/* @var AbstractRefreshToken $refreshToken */ |
91
|
|
|
$this->expireToken($refreshToken, false); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$em->flush(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function expireToken(AbstractRefreshToken $refreshToken, bool $flush = true): void |
98
|
|
|
{ |
99
|
|
|
$em = $this->getEntityManager(); |
100
|
|
|
if (!$refreshToken->isExpired()) { |
101
|
|
|
$refreshToken->setExpiresAt(new \DateTimeImmutable()); |
102
|
|
|
} |
103
|
|
|
if ($flush) { |
104
|
|
|
$em->flush(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function getEntityManager(): EntityManager |
109
|
|
|
{ |
110
|
|
|
/** @var EntityManager|null $em */ |
111
|
|
|
$em = $this->registry->getManagerForClass($this->className); |
112
|
|
|
if (!$em) { |
|
|
|
|
113
|
|
|
throw new EntityNotFoundException('No entity found for class RefreshToken::class.'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $em; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|