|
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\AuthorizationCodeGrant\Event as AuthorizationCodeEvent; |
|
17
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
|
18
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
|
19
|
|
|
use OAuth2Framework\Component\Core\Event\Event; |
|
20
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
|
21
|
|
|
use OAuth2Framework\Component\Core\Token\Token; |
|
22
|
|
|
use OAuth2Framework\Component\Core\Token\TokenId; |
|
23
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
|
24
|
|
|
|
|
25
|
|
|
class AuthorizationCode extends Token |
|
26
|
|
|
{ |
|
27
|
|
|
private $queryParameters; |
|
28
|
|
|
private $redirectUri; |
|
29
|
|
|
private $used; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(AuthorizationCodeId $authorizationCodeId, ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($authorizationCodeId, $clientId, $userAccountId, $parameter, $metadata, $expiresAt, $resourceServerId); |
|
34
|
|
|
$this->queryParameters = $queryParameters; |
|
35
|
|
|
$this->redirectUri = $redirectUri; |
|
36
|
|
|
$this->used = false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getSchema(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return 'https://oauth2-framework.spomky-labs.com/schemas/model/authorization-code/1.0/schema'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setTokenId(TokenId $tokenId): void |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$tokenId instanceof AuthorizationCodeId) { |
|
50
|
|
|
throw new \RuntimeException('The token ID must be an Authorization Code ID.'); |
|
51
|
|
|
} |
|
52
|
|
|
parent::setTokenId($tokenId); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getQueryParameters(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->queryParameters; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setQueryParameters(array $queryParameters): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->queryParameters = $queryParameters; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isUsed(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->used; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function markAsUsed(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->used = true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getQueryParams(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->queryParameters; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getQueryParam(string $key) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
if (!$this->hasQueryParam($key)) { |
|
83
|
|
|
throw new \RuntimeException(\sprintf('Query parameter with key "%s" does not exist.', $key)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->queryParameters[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function hasQueryParam(string $key): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return \array_key_exists($key, $this->getQueryParams()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getRedirectUri(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->redirectUri; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function setRedirectUri(string $redirectUri): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->redirectUri = $redirectUri; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function toArray(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
'code' => $this->getTokenId()->getValue(), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function jsonSerialize() |
|
115
|
|
|
{ |
|
116
|
|
|
$data = parent::jsonSerialize() + [ |
|
117
|
|
|
'auth_code_id' => $this->getTokenId()->getValue(), |
|
118
|
|
|
'query_parameters' => (object) $this->getQueryParameters(), |
|
119
|
|
|
'redirect_uri' => $this->getRedirectUri(), |
|
120
|
|
|
'is_used' => $this->isUsed(), |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
return $data; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function apply(Event $event): void |
|
127
|
|
|
{ |
|
128
|
|
|
$map = $this->getEventMap(); |
|
129
|
|
|
if (!\array_key_exists($event->getType(), $map)) { |
|
130
|
|
|
throw new \RuntimeException('Unsupported event.'); |
|
131
|
|
|
} |
|
132
|
|
|
if ($this->getTokenId()->getValue() !== $event->getDomainId()->getValue()) { |
|
133
|
|
|
throw new \RuntimeException('Event not applicable for this authorization code.'); |
|
134
|
|
|
} |
|
135
|
|
|
$method = $map[$event->getType()]; |
|
136
|
|
|
|
|
137
|
|
|
$this->$method($event); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function getEventMap(): array |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
|
|
AuthorizationCodeEvent\AuthorizationCodeCreatedEvent::class => 'applyAuthorizationCodeCreatedEvent', |
|
144
|
|
|
AuthorizationCodeEvent\AuthorizationCodeMarkedAsUsedEvent::class => 'applyAuthorizationCodeMarkedAsUsedEvent', |
|
145
|
|
|
AuthorizationCodeEvent\AuthorizationCodeRevokedEvent::class => 'applyAuthorizationCodeRevokedEvent', |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
protected function applyAuthorizationCodeCreatedEvent(AuthorizationCodeEvent\AuthorizationCodeCreatedEvent $event): void |
|
150
|
|
|
{ |
|
151
|
|
|
$this->setTokenId($event->getAuthorizationCodeId()); |
|
152
|
|
|
$this->setClientId($event->getClientId()); |
|
153
|
|
|
$this->setResourceOwnerId($event->getUserAccountId()); |
|
154
|
|
|
$this->setQueryParameters($event->getQueryParameters()); |
|
155
|
|
|
$this->setRedirectUri($event->getRedirectUri()); |
|
156
|
|
|
$this->setExpiresAt($event->getExpiresAt()); |
|
157
|
|
|
$this->setParameter($event->getParameter()); |
|
158
|
|
|
$this->setMetadata($event->getMetadata()); |
|
159
|
|
|
$this->setExpiresAt($event->getExpiresAt()); |
|
160
|
|
|
$this->setResourceServerId($event->getResourceServerId()); |
|
161
|
|
|
$this->used = false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function applyAuthorizationCodeMarkedAsUsedEvent(AuthorizationCodeEvent\AuthorizationCodeMarkedAsUsedEvent $event): void |
|
165
|
|
|
{ |
|
166
|
|
|
$this->markAsUsed(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function applyAuthorizationCodeRevokedEvent(AuthorizationCodeEvent\AuthorizationCodeRevokedEvent $event): void |
|
170
|
|
|
{ |
|
171
|
|
|
$this->markAsRevoked(); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
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
@returnannotation as described here.