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

SoftwareRule::handle()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 15
nc 6
nop 4
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\Core\JWKSet;
17
use Jose\Component\Signature\JWSLoader;
18
use OAuth2Framework\Component\Server\Core\Client\ClientId;
19
use OAuth2Framework\Component\Server\Core\DataBag\DataBag;
20
21
final class SoftwareRule implements Rule
22
{
23
    /**
24
     * @var JWSLoader
25
     */
26
    private $jwsLoader;
27
28
    /**
29
     * @var bool
30
     */
31
    private $isSoftwareStatementRequired;
32
33
    /**
34
     * @var JWKSet
35
     */
36
    private $softwareStatementSignatureKeySet;
37
38
    /**
39
     * @var string[]
40
     */
41
    private $allowedSignatureAlgorithms;
42
43
    /**
44
     * @param JWSLoader $jwsLoader
45
     * @param JWKSet    $signatureKeySet
46
     * @param bool      $isSoftwareStatementRequired
47
     * @param array     $allowedSignatureAlgorithms
48
     */
49
    public function __construct(JWSLoader $jwsLoader, JWKSet $signatureKeySet, bool $isSoftwareStatementRequired, array $allowedSignatureAlgorithms)
50
    {
51
        $this->jwsLoader = $jwsLoader;
52
        $this->softwareStatementSignatureKeySet = $signatureKeySet;
53
        $this->isSoftwareStatementRequired = $isSoftwareStatementRequired;
54
        $this->allowedSignatureAlgorithms = $allowedSignatureAlgorithms;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function isSoftwareStatementRequired(): bool
61
    {
62
        return $this->isSoftwareStatementRequired;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, callable $next): DataBag
69
    {
70
        Assertion::false($this->isSoftwareStatementRequired() && !$commandParameters->has('software_statement'), 'The parameter "software_statement" is mandatory.');
71
        if ($commandParameters->has('software_statement')) {
72
            $statement = $commandParameters->get('software_statement');
73
            Assertion::string($statement, 'The software statement should be a string.');
74
            $software_statement = $this->loadSoftwareStatement($statement);
75
            $validatedParameters = $validatedParameters->with('software_statement', $commandParameters->get('software_statement'));
76
        } else {
77
            $software_statement = [];
78
        }
79
80
        foreach (['software_id', 'software_version'] as $key) {
81
            if ($commandParameters->has($key)) {
82
                $validatedParameters = $validatedParameters->with($key, $commandParameters->get($key));
83
            }
84
        }
85
86
        $validatedParameters = $next($clientId, $commandParameters, $validatedParameters);
87
        $validatedParameters = $validatedParameters->withParameters($software_statement);
88
89
        return $validatedParameters;
90
    }
91
92
    /**
93
     * @param string $software_statement
94
     *
95
     * @return array
96
     */
97
    private function loadSoftwareStatement(string $software_statement): array
98
    {
99
        try {
100
            $jws = $this->jwsLoader->load($software_statement);
0 ignored issues
show
Bug introduced by
The method load() does not seem to exist on object<Jose\Component\Signature\JWSLoader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            $signatureVerified = $this->jwsLoader->verifyWithKeySet($jws, $this->softwareStatementSignatureKeySet);
0 ignored issues
show
Bug introduced by
The method verifyWithKeySet() does not exist on Jose\Component\Signature\JWSLoader. Did you maybe mean loadAndVerifyWithKeySet()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
102
            Assertion::inArray($jws->getSignature($signatureVerified)->getProtectedHeader('alg'), $this->allowedSignatureAlgorithms);
103
            $claims = json_decode($jws->getPayload(), true);
104
            Assertion::isArray($claims, 'Invalid Software Statement.');
105
106
            return $claims;
107
        } catch (\Exception $e) {
108
            throw new \InvalidArgumentException('Invalid Software Statement.', $e->getCode(), $e);
109
        }
110
    }
111
}
112