Failed Conditions
Push — ng ( 3a2d0f...7d4708 )
by Florent
04:04
created

UserinfoEndpointAlgorithmsRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\Server\ClientRegistrationEndpoint\Rule;
15
16
use Jose\Component\Encryption\JWEBuilder;
17
use Jose\Component\Signature\JWSBuilder;
18
use OAuth2Framework\Component\Server\Core\Client\ClientId;
19
use OAuth2Framework\Component\Server\Core\DataBag\DataBag;
20
21
final class UserinfoEndpointAlgorithmsRule implements Rule
22
{
23
    /**
24
     * @var JWSBuilder|null
25
     */
26
    private $jwsBuilder;
27
28
    /**
29
     * @var JWEBuilder|null
30
     */
31
    private $jweBuilder;
32
33
    /**
34
     * UserinfoEndpointAlgorithmsRule constructor.
35
     *
36
     * @param JWSBuilder|null $jwsBuilder
37
     * @param JWEBuilder|null $jweBuilder
38
     */
39
    public function __construct(?JWSBuilder $jwsBuilder, ?JWEBuilder $jweBuilder)
40
    {
41
        $this->jwsBuilder = $jwsBuilder;
42
        $this->jweBuilder = $jweBuilder;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, callable $next): DataBag
49
    {
50
        if ($commandParameters->has('userinfo_signed_response_alg') && null !== $this->jwsBuilder) {
51
            $this->checkAlgorithms('userinfo_signed_response_alg', $commandParameters, $this->jwsBuilder->getSignatureAlgorithmManager()->list());
52
            $validatedParameters = $validatedParameters->with('userinfo_signed_response_alg', $commandParameters->get('userinfo_signed_response_alg'));
53
        }
54
55
        if ($commandParameters->has('userinfo_encrypted_response_alg') && $commandParameters->has('userinfo_encrypted_response_enc') && null !== $this->jweBuilder) {
56
            $this->checkAlgorithms('userinfo_encrypted_response_alg', $commandParameters, $this->jwsBuilder->getSignatureAlgorithmManager()->list());
57
            $validatedParameters = $validatedParameters->with('userinfo_encrypted_response_alg', $commandParameters->get('userinfo_encrypted_response_alg'));
58
            $this->checkAlgorithms('userinfo_encrypted_response_enc', $commandParameters, $this->jwsBuilder->getSignatureAlgorithmManager()->list());
59
            $validatedParameters = $validatedParameters->with('userinfo_encrypted_response_enc', $commandParameters->get('userinfo_encrypted_response_enc'));
60
        }
61
62
        return $next($clientId, $commandParameters, $validatedParameters);
63
    }
64
65
    /**
66
     * @param string  $parameter
67
     * @param DataBag $commandParameters
68
     * @param array   $allowedAlgorithms
69
     */
70
    private function checkAlgorithms(string $parameter, DataBag $commandParameters, array $allowedAlgorithms)
71
    {
72
        $algorithm = $commandParameters->get($parameter);
73
        if (!is_string($algorithm) || !in_array($algorithm, $allowedAlgorithms)) {
74
            throw new \InvalidArgumentException(sprintf('The parameter "%s" must be an algorithm supported by this server. Please choose one of the following value(s): %s', $parameter, implode(', ', $allowedAlgorithms)));
75
        }
76
    }
77
}
78