GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c2e63e...e620da )
by Lukáš
12s
created

OAuth2Presenter::actionAuthorize()   C

Complexity

Conditions 7
Paths 44

Size

Total Lines 33
Code Lines 22

Duplication

Lines 5
Ratio 15.15 %

Importance

Changes 0
Metric Value
dl 5
loc 33
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 22
nc 44
nop 0
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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));
0 ignored issues
show
Documentation introduced by
$this->authorizationServ...erRequest(), $response) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Nette\Application\IResponse>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
57
		} catch (AbortException $e) {
58
			throw $e;
59
60
		} catch (OAuthServerException $e) {
61
			$this->sendResponse($e->generateHttpResponse($response));
0 ignored issues
show
Documentation introduced by
$e->generateHttpResponse($response) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Nette\Application\IResponse>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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));
0 ignored issues
show
Documentation introduced by
$e->generateHttpResponse($response) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Nette\Application\IResponse>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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