Failed Conditions
Push — master ( 22ddb5...a86abe )
by Florent
03:15
created

ClientAuthenticationMiddleware::process()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Middleware;
15
16
use Interop\Http\Server\RequestHandlerInterface;
17
use Interop\Http\Server\MiddlewareInterface;
18
use OAuth2Framework\Component\Server\Model\Client\ClientRepositoryInterface;
19
use OAuth2Framework\Component\Server\Response\OAuth2Exception;
20
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager;
21
use OAuth2Framework\Component\Server\TokenEndpointAuthMethod\TokenEndpointAuthMethodManager;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
final class ClientAuthenticationMiddleware implements MiddlewareInterface
25
{
26
    /**
27
     * @var TokenEndpointAuthMethodManager
28
     */
29
    private $tokenEndpointAuthMethodManager;
30
31
    /**
32
     * @var bool
33
     */
34
    private $authenticationRequired;
35
36
    /**
37
     * @var ClientRepositoryInterface
38
     */
39
    private $clientRepository;
40
41
    /**
42
     * ClientAuthenticationMiddleware constructor.
43
     *
44
     * @param ClientRepositoryInterface      $clientRepository
45
     * @param TokenEndpointAuthMethodManager $tokenEndpointAuthMethodManager
46
     * @param bool                           $authenticationRequired
47
     */
48
    public function __construct(ClientRepositoryInterface $clientRepository, TokenEndpointAuthMethodManager $tokenEndpointAuthMethodManager, bool $authenticationRequired)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $tokenEndpointAuthMethodManager exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
Comprehensibility Naming introduced by
The variable name $authenticationRequired exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
49
    {
50
        $this->clientRepository = $clientRepository;
51
        $this->tokenEndpointAuthMethodManager = $tokenEndpointAuthMethodManager;
52
        $this->authenticationRequired = $authenticationRequired;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler)
59
    {
60
        $clientId = $this->tokenEndpointAuthMethodManager->findClientInformationInTheRequest($request, $authentication_method, $client_credentials);
61
        $client = null;
62
        if (null !== $clientId) {
63
            $client = $this->clientRepository->find($clientId);
64
        }
65
        if (null !== $client && false === $this->tokenEndpointAuthMethodManager->isClientAuthenticated($request, $client, $authentication_method, $client_credentials)) {
0 ignored issues
show
Bug introduced by
It seems like $authentication_method can be null; however, isClientAuthenticated() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66
            $client = null;
67
        }
68
        if (true === $this->authenticationRequired && null === $client) {
69
            throw new OAuth2Exception(
70
                401,
71
                [
72
                    'error' => OAuth2ResponseFactoryManager::ERROR_INVALID_CLIENT,
73
                    'error_description' => 'Client authentication failed.',
74
                ]
75
            );
76
        }
77
        if (null !== $client) {
78
            $request = $request->withAttribute('client', $client);
79
        }
80
81
        return $requestHandler->handle($request);
82
    }
83
}
84