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 Http\Client\HttpClient; |
17
|
|
|
use Http\Message\RequestFactory; |
18
|
|
|
use function League\Uri\parse; |
19
|
|
|
use OAuth2Framework\Component\ClientRule\Rule; |
20
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
21
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
22
|
|
|
|
23
|
|
|
class SectorIdentifierUriRule implements Rule |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var HttpClient |
27
|
|
|
*/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RequestFactory |
32
|
|
|
*/ |
33
|
|
|
private $requestFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* SectorIdentifierUriRule constructor. |
37
|
|
|
* |
38
|
|
|
* @param RequestFactory $requestFactory |
39
|
|
|
* @param HttpClient $client |
40
|
|
|
*/ |
41
|
|
|
public function __construct(RequestFactory $requestFactory, HttpClient $client) |
42
|
|
|
{ |
43
|
|
|
$this->requestFactory = $requestFactory; |
44
|
|
|
$this->client = $client; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, callable $next): DataBag |
51
|
|
|
{ |
52
|
|
|
if ($commandParameters->has('sector_identifier_uri')) { |
53
|
|
|
$this->checkSectorIdentifierUri($commandParameters->get('sector_identifier_uri')); |
54
|
|
|
$validatedParameters = $validatedParameters->with('sector_identifier_uri', $commandParameters->get('sector_identifier_uri')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $next($clientId, $commandParameters, $validatedParameters); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $url |
62
|
|
|
* |
63
|
|
|
* @throws \InvalidArgumentException |
64
|
|
|
* @throws \Http\Client\Exception |
65
|
|
|
*/ |
66
|
|
|
private function checkSectorIdentifierUri(string $url) |
67
|
|
|
{ |
68
|
|
|
$data = parse($url); |
69
|
|
|
|
70
|
|
|
if ($data['scheme'] !== 'https' || $data['host'] === null) { |
71
|
|
|
throw new \InvalidArgumentException(sprintf('The sector identifier URI "%s" is not valid.', $url)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$request = $this->requestFactory->createRequest('GET', $url); |
75
|
|
|
$response = $this->client->sendRequest($request); |
76
|
|
|
if (200 !== $response->getStatusCode()) { |
77
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to get Uris from the Sector Identifier Uri "%s".', $url)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$body = $response->getBody()->getContents(); |
81
|
|
|
$data = json_decode($body, true); |
82
|
|
|
if (!is_array($data) || empty($data)) { |
83
|
|
|
throw new \InvalidArgumentException('The provided sector identifier URI is not valid: it must contain at least one URI.'); |
84
|
|
|
} |
85
|
|
|
foreach ($data as $sector_url) { |
|
|
|
|
86
|
|
|
//FIXME |
87
|
|
|
//Assertion::url($sector_url, 'The provided sector identifier URI is not valid: it must contain only URIs.'); |
|
|
|
|
88
|
|
|
//Assertion::inArray(mb_substr($sector_url, 0, mb_strpos($sector_url, '://', 0, '8bit'), '8bit'), $allowedProtocols, sprintf('An URL provided in the sector identifier URI is not valid: scheme must be one of the following: %s.', implode(', ', $allowedProtocols))); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for
foreach
loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.