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