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\Component\AuthorizationCodeGrant; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
17
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
18
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId; |
19
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
20
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
21
|
|
|
|
22
|
|
|
abstract class AbstractAuthorizationCode implements AuthorizationCode |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $queryParameters; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $redirectUri; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $used; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \DateTimeImmutable |
41
|
|
|
*/ |
42
|
|
|
private $expiresAt; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ResourceOwnerId |
46
|
|
|
*/ |
47
|
|
|
private $userAccountId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ClientId |
51
|
|
|
*/ |
52
|
|
|
private $clientId; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var DataBag |
56
|
|
|
*/ |
57
|
|
|
private $parameter; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var DataBag |
61
|
|
|
*/ |
62
|
|
|
private $metadata; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
private $revoked; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var ResourceServerId|null |
71
|
|
|
*/ |
72
|
|
|
private $resourceServerId; |
73
|
|
|
|
74
|
|
|
public function __construct(ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId) |
75
|
|
|
{ |
76
|
|
|
$this->queryParameters = $queryParameters; |
77
|
|
|
$this->redirectUri = $redirectUri; |
78
|
|
|
$this->used = false; |
79
|
|
|
$this->userAccountId = $userAccountId; |
80
|
|
|
$this->clientId = $clientId; |
81
|
|
|
$this->parameter = $parameter; |
82
|
|
|
$this->metadata = $metadata; |
83
|
|
|
$this->expiresAt = $expiresAt; |
84
|
|
|
$this->resourceServerId = $resourceServerId; |
85
|
|
|
$this->revoked = false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function isUsed(): bool |
89
|
|
|
{ |
90
|
|
|
return $this->used; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function markAsUsed(): void |
94
|
|
|
{ |
95
|
|
|
$this->used = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getQueryParameters(): array |
99
|
|
|
{ |
100
|
|
|
return $this->queryParameters; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getQueryParameter(string $key) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if (!$this->hasQueryParameter($key)) { |
106
|
|
|
throw new \InvalidArgumentException(\Safe\sprintf('Query parameter with key "%s" does not exist.', $key)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->queryParameters[$key]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function hasQueryParameter(string $key): bool |
113
|
|
|
{ |
114
|
|
|
return \array_key_exists($key, $this->getQueryParameters()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getRedirectUri(): string |
118
|
|
|
{ |
119
|
|
|
return $this->redirectUri; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function toArray(): array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
'code' => $this->getId()->getValue(), |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getExpiresAt(): \DateTimeImmutable |
130
|
|
|
{ |
131
|
|
|
return $this->expiresAt; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function hasExpired(): bool |
135
|
|
|
{ |
136
|
|
|
return $this->expiresAt->getTimestamp() < \time(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getUserAccountId(): UserAccountId |
140
|
|
|
{ |
141
|
|
|
return $this->userAccountId; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getClientId(): ClientId |
145
|
|
|
{ |
146
|
|
|
return $this->clientId; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getParameter(): DataBag |
150
|
|
|
{ |
151
|
|
|
return $this->parameter; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getMetadata(): DataBag |
155
|
|
|
{ |
156
|
|
|
return $this->metadata; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getResourceServerId(): ?ResourceServerId |
160
|
|
|
{ |
161
|
|
|
return $this->resourceServerId; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getExpiresIn(): int |
165
|
|
|
{ |
166
|
|
|
$expiresAt = $this->expiresAt; |
167
|
|
|
if (null === $expiresAt) { |
168
|
|
|
return 0; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->expiresAt->getTimestamp() - \time() < 0 ? 0 : $this->expiresAt->getTimestamp() - \time(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function jsonSerialize() |
175
|
|
|
{ |
176
|
|
|
$data = [ |
177
|
|
|
'auth_code_id' => $this->getId()->getValue(), |
178
|
|
|
'query_parameters' => (object) $this->getQueryParameters(), |
179
|
|
|
'redirect_uri' => $this->getRedirectUri(), |
180
|
|
|
'is_used' => $this->isUsed(), |
181
|
|
|
'expires_at' => $this->getExpiresAt()->getTimestamp(), |
182
|
|
|
'client_id' => $this->getClientId()->getValue(), |
183
|
|
|
'parameters' => (object) $this->getParameter()->all(), |
184
|
|
|
'metadatas' => (object) $this->getMetadata()->all(), |
185
|
|
|
'resource_owner_id' => $this->getUserAccountId()->getValue(), |
186
|
|
|
'resource_owner_class' => \get_class($this->getUserAccountId()), |
187
|
|
|
'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null, |
188
|
|
|
]; |
189
|
|
|
|
190
|
|
|
return $data; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.