1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Bundle\Tests\TestBundle\Entity; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Bundle\Service\RandomIdGenerator; |
17
|
|
|
use OAuth2Framework\Component\Core\AccessToken\AccessToken; |
18
|
|
|
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId; |
19
|
|
|
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository; |
20
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
21
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
22
|
|
|
use OAuth2Framework\Component\Core\Event\Event; |
23
|
|
|
use OAuth2Framework\Component\Core\Event\EventStore; |
24
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId; |
25
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
26
|
|
|
use SimpleBus\Message\Bus\MessageBus; |
27
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
28
|
|
|
|
29
|
|
|
final class AccessTokenByReferenceRepository implements AccessTokenRepository |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $lifetime; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $minLength; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $maxLength; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var EventStore |
48
|
|
|
*/ |
49
|
|
|
private $eventStore; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var MessageBus |
53
|
|
|
*/ |
54
|
|
|
private $eventBus; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var AdapterInterface |
58
|
|
|
*/ |
59
|
|
|
private $cache; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* AccessTokenByReferenceRepository constructor. |
63
|
|
|
* |
64
|
|
|
* @param int $minLength |
65
|
|
|
* @param int $maxLength |
66
|
|
|
* @param int $lifetime |
67
|
|
|
* @param EventStore $eventStore |
68
|
|
|
* @param MessageBus $eventBus |
69
|
|
|
* @param AdapterInterface $cache |
70
|
|
|
*/ |
71
|
|
|
public function __construct(int $minLength, int $maxLength, int $lifetime, EventStore $eventStore, MessageBus $eventBus, AdapterInterface $cache) |
72
|
|
|
{ |
73
|
|
|
$this->minLength = $minLength; |
74
|
|
|
$this->maxLength = $maxLength; |
75
|
|
|
$this->lifetime = $lifetime; |
76
|
|
|
$this->eventStore = $eventStore; |
77
|
|
|
$this->eventBus = $eventBus; |
78
|
|
|
$this->cache = $cache; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function find(AccessTokenId $accessTokenId) |
85
|
|
|
{ |
86
|
|
|
$accessToken = $this->getFromCache($accessTokenId); |
87
|
|
|
if (null === $accessToken) { |
88
|
|
|
$events = $this->eventStore->findAllForDomainId($accessTokenId); |
89
|
|
|
if (!empty($events)) { |
90
|
|
|
$accessToken = $this->getFromEvents($events); |
91
|
|
|
$this->cacheObject($accessToken); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $accessToken; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param AccessToken $accessToken |
100
|
|
|
*/ |
101
|
|
|
public function save(AccessToken $accessToken) |
102
|
|
|
{ |
103
|
|
|
$events = $accessToken->recordedMessages(); |
104
|
|
|
foreach ($events as $event) { |
105
|
|
|
$this->eventStore->save($event); |
106
|
|
|
$this->eventBus->handle($event); |
107
|
|
|
} |
108
|
|
|
$accessToken->eraseMessages(); |
109
|
|
|
$this->cacheObject($accessToken); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function create(ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, ? ResourceServerId $resourceServerId): AccessToken |
116
|
|
|
{ |
117
|
|
|
$expiresAt = new \DateTimeImmutable(sprintf('now +%u seconds', $this->lifetime)); |
118
|
|
|
$accessTokenId = AccessTokenId::create(RandomIdGenerator::generate($this->minLength, $this->maxLength)); |
119
|
|
|
$accessToken = AccessToken::createEmpty(); |
120
|
|
|
$accessToken = $accessToken->create($accessTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId); |
121
|
|
|
|
122
|
|
|
return $accessToken; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Event[] $events |
127
|
|
|
* |
128
|
|
|
* @return AccessToken |
129
|
|
|
*/ |
130
|
|
|
private function getFromEvents(array $events): AccessToken |
131
|
|
|
{ |
132
|
|
|
$accessToken = AccessToken::createEmpty(); |
133
|
|
|
foreach ($events as $event) { |
134
|
|
|
$accessToken = $accessToken->apply($event); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $accessToken; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param AccessTokenId $accessTokenId |
142
|
|
|
* |
143
|
|
|
* @return AccessToken|null |
144
|
|
|
*/ |
145
|
|
|
private function getFromCache(AccessTokenId $accessTokenId): ? AccessToken |
146
|
|
|
{ |
147
|
|
|
$itemKey = sprintf('oauth2-access_token-%s', $accessTokenId->getValue()); |
148
|
|
|
$item = $this->cache->getItem($itemKey); |
149
|
|
|
if ($item->isHit()) { |
150
|
|
|
return $item->get(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param AccessToken $accessToken |
158
|
|
|
*/ |
159
|
|
|
private function cacheObject(AccessToken $accessToken) |
160
|
|
|
{ |
161
|
|
|
$itemKey = sprintf('oauth2-access_token-%s', $accessToken->getTokenId()->getValue()); |
162
|
|
|
$item = $this->cache->getItem($itemKey); |
163
|
|
|
$item->set($accessToken); |
164
|
|
|
$item->tag(['oauth2_server', 'access_token', $itemKey]); |
165
|
|
|
$this->cache->save($item); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|