1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AdvancedLearning\Oauth2Server\Tests; |
4
|
|
|
|
5
|
|
|
use AdvancedLearning\Oauth2Server\Models\Client; |
|
|
|
|
6
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\AccessTokenRepository; |
|
|
|
|
7
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\ClientRepository; |
|
|
|
|
8
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\RefreshTokenRepository; |
|
|
|
|
9
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\ScopeRepository; |
|
|
|
|
10
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\UserRepository; |
|
|
|
|
11
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
|
|
|
12
|
|
|
use GraphQL\Type\Definition\Type; |
|
|
|
|
13
|
|
|
use GuzzleHttp\Psr7\Response; |
|
|
|
|
14
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
|
|
|
|
15
|
|
|
use Lcobucci\JWT\Parser; |
|
|
|
|
16
|
|
|
use League\Flysystem\Exception; |
|
|
|
|
17
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
|
|
|
|
18
|
|
|
use League\OAuth2\Server\CryptTrait; |
|
|
|
|
19
|
|
|
use League\OAuth2\Server\Grant\ClientCredentialsGrant; |
|
|
|
|
20
|
|
|
use League\OAuth2\Server\Grant\PasswordGrant; |
|
|
|
|
21
|
|
|
use Riddler7\Oauth2GraphQL\Controller; |
22
|
|
|
use Riddler7\Oauth2GraphQL\Helpers\OauthContext; |
23
|
|
|
use Riddler7\Oauth2GraphQL\Tests\BlankMutation; |
24
|
|
|
use Riddler7\Oauth2GraphQL\Tests\BlankQuery; |
25
|
|
|
use Riddler7\Oauth2GraphQL\Tests\BlankType; |
26
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
|
|
|
27
|
|
|
use SilverStripe\Core\Config\Config; |
|
|
|
|
28
|
|
|
use SilverStripe\Core\Environment; |
|
|
|
|
29
|
|
|
use SilverStripe\GraphQL\Manager; |
|
|
|
|
30
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
|
|
|
31
|
|
|
use SilverStripe\Security\Member; |
|
|
|
|
32
|
|
|
use SilverStripe\Security\Security; |
|
|
|
|
33
|
|
|
use function file_get_contents; |
34
|
|
|
use function file_put_contents; |
35
|
|
|
use function sys_get_temp_dir; |
36
|
|
|
|
37
|
|
|
class GraphqlTest extends SapphireTest |
38
|
|
|
{ |
39
|
|
|
use CryptTrait, OauthContext; |
40
|
|
|
|
41
|
|
|
protected static $fixture_file = 'tests/OAuthFixture.yml'; |
42
|
|
|
|
43
|
|
|
protected static $privateKeyFile = 'private.key'; |
44
|
|
|
|
45
|
|
|
protected static $publicKeyFile = 'public.key'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Setup test environment. |
49
|
|
|
*/ |
50
|
|
|
public function setUp() |
51
|
|
|
{ |
52
|
|
|
parent::setUp(); |
53
|
|
|
|
54
|
|
|
// copy private key so we can set correct permissions, file gets removed when tests finish |
55
|
|
|
$path = $this->getPrivateKeyPath(); |
56
|
|
|
file_put_contents($path, file_get_contents(__DIR__ . '/' . self::$privateKeyFile)); |
57
|
|
|
chmod($path, 0660); |
58
|
|
|
Environment::setEnv('OAUTH_PRIVATE_KEY_PATH', $path); |
59
|
|
|
|
60
|
|
|
// copy public key |
61
|
|
|
$path = $this->getPublicKeyPath(); |
62
|
|
|
file_put_contents($path, file_get_contents(__DIR__ . '/' . self::$publicKeyFile)); |
63
|
|
|
chmod($path, 0660); |
64
|
|
|
Environment::setEnv('OAUTH_PUBLIC_KEY_PATH', $path); |
65
|
|
|
|
66
|
|
|
Security::force_database_is_ready(true); |
67
|
|
|
|
68
|
|
|
$this->setEncryptionKey('lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGraphQLMember() |
72
|
|
|
{ |
73
|
|
|
$userRepository = new UserRepository(); |
74
|
|
|
$refreshRepository = new RefreshTokenRepository(); |
75
|
|
|
|
76
|
|
|
$server = $this->getAuthorisationServer(); |
77
|
|
|
$server->enableGrantType( |
78
|
|
|
new PasswordGrant($userRepository, $refreshRepository), |
79
|
|
|
new \DateInterval('PT1H') |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$client = $this->objFromFixture(Client::class, 'webapp'); |
83
|
|
|
$member = $this->objFromFixture(Member::class, 'member1'); |
84
|
|
|
|
85
|
|
|
$request = (new ServerRequest( |
86
|
|
|
'POST', |
87
|
|
|
'', |
88
|
|
|
['Content-Type' => 'application/json'] |
89
|
|
|
))->withParsedBody([ |
90
|
|
|
'grant_type' => 'password', |
91
|
|
|
'client_id' => $client->Identifier, |
92
|
|
|
'client_secret' => $client->Secret, |
93
|
|
|
'scope' => 'members', |
94
|
|
|
'username' => $member->Email, |
95
|
|
|
'password' => 'password1' |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$response = new Response(); |
99
|
|
|
$response = $server->respondToAccessTokenRequest($request, $response); |
100
|
|
|
|
101
|
|
|
$data = json_decode((string)$response->getBody(), true); |
102
|
|
|
$token = $data['access_token']; |
103
|
|
|
|
104
|
|
|
// check for fn/ln |
105
|
|
|
$decoded = (new Parser())->parse($token); |
106
|
|
|
|
107
|
|
|
$this->assertEquals('My', $decoded->getClaim('fn'), 'First name should be correctly set'); |
108
|
|
|
$this->assertEquals('Test', $decoded->getClaim('ln'), 'Last name should be correctly set'); |
109
|
|
|
|
110
|
|
|
// create request |
111
|
|
|
$request = new HTTPRequest('GET', '/'); |
112
|
|
|
$request->addHeader('authorization', 'Bearer ' . $token); |
113
|
|
|
// fake server port |
114
|
|
|
$_SERVER['SERVER_PORT'] = 443; |
115
|
|
|
|
116
|
|
|
$authMember = (new \Riddler7\Oauth2GraphQL\Authenticator())->authenticate($request); |
117
|
|
|
|
118
|
|
|
$this->assertEquals($member->ID, $authMember->ID, 'Member should exist in DB'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testGraphQLContexts() |
122
|
|
|
{ |
123
|
|
|
$userRepository = new UserRepository(); |
124
|
|
|
$refreshRepository = new RefreshTokenRepository(); |
125
|
|
|
|
126
|
|
|
$server = $this->getAuthorisationServer(); |
127
|
|
|
$server->enableGrantType( |
128
|
|
|
new PasswordGrant($userRepository, $refreshRepository), |
129
|
|
|
new \DateInterval('PT1H') |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$client = $this->objFromFixture(Client::class, 'webapp'); |
133
|
|
|
$member = $this->objFromFixture(Member::class, 'member1'); |
134
|
|
|
|
135
|
|
|
$request = (new ServerRequest( |
136
|
|
|
'POST', |
137
|
|
|
'', |
138
|
|
|
['Content-Type' => 'application/json'] |
139
|
|
|
))->withParsedBody([ |
140
|
|
|
'grant_type' => 'password', |
141
|
|
|
'client_id' => $client->Identifier, |
142
|
|
|
'client_secret' => $client->Secret, |
143
|
|
|
'scope' => 'members', |
144
|
|
|
'username' => $member->Email, |
145
|
|
|
'password' => 'password1' |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
$response = new Response(); |
149
|
|
|
$response = $server->respondToAccessTokenRequest($request, $response); |
150
|
|
|
|
151
|
|
|
$data = json_decode((string)$response->getBody(), true); |
152
|
|
|
$token = $data['access_token']; |
153
|
|
|
|
154
|
|
|
// create request |
155
|
|
|
$request = new HTTPRequest('GET', '/grqphql'); |
156
|
|
|
$request->addHeader('authorization', 'Bearer ' . $token); |
157
|
|
|
// fake server port |
158
|
|
|
$_SERVER['SERVER_PORT'] = 443; |
159
|
|
|
|
160
|
|
|
// var to store context |
161
|
|
|
$context = []; |
162
|
|
|
|
163
|
|
|
// setup blank schema |
164
|
|
|
Config::modify()->set(Manager::class, 'schemas', [ |
165
|
|
|
'myschema' => [ |
166
|
|
|
'types' => [ |
167
|
|
|
'Blank' => BlankType::class |
168
|
|
|
], |
169
|
|
|
'queries' => [ |
170
|
|
|
'BlankQuery' => BlankQuery::class |
171
|
|
|
], |
172
|
|
|
'mutations' => [ |
173
|
|
|
'BlankMutation' => BlankMutation::class |
174
|
|
|
] |
175
|
|
|
] |
176
|
|
|
]); |
177
|
|
|
|
178
|
|
|
$manager = new Manager('myschema'); |
179
|
|
|
|
180
|
|
|
// extract the context |
181
|
|
|
$manager->addMiddleware(new GraphQLSchemaExtractor(function ($currentContext) use (&$context) { |
182
|
|
|
$context = $currentContext; |
183
|
|
|
})); |
184
|
|
|
|
185
|
|
|
$controller = new Controller($manager); |
186
|
|
|
$response = $controller->index($request); |
|
|
|
|
187
|
|
|
|
188
|
|
|
$this->assertEquals($client->Identifier, $context['oauthClientIdentifier']); |
189
|
|
|
$this->assertEquals(1, count($context['oauthScopes'])); |
190
|
|
|
$this->assertEquals('members', $context['oauthScopes'][0]); |
191
|
|
|
|
192
|
|
|
// test the context helper |
193
|
|
|
$this->assertEquals( |
194
|
|
|
true, |
195
|
|
|
$this->hasOauthClient($context), |
196
|
|
|
'Context should contain a client' |
197
|
|
|
); |
198
|
|
|
$this->assertEquals( |
199
|
|
|
true, |
200
|
|
|
$this->hasScope($context, 'members'), |
201
|
|
|
'Context should have a \'members\' scope' |
202
|
|
|
); |
203
|
|
|
$this->assertEquals( |
204
|
|
|
false, |
205
|
|
|
$this->hasScope($context, 'admin'), |
206
|
|
|
'Context should not have an \'admin\' scope' |
207
|
|
|
); |
208
|
|
|
$this->assertEquals( |
209
|
|
|
true, |
210
|
|
|
$this->hasScopes($context, |
211
|
|
|
['members']) |
212
|
|
|
); |
213
|
|
|
$this->assertEquals( |
214
|
|
|
false, |
215
|
|
|
$this->hasScopes($context, |
216
|
|
|
['admin']) |
217
|
|
|
); |
218
|
|
|
$this->assertEquals( |
219
|
|
|
$client->ID, |
220
|
|
|
$this->getOauthClient($context)->ID, |
221
|
|
|
'The ids for the Oauth Client should match' |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Verify an error response is returned if there is no oauth client. |
227
|
|
|
*/ |
228
|
|
|
public function testNoClient() |
229
|
|
|
{ |
230
|
|
|
$request = new HTTPRequest('GET', '/grqphql'); |
231
|
|
|
// fake server port |
232
|
|
|
$_SERVER['SERVER_PORT'] = 443; |
233
|
|
|
|
234
|
|
|
// var to store context |
235
|
|
|
$context = []; |
|
|
|
|
236
|
|
|
|
237
|
|
|
// setup blank schema |
238
|
|
|
Config::modify()->set(Manager::class, 'schemas', [ |
239
|
|
|
'myschema' => [ |
240
|
|
|
'types' => [ |
241
|
|
|
'Blank' => BlankType::class |
242
|
|
|
], |
243
|
|
|
'queries' => [ |
244
|
|
|
'BlankQuery' => BlankQuery::class |
245
|
|
|
], |
246
|
|
|
'mutations' => [ |
247
|
|
|
'BlankMutation' => BlankMutation::class |
248
|
|
|
] |
249
|
|
|
] |
250
|
|
|
]); |
251
|
|
|
|
252
|
|
|
$manager = new Manager('myschema'); |
253
|
|
|
|
254
|
|
|
$controller = new Controller($manager); |
255
|
|
|
|
256
|
|
|
$response = $controller->index($request); |
257
|
|
|
$json = json_decode($response->getBody(), true); |
258
|
|
|
|
259
|
|
|
$error = $json['errors'][0]; |
260
|
|
|
|
261
|
|
|
$this->assertEquals(403, $error['code'], 'Should return a 403 error code'); |
262
|
|
|
$this->assertEquals($error['message'], 'A valid client is required', 'Should have a useful message'); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Setup the Authorization Server. |
268
|
|
|
* |
269
|
|
|
* @return AuthorizationServer |
270
|
|
|
*/ |
271
|
|
|
protected function getAuthorisationServer() |
272
|
|
|
{ |
273
|
|
|
// Init our repositories |
274
|
|
|
$clientRepository = new ClientRepository(); // instance of ClientRepositoryInterface |
275
|
|
|
$scopeRepository = new ScopeRepository(); // instance of ScopeRepositoryInterface |
276
|
|
|
$accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface |
277
|
|
|
|
278
|
|
|
// Path to public and private keys |
279
|
|
|
$privateKey = $this->getPrivateKeyPath(); |
280
|
|
|
$encryptionKey = $this->encryptionKey; |
281
|
|
|
|
282
|
|
|
// Setup the authorization server |
283
|
|
|
$server = new AuthorizationServer( |
284
|
|
|
$clientRepository, |
285
|
|
|
$accessTokenRepository, |
286
|
|
|
$scopeRepository, |
287
|
|
|
$privateKey, |
288
|
|
|
$encryptionKey |
289
|
|
|
); |
290
|
|
|
|
291
|
|
|
return $server; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get the resource server. |
296
|
|
|
* |
297
|
|
|
* @return \League\OAuth2\Server\ResourceServer |
298
|
|
|
*/ |
299
|
|
|
protected function getResourceServer() |
300
|
|
|
{ |
301
|
|
|
// Init our repositories |
302
|
|
|
$accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface |
303
|
|
|
|
304
|
|
|
// Path to authorization server's public key |
305
|
|
|
$publicKeyPath = $this->getPublicKeyPath(); |
306
|
|
|
|
307
|
|
|
// Setup the authorization server |
308
|
|
|
$server = new \League\OAuth2\Server\ResourceServer( |
|
|
|
|
309
|
|
|
$accessTokenRepository, |
310
|
|
|
$publicKeyPath |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
return $server; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Get the full path the private key. |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
protected function getPrivateKeyPath() |
322
|
|
|
{ |
323
|
|
|
return sys_get_temp_dir() . '/' . self::$privateKeyFile; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Get the full path the public key. |
328
|
|
|
* |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
|
|
protected function getPublicKeyPath() |
332
|
|
|
{ |
333
|
|
|
return sys_get_temp_dir() . '/' . self::$publicKeyFile; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Cleanup test environment. |
338
|
|
|
*/ |
339
|
|
|
protected function tearDown() |
340
|
|
|
{ |
341
|
|
|
parent::tearDown(); |
342
|
|
|
// remove private key after tests have finished |
343
|
|
|
unlink($this->getPrivateKeyPath()); |
344
|
|
|
// remove public key after tests have finished |
345
|
|
|
unlink($this->getPublicKeyPath()); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Generates a response with an access token using the client grant. |
350
|
|
|
* |
351
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
|
|
|
352
|
|
|
*/ |
353
|
|
|
protected function generateClientAccessToken() |
354
|
|
|
{ |
355
|
|
|
$server = $this->getAuthorisationServer(); |
356
|
|
|
// Enable the client credentials grant on the server |
357
|
|
|
$server->enableGrantType( |
358
|
|
|
new ClientCredentialsGrant(), |
359
|
|
|
new \DateInterval('PT1H') // access tokens will expire after 1 hour |
360
|
|
|
); |
361
|
|
|
|
362
|
|
|
$client = $this->objFromFixture(Client::class, 'webapp'); |
363
|
|
|
|
364
|
|
|
$request = $this->getClientRequest($client); |
365
|
|
|
|
366
|
|
|
$response = new Response(); |
367
|
|
|
return $server->respondToAccessTokenRequest($request, $response); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Get PSR7 request object to be used for a client grant. |
372
|
|
|
* |
373
|
|
|
* @param Client $client |
374
|
|
|
* |
375
|
|
|
* @return ServerRequest |
376
|
|
|
*/ |
377
|
|
|
protected function getClientRequest(Client $client) |
378
|
|
|
{ |
379
|
|
|
// setup server vars |
380
|
|
|
$_SERVER['SERVER_PORT'] = 80; |
381
|
|
|
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; |
382
|
|
|
|
383
|
|
|
return (new ServerRequest( |
384
|
|
|
'POST', |
385
|
|
|
'', |
386
|
|
|
['Content-Type' => 'application/json'] |
387
|
|
|
))->withParsedBody([ |
388
|
|
|
'grant_type' => 'client_credentials', |
389
|
|
|
'client_id' => $client->Identifier, |
390
|
|
|
'client_secret' => $client->Secret, |
391
|
|
|
'scope' => 'members' |
392
|
|
|
]); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths