|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lookyman\Nette\OAuth2\Server\UI; |
|
6
|
|
|
|
|
7
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
|
8
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
9
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
|
10
|
|
|
use Lookyman\Nette\OAuth2\Server\Psr7\ApplicationPsr7ResponseInterface; |
|
11
|
|
|
use Nette\Application\AbortException; |
|
12
|
|
|
use Nette\Application\IResponse; |
|
13
|
|
|
use Nette\Application\UI\Control; |
|
14
|
|
|
use Nette\Http\IResponse as HttpResponse; |
|
15
|
|
|
use Nette\Http\Session; |
|
16
|
|
|
use Nextras\Application\UI\SecuredLinksControlTrait; |
|
17
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
18
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @method void onResponse(IResponse $response) |
|
22
|
|
|
*/ |
|
23
|
|
|
final class ApproveControl extends Control implements LoggerAwareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use LoggerAwareTrait; |
|
26
|
|
|
use Psr7Trait; |
|
27
|
|
|
use SecuredLinksControlTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var callable[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public $onResponse; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var AuthorizationRequest |
|
36
|
|
|
*/ |
|
37
|
|
|
private $authorizationRequest; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var AuthorizationServer |
|
41
|
|
|
*/ |
|
42
|
|
|
private $authorizationServer; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Session |
|
46
|
|
|
*/ |
|
47
|
|
|
private $session; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $templateFile; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct(AuthorizationServer $authorizationServer, Session $session, AuthorizationRequest $authorizationRequest) |
|
55
|
|
|
{ |
|
56
|
|
|
parent::__construct(); |
|
57
|
|
|
$this->authorizationServer = $authorizationServer; |
|
58
|
|
|
$this->session = $session; |
|
59
|
|
|
$this->authorizationRequest = $authorizationRequest; |
|
60
|
|
|
$this->templateFile = __DIR__ . '/templates/approve.latte'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function render() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->template->setFile($this->templateFile); |
|
|
|
|
|
|
66
|
|
|
$this->template->authorizationRequest = $this->authorizationRequest; |
|
|
|
|
|
|
67
|
|
|
$this->template->render(); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @secured |
|
72
|
|
|
*/ |
|
73
|
|
|
public function handleApprove() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->authorizationRequest->setAuthorizationApproved(true); |
|
76
|
|
|
$this->completeAuthorizationRequest(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @secured |
|
81
|
|
|
*/ |
|
82
|
|
|
public function handleDeny() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->authorizationRequest->setAuthorizationApproved(false); |
|
85
|
|
|
$this->completeAuthorizationRequest(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function completeAuthorizationRequest() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->session->getSection(OAuth2Presenter::SESSION_NAMESPACE)->remove(); |
|
91
|
|
|
|
|
92
|
|
|
$response = $this->createResponse(); |
|
93
|
|
|
try { |
|
94
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
|
95
|
|
|
$response = $this->authorizationServer->completeAuthorizationRequest($this->authorizationRequest, $response); |
|
96
|
|
|
$this->onResponse($response); |
|
97
|
|
|
|
|
98
|
|
|
} catch (AbortException $e) { |
|
99
|
|
|
throw $e; |
|
100
|
|
|
|
|
101
|
|
|
} catch (OAuthServerException $e) { |
|
102
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
|
103
|
|
|
$response = $e->generateHttpResponse($response); |
|
104
|
|
|
$this->onResponse($response); |
|
105
|
|
|
|
|
106
|
|
|
} catch (\Throwable $e) { |
|
107
|
|
|
if ($this->logger) { |
|
108
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
|
109
|
|
|
} |
|
110
|
|
|
$body = $this->createStream(); |
|
111
|
|
|
$body->write('Unknown error'); |
|
112
|
|
|
$this->onResponse($response->withStatus(HttpResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body)); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function setTemplateFile(string $file) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->templateFile = $file; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.