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