|
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\Component\ClientRegistrationEndpoint\InitialAccessToken; |
|
17
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId; |
|
18
|
|
|
use OAuth2Framework\Component\Core\Event\Event; |
|
19
|
|
|
use OAuth2Framework\Component\Core\Event\EventStore; |
|
20
|
|
|
use SimpleBus\Message\Recorder\RecordsMessages; |
|
21
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class InitialAccessTokenRepository implements \OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenRepository |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EventStore |
|
27
|
|
|
*/ |
|
28
|
|
|
private $eventStore; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var RecordsMessages |
|
32
|
|
|
*/ |
|
33
|
|
|
private $eventRecorder; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var AdapterInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $cache; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* InitialAccessTokenRepository constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param EventStore $eventStore |
|
44
|
|
|
* @param RecordsMessages $eventRecorder |
|
45
|
|
|
* @param AdapterInterface $cache |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(EventStore $eventStore, RecordsMessages $eventRecorder, AdapterInterface $cache) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->eventStore = $eventStore; |
|
50
|
|
|
$this->eventRecorder = $eventRecorder; |
|
51
|
|
|
$this->cache = $cache; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function find(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken |
|
58
|
|
|
{ |
|
59
|
|
|
$initialAccessToken = $this->getFromCache($initialAccessTokenId); |
|
60
|
|
|
if (null === $initialAccessToken) { |
|
61
|
|
|
$events = $this->eventStore->findAllForDomainId($initialAccessTokenId); |
|
62
|
|
|
if (!empty($events)) { |
|
63
|
|
|
$initialAccessToken = $this->getFromEvents($events); |
|
64
|
|
|
$this->cacheObject($initialAccessToken); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $initialAccessToken; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function save(InitialAccessToken $initialAccessToken) |
|
75
|
|
|
{ |
|
76
|
|
|
$events = $initialAccessToken->recordedMessages(); |
|
77
|
|
|
foreach ($events as $event) { |
|
78
|
|
|
$this->eventStore->save($event); |
|
79
|
|
|
$this->eventRecorder->record($event); |
|
80
|
|
|
} |
|
81
|
|
|
$initialAccessToken->eraseMessages(); |
|
82
|
|
|
$this->cacheObject($initialAccessToken); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Event[] $events |
|
87
|
|
|
* |
|
88
|
|
|
* @return InitialAccessToken |
|
89
|
|
|
*/ |
|
90
|
|
|
private function getFromEvents(array $events): InitialAccessToken |
|
91
|
|
|
{ |
|
92
|
|
|
$initialAccessToken = InitialAccessToken::createEmpty(); |
|
93
|
|
|
foreach ($events as $event) { |
|
94
|
|
|
$initialAccessToken = $initialAccessToken->apply($event); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $initialAccessToken; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param InitialAccessTokenId $initialAccessTokenId |
|
102
|
|
|
* |
|
103
|
|
|
* @return InitialAccessToken|null |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getFromCache(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken |
|
106
|
|
|
{ |
|
107
|
|
|
$itemKey = sprintf('oauth2-initial_access_token-%s', $initialAccessTokenId->getValue()); |
|
108
|
|
|
$item = $this->cache->getItem($itemKey); |
|
109
|
|
|
if ($item->isHit()) { |
|
110
|
|
|
return $item->get(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param InitialAccessToken $initialAccessToken |
|
118
|
|
|
*/ |
|
119
|
|
|
private function cacheObject(InitialAccessToken $initialAccessToken) |
|
120
|
|
|
{ |
|
121
|
|
|
$itemKey = sprintf('oauth2-initial_access_token-%s', $initialAccessToken->getUserAccountId()->getValue()); |
|
122
|
|
|
$item = $this->cache->getItem($itemKey); |
|
123
|
|
|
$item->set($initialAccessToken); |
|
124
|
|
|
$item->tag(['oauth2_server', 'initial_access_token', $itemKey]); |
|
125
|
|
|
$this->cache->save($item); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|