1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TH\OAuth2\Controllers; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
9
|
|
|
use OAuth2\Controller\AuthorizeControllerInterface; |
10
|
|
|
use OAuth2\HttpFoundationBridge\Request as BridgeRequest; |
11
|
|
|
use OAuth2\HttpFoundationBridge\Response as BridgeResponse; |
12
|
|
|
use TH\OAuth2\AuthorizeRenderer; |
13
|
|
|
|
14
|
|
|
class AuthorizeValidator |
15
|
|
|
{ |
16
|
|
|
private $urlGenerator; |
17
|
|
|
|
18
|
|
|
private $oauth2AuthorizeController; |
19
|
|
|
|
20
|
|
|
private $authorizeRenderer; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
UrlGeneratorInterface $urlGenerator, |
24
|
|
|
AuthorizeControllerInterface $oauth2AuthorizeController, |
25
|
|
|
AuthorizeRenderer $authorizeRenderer |
26
|
|
|
) { |
27
|
|
|
$this->urlGenerator = $urlGenerator; |
28
|
|
|
$this->oauth2AuthorizeController = $oauth2AuthorizeController; |
29
|
|
|
$this->authorizeRenderer = $authorizeRenderer; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __invoke(Application $app, Request $request) |
33
|
|
|
{ |
34
|
|
|
$request = BridgeRequest::createFromRequest($request); |
35
|
|
|
$response = new BridgeResponse; |
36
|
|
|
|
37
|
|
|
if (!$this->oauth2AuthorizeController->validateAuthorizeRequest($request, $response)) { |
38
|
|
|
return $response; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$token = $app['security.token_storage']->getToken(); |
42
|
|
|
$user = null; |
43
|
|
|
|
44
|
|
|
if ($token instanceof TokenInterface) { |
45
|
|
|
$user = $token->getUser(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->authorizeRenderer->render( |
49
|
|
|
$this->urlGenerator->generate('oauth2_authorize_handler', $request->query->all()), |
50
|
|
|
$request->query->get('client_id'), |
51
|
|
|
$request->query->get('response_type'), |
52
|
|
|
$user |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|