Failed Conditions
Push — master ( d3ed62...7f2d83 )
by Florent
281:43 queued 02:00
created

AuthorizationEndpoint::process()   C

Complexity

Conditions 11
Paths 218

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 6.2583
c 0
b 0
f 0
cc 11
nc 218
nop 2

How to fix   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 Http\Message\MessageFactory;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
18
use OAuth2Framework\Component\AuthorizationEndpoint\Extension\ExtensionManager;
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\OAuth2Message;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\MiddlewareInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
abstract class AuthorizationEndpoint implements MiddlewareInterface
29
{
30
    protected $messageFactory;
31
32
    protected $authorizationRequestLoader;
33
34
    protected $parameterCheckerManager;
35
36
    protected $userAccountDiscovery;
37
38
    protected $userAccountCheckerManager;
39
40
    protected $extensionManager;
41
42
    public function __construct(MessageFactory $messageFactory, AuthorizationRequestLoader $authorizationRequestLoader, ParameterCheckerManager $parameterCheckerManager, UserAccountDiscovery $userAccountDiscovery, UserAccountCheckerManager $userAccountCheckerManager, ExtensionManager $extensionManager)
43
    {
44
        $this->messageFactory = $messageFactory;
45
        $this->authorizationRequestLoader = $authorizationRequestLoader;
46
        $this->parameterCheckerManager = $parameterCheckerManager;
47
        $this->userAccountDiscovery = $userAccountDiscovery;
48
        $this->userAccountCheckerManager = $userAccountCheckerManager;
49
        $this->extensionManager = $extensionManager;
50
    }
51
52
    abstract protected function redirectToLoginPage(ServerRequestInterface $request, Authorization $authorization): ResponseInterface;
53
54
    abstract protected function processConsentScreen(ServerRequestInterface $request, Authorization $authorization): ResponseInterface;
55
56
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58
        try {
59
            $authorization = $this->createAuthorizationFromRequest($request);
60
            $isFullyAuthenticated = null;
61
            $userAccount = $this->userAccountDiscovery->find($isFullyAuthenticated);
62
            if (!\is_bool($isFullyAuthenticated)) {
63
                $isFullyAuthenticated = false;
64
            }
65
            if (null !== $userAccount) {
66
                $authorization->setUserAccount($userAccount, $isFullyAuthenticated);
67
            }
68
            $this->userAccountCheckerManager->check($authorization, $userAccount, $isFullyAuthenticated);
69
            if (null === $userAccount) {
70
                return $this->redirectToLoginPage($request, $authorization);
71
            }
72
            $authorization = $this->extensionManager->processBefore($request, $authorization);
73
74
            return $this->processConsentScreen($request, $authorization);
75
        } catch (OAuth2AuthorizationException $e) {
76
            throw $e;
77
        } catch (Exception\ProcessAuthorizationException $e) {
78
            $authorization = $e->getAuthorization();
79
            $authorization = $this->extensionManager->processAfter($request, $authorization);
80
            if (false === $authorization->isAuthorized()) {
81
                $this->throwRedirectionException($authorization, OAuth2Message::ERROR_ACCESS_DENIED, 'The resource owner denied access to your client.');
82
            }
83
84
            $responseType = $authorization->getResponseType();
85
86
            try {
87
                $authorization = $responseType->preProcess($authorization);
88
                $authorization = $responseType->process($authorization);
89
            } catch (OAuth2Message $e) {
90
                $this->throwRedirectionException($authorization, $e->getMessage(), $e->getErrorDescription());
91
            }
92
93
            return $this->buildResponse($authorization);
94
        } catch (Exception\CreateRedirectionException $e) {
95
            $this->throwRedirectionException($e->getAuthorization(), $e->getMessage(), $e->getDescription());
96
        } catch (Exception\ShowConsentScreenException $e) {
97
            return $this->processConsentScreen($request, $e->getAuthorization());
98
        } catch (Exception\RedirectToLoginPageException $e) {
99
            return $this->redirectToLoginPage($request, $e->getAuthorization());
100
        }
101
    }
102
103
    protected function buildResponse(Authorization $authorization): ResponseInterface
104
    {
105
        $response = $authorization->getResponseMode()->buildResponse(
106
            $this->messageFactory->createResponse(),
107
            $authorization->getRedirectUri(),
108
            $authorization->getResponseParameters()
109
        );
110
        foreach ($authorization->getResponseHeaders() as $k => $v) {
111
            $response = $response->withHeader($k, $v);
112
        }
113
114
        return $response;
115
    }
116
117
    protected function throwRedirectionException(Authorization $authorization, string $error, string $errorDescription)
118
    {
119
        $params = $authorization->getResponseParameters();
120
        if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) {
121
            throw new OAuth2Message(400, $error, $errorDescription);
122
        }
123
        $params += [
124
            'response_mode' => $authorization->getResponseMode(),
125
            'redirect_uri' => $authorization->getRedirectUri(),
126
        ];
127
128
        throw new OAuth2Message(303, $error, $errorDescription, $params);
129
    }
130
131
    public function createAuthorizationFromRequest(ServerRequestInterface $request): Authorization
132
    {
133
        $authorization = $this->authorizationRequestLoader->load($request);
134
        $authorization = $this->parameterCheckerManager->process($authorization);
135
136
        return $authorization;
137
    }
138
}
139