|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Tests\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Entity\ClientRepository; |
|
16
|
|
|
use LoginCidadao\OpenIDBundle\Storage\ClientCredentials; |
|
17
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository; |
|
18
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface; |
|
19
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\TagUri; |
|
20
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
|
|
23
|
|
|
class ClientCredentialsTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testCheckClientCredentials() |
|
26
|
|
|
{ |
|
27
|
|
|
$clientId = '123_randomId'; |
|
28
|
|
|
$clientSecret = 'client_secret'; |
|
29
|
|
|
|
|
30
|
|
|
$client = $this->getClient(123, 'randomId', $clientSecret); |
|
31
|
|
|
|
|
32
|
|
|
$em = $this->getEntityManagerFind($client, 'findOneBy'); |
|
33
|
|
|
|
|
34
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
35
|
|
|
$result = $clientCredentials->checkClientCredentials($clientId, $clientSecret); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertTrue($result); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testCheckInvalidClientCredentials() |
|
41
|
|
|
{ |
|
42
|
|
|
$clientId = '123'; |
|
43
|
|
|
$clientSecret = 'client_secret'; |
|
44
|
|
|
|
|
45
|
|
|
$client = $this->getClient(123, 'randomId', $clientSecret); |
|
46
|
|
|
|
|
47
|
|
|
$em = $this->getEntityManagerFind($client, 'find'); |
|
48
|
|
|
|
|
49
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
50
|
|
|
$result = $clientCredentials->checkClientCredentials($clientId, 'wrong'); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertFalse($result); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testCheckNonExistentClient() |
|
56
|
|
|
{ |
|
57
|
|
|
$em = $this->getEntityManagerFind(null, 'find'); |
|
58
|
|
|
|
|
59
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
60
|
|
|
$result = $clientCredentials->checkClientCredentials(123, 'wrong'); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertFalse($result); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testGetClientDetails() |
|
66
|
|
|
{ |
|
67
|
|
|
$id = 123; |
|
68
|
|
|
$randomId = 'randomId'; |
|
69
|
|
|
|
|
70
|
|
|
$client = $this->getClient($id, $randomId, 'client_secret'); |
|
71
|
|
|
|
|
72
|
|
|
$em = $this->getEntityManagerFind($client, 'findOneBy'); |
|
73
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
74
|
|
|
|
|
75
|
|
|
$details = $clientCredentials->getClientDetails("{$id}_{$randomId}"); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertNotEmpty($details); |
|
78
|
|
|
$this->assertCount(3, $details); |
|
79
|
|
|
$this->assertArrayHasKey('redirect_uri', $details); |
|
80
|
|
|
$this->assertArrayHasKey('client_id', $details); |
|
81
|
|
|
$this->assertArrayHasKey('grant_types', $details); |
|
82
|
|
|
$this->assertEquals($client->getPublicId(), $details['client_id']); |
|
83
|
|
|
$this->assertEquals($client->getAllowedGrantTypes(), $details['grant_types']); |
|
84
|
|
|
$this->assertContains($client->getRedirectUris()[0], $details['redirect_uri']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testGetClientNotFoundDetails() |
|
88
|
|
|
{ |
|
89
|
|
|
$id = 123; |
|
90
|
|
|
$randomId = 'randomId'; |
|
91
|
|
|
|
|
92
|
|
|
$em = $this->getEntityManagerFind(null, 'findOneBy'); |
|
93
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
94
|
|
|
|
|
95
|
|
|
/** @var array|bool $details */ |
|
96
|
|
|
$details = $clientCredentials->getClientDetails("{$id}_{$randomId}"); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertFalse($details); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testIsPublicClient() |
|
102
|
|
|
{ |
|
103
|
|
|
$id = 123; |
|
104
|
|
|
$randomId = 'randomId'; |
|
105
|
|
|
|
|
106
|
|
|
$client = $this->getClient($id, $randomId, 'client_secret'); |
|
107
|
|
|
|
|
108
|
|
|
$em = $this->getEntityManagerFind($client, 'findOneBy'); |
|
109
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
110
|
|
|
|
|
111
|
|
|
$result = $clientCredentials->isPublicClient("{$id}_{$randomId}"); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertFalse($result); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testIsPublicClientNotFound() |
|
117
|
|
|
{ |
|
118
|
|
|
$id = 123; |
|
119
|
|
|
$randomId = 'randomId'; |
|
120
|
|
|
|
|
121
|
|
|
$em = $this->getEntityManagerFind(null, 'findOneBy'); |
|
122
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
123
|
|
|
|
|
124
|
|
|
$result = $clientCredentials->isPublicClient("{$id}_{$randomId}"); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertFalse($result); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testGetClientScopeWithRemoteClaim() |
|
130
|
|
|
{ |
|
131
|
|
|
$id = 123; |
|
132
|
|
|
$randomId = 'randomId'; |
|
133
|
|
|
$expectedRemoteClaim = 'tag:example.com,2017:my_claim'; |
|
134
|
|
|
|
|
135
|
|
|
$client = $this->getClient($id, $randomId, 'client_secret'); |
|
136
|
|
|
|
|
137
|
|
|
$em = $this->getEntityManagerFind($client, 'findOneBy', null, $expectedRemoteClaim); |
|
138
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
139
|
|
|
|
|
140
|
|
|
$scopes = explode(' ', $clientCredentials->getClientScope("{$id}_{$randomId}")); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertContains('name', $scopes); |
|
143
|
|
|
$this->assertContains('openid', $scopes); |
|
144
|
|
|
$this->assertContains($expectedRemoteClaim, $scopes); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testGetClientScopeWithoutRemoteClaim() |
|
148
|
|
|
{ |
|
149
|
|
|
$id = 123; |
|
150
|
|
|
$randomId = 'randomId'; |
|
151
|
|
|
|
|
152
|
|
|
$client = $this->getClient($id, $randomId, 'client_secret'); |
|
153
|
|
|
|
|
154
|
|
|
$em = $this->getEntityManagerFind($client, 'findOneBy'); |
|
155
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
156
|
|
|
|
|
157
|
|
|
$scopes = explode(' ', $clientCredentials->getClientScope("{$id}_{$randomId}")); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertContains('name', $scopes); |
|
160
|
|
|
$this->assertContains('openid', $scopes); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testGetClientNotFoundScope() |
|
164
|
|
|
{ |
|
165
|
|
|
$id = 123; |
|
166
|
|
|
$randomId = 'randomId'; |
|
167
|
|
|
|
|
168
|
|
|
$em = $this->getEntityManagerFind(null, 'findOneBy'); |
|
169
|
|
|
$clientCredentials = new ClientCredentials($em); |
|
170
|
|
|
|
|
171
|
|
|
/** @var string|bool $scopes */ |
|
172
|
|
|
$scopes = $clientCredentials->getClientScope("{$id}_{$randomId}"); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertFalse($scopes); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
179
|
|
|
*/ |
|
180
|
|
|
private function getEntityManager() |
|
181
|
|
|
{ |
|
182
|
|
|
$em = $this->createMock('Doctrine\ORM\EntityManagerInterface'); |
|
183
|
|
|
|
|
184
|
|
|
return $em; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
private function getEntityManagerFind($client, $findMethod, $em = null, $expectedRemoteClaim = null) |
|
188
|
|
|
{ |
|
189
|
|
|
/** @var MockObject|ClientRepository $clientRepo */ |
|
190
|
|
|
$clientRepo = $this->getClientRepository(); |
|
191
|
|
|
$clientRepo->expects($this->once())->method($findMethod)->willReturn($client); |
|
192
|
|
|
|
|
193
|
|
|
$remoteClaimRepo = $this->getRemoteClaimRepository(); |
|
194
|
|
|
|
|
195
|
|
|
$em = $em ?: $this->getEntityManager(); |
|
196
|
|
|
$em->expects($this->atMost(2))->method('getRepository') |
|
197
|
|
|
->willReturnCallback(function ($entity) use ($clientRepo, $remoteClaimRepo, $expectedRemoteClaim) { |
|
198
|
|
|
switch ($entity) { |
|
199
|
|
|
case 'LoginCidadaoOAuthBundle:Client': |
|
200
|
|
|
return $clientRepo; |
|
201
|
|
|
case 'LoginCidadaoRemoteClaimsBundle:RemoteClaim': |
|
202
|
|
|
$remoteClaims = []; |
|
203
|
|
|
if ($expectedRemoteClaim !== null) { |
|
204
|
|
|
$expectedRemoteClaim = TagUri::createFromString($expectedRemoteClaim); |
|
205
|
|
|
/** @var RemoteClaimInterface|MockObject $remoteClaim */ |
|
206
|
|
|
$remoteClaim = $this->createMock(RemoteClaimInterface::class); |
|
207
|
|
|
$remoteClaim->expects($this->any())->method('getName')->willReturn($expectedRemoteClaim); |
|
208
|
|
|
$remoteClaims[] = $remoteClaim; |
|
209
|
|
|
} |
|
210
|
|
|
$remoteClaimRepo->expects($this->once())->method('findAll')->willReturn($remoteClaims); |
|
211
|
|
|
|
|
212
|
|
|
return $remoteClaimRepo; |
|
213
|
|
|
default: |
|
214
|
|
|
return null; |
|
215
|
|
|
} |
|
216
|
|
|
}); |
|
217
|
|
|
|
|
218
|
|
|
return $em; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return ClientRepository|\PHPUnit_Framework_MockObject_MockObject |
|
223
|
|
|
*/ |
|
224
|
|
|
private function getClientRepository() |
|
225
|
|
|
{ |
|
226
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository') |
|
227
|
|
|
->disableOriginalConstructor()->getMock(); |
|
228
|
|
|
|
|
229
|
|
|
return $repo; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return RemoteClaimRepository|\PHPUnit_Framework_MockObject_MockObject |
|
234
|
|
|
*/ |
|
235
|
|
|
private function getRemoteClaimRepository() |
|
236
|
|
|
{ |
|
237
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository') |
|
238
|
|
|
->disableOriginalConstructor()->getMock(); |
|
239
|
|
|
|
|
240
|
|
|
return $repo; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
private function getClient($id, $randomId, $secret) |
|
244
|
|
|
{ |
|
245
|
|
|
$client = new Client(); |
|
246
|
|
|
$client->setId($id); |
|
247
|
|
|
$client->setRandomId($randomId); |
|
248
|
|
|
$client->setSecret($secret); |
|
249
|
|
|
$client->setRedirectUris(['https://redirect.uri']); |
|
250
|
|
|
$client->setAllowedGrantTypes(['authorization_code']); |
|
251
|
|
|
$client->setAllowedScopes(['name', 'openid']); |
|
252
|
|
|
|
|
253
|
|
|
return $client; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|