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 ( 281d14...808ba3 )
by Cees-Jan
11:23 queued 01:45
created

Oauth1Middleware::validateOptions()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 8
nop 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Oauth1\Middleware;
4
5
use ApiClients\Foundation\Middleware\MiddlewareInterface;
6
use ApiClients\Foundation\Middleware\PostTrait;
7
use ApiClients\Foundation\Oauth1\Options;
8
use JacobKiers\OAuth\Consumer\ConsumerInterface;
9
use JacobKiers\OAuth\Request\Request as OAuthRequest;
10
use JacobKiers\OAuth\SignatureMethod\SignatureMethodInterface;
11
use JacobKiers\OAuth\Token\TokenInterface;
12
use Psr\Http\Message\RequestInterface;
13
use React\EventLoop\LoopInterface;
14
use React\Promise\CancellablePromiseInterface;
15
use function React\Promise\resolve;
16
use function GuzzleHttp\Psr7\parse_query;
17
use function WyriHaximus\React\futurePromise;
18
19
class Oauth1Middleware implements MiddlewareInterface
20
{
21
    use PostTrait;
22
23
    /**
24
     * @var LoopInterface
25
     */
26
    private $loop;
27
28
    /**
29
     * @param LoopInterface $loop
30
     */
31
    public function __construct(LoopInterface $loop)
32
    {
33
        $this->loop = $loop;
34
    }
35
36
    /**
37
     * @param RequestInterface $request
38
     * @param array $options
39
     * @return CancellablePromiseInterface
40
     */
41
    public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
42
    {
43
        if (!$this->validateOptions($options)) {
44
            return resolve($request);
45
        }
46
47
        return futurePromise($this->loop, [$request, $options])->then(function ($args) {
48
            return resolve($this->signRequest(...$args));
0 ignored issues
show
Bug introduced by
The call to signRequest() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
49
        });
50
    }
51
52
    private function validateOptions(array $options): bool
53
    {
54
        if (!isset($options[self::class])) {
55
            return false;
56
        }
57
58
        if (!isset($options[self::class][Options::CONSUMER])) {
59
            return false;
60
        }
61
62
        if (!($options[self::class][Options::CONSUMER] instanceof ConsumerInterface)) {
63
            return false;
64
        }
65
66
        if (!isset($options[self::class][Options::TOKEN])) {
67
            return false;
68
        }
69
70
        if (!($options[self::class][Options::TOKEN] instanceof TokenInterface)) {
71
            return false;
72
        }
73
74
        if (!isset($options[self::class][Options::SIGNATURE_METHOD])) {
75
            return false;
76
        }
77
78
        if (!($options[self::class][Options::SIGNATURE_METHOD] instanceof SignatureMethodInterface)) {
79
            return false;
80
        }
81
82
        return true;
83
    }
84
85
    private function signRequest(RequestInterface $request, array $options): RequestInterface
86
    {
87
        $oauthRequest = OAuthRequest::fromConsumerAndToken(
88
            $options[self::class][Options::CONSUMER],
89
            $options[self::class][Options::TOKEN],
90
            $request->getMethod(),
91
            (string)$request->getUri(),
92
            $this->extractParamsFromQuery(
93
                $request->getUri()->getQuery()
94
            )
95
        );
96
        $oauthRequest->setParameter('oauth_version', '1.0', false);
97
        $oauthRequest->signRequest(
98
            $options[self::class][Options::SIGNATURE_METHOD],
99
            $options[self::class][Options::CONSUMER],
100
            $options[self::class][Options::TOKEN]
101
        );
102
103
        return $request->withAddedHeader(
104
            'Authorization',
105
            trim(substr($oauthRequest->toHeader(), 15))
106
        );
107
    }
108
109
    private function extractParamsFromQuery(string $query): array
110
    {
111
        $params = parse_query($query);
112
113
        uksort($params, 'strcmp');
114
115
        foreach ($params as $key => $value) {
116
            if ($value !== null) {
117
                continue;
118
            }
119
120
            unset($params[$key]);
121
        }
122
123
        return $params;
124
    }
125
}
126