JwksRule   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B handle() 0 34 10
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\ClientRule;
15
16
use Jose\Component\Core\JWKSet;
17
use Jose\Component\KeyManagement\JKUFactory;
18
use OAuth2Framework\Component\Core\Client\ClientId;
19
use OAuth2Framework\Component\Core\DataBag\DataBag;
20
21
class JwksRule implements Rule
22
{
23
    /**
24
     * @var null|JKUFactory
25
     */
26
    private $jkuFactory;
27
28
    public function __construct(?JKUFactory $jkuFactory)
29
    {
30
        $this->jkuFactory = $jkuFactory;
31
    }
32
33
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, RuleHandler $next): DataBag
34
    {
35
        if ($commandParameters->has('jwks') && $commandParameters->has('jwks_uri')) {
36
            throw new \InvalidArgumentException('The parameters "jwks" and "jwks_uri" cannot be set together.');
37
        }
38
39
        if ($commandParameters->has('jwks')) {
40
            try {
41
                $keyset = JWKSet::createFromKeyData($commandParameters->get('jwks'));
0 ignored issues
show
Bug introduced by
It seems like $commandParameters->get('jwks') can also be of type null; however, parameter $data of Jose\Component\Core\JWKSet::createFromKeyData() 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

41
                $keyset = JWKSet::createFromKeyData(/** @scrutinizer ignore-type */ $commandParameters->get('jwks'));
Loading history...
42
            } catch (\Throwable $e) {
43
                throw new \InvalidArgumentException('The parameter "jwks" must be a valid JWKSet object.', 0, $e);
44
            }
45
            if (0 === \count($keyset)) {
46
                throw new \InvalidArgumentException('The parameter "jwks" must not be empty.');
47
            }
48
            $validatedParameters->set('jwks', $commandParameters->get('jwks'));
49
        }
50
        if ($commandParameters->has('jwks_uri')) {
51
            if (null === $this->jkuFactory) {
52
                throw new \InvalidArgumentException('Distant key sets cannot be used. Please use "jwks" instead of "jwks_uri".');
53
            }
54
55
            try {
56
                $jwks = $this->jkuFactory->loadFromUrl($commandParameters->get('jwks_uri'));
0 ignored issues
show
Bug introduced by
It seems like $commandParameters->get('jwks_uri') can also be of type null; however, parameter $url of Jose\Component\KeyManage...UFactory::loadFromUrl() 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

56
                $jwks = $this->jkuFactory->loadFromUrl(/** @scrutinizer ignore-type */ $commandParameters->get('jwks_uri'));
Loading history...
57
            } catch (\Throwable $e) {
58
                throw new \InvalidArgumentException('The parameter "jwks_uri" must be a valid uri to a JWKSet.', 0, $e);
59
            }
60
            if (0 === $jwks->count()) {
61
                throw new \InvalidArgumentException('The distant key set is empty.');
62
            }
63
            $validatedParameters->set('jwks_uri', $commandParameters->get('jwks_uri'));
64
        }
65
66
        return $next->handle($clientId, $commandParameters, $validatedParameters);
67
    }
68
}
69