1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\UI; |
5
|
|
|
|
6
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
7
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
8
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
9
|
|
|
use Nette\Application\AbortException; |
10
|
|
|
use Nette\Application\IResponse; |
11
|
|
|
use Nette\Application\UI\Control; |
12
|
|
|
use Nette\Http\IResponse as HttpResponse; |
13
|
|
|
use Nette\Http\Session; |
14
|
|
|
use Nextras\Application\UI\SecuredLinksControlTrait; |
15
|
|
|
use Psr\Log\LoggerAwareInterface; |
16
|
|
|
use Psr\Log\LoggerAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @method void onResponse(IResponse $response) |
20
|
|
|
*/ |
21
|
|
|
class ApproveControl extends Control implements LoggerAwareInterface |
22
|
|
|
{ |
23
|
|
|
use LoggerAwareTrait; |
24
|
|
|
use Psr7Trait; |
25
|
|
|
use SecuredLinksControlTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var callable[] |
29
|
|
|
*/ |
30
|
|
|
public $onResponse; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AuthorizationRequest |
34
|
|
|
*/ |
35
|
|
|
private $authorizationRequest; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AuthorizationServer |
39
|
|
|
*/ |
40
|
|
|
private $authorizationServer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Session |
44
|
|
|
*/ |
45
|
|
|
private $session; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $templateFile; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AuthorizationServer $authorizationServer |
54
|
|
|
* @param Session $session |
55
|
|
|
* @param AuthorizationRequest $authorizationRequest |
56
|
|
|
*/ |
57
|
|
|
public function __construct(AuthorizationServer $authorizationServer, Session $session, AuthorizationRequest $authorizationRequest) |
58
|
|
|
{ |
59
|
|
|
$this->authorizationServer = $authorizationServer; |
60
|
|
|
$this->session = $session; |
61
|
|
|
$this->authorizationRequest = $authorizationRequest; |
62
|
|
|
$this->templateFile = __DIR__ . '/templates/approve.latte'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function render() |
66
|
|
|
{ |
67
|
|
|
$this->template->setFile($this->templateFile); |
|
|
|
|
68
|
|
|
$this->template->authorizationRequest = $this->authorizationRequest; |
|
|
|
|
69
|
|
|
$this->template->render(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @secured |
74
|
|
|
*/ |
75
|
|
|
public function handleApprove() |
76
|
|
|
{ |
77
|
|
|
$this->authorizationRequest->setAuthorizationApproved(true); |
78
|
|
|
$this->completeAuthorizationRequest(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @secured |
83
|
|
|
*/ |
84
|
|
|
public function handleDeny() |
85
|
|
|
{ |
86
|
|
|
$this->authorizationRequest->setAuthorizationApproved(false); |
87
|
|
|
$this->completeAuthorizationRequest(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function completeAuthorizationRequest() |
91
|
|
|
{ |
92
|
|
|
$this->session->getSection(OAuth2Presenter::SESSION_NAMESPACE)->remove(); |
93
|
|
|
|
94
|
|
|
$response = $this->createResponse(); |
95
|
|
|
try { |
96
|
|
|
$this->onResponse($this->authorizationServer->completeAuthorizationRequest($this->authorizationRequest, $response)); |
|
|
|
|
97
|
|
|
|
98
|
|
|
} catch (AbortException $e) { |
99
|
|
|
throw $e; |
100
|
|
|
|
101
|
|
|
} catch (OAuthServerException $e) { |
102
|
|
|
$this->onResponse($e->generateHttpResponse($response)); |
|
|
|
|
103
|
|
|
|
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
if ($this->logger) { |
106
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
107
|
|
|
} |
108
|
|
|
$body = $this->createStream(); |
109
|
|
|
$body->write('Unknown error'); |
110
|
|
|
$this->onResponse($response->withStatus(HttpResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body)); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $file |
116
|
|
|
*/ |
117
|
|
|
public function setTemplateFile(string $file) |
118
|
|
|
{ |
119
|
|
|
$this->templateFile = $file; |
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@property
annotation 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.