Failed Conditions
Push — master ( 5c97ee...fe523f )
by Florent
04:12
created

checkSectorIdentifierUri()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 5
nop 2
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
final 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
        $validatedParameters = $next($clientId, $commandParameters, $validatedParameters);
53
54
        if ($commandParameters->has('sector_identifier_uri')) {
55
            $redirectUris = $validatedParameters->has('redirect_uris') ? $validatedParameters->get('redirect_uris') : [];
56
            $this->checkSectorIdentifierUri($commandParameters->get('sector_identifier_uri'), $redirectUris);
57
            $validatedParameters = $validatedParameters->with('sector_identifier_uri', $commandParameters->get('sector_identifier_uri'));
58
        }
59
60
        return $validatedParameters;
61
    }
62
63
    private function checkSectorIdentifierUri(string $url, array $redirectUris)
64
    {
65
        $data = parse($url);
66
67
        if ('https' !== $data['scheme'] || null === $data['host']) {
68
            throw new \InvalidArgumentException(sprintf('The sector identifier URI "%s" is not valid.', $url));
69
        }
70
71
        $request = $this->requestFactory->createRequest('GET', $url);
72
        $response = $this->client->sendRequest($request);
73
        if (200 !== $response->getStatusCode()) {
74
            throw new \InvalidArgumentException(sprintf('Unable to get Uris from the Sector Identifier Uri "%s".', $url));
75
        }
76
77
        $body = $response->getBody()->getContents();
78
        $data = json_decode($body, true);
79
        if (!is_array($data) || empty($data)) {
80
            throw new \InvalidArgumentException('The provided sector identifier URI is not valid: it must contain at least one URI.');
81
        }
82
83
        $diff = array_diff($redirectUris, $data);
84
        if (!empty($diff)) {
85
            throw new \InvalidArgumentException('The provided sector identifier URI is not valid: it must contain at least the redirect URI(s) set in the registration request.');
86
        }
87
    }
88
}
89