-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathAuthorizeController.php
288 lines (247 loc) · 8.4 KB
/
AuthorizeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
declare(strict_types=1);
/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\OAuthServerBundle\Controller;
use FOS\OAuthServerBundle\Event\OAuthEvent;
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Controller handling basic authorization.
*
* @author Chris Jones <[email protected]>
*/
class AuthorizeController
{
/**
* @var ClientInterface
*/
private $client;
/**
* @var SessionInterface
*/
private $session;
/**
* @var Form
*/
private $authorizeForm;
/**
* @var AuthorizeFormHandler
*/
private $authorizeFormHandler;
/**
* @var OAuth2
*/
private $oAuth2Server;
/**
* @var EngineInterface
*/
private $templating;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var UrlGeneratorInterface
*/
private $router;
/**
* @var ClientManagerInterface
*/
private $clientManager;
/**
* @var string
*/
private $templateEngineType;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
* Thus, this is considered a bad practice to fetch services directly from container.
*
* @todo This controller could be refactored to not rely on so many dependencies
*
* @param RequestStack $requestStack
* @param Form $authorizeForm
* @param AuthorizeFormHandler $authorizeFormHandler
* @param OAuth2 $oAuth2Server
* @param EngineInterface $templating
* @param TokenStorageInterface $tokenStorage
* @param UrlGeneratorInterface $router
* @param ClientManagerInterface $clientManager
* @param EventDispatcherInterface $eventDispatcher
* @param SessionInterface $session
* @param string $templateEngineType
*/
public function __construct(
RequestStack $requestStack,
Form $authorizeForm,
AuthorizeFormHandler $authorizeFormHandler,
OAuth2 $oAuth2Server,
EngineInterface $templating,
TokenStorageInterface $tokenStorage,
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcherInterface $eventDispatcher,
SessionInterface $session = null,
$templateEngineType = 'twig'
) {
$this->requestStack = $requestStack;
$this->session = $session;
$this->authorizeForm = $authorizeForm;
$this->authorizeFormHandler = $authorizeFormHandler;
$this->oAuth2Server = $oAuth2Server;
$this->templating = $templating;
$this->tokenStorage = $tokenStorage;
$this->router = $router;
$this->clientManager = $clientManager;
$this->templateEngineType = $templateEngineType;
$this->eventDispatcher = $eventDispatcher;
}
/**
* Authorize.
*/
public function authorizeAction(Request $request)
{
$user = $this->tokenStorage->getToken()->getUser();
if (!$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
$this->session->invalidate(600);
$this->session->set('_fos_oauth_server.ensure_logout', true);
}
$form = $this->authorizeForm;
$formHandler = $this->authorizeFormHandler;
/** @var OAuthEvent $event */
$event = $this->eventDispatcher->dispatch(
OAuthEvent::PRE_AUTHORIZATION_PROCESS,
new OAuthEvent($user, $this->getClient())
);
if ($event->isAuthorizedClient()) {
$scope = $request->get('scope', null);
try {
return $this->oAuth2Server->finishClientAuthorization(true, $user, $request, $scope);
} catch (OAuth2ServerException $e) {
return $e->getHttpResponse();
}
}
if (true === $formHandler->process()) {
return $this->processSuccess($user, $formHandler, $request);
}
$data = [
'form' => $form->createView(),
'client' => $this->getClient(),
];
return $this->renderAuthorize($data, $this->templating, $this->templateEngineType);
}
/**
* @param UserInterface $user
* @param AuthorizeFormHandler $formHandler
* @param Request $request
*
* @return Response
*/
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
{
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
$this->tokenStorage->setToken(null);
$this->session->invalidate();
}
$this->eventDispatcher->dispatch(
OAuthEvent::POST_AUTHORIZATION_PROCESS,
new OAuthEvent($user, $this->getClient(), $formHandler->isAccepted())
);
$formName = $this->authorizeForm->getName();
if (!$request->query->all() && $request->request->has($formName)) {
$request->query->add($request->request->get($formName));
}
try {
return $this->oAuth2Server
->finishClientAuthorization($formHandler->isAccepted(), $user, $request, $formHandler->getScope())
;
} catch (OAuth2ServerException $e) {
return $e->getHttpResponse();
}
}
/**
* Generate the redirection url when the authorize is completed.
*
* @param UserInterface $user
*
* @return string
*/
protected function getRedirectionUrl(UserInterface $user)
{
return $this->router->generate('fos_oauth_server_profile_show');
}
/**
* @return ClientInterface
*/
protected function getClient()
{
if (null !== $this->client) {
return $this->client;
}
if (null === $request = $this->getCurrentRequest()) {
throw new NotFoundHttpException('Client not found.');
}
if (null === $clientId = $request->get('client_id')) {
$formData = $request->get($this->authorizeForm->getName(), []);
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
}
$this->client = $this->clientManager->findClientByPublicId($clientId);
if (null === $this->client) {
throw new NotFoundHttpException('Client not found.');
}
return $this->client;
}
/**
* @throws \RuntimeException
*/
protected function renderAuthorize(array $data, EngineInterface $engine, string $engineType): Response
{
return $engine->renderResponse(
'@FOSOAuthServer/Authorize/authorize.html.'.$engineType,
$data
);
}
/**
* @return null|Request
*/
private function getCurrentRequest()
{
$request = $this->requestStack->getCurrentRequest();
if (null === $request) {
throw new \RuntimeException('No current request.');
}
return $request;
}
}