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