Failed Conditions
Push — ng ( 280307...a36945 )
by Florent
03:55
created

AuthorizationEndpoint::process()   C

Complexity

Conditions 12
Paths 86

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 5.6668
c 0
b 0
f 0
cc 12
eloc 36
nc 86
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 Psr\Http\Server\RequestHandlerInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use OAuth2Framework\Component\AuthorizationEndpoint\ConsentScreen\ExtensionManager;
19
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
20
use OAuth2Framework\Component\AuthorizationEndpoint\UserAccountDiscovery\UserAccountDiscoveryManager;
21
use OAuth2Framework\Component\Core\Exception\OAuth2Exception;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
25
abstract class AuthorizationEndpoint implements MiddlewareInterface
26
{
27
    /**
28
     * @var UserAccountDiscoveryManager
29
     */
30
    private $userAccountDiscoveryManager;
31
32
    /**
33
     * @var ExtensionManager
34
     */
35
    private $consentScreenExtensionManager;
36
37
    /**
38
     * @var AuthorizationFactory
39
     */
40
    private $authorizationFactory;
41
42
    /**
43
     * AuthorizationEndpoint constructor.
44
     *
45
     * @param AuthorizationFactory        $authorizationFactory
46
     * @param UserAccountDiscoveryManager $userAccountDiscoveryManager
47
     * @param ExtensionManager            $consentScreenExtensionManager
48
     */
49
    public function __construct(AuthorizationFactory $authorizationFactory, UserAccountDiscoveryManager $userAccountDiscoveryManager, ExtensionManager $consentScreenExtensionManager)
50
    {
51
        $this->authorizationFactory = $authorizationFactory;
52
        $this->userAccountDiscoveryManager = $userAccountDiscoveryManager;
53
        $this->consentScreenExtensionManager = $consentScreenExtensionManager;
54
    }
55
56
    /**
57
     * @param Authorization          $authorization
58
     * @param ServerRequestInterface $request
59
     *
60
     * @return ResponseInterface
61
     */
62
    abstract protected function redirectToLoginPage(Authorization $authorization, ServerRequestInterface $request): ResponseInterface;
63
64
    /**
65
     * @param ServerRequestInterface $request
66
     * @param Authorization          $authorization
67
     *
68
     * @return ResponseInterface
69
     */
70
    abstract protected function processConsentScreen(ServerRequestInterface $request, Authorization $authorization): ResponseInterface;
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
76
    {
77
        try {
78
            $authorization = $this->authorizationFactory->createAuthorizationFromRequest($request);
79
            $authorization = $this->userAccountDiscoveryManager->find($authorization);
80
            $this->userAccountDiscoveryManager->check($authorization);
81
82
            if (null === $authorization->getUserAccount()) {
83
                return $this->redirectToLoginPage($authorization, $request);
84
            }
85
86
            $authorization = $this->consentScreenExtensionManager->processBefore($request, $authorization);
87
88
            return $this->processConsentScreen($request, $authorization);
89
        } catch (OAuth2AuthorizationException $e) {
90
            $data = $e->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not seem to exist on object<OAuth2Framework\C...AuthorizationException>.

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...
91
            if (null !== $e->getAuthorization()) {
92
                $redirectUri = $e->getAuthorization()->getRedirectUri();
93
                $responseMode = $e->getAuthorization()->getResponseMode();
94
                if (null !== $redirectUri && null !== $responseMode) {
95
                    $data['redirect_uri'] = $redirectUri;
96
                    $data['response_mode'] = $responseMode;
97
98
                    throw new OAuth2AuthorizationException(302, $data, $e->getAuthorization(), $e);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,string|obje...eMode\\ResponseMode>"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$e->getAuthorization() is of type object<OAuth2Framework\C...Endpoint\Authorization>, but the function expects a null|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$e is of type object<OAuth2Framework\C...AuthorizationException>, but the function expects a object<OAuth2Framework\C...Endpoint\Authorization>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
                }
100
            }
101
102
            throw $e;
103
        } catch (Exception\ProcessAuthorizationException $e) {
104
            $authorization = $e->getAuthorization();
105
            $authorization = $this->consentScreenExtensionManager->processAfter($request, $authorization);
106
            if (false === $authorization->isAuthorized()) {
107
                $this->throwRedirectionException($authorization, OAuth2Exception::ERROR_ACCESS_DENIED, 'The resource owner denied access to your client.');
108
            }
109
110
            try {
111
                $responseType = $authorization->getResponseType();
112
                $authorization = $responseType->process($authorization);
113
            } catch (OAuth2Exception $e) {
114
                $this->throwRedirectionException($authorization, $e->getData()['error'], $e->getData()['error_description']);
115
            }
116
117
            return $this->buildResponse($authorization);
118
        } catch (Exception\CreateRedirectionException $e) {
119
            $this->throwRedirectionException($e->getAuthorization(), $e->getMessage(), $e->getDescription());
120
        } catch (Exception\ShowConsentScreenException $e) {
121
            return $this->processConsentScreen($request, $e->getAuthorization());
122
        } catch (Exception\RedirectToLoginPageException $e) {
123
            return $this->redirectToLoginPage($e->getAuthorization(), $request);
124
        }
125
    }
126
127
    /**
128
     * @param Authorization $authorization
129
     *
130
     * @throws OAuth2Exception
131
     *
132
     * @return ResponseInterface
133
     */
134
    private function buildResponse(Authorization $authorization): ResponseInterface
135
    {
136
        if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) {
137
            throw new OAuth2Exception(400, ['error' => 'EEE', 'error_description' => 'FFF']);
0 ignored issues
show
Bug introduced by
The call to OAuth2Exception::__construct() misses a required argument $errorDescription.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array('error' => 'EEE', ..._description' => 'FFF') is of type array<string,string,{"er...description":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
        }
139
140
        $response = $authorization->getResponseMode()->buildResponse(
141
            $authorization->getRedirectUri(),
142
            $authorization->getResponseParameters()
143
        );
144
        foreach ($authorization->getResponseHeaders() as $k => $v) {
145
            $response = $response->withHeader($k, $v);
146
        }
147
148
        return $response;
149
    }
150
151
    /**
152
     * @param Authorization $authorization
153
     * @param string        $error
154
     * @param string        $error_description
155
     *
156
     * @throws OAuth2Exception
157
     */
158
    private function throwRedirectionException(Authorization $authorization, string $error, string $error_description)
159
    {
160
        $params = $authorization->getResponseParameters();
161
        if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) {
162
            throw new OAuth2Exception(400, $error, $error_description, $params);
0 ignored issues
show
Documentation introduced by
$params is of type array, but the function expects a object<Exception>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
        }
164
        $params += [
165
            'response_mode' => $authorization->getResponseMode(),
166
            'redirect_uri' => $authorization->getRedirectUri(),
167
        ];
168
169
        throw new OAuth2Exception(302, $error, $error_description, $params);
0 ignored issues
show
Documentation introduced by
$params is of type array, but the function expects a object<Exception>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
170
    }
171
}
172