AuthorizationCodeRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\AuthorizationCodeGrant\AuthorizationCode as AuthorizationCodeInterface;
18
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeId;
19
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeRepository as AuthorizationCodeRepositoryInterface;
20
use OAuth2Framework\Component\Core\Client\ClientId;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
23
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
24
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\AuthorizationCode;
25
use Psr\Cache\CacheItemPoolInterface;
26
27
final class AuthorizationCodeRepository implements AuthorizationCodeRepositoryInterface
28
{
29
    /**
30
     * @var CacheItemPoolInterface
31
     */
32
    private $cache;
33
34
    public function __construct(CacheItemPoolInterface $cache)
35
    {
36
        $this->cache = $cache;
37
        $this->load();
38
    }
39
40
    public function find(AuthorizationCodeId $authorizationCodeId): ?AuthorizationCodeInterface
41
    {
42
        $item = $this->cache->getItem('AuthorizationCode-'.$authorizationCodeId->getValue());
43
        if ($item->isHit()) {
44
            return $item->get();
45
        }
46
47
        return null;
48
    }
49
50
    public function save(AuthorizationCodeInterface $authorizationCode): void
51
    {
52
        Assertion::isInstanceOf($authorizationCode, AuthorizationCode::class, 'Unsupported authorization code class');
53
        $item = $this->cache->getItem('AuthorizationCode-'.$authorizationCode->getId()->getValue());
54
        $item->set($authorizationCode);
55
        $this->cache->save($item);
56
    }
57
58
    public function create(ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId): AuthorizationCodeInterface
59
    {
60
        return new AuthorizationCode(
61
            new AuthorizationCodeId(bin2hex(random_bytes(32))),
62
            $clientId,
63
            $userAccountId,
64
            $queryParameters,
65
            $redirectUri,
66
            $expiresAt,
67
            $parameter,
68
            $metadata,
69
            $resourceServerId
70
        );
71
    }
72
73
    private function load(): void
74
    {
75
        foreach ($this->getData() as $datum) {
76
            $authorizationCode = new AuthorizationCode(
77
                new AuthorizationCodeId($datum['authorization_code_id']),
78
                new ClientId($datum['client_id']),
79
                new UserAccountId($datum['user_account_id']),
80
                $datum['query_parameters'],
81
                $datum['redirect_uri'],
82
                $datum['expires_at'],
83
                new DataBag($datum['parameter']),
84
                new DataBag($datum['metadata']),
85
                $datum['resource_server_id']
86
            );
87
            if ($datum['is_revoked']) {
88
                $authorizationCode->markAsRevoked();
89
            }
90
            if ($datum['is_used']) {
91
                $authorizationCode->markAsUsed();
92
            }
93
            $this->save($authorizationCode);
94
        }
95
    }
96
97
    private function getData(): array
98
    {
99
        return [
100
            [
101
                'authorization_code_id' => 'VALID_AUTHORIZATION_CODE',
102
                'client_id' => 'CLIENT_ID_3',
103
                'user_account_id' => 'john.1',
104
                'query_parameters' => [],
105
                'redirect_uri' => 'http://localhost/callback',
106
                'expires_at' => new \DateTimeImmutable('now +1 day'),
107
                'parameter' => [],
108
                'metadata' => [],
109
                'resource_server_id' => null,
110
                'is_revoked' => false,
111
                'is_used' => false,
112
            ],
113
            [
114
                'authorization_code_id' => 'VALID_AUTHORIZATION_CODE_FOR_CONFIDENTIAL_CLIENT',
115
                'client_id' => 'CLIENT_ID_5',
116
                'user_account_id' => 'john.1',
117
                'query_parameters' => [],
118
                'redirect_uri' => 'http://localhost/callback',
119
                'expires_at' => new \DateTimeImmutable('now +1 day'),
120
                'parameter' => [],
121
                'metadata' => [],
122
                'resource_server_id' => null,
123
                'is_revoked' => false,
124
                'is_used' => false,
125
            ],
126
            [
127
                'authorization_code_id' => 'REVOKED_AUTHORIZATION_CODE',
128
                'client_id' => 'CLIENT_ID_3',
129
                'user_account_id' => 'john.1',
130
                'query_parameters' => [],
131
                'redirect_uri' => 'http://localhost/callback',
132
                'expires_at' => new \DateTimeImmutable('now +1 day'),
133
                'parameter' => [],
134
                'metadata' => [],
135
                'resource_server_id' => null,
136
                'is_revoked' => true,
137
                'is_used' => false,
138
            ],
139
            [
140
                'authorization_code_id' => 'EXPIRED_AUTHORIZATION_CODE',
141
                'client_id' => 'CLIENT_ID_3',
142
                'user_account_id' => 'john.1',
143
                'query_parameters' => [],
144
                'redirect_uri' => 'http://localhost/callback',
145
                'expires_at' => new \DateTimeImmutable('now -1 day'),
146
                'parameter' => [],
147
                'metadata' => [],
148
                'resource_server_id' => null,
149
                'is_revoked' => false,
150
                'is_used' => false,
151
            ],
152
            [
153
                'authorization_code_id' => 'USED_AUTHORIZATION_CODE',
154
                'client_id' => 'CLIENT_ID_3',
155
                'user_account_id' => 'john.1',
156
                'query_parameters' => [],
157
                'redirect_uri' => 'http://localhost/callback',
158
                'expires_at' => new \DateTimeImmutable('now +1 day'),
159
                'parameter' => [],
160
                'metadata' => [],
161
                'resource_server_id' => null,
162
                'is_revoked' => false,
163
                'is_used' => true,
164
            ],
165
        ];
166
    }
167
}
168