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 Lookyman\Nette\OAuth2\Server\Psr7\ApplicationPsr7ResponseInterface; |
10
|
|
|
use Lookyman\Nette\OAuth2\Server\RedirectConfig; |
11
|
|
|
use Lookyman\Nette\OAuth2\Server\Storage\IAuthorizationRequestSerializer; |
12
|
|
|
use Nette\Application\AbortException; |
13
|
|
|
use Nette\Application\UI\Presenter; |
14
|
|
|
use Nette\Http\IRequest; |
15
|
|
|
use Nette\Http\IResponse; |
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerAwareTrait; |
18
|
|
|
|
19
|
|
|
final class OAuth2Presenter extends Presenter implements LoggerAwareInterface |
20
|
|
|
{ |
21
|
|
|
use LoggerAwareTrait; |
22
|
|
|
use Psr7Trait; |
23
|
|
|
|
24
|
|
|
const SESSION_NAMESPACE = 'nette-oauth2-server'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var IAuthorizationRequestSerializer |
28
|
|
|
* @inject |
29
|
|
|
*/ |
30
|
|
|
public $authorizationRequestSerializer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AuthorizationServer |
34
|
|
|
* @inject |
35
|
|
|
*/ |
36
|
|
|
public $authorizationServer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var RedirectConfig |
40
|
|
|
* @inject |
41
|
|
|
*/ |
42
|
|
|
public $redirectConfig; |
43
|
|
|
|
44
|
|
|
public function actionAccessToken() |
45
|
|
|
{ |
46
|
|
|
$response = $this->createResponse(); |
47
|
|
|
|
48
|
|
View Code Duplication |
if (!$this->getHttpRequest()->isMethod(IRequest::POST)) { |
|
|
|
|
49
|
|
|
$body = $this->createStream(); |
50
|
|
|
$body->write('Method not allowed'); |
51
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
52
|
|
|
$response = $response->withStatus(IResponse::S405_METHOD_NOT_ALLOWED)->withBody($body); |
53
|
|
|
$this->sendResponse($response); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
58
|
|
|
$response = $this->authorizationServer->respondToAccessTokenRequest($this->createServerRequest(), $response); |
59
|
|
|
$this->sendResponse($response); |
60
|
|
|
|
61
|
|
|
} catch (AbortException $e) { |
|
|
|
|
62
|
|
|
throw $e; |
63
|
|
|
|
64
|
|
|
} catch (OAuthServerException $e) { |
|
|
|
|
65
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
66
|
|
|
$response = $e->generateHttpResponse($response); |
67
|
|
|
$this->sendResponse($response); |
68
|
|
|
|
69
|
|
|
} catch (\Throwable $e) { |
70
|
|
|
if ($this->logger) { |
71
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
72
|
|
|
} |
73
|
|
|
$body = $this->createStream(); |
74
|
|
|
$body->write('Unknown error'); |
75
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
76
|
|
|
$response = $response->withStatus(IResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body); |
77
|
|
|
$this->sendResponse($response); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function actionAuthorize() |
82
|
|
|
{ |
83
|
|
|
$response = $this->createResponse(); |
84
|
|
|
|
85
|
|
View Code Duplication |
if (!$this->getHttpRequest()->isMethod(IRequest::GET)) { |
|
|
|
|
86
|
|
|
$body = $this->createStream(); |
87
|
|
|
$body->write('Method not allowed'); |
88
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
89
|
|
|
$response = $response->withStatus(IResponse::S405_METHOD_NOT_ALLOWED)->withBody($body); |
90
|
|
|
$this->sendResponse($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$this->getSession(self::SESSION_NAMESPACE)->authorizationRequest = $this->authorizationRequestSerializer->serialize( |
95
|
|
|
$this->authorizationServer->validateAuthorizationRequest($this->createServerRequest()) |
96
|
|
|
); |
97
|
|
|
if (!$this->getUser()->isLoggedIn()) { |
98
|
|
|
$this->redirectConfig->redirectToLoginDestination($this); |
99
|
|
|
} |
100
|
|
|
$this->redirectConfig->redirectToApproveDestination($this); |
101
|
|
|
|
102
|
|
|
} catch (AbortException $e) { |
|
|
|
|
103
|
|
|
throw $e; |
104
|
|
|
|
105
|
|
|
} catch (OAuthServerException $e) { |
|
|
|
|
106
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
107
|
|
|
$response = $e->generateHttpResponse($response); |
108
|
|
|
$this->sendResponse($response); |
109
|
|
|
|
110
|
|
|
} catch (\Throwable $e) { |
111
|
|
|
if ($this->logger) { |
112
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
113
|
|
|
} |
114
|
|
|
$body = $this->createStream(); |
115
|
|
|
$body->write('Unknown error'); |
116
|
|
|
/** @var ApplicationPsr7ResponseInterface $response */ |
117
|
|
|
$response = $response->withStatus(IResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body); |
118
|
|
|
$this->sendResponse($response); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.