|
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 Base64Url\Base64Url; |
|
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\ResourceOwner\ResourceOwnerId; |
|
23
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
|
24
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
|
25
|
|
|
|
|
26
|
|
|
class AccessTokenByReferenceRepository implements AccessTokenRepository |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $lifetime; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $minLength; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $maxLength; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var AdapterInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $cache; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* AccessTokenByReferenceRepository constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int $minLength |
|
52
|
|
|
* @param int $maxLength |
|
53
|
|
|
* @param int $lifetime |
|
54
|
|
|
* @param AdapterInterface $cache |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(int $minLength, int $maxLength, int $lifetime, AdapterInterface $cache) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->minLength = $minLength; |
|
59
|
|
|
$this->maxLength = $maxLength; |
|
60
|
|
|
$this->lifetime = $lifetime; |
|
61
|
|
|
$this->cache = $cache; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function find(AccessTokenId $accessTokenId) |
|
68
|
|
|
{ |
|
69
|
|
|
$accessToken = $this->getFromCache($accessTokenId); |
|
70
|
|
|
|
|
71
|
|
|
return $accessToken; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param AccessToken $accessToken |
|
76
|
|
|
*/ |
|
77
|
|
|
public function save(AccessToken $accessToken) |
|
78
|
|
|
{ |
|
79
|
|
|
$accessToken->eraseMessages(); |
|
80
|
|
|
$this->cacheObject($accessToken); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function create(ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, ? ResourceServerId $resourceServerId): AccessToken |
|
87
|
|
|
{ |
|
88
|
|
|
$expiresAt = new \DateTimeImmutable(sprintf('now +%u seconds', $this->lifetime)); |
|
89
|
|
|
$length = random_int($this->minLength, $this->maxLength); |
|
90
|
|
|
$accessTokenId = AccessTokenId::create(Base64Url::encode(random_bytes($length))); |
|
91
|
|
|
$accessToken = AccessToken::createEmpty(); |
|
92
|
|
|
$accessToken = $accessToken->create($accessTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId); |
|
93
|
|
|
|
|
94
|
|
|
return $accessToken; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param AccessTokenId $accessTokenId |
|
99
|
|
|
* |
|
100
|
|
|
* @return AccessToken|null |
|
101
|
|
|
*/ |
|
102
|
|
|
private function getFromCache(AccessTokenId $accessTokenId): ? AccessToken |
|
103
|
|
|
{ |
|
104
|
|
|
$itemKey = sprintf('oauth2-access_token-%s', $accessTokenId->getValue()); |
|
105
|
|
|
$item = $this->cache->getItem($itemKey); |
|
106
|
|
|
if ($item->isHit()) { |
|
107
|
|
|
return $item->get(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param AccessToken $accessToken |
|
115
|
|
|
*/ |
|
116
|
|
|
private function cacheObject(AccessToken $accessToken) |
|
117
|
|
|
{ |
|
118
|
|
|
$itemKey = sprintf('oauth2-access_token-%s', $accessToken->getTokenId()->getValue()); |
|
119
|
|
|
$item = $this->cache->getItem($itemKey); |
|
120
|
|
|
$item->set($accessToken); |
|
121
|
|
|
$item->tag(['oauth2_server', 'access_token', $itemKey]); |
|
122
|
|
|
$this->cache->save($item); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|