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\ServerBundle\Entity; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\Client\Client as ClientInterface; |
17
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
18
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
19
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId; |
20
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This class is used for every client types. |
24
|
|
|
* A client is a resource owner with a set of allowed grant types and can perform requests against |
25
|
|
|
* available endpoints. |
26
|
|
|
*/ |
27
|
|
|
class Client implements ClientInterface |
28
|
|
|
{ |
29
|
|
|
protected $clientId; |
30
|
|
|
protected $ownerId; |
31
|
|
|
protected $parameter; |
32
|
|
|
protected $deleted; |
33
|
|
|
|
34
|
|
|
public function __construct(ClientId $clientId, DataBag $parameters, ?UserAccountId $ownerId) |
35
|
|
|
{ |
36
|
|
|
$this->clientId = $clientId; |
37
|
|
|
$this->parameter = $parameters; |
38
|
|
|
$this->ownerId = $ownerId; |
39
|
|
|
$this->deleted = false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getClientId(): ClientId |
43
|
|
|
{ |
44
|
|
|
$id = $this->getPublicId(); |
45
|
|
|
if (!$id instanceof ClientId) { |
46
|
|
|
throw new \RuntimeException('Client not initialized.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $id; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getOwnerId(): ?UserAccountId |
53
|
|
|
{ |
54
|
|
|
return $this->ownerId; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setOwnerId(UserAccountId $ownerId): void |
58
|
|
|
{ |
59
|
|
|
$this->ownerId = $ownerId; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setParameter(DataBag $parameter): void |
63
|
|
|
{ |
64
|
|
|
$this->parameter = $parameter; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function markAsDeleted(): void |
68
|
|
|
{ |
69
|
|
|
$this->deleted = true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function isDeleted(): bool |
73
|
|
|
{ |
74
|
|
|
return $this->deleted; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function isGrantTypeAllowed(string $grant_type): bool |
78
|
|
|
{ |
79
|
|
|
$grant_types = $this->has('grant_types') ? $this->get('grant_types') : []; |
80
|
|
|
if (!\is_array($grant_types)) { |
81
|
|
|
throw new \InvalidArgumentException('The metadata "grant_types" must be an array.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return \in_array($grant_type, $grant_types, true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function isResponseTypeAllowed(string $response_type): bool |
88
|
|
|
{ |
89
|
|
|
$response_types = $this->has('response_types') ? $this->get('response_types') : []; |
90
|
|
|
if (!\is_array($response_types)) { |
91
|
|
|
throw new \InvalidArgumentException('The metadata "response_types" must be an array.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return \in_array($response_type, $response_types, true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function isPublic(): bool |
98
|
|
|
{ |
99
|
|
|
return 'none' === $this->getTokenEndpointAuthenticationMethod(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getTokenEndpointAuthenticationMethod(): string |
103
|
|
|
{ |
104
|
|
|
if ($this->has('token_endpoint_auth_method')) { |
105
|
|
|
return $this->get('token_endpoint_auth_method'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return 'client_secret_basic'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getClientCredentialsExpiresAt(): int |
112
|
|
|
{ |
113
|
|
|
if ($this->has('client_secret_expires_at')) { |
114
|
|
|
return $this->get('client_secret_expires_at'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return 0; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function areClientCredentialsExpired(): bool |
121
|
|
|
{ |
122
|
|
|
if (0 === $this->getClientCredentialsExpiresAt()) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return \time() > $this->getClientCredentialsExpiresAt(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getPublicId(): ResourceOwnerId |
130
|
|
|
{ |
131
|
|
|
if (null === $this->clientId) { |
132
|
|
|
throw new \RuntimeException('Client not initialized.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->clientId; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function has(string $key): bool |
139
|
|
|
{ |
140
|
|
|
return $this->parameter->has($key); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function get(string $key) |
144
|
|
|
{ |
145
|
|
|
if (!$this->has($key)) { |
146
|
|
|
throw new \InvalidArgumentException(\Safe\sprintf('Configuration value with key "%s" does not exist.', $key)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->parameter->get($key); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function all(): array |
153
|
|
|
{ |
154
|
|
|
$all = $this->parameter->all(); |
155
|
|
|
$all['client_id'] = $this->getPublicId()->getValue(); |
156
|
|
|
|
157
|
|
|
return $all; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function jsonSerialize() |
161
|
|
|
{ |
162
|
|
|
$data = [ |
163
|
|
|
'client_id' => $this->getPublicId()->getValue(), |
164
|
|
|
'owner_id' => $this->getOwnerId() ? $this->getOwnerId()->getValue() : null, |
165
|
|
|
'parameters' => (object) $this->all(), |
166
|
|
|
'is_deleted' => $this->isDeleted(), |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
return $data; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|