1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Lookyman\Nette\OAuth2\Server\UI; |
6
|
|
|
|
7
|
|
|
use Lookyman\Nette\OAuth2\Server\Psr7\ApplicationPsr7ResponseInterface; |
8
|
|
|
use Lookyman\Nette\OAuth2\Server\Storage\IAuthorizationRequestSerializer; |
9
|
|
|
use Lookyman\Nette\OAuth2\Server\User\UserEntity; |
10
|
|
|
use Nette\Application\IResponse; |
11
|
|
|
use Nette\Http\IResponse as HttpResponse; |
12
|
|
|
use Nextras\Application\UI\SecuredLinksPresenterTrait; |
13
|
|
|
|
14
|
|
|
trait ApprovePresenterTrait |
15
|
|
|
{ |
16
|
|
|
use SecuredLinksPresenterTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var IApproveControlFactory |
20
|
|
|
* @inject |
21
|
|
|
*/ |
22
|
|
|
public $approveControlFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var IAuthorizationRequestSerializer |
26
|
|
|
* @inject |
27
|
|
|
*/ |
28
|
|
|
public $authorizationRequestSerializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RedirectService |
32
|
|
|
* @inject |
33
|
|
|
*/ |
34
|
|
|
public $redirectService; |
35
|
|
|
|
36
|
|
|
protected function createComponentApprove(): ApproveControl |
37
|
|
|
{ |
38
|
|
|
if (!$this->getUser()->isLoggedIn()) { |
39
|
|
|
$this->redirectService->redirectToLoginDestination($this); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @var string $data */ |
43
|
|
|
$data = $this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest; |
44
|
|
|
$authorizationRequest = $data ? $this->authorizationRequestSerializer->unserialize($data) : null; |
45
|
|
|
|
46
|
|
|
if ($authorizationRequest) { |
47
|
|
|
if (!$authorizationRequest->getUser()) { |
48
|
|
|
$authorizationRequest->setUser(new UserEntity($this->getUser()->getId())); |
49
|
|
|
} |
50
|
|
|
$control = $this->approveControlFactory->create($authorizationRequest); |
51
|
|
|
$control->onResponse[] = function (ApplicationPsr7ResponseInterface $response) { |
52
|
|
|
$this->sendResponse($response); |
53
|
|
|
}; |
54
|
|
|
return $control; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->error(null, HttpResponse::S400_BAD_REQUEST); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint |
62
|
|
|
*/ |
63
|
|
|
abstract public function error($message = null, $code = HttpResponse::S404_NOT_FOUND); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint |
67
|
|
|
*/ |
68
|
|
|
abstract public function getSession($namespace = null); |
69
|
|
|
|
70
|
|
|
abstract public function getUser(); |
71
|
|
|
|
72
|
|
|
abstract public function sendResponse(IResponse $response); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|