1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 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\Server\Endpoint\Authorization; |
15
|
|
|
|
16
|
|
|
use Interop\Http\Server\RequestHandlerInterface; |
17
|
|
|
use Interop\Http\Server\MiddlewareInterface; |
18
|
|
|
use OAuth2Framework\Component\Server\Endpoint\Authorization\AfterConsentScreen\AfterConsentScreenManager; |
19
|
|
|
use OAuth2Framework\Component\Server\Endpoint\Authorization\BeforeConsentScreen\BeforeConsentScreenManager; |
20
|
|
|
use OAuth2Framework\Component\Server\Endpoint\Authorization\UserAccountDiscovery\UserAccountDiscoveryManager; |
21
|
|
|
use OAuth2Framework\Component\Server\Response\OAuth2Exception; |
22
|
|
|
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager; |
23
|
|
|
use OAuth2Framework\Component\Server\ResponseType\ResponseTypeProcessor; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
|
27
|
|
|
abstract class AuthorizationEndpoint implements MiddlewareInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var UserAccountDiscoveryManager |
31
|
|
|
*/ |
32
|
|
|
private $userAccountDiscoveryManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var BeforeConsentScreenManager |
36
|
|
|
*/ |
37
|
|
|
private $beforeConsentScreenManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var AfterConsentScreenManager |
41
|
|
|
*/ |
42
|
|
|
private $afterConsentScreenManager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AuthorizationFactory |
46
|
|
|
*/ |
47
|
|
|
private $authorizationFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* AuthorizationEndpoint constructor. |
51
|
|
|
* |
52
|
|
|
* @param AuthorizationFactory $authorizationFactory |
53
|
|
|
* @param UserAccountDiscoveryManager $userAccountDiscoveryManager |
54
|
|
|
* @param BeforeConsentScreenManager $beforeConsentScreenManager |
55
|
|
|
* @param AfterConsentScreenManager $afterConsentScreenManager |
56
|
|
|
*/ |
57
|
|
|
public function __construct(AuthorizationFactory $authorizationFactory, UserAccountDiscoveryManager $userAccountDiscoveryManager, BeforeConsentScreenManager $beforeConsentScreenManager, AfterConsentScreenManager $afterConsentScreenManager) |
58
|
|
|
{ |
59
|
|
|
$this->authorizationFactory = $authorizationFactory; |
60
|
|
|
$this->userAccountDiscoveryManager = $userAccountDiscoveryManager; |
61
|
|
|
$this->beforeConsentScreenManager = $beforeConsentScreenManager; |
62
|
|
|
$this->afterConsentScreenManager = $afterConsentScreenManager; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Authorization $authorization |
67
|
|
|
* @param ServerRequestInterface $request |
68
|
|
|
* |
69
|
|
|
* @return ResponseInterface |
70
|
|
|
*/ |
71
|
|
|
abstract protected function redirectToLoginPage(Authorization $authorization, ServerRequestInterface $request): ResponseInterface; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ServerRequestInterface $request |
75
|
|
|
* @param Authorization $authorization |
76
|
|
|
* |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
*/ |
79
|
|
|
abstract protected function processConsentScreen(ServerRequestInterface $request, Authorization $authorization): ResponseInterface; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler) |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
|
|
$authorization = $this->authorizationFactory->createAuthorizationFromRequest($request); |
88
|
|
|
$authorization = $this->userAccountDiscoveryManager->find($request, $authorization); |
89
|
|
|
|
90
|
|
|
if (null === $authorization->getUserAccount()) { |
91
|
|
|
return $this->redirectToLoginPage($authorization, $request); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$authorization = $this->beforeConsentScreenManager->process($request, $authorization); |
95
|
|
|
|
96
|
|
|
return $this->processConsentScreen($request, $authorization); |
97
|
|
|
} catch (OAuth2Exception $e) { |
98
|
|
|
$data = $e->getData(); |
99
|
|
|
if (array_key_exists('authorization', $data)) { |
100
|
|
|
$authorization = $data['authorization']; |
101
|
|
|
unset($data['authorization']); |
102
|
|
|
$redirectUri = $authorization->getRedirectUri(); |
103
|
|
|
$responseMode = $authorization->getResponseMode(); |
104
|
|
|
if (null !== $redirectUri && null !== $responseMode) { |
105
|
|
|
$data['redirect_uri'] = $redirectUri; |
106
|
|
|
$data['response_mode'] = $responseMode; |
107
|
|
|
|
108
|
|
|
throw new OAuth2Exception(302, $data, $e); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw $e; |
113
|
|
|
} catch (Exception\ProcessAuthorizationException $e) { |
114
|
|
|
$authorization = $e->getAuthorization(); |
115
|
|
|
$authorization = $this->afterConsentScreenManager->process($request, $authorization); |
116
|
|
|
if ($authorization->isAuthorized() === false) { |
117
|
|
|
$this->throwRedirectionException($authorization, OAuth2ResponseFactoryManager::ERROR_ACCESS_DENIED, 'The resource owner denied access to your client.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$responseTypeProcessor = ResponseTypeProcessor::create($authorization); |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$authorization = $responseTypeProcessor->process(); |
124
|
|
|
} catch (OAuth2Exception $e) { |
125
|
|
|
$this->throwRedirectionException($authorization, $e->getData()['error'], $e->getData()['error_description']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->buildResponse($authorization); |
129
|
|
|
} catch (Exception\CreateRedirectionException $e) { |
130
|
|
|
$this->throwRedirectionException($e->getAuthorization(), $e->getMessage(), $e->getDescription()); |
131
|
|
|
} catch (Exception\ShowConsentScreenException $e) { |
132
|
|
|
return $this->processConsentScreen($request, $e->getAuthorization()); |
133
|
|
|
} catch (Exception\RedirectToLoginPageException $e) { |
134
|
|
|
return $this->redirectToLoginPage($e->getAuthorization(), $request); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Authorization $authorization |
140
|
|
|
* |
141
|
|
|
* @throws OAuth2Exception |
142
|
|
|
* |
143
|
|
|
* @return ResponseInterface |
144
|
|
|
*/ |
145
|
|
|
private function buildResponse(Authorization $authorization): ResponseInterface |
146
|
|
|
{ |
147
|
|
|
if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) { |
148
|
|
|
throw new OAuth2Exception(400, ['error' => 'EEE', 'error_description' => 'FFF']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$response = $authorization->getResponseMode()->buildResponse( |
152
|
|
|
$authorization->getRedirectUri(), |
153
|
|
|
$authorization->getResponseParameters() |
154
|
|
|
); |
155
|
|
|
foreach ($authorization->getResponseHeaders() as $k => $v) { |
156
|
|
|
$response = $response->withHeader($k, $v); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $response; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param Authorization $authorization |
164
|
|
|
* @param string $error |
165
|
|
|
* @param string $error_description |
166
|
|
|
* |
167
|
|
|
* @throws OAuth2Exception |
168
|
|
|
*/ |
169
|
|
|
private function throwRedirectionException(Authorization $authorization, string $error, string $error_description) |
170
|
|
|
{ |
171
|
|
|
$params = [ |
172
|
|
|
'error' => $error, |
173
|
|
|
'error_description' => $error_description, |
174
|
|
|
]; |
175
|
|
|
$params += $authorization->getResponseParameters(); |
176
|
|
|
if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) { |
177
|
|
|
throw new OAuth2Exception(400, $params); |
178
|
|
|
} |
179
|
|
|
$params += [ |
180
|
|
|
'response_mode' => $authorization->getResponseMode(), |
181
|
|
|
'redirect_uri' => $authorization->getRedirectUri(), |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
|
throw new OAuth2Exception(302, $params); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|