|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2019 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\ServerBundle\Tests\TestBundle\Repository; |
|
15
|
|
|
|
|
16
|
|
|
use Assert\Assertion; |
|
17
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessToken as InitialAccessTokenInterface; |
|
18
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId; |
|
19
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
|
20
|
|
|
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\InitialAccessToken; |
|
21
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class InitialAccessTokenRepository implements \OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenRepository |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var CacheItemPoolInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $cache; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(CacheItemPoolInterface $cache) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->cache = $cache; |
|
33
|
|
|
$this->createInitialAccessTokens(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function find(InitialAccessTokenId $initialAccessTokenId): ?InitialAccessTokenInterface |
|
37
|
|
|
{ |
|
38
|
|
|
$item = $this->cache->getItem('InitialAccessToken-'.$initialAccessTokenId->getValue()); |
|
39
|
|
|
if ($item->isHit()) { |
|
40
|
|
|
return $item->get(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function save(InitialAccessTokenInterface $initialAccessToken): void |
|
47
|
|
|
{ |
|
48
|
|
|
Assertion::isInstanceOf($initialAccessToken, InitialAccessToken::class, 'Unsupported entity'); |
|
49
|
|
|
$item = $this->cache->getItem('InitialAccessToken-'.$initialAccessToken->getId()->getValue()); |
|
50
|
|
|
$item->set($initialAccessToken); |
|
51
|
|
|
$this->cache->save($item); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function createInitialAccessTokens() |
|
55
|
|
|
{ |
|
56
|
|
|
$iat = new InitialAccessToken( |
|
57
|
|
|
new InitialAccessTokenId('VALID_INITIAL_ACCESS_TOKEN_ID'), |
|
58
|
|
|
new UserAccountId('john.1'), |
|
59
|
|
|
new \DateTimeImmutable('now +1 day') |
|
60
|
|
|
); |
|
61
|
|
|
$this->save($iat); |
|
62
|
|
|
|
|
63
|
|
|
$iat = new InitialAccessToken( |
|
64
|
|
|
new InitialAccessTokenId('EXPIRED_INITIAL_ACCESS_TOKEN_ID'), |
|
65
|
|
|
new UserAccountId('john.1'), |
|
66
|
|
|
new \DateTimeImmutable('now -1 day') |
|
67
|
|
|
); |
|
68
|
|
|
$this->save($iat); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|