Failed Conditions
Push — master ( 819484...23fc45 )
by Florent
03:33
created

checkSectorIdentifierUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
It seems like $commandParameters->get('sector_identifier_uri') can also be of type null; however, parameter $url of OAuth2Framework\Componen...ckSectorIdentifierUri() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $this->checkSectorIdentifierUri(/** @scrutinizer ignore-type */ $commandParameters->get('sector_identifier_uri'), $redirectUris);
Loading history...
Bug introduced by
It seems like $redirectUris can also be of type null; however, parameter $redirectUris of OAuth2Framework\Componen...ckSectorIdentifierUri() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $this->checkSectorIdentifierUri($commandParameters->get('sector_identifier_uri'), /** @scrutinizer ignore-type */ $redirectUris);
Loading history...
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