Failed Conditions
Push — master ( 323120...a399af )
by Florent
05:26
created

AuthorizationEndpoint::process()   C

Complexity

Conditions 11
Paths 97

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 6.726
c 0
b 0
f 0
cc 11
nc 97
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\AuthorizationEndpoint;
15
16
use Base64Url\Base64Url;
17
use Http\Message\MessageFactory;
18
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequestLoader;
19
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterCheckerManager;
20
use OAuth2Framework\Component\AuthorizationEndpoint\UserAccount\UserAccountCheckerManager;
21
use OAuth2Framework\Component\AuthorizationEndpoint\UserAccount\UserAccountDiscovery;
22
use OAuth2Framework\Component\Core\Message\OAuth2Error;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
use Symfony\Component\HttpFoundation\Session\SessionInterface;
27
use Symfony\Component\Routing\RouterInterface;
28
29
abstract class AuthorizationEndpoint extends AbstractEndpoint
30
{
31
    private $authorizationRequestLoader;
32
33
    private $parameterCheckerManager;
34
35
    private $userAccountDiscovery;
36
37
    private $userAccountCheckerManager;
38
39
    private $router;
40
41
    private $consentRepository;
42
43
    public function __construct(MessageFactory $messageFactory, AuthorizationRequestLoader $authorizationRequestLoader, ParameterCheckerManager $parameterCheckerManager, UserAccountDiscovery $userAccountDiscovery, UserAccountCheckerManager $userAccountCheckerManager, SessionInterface $session, RouterInterface $router, ConsentRepository $consentRepository)
44
    {
45
        parent::__construct($messageFactory, $session);
46
        $this->authorizationRequestLoader = $authorizationRequestLoader;
47
        $this->parameterCheckerManager = $parameterCheckerManager;
48
        $this->userAccountDiscovery = $userAccountDiscovery;
49
        $this->userAccountCheckerManager = $userAccountCheckerManager;
50
        $this->router = $router;
51
        $this->consentRepository = $consentRepository;
52
    }
53
54
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
55
    {
56
        try {
57
            $authorization = $this->authorizationRequestLoader->load($request);
58
            $authorization = $this->parameterCheckerManager->process($authorization);
59
            $userAccount = $this->userAccountDiscovery->find();
60
61
            if (null !== $userAccount) {
62
                $isFullyAuthenticated = $this->userAccountDiscovery->isFullyAuthenticated();
63
                $authorization->setUserAccount($userAccount, $isFullyAuthenticated);
64
                $this->userAccountCheckerManager->check($authorization);
65
66
                switch (true) {
67
                    case $authorization->hasPrompt('none'):
68
                        if (!$this->consentRepository->hasConsentBeenGiven($authorization)) {
69
                            throw $this->buildOAuth2Error($authorization, OAuth2Error::ERROR_INTERACTION_REQUIRED, 'The resource owner consent is required.');
70
                        }
71
                        $authorization->allow();
72
                        $routeName = 'authorization_process_endpoint';
73
                        break;
74
                    case $authorization->hasPrompt('login'):
75
                        $routeName = 'authorization_login_endpoint';
76
                        break;
77
                    case $authorization->hasPrompt('select_account'):
78
                        $routeName = 'authorization_select_account_endpoint';
79
                        break;
80
                    case $authorization->hasPrompt('consent'):
81
                    default:
82
                        $routeName = 'authorization_consent_endpoint';
83
                        break;
84
                }
85
86
                $authorizationId = Base64Url::encode(random_bytes(64));
87
                $authorizationId = $this->saveAuthorization($authorizationId, $authorization);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $authorizationId is correct as $this->saveAuthorization...tionId, $authorization) (which targets OAuth2Framework\Componen...nt::saveAuthorization()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
                $redirectTo = $this->router->generate($routeName, ['authorization_id' => $authorizationId]);
89
90
                return $this->createRedirectResponse($redirectTo);
91
            } else {
92
                if ($authorization->hasPrompt('none')) {
93
                    if (!$this->consentRepository->hasConsentBeenGiven($authorization)) {
94
                        throw $this->buildOAuth2Error($authorization, OAuth2Error::ERROR_LOGIN_REQUIRED, 'The resource owner is not logged in.');
95
                    }
96
                    $authorization->allow();
97
                    $routeName = 'authorization_process_endpoint';
98
                } else {
99
                    $routeName = 'authorization_login_endpoint';
100
                }
101
102
                $authorizationId = Base64Url::encode(random_bytes(64));
103
                $authorizationId = $this->saveAuthorization($authorizationId, $authorization);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $authorizationId is correct as $this->saveAuthorization...tionId, $authorization) (which targets OAuth2Framework\Componen...nt::saveAuthorization()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
                $redirectTo = $this->router->generate($routeName, ['authorization_id' => $authorizationId]);
105
106
                return $this->createRedirectResponse($redirectTo);
107
            }
108
        } catch (OAuth2Error $e) {
109
            throw $e;
110
        } catch (\Exception $e) {
111
            throw new OAuth2Error(400, OAuth2Error::ERROR_INVALID_REQUEST, null);
112
        }
113
    }
114
}
115