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\AuthorizationEndpoint\Rule; |
15
|
|
|
|
16
|
|
|
use Assert\Assertion; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Message\RequestFactory; |
19
|
|
|
use function League\Uri\parse; |
20
|
|
|
use OAuth2Framework\Component\ClientRule\Rule; |
21
|
|
|
use OAuth2Framework\Component\ClientRule\RuleHandler; |
22
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
23
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
24
|
|
|
|
25
|
|
|
final class SectorIdentifierUriRule implements Rule |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var HttpClient |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RequestFactory |
34
|
|
|
*/ |
35
|
|
|
private $requestFactory; |
36
|
|
|
|
37
|
|
|
public function __construct(RequestFactory $requestFactory, HttpClient $client) |
38
|
|
|
{ |
39
|
|
|
$this->requestFactory = $requestFactory; |
40
|
|
|
$this->client = $client; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, RuleHandler $next): DataBag |
44
|
|
|
{ |
45
|
|
|
$validatedParameters = $next->handle($clientId, $commandParameters, $validatedParameters); |
46
|
|
|
|
47
|
|
|
if ($commandParameters->has('sector_identifier_uri')) { |
48
|
|
|
$redirectUris = $validatedParameters->has('redirect_uris') ? $validatedParameters->get('redirect_uris') : []; |
49
|
|
|
$this->checkSectorIdentifierUri($commandParameters->get('sector_identifier_uri'), $redirectUris); |
|
|
|
|
50
|
|
|
$validatedParameters->set('sector_identifier_uri', $commandParameters->get('sector_identifier_uri')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $validatedParameters; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function checkSectorIdentifierUri(string $url, array $redirectUris): void |
57
|
|
|
{ |
58
|
|
|
$data = parse($url); |
59
|
|
|
Assertion::eq('https', $data['scheme'], \Safe\sprintf('The sector identifier URI "%s" is not valid.', $url)); |
60
|
|
|
Assertion::notEmpty($data['host'], \Safe\sprintf('The sector identifier URI "%s" is not valid.', $url)); |
61
|
|
|
|
62
|
|
|
$request = $this->requestFactory->createRequest('GET', $url); |
63
|
|
|
$response = $this->client->sendRequest($request); |
64
|
|
|
Assertion::eq(200, $response->getStatusCode(), \Safe\sprintf('Unable to get Uris from the Sector Identifier Uri "%s".', $url)); |
65
|
|
|
|
66
|
|
|
$body = $response->getBody()->getContents(); |
67
|
|
|
$data = \Safe\json_decode($body, true); |
68
|
|
|
Assertion::isArray($data, 'The provided sector identifier URI is not valid: it must contain at least one URI.'); |
69
|
|
|
Assertion::notEmpty($data, 'The provided sector identifier URI is not valid: it must contain at least one URI.'); |
70
|
|
|
|
71
|
|
|
$diff = \array_diff($redirectUris, $data); |
72
|
|
|
Assertion::noContent($diff, 'The provided sector identifier URI is not valid: it must contain at least the redirect URI(s) set in the registration request.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|