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(); |
|
|
|
|
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); |
|
|
|
|
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']); |
|
|
|
|
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); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
$params += [ |
165
|
|
|
'response_mode' => $authorization->getResponseMode(), |
166
|
|
|
'redirect_uri' => $authorization->getRedirectUri(), |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
throw new OAuth2Exception(302, $error, $error_description, $params); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
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.