|
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 as AccessTokenRepositoryInterface; |
|
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
|
|
|
|
|
25
|
|
|
class AccessTokenRepository implements AccessTokenRepositoryInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var AccessToken[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $accessTokens = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function find(AccessTokenId $accessTokenId) |
|
36
|
|
|
{ |
|
37
|
|
|
return array_key_exists($accessTokenId->getValue(), $this->accessTokens) ? $this->accessTokens[$accessTokenId->getValue()] : null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function save(AccessToken $accessToken) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->accessTokens[$accessToken->getTokenId()->getValue()] = $accessToken; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function create(ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, ? ResourceServerId $resourceServerId): AccessToken |
|
52
|
|
|
{ |
|
53
|
|
|
$expiresAt = new \DateTimeImmutable(sprintf('now +1800 seconds')); |
|
54
|
|
|
$length = random_int(50, 100); |
|
55
|
|
|
$accessTokenId = AccessTokenId::create(Base64Url::encode(random_bytes($length))); |
|
56
|
|
|
$accessToken = AccessToken::createEmpty(); |
|
57
|
|
|
$accessToken = $accessToken->create($accessTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId); |
|
58
|
|
|
|
|
59
|
|
|
return $accessToken; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|