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 Lookyman\NetteOAuth2Server\RedirectConfig; |
9
|
|
|
use Lookyman\NetteOAuth2Server\Storage\IAuthorizationRequestSerializer; |
10
|
|
|
use Nette\Application\AbortException; |
11
|
|
|
use Nette\Application\UI\Presenter; |
12
|
|
|
use Nette\Http\IRequest; |
13
|
|
|
use Nette\Http\IResponse; |
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
15
|
|
|
use Psr\Log\LoggerAwareTrait; |
16
|
|
|
|
17
|
|
|
class OAuth2Presenter extends Presenter implements LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use LoggerAwareTrait; |
20
|
|
|
use Psr7Trait; |
21
|
|
|
|
22
|
|
|
const SESSION_NAMESPACE = 'nette-oauth2-server'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var IAuthorizationRequestSerializer |
26
|
|
|
* @inject |
27
|
|
|
*/ |
28
|
|
|
public $authorizationRequestSerializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AuthorizationServer |
32
|
|
|
* @inject |
33
|
|
|
*/ |
34
|
|
|
public $authorizationServer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RedirectConfig |
38
|
|
|
* @inject |
39
|
|
|
*/ |
40
|
|
|
public $redirectConfig; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws AbortException |
44
|
|
|
*/ |
45
|
|
|
public function actionAccessToken() |
46
|
|
|
{ |
47
|
|
View Code Duplication |
if (!$this->getHttpRequest()->isMethod(IRequest::POST)) { |
|
|
|
|
48
|
|
|
$body = $this->createStream(); |
49
|
|
|
$body->write('Method not allowed'); |
50
|
|
|
$this->sendResponse($this->createResponse()->withStatus(IResponse::S405_METHOD_NOT_ALLOWED)->withBody($body)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$response = $this->createResponse(); |
54
|
|
|
try { |
55
|
|
|
$this->sendResponse($this->authorizationServer->respondToAccessTokenRequest($this->createServerRequest(), $response)); |
|
|
|
|
56
|
|
|
|
57
|
|
|
} catch (AbortException $e) { |
58
|
|
|
throw $e; |
59
|
|
|
|
60
|
|
|
} catch (OAuthServerException $e) { |
61
|
|
|
$this->sendResponse($e->generateHttpResponse($response)); |
|
|
|
|
62
|
|
|
|
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
if ($this->logger) { |
65
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
66
|
|
|
} |
67
|
|
|
$body = $this->createStream(); |
68
|
|
|
$body->write('Unknown error'); |
69
|
|
|
$this->sendResponse($response->withStatus(IResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws AbortException |
75
|
|
|
*/ |
76
|
|
|
public function actionAuthorize() |
77
|
|
|
{ |
78
|
|
View Code Duplication |
if (!$this->getHttpRequest()->isMethod(IRequest::GET)) { |
|
|
|
|
79
|
|
|
$body = $this->createStream(); |
80
|
|
|
$body->write('Method not allowed'); |
81
|
|
|
$this->sendResponse($this->createResponse()->withStatus(IResponse::S405_METHOD_NOT_ALLOWED)->withBody($body)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$response = $this->createResponse(); |
85
|
|
|
try { |
86
|
|
|
$this->getSession(self::SESSION_NAMESPACE)->authorizationRequest = $this->authorizationRequestSerializer->serialize( |
87
|
|
|
$this->authorizationServer->validateAuthorizationRequest($this->createServerRequest()) |
88
|
|
|
); |
89
|
|
|
if (!$this->getUser()->isLoggedIn()) { |
90
|
|
|
$this->redirect(...$this->redirectConfig->getLoginDestination()); |
91
|
|
|
} |
92
|
|
|
$this->redirect(...$this->redirectConfig->getApproveDestination()); |
93
|
|
|
|
94
|
|
|
} catch (AbortException $e) { |
95
|
|
|
throw $e; |
96
|
|
|
|
97
|
|
|
} catch (OAuthServerException $e) { |
98
|
|
|
$this->sendResponse($e->generateHttpResponse($response)); |
|
|
|
|
99
|
|
|
|
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
if ($this->logger) { |
102
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]); |
103
|
|
|
} |
104
|
|
|
$body = $this->createStream(); |
105
|
|
|
$body->write('Unknown error'); |
106
|
|
|
$this->sendResponse($response->withStatus(IResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.