1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\UI; |
5
|
|
|
|
6
|
|
|
use Lookyman\NetteOAuth2Server\Psr7\ApplicationPsr7ResponseInterface; |
7
|
|
|
use Lookyman\NetteOAuth2Server\RedirectConfig; |
8
|
|
|
use Lookyman\NetteOAuth2Server\Storage\IAuthorizationRequestSerializer; |
9
|
|
|
use Lookyman\NetteOAuth2Server\User\UserEntity; |
10
|
|
|
use Nette\Application\AbortException; |
11
|
|
|
use Nette\Application\BadRequestException; |
12
|
|
|
use Nette\Application\IResponse; |
13
|
|
|
use Nette\Http\IResponse as HttpResponse; |
14
|
|
|
use Nette\Http\Session; |
15
|
|
|
use Nette\Http\SessionSection; |
16
|
|
|
use Nette\Security\User; |
17
|
|
|
use Nextras\Application\UI\SecuredLinksPresenterTrait; |
18
|
|
|
|
19
|
|
|
trait ApprovePresenterTrait |
20
|
|
|
{ |
21
|
|
|
use SecuredLinksPresenterTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var IApproveControlFactory |
25
|
|
|
* @inject |
26
|
|
|
*/ |
27
|
|
|
public $approveControlFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var IAuthorizationRequestSerializer |
31
|
|
|
* @inject |
32
|
|
|
*/ |
33
|
|
|
public $authorizationRequestSerializer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RedirectConfig |
37
|
|
|
* @inject |
38
|
|
|
*/ |
39
|
|
|
public $redirectConfig; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return ApproveControl |
43
|
|
|
* @throws AbortException |
44
|
|
|
* @throws BadRequestException |
45
|
|
|
*/ |
46
|
|
|
protected function createComponentApprove(): ApproveControl |
47
|
|
|
{ |
48
|
|
|
if (!$this->getUser()->isLoggedIn()) { |
49
|
|
|
$this->redirect(...$this->redirectConfig->getLoginDestination()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @var string $data */ |
53
|
|
|
$data = $this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest; |
54
|
|
|
$authorizationRequest = $data ? $this->authorizationRequestSerializer->unserialize($data) : null; |
55
|
|
|
|
56
|
|
|
if ($authorizationRequest) { |
57
|
|
|
if (!$authorizationRequest->getUser()) { |
58
|
|
|
$authorizationRequest->setUser(new UserEntity($this->getUser()->getId())); |
59
|
|
|
} |
60
|
|
|
$control = $this->approveControlFactory->create($authorizationRequest); |
61
|
|
|
$control->onResponse[] = function (ApplicationPsr7ResponseInterface $response) { |
62
|
|
|
$this->sendResponse($response); |
63
|
|
|
}; |
64
|
|
|
return $control; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->error(null, HttpResponse::S400_BAD_REQUEST); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string|null $message |
72
|
|
|
* @param int $code |
73
|
|
|
* @throws BadRequestException |
74
|
|
|
*/ |
75
|
|
|
abstract public function error($message = null, $code = HttpResponse::S404_NOT_FOUND); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|null $namespace |
79
|
|
|
* @return Session|SessionSection |
80
|
|
|
*/ |
81
|
|
|
abstract public function getSession($namespace = null); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return User |
85
|
|
|
*/ |
86
|
|
|
abstract public function getUser(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $code [optional] |
90
|
|
|
* @param string|null $destination |
91
|
|
|
* @param array|mixed $args |
92
|
|
|
* @throws AbortException |
93
|
|
|
*/ |
94
|
|
|
abstract public function redirect($code, $destination = null, $args = []); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param IResponse $response |
98
|
|
|
* @throws AbortException |
99
|
|
|
*/ |
100
|
|
|
abstract public function sendResponse(IResponse $response); |
101
|
|
|
} |
102
|
|
|
|