ClientAuthenticationMethodRule   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 31
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 4
A __construct() 0 3 1
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\ClientAuthentication\Rule;
15
16
use OAuth2Framework\Component\ClientAuthentication\AuthenticationMethodManager;
17
use OAuth2Framework\Component\ClientRule\Rule;
18
use OAuth2Framework\Component\ClientRule\RuleHandler;
19
use OAuth2Framework\Component\Core\Client\ClientId;
20
use OAuth2Framework\Component\Core\DataBag\DataBag;
21
use function Safe\sprintf;
22
23
final class ClientAuthenticationMethodRule implements Rule
24
{
25
    /**
26
     * @var AuthenticationMethodManager
27
     */
28
    private $clientAuthenticationMethodManager;
29
30
    public function __construct(AuthenticationMethodManager $clientAuthenticationMethodManager)
31
    {
32
        $this->clientAuthenticationMethodManager = $clientAuthenticationMethodManager;
33
    }
34
35
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, RuleHandler $next): DataBag
36
    {
37
        if (!$commandParameters->has('token_endpoint_auth_method')) {
38
            $commandParameters->set('token_endpoint_auth_method', 'client_secret_basic');
39
        }
40
41
        if (!\is_string($commandParameters->get('token_endpoint_auth_method'))) {
42
            throw new \InvalidArgumentException('The parameter "token_endpoint_auth_method" must be a string.');
43
        }
44
        if (!$this->clientAuthenticationMethodManager->has($commandParameters->get('token_endpoint_auth_method'))) {
0 ignored issues
show
Bug introduced by
It seems like $commandParameters->get(..._endpoint_auth_method') can also be of type null; however, parameter $name of OAuth2Framework\Componen...ionMethodManager::has() 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

44
        if (!$this->clientAuthenticationMethodManager->has(/** @scrutinizer ignore-type */ $commandParameters->get('token_endpoint_auth_method'))) {
Loading history...
45
            throw new \InvalidArgumentException(sprintf('The token endpoint authentication method "%s" is not supported. Please use one of the following values: %s', $commandParameters->get('token_endpoint_auth_method'), implode(', ', $this->clientAuthenticationMethodManager->list())));
46
        }
47
48
        $clientAuthenticationMethod = $this->clientAuthenticationMethodManager->get($commandParameters->get('token_endpoint_auth_method'));
0 ignored issues
show
Bug introduced by
It seems like $commandParameters->get(..._endpoint_auth_method') can also be of type null; however, parameter $name of OAuth2Framework\Componen...ionMethodManager::get() 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

48
        $clientAuthenticationMethod = $this->clientAuthenticationMethodManager->get(/** @scrutinizer ignore-type */ $commandParameters->get('token_endpoint_auth_method'));
Loading history...
49
        $validatedParameters = $next->handle($clientId, $commandParameters, $validatedParameters);
50
        $validatedParameters = $clientAuthenticationMethod->checkClientConfiguration($commandParameters, $validatedParameters);
51
        $validatedParameters->set('token_endpoint_auth_method', $commandParameters->get('token_endpoint_auth_method'));
52
53
        return $validatedParameters;
54
    }
55
}
56