-
Notifications
You must be signed in to change notification settings - Fork 114
/
AuthorizationController.php
80 lines (64 loc) · 2.43 KB
/
AuthorizationController.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
<?php
declare(strict_types=1);
namespace Trikoder\Bundle\OAuth2Bundle\Controller;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent;
use Trikoder\Bundle\OAuth2Bundle\League\Entity\User;
use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;
final class AuthorizationController
{
/**
* @var AuthorizationServer
*/
private $server;
/**
* @var Security
*/
private $security;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct(AuthorizationServer $server, Security $security, EventDispatcherInterface $eventDispatcher)
{
$this->server = $server;
$this->security = $security;
$this->eventDispatcher = $eventDispatcher;
}
public function indexAction(ServerRequestInterface $serverRequest, ResponseFactoryInterface $responseFactory): ResponseInterface
{
$serverResponse = $responseFactory->createResponse();
try {
$authRequest = $this->server->validateAuthorizationRequest($serverRequest);
$authRequest->setUser($this->getUserEntity());
/** @var AuthorizationRequestResolveEvent $event */
$event = $this->eventDispatcher->dispatch(
OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE,
new AuthorizationRequestResolveEvent($authRequest)
);
if ($event->hasResponse()) {
return $event->getResponse();
}
$authRequest->setAuthorizationApproved($event->getAuthorizationResolution());
return $this->server->completeAuthorizationRequest($authRequest, $serverResponse);
} catch (OAuthServerException $e) {
return $e->generateHttpResponse($serverResponse);
}
}
private function getUserEntity(): User
{
$userEntity = new User();
$user = $this->security->getUser();
if ($user instanceof UserInterface) {
$userEntity->setIdentifier($user->getUsername());
}
return $userEntity;
}
}