Failed Conditions
Push — master ( b8d841...bc596e )
by Florent
28:20
created

AuthorizationEndpoint   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 14
dl 0
loc 132
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
redirectToLoginPage() 0 1 ?
processConsentScreen() 0 1 ?
C process() 0 46 11
A buildResponse() 0 13 2
A throwRedirectionException() 0 13 3
A createAuthorizationFromRequest() 0 7 1
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\ConsentScreen\ExtensionManager;
18
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
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
    /**
31
     * @var UserAccountDiscovery
32
     */
33
    protected $userAccountDiscovery;
34
35
    /**
36
     * @var UserAccountCheckerManager
37
     */
38
    protected $userAccountCheckerManager;
39
40
    /**
41
     * @var ExtensionManager
42
     */
43
    protected $consentScreenExtensionManager;
44
45
    /**
46
     * @var AuthorizationRequestLoader
47
     */
48
    protected $authorizationRequestLoader;
49
50
    /**
51
     * @var ParameterCheckerManager
52
     */
53
    protected $parameterCheckerManager;
54
55
    /**
56
     * @var MessageFactory
57
     */
58
    protected $messageFactory;
59
60
    /**
61
     * AuthorizationEndpoint constructor.
62
     */
63
    public function __construct(MessageFactory $messageFactory, AuthorizationRequestLoader $authorizationRequestLoader, ParameterCheckerManager $parameterCheckerManager, UserAccountDiscovery $userAccountDiscovery, UserAccountCheckerManager $userAccountCheckerManager, ExtensionManager $consentScreenExtensionManager)
64
    {
65
        $this->messageFactory = $messageFactory;
66
        $this->authorizationRequestLoader = $authorizationRequestLoader;
67
        $this->parameterCheckerManager = $parameterCheckerManager;
68
        $this->userAccountDiscovery = $userAccountDiscovery;
69
        $this->userAccountCheckerManager = $userAccountCheckerManager;
70
        $this->consentScreenExtensionManager = $consentScreenExtensionManager;
71
    }
72
73
    abstract protected function redirectToLoginPage(ServerRequestInterface $request, Authorization $authorization): ResponseInterface;
74
75
    abstract protected function processConsentScreen(ServerRequestInterface $request, Authorization $authorization): ResponseInterface;
76
77
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
78
    {
79
        try {
80
            $authorization = $this->createAuthorizationFromRequest($request);
81
            $isFullyAuthenticated = null;
82
            $userAccount = $this->userAccountDiscovery->find($isFullyAuthenticated);
83
            if (!\is_bool($isFullyAuthenticated)) {
84
                $isFullyAuthenticated = false;
85
            }
86
            if (null !== $userAccount) {
87
                $authorization->setUserAccount($userAccount, $isFullyAuthenticated);
88
            }
89
            $this->userAccountCheckerManager->check($authorization, $userAccount, $isFullyAuthenticated);
90
            if (null === $userAccount) {
91
                return $this->redirectToLoginPage($request, $authorization);
92
            }
93
            $authorization = $this->consentScreenExtensionManager->processBefore($request, $authorization);
94
95
            return $this->processConsentScreen($request, $authorization);
96
        } catch (OAuth2AuthorizationException $e) {
97
            throw $e;
98
        } catch (Exception\ProcessAuthorizationException $e) {
99
            $authorization = $e->getAuthorization();
100
            $authorization = $this->consentScreenExtensionManager->processAfter($request, $authorization);
101
            if (false === $authorization->isAuthorized()) {
102
                $this->throwRedirectionException($authorization, OAuth2Message::ERROR_ACCESS_DENIED, 'The resource owner denied access to your client.');
103
            }
104
105
            $responseType = $authorization->getResponseType();
106
107
            try {
108
                $authorization = $responseType->preProcess($authorization);
109
                $authorization = $responseType->process($authorization);
110
            } catch (OAuth2Message $e) {
111
                $this->throwRedirectionException($authorization, $e->getMessage(), $e->getErrorDescription());
112
            }
113
114
            return $this->buildResponse($authorization);
115
        } catch (Exception\CreateRedirectionException $e) {
116
            $this->throwRedirectionException($e->getAuthorization(), $e->getMessage(), $e->getDescription());
117
        } catch (Exception\ShowConsentScreenException $e) {
118
            return $this->processConsentScreen($request, $e->getAuthorization());
119
        } catch (Exception\RedirectToLoginPageException $e) {
120
            return $this->redirectToLoginPage($request, $e->getAuthorization());
121
        }
122
    }
123
124
    protected function buildResponse(Authorization $authorization): ResponseInterface
125
    {
126
        $response = $authorization->getResponseMode()->buildResponse(
127
            $this->messageFactory->createResponse(),
128
            $authorization->getRedirectUri(),
129
            $authorization->getResponseParameters()
130
        );
131
        foreach ($authorization->getResponseHeaders() as $k => $v) {
132
            $response = $response->withHeader($k, $v);
133
        }
134
135
        return $response;
136
    }
137
138
    protected function throwRedirectionException(Authorization $authorization, string $error, string $errorDescription)
139
    {
140
        $params = $authorization->getResponseParameters();
141
        if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) {
142
            throw new OAuth2Message(400, $error, $errorDescription);
143
        }
144
        $params += [
145
            'response_mode' => $authorization->getResponseMode(),
146
            'redirect_uri' => $authorization->getRedirectUri(),
147
        ];
148
149
        throw new OAuth2Message(302, $error, $errorDescription, $params);
150
    }
151
152
    public function createAuthorizationFromRequest(ServerRequestInterface $request): Authorization
153
    {
154
        $authorization = $this->authorizationRequestLoader->load($request);
155
        $authorization = $this->parameterCheckerManager->process($authorization);
156
157
        return $authorization;
158
    }
159
}
160