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 Nextras\Application\UI\SecuredLinksControlTrait; |
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
15
|
|
|
use Psr\Log\LoggerAwareTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method void onAuthorizationComplete() |
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 $onAuthorizationComplete; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var callable[] |
34
|
|
|
*/ |
35
|
|
|
public $onResponse; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AuthorizationRequest |
39
|
|
|
*/ |
40
|
|
|
private $authorizationRequest; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var AuthorizationServer |
44
|
|
|
*/ |
45
|
|
|
private $authorizationServer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $templateFile; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AuthorizationServer $authorizationServer |
54
|
|
|
* @param AuthorizationRequest $authorizationRequest |
55
|
|
|
*/ |
56
|
|
|
public function __construct(AuthorizationServer $authorizationServer, AuthorizationRequest $authorizationRequest) |
57
|
|
|
{ |
58
|
|
|
$this->authorizationServer = $authorizationServer; |
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->onAuthorizationComplete(); |
91
|
|
|
|
92
|
|
|
$response = $this->createResponse(); |
93
|
|
|
try { |
94
|
|
|
$this->onResponse($this->authorizationServer->completeAuthorizationRequest($this->authorizationRequest, $response)); |
|
|
|
|
95
|
|
|
|
96
|
|
|
} catch (AbortException $e) { |
97
|
|
|
throw $e; |
98
|
|
|
|
99
|
|
|
} catch (OAuthServerException $e) { |
100
|
|
|
$this->onResponse($e->generateHttpResponse($response)); |
|
|
|
|
101
|
|
|
|
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
if ($this->logger) { |
104
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
105
|
|
|
} |
106
|
|
|
$body = $this->createStream(); |
107
|
|
|
$body->write('Unknown error'); |
108
|
|
|
$this->onResponse($response->withStatus(HttpResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $file |
114
|
|
|
*/ |
115
|
|
|
public function setTemplateFile(string $file) |
116
|
|
|
{ |
117
|
|
|
$this->templateFile = $file; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.