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.

AuthenticationMiddleware   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 168
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setNonceGenerator() 0 4 1
A setTimestampGenerator() 0 4 1
A __invoke() 0 8 1
A onBefore() 0 9 2
B createHeaders() 0 24 1
A createDataElement() 0 14 2
A createOAuthHeaders() 0 11 1
1
<?php
2
3
namespace CMPayments\GuzzlePSPAuthenticationMiddleware;
4
5
use Psr\Http\Message\RequestInterface;
6
7
/**
8
 * Class ConnectionMiddleware
9
 *
10
 * @package CMPayments\GuzzlePSPAuthenticationMiddleware
11
 */
12
class AuthenticationMiddleware
13
{
14
    /**
15
     * @var string
16
     */
17
    private $key;
18
19
    /**
20
     * @var string
21
     */
22
    private $secret;
23
24
    /**
25
     * @var NonceGeneratorInterface
26
     */
27
    private $nonceGenerator;
28
29
    /**
30
     * @var TimestampGeneratorInterface
31
     */
32
    private $timestampGenerator;
33
34
    /**
35
     * AuthenticationMiddleware constructor.
36
     *
37
     * @param string $key    OAuth-Consumer-Key
38
     * @param string $secret OAuth-Consumer-Secret
39
     */
40 2
    public function __construct($key, $secret)
41
    {
42 2
        $this->key = $key;
43 2
        $this->secret = $secret;
44
45 2
        $this->nonceGenerator = new RandomNonceGenerator();
46 2
        $this->timestampGenerator = new RandomTimestampGenerator();
47 2
    }
48
49
    /**
50
     * Overwrite the nonceGenerator-class. Used for the unit-test
51
     *
52
     * @param NonceGeneratorInterface $nonceGenerator
53
     */
54 2
    public function setNonceGenerator($nonceGenerator)
55
    {
56 2
        $this->nonceGenerator = $nonceGenerator;
57 2
    }
58
59
    /**
60
     * Overwrite the timestampGenerator-class. Used for the unit-test
61
     *
62
     * @param TimestampGeneratorInterface $timestampGenerator
63
     */
64 2
    public function setTimestampGenerator($timestampGenerator)
65
    {
66 2
        $this->timestampGenerator = $timestampGenerator;
67 2
    }
68
69
70
    /**
71
     * This function is called by Guzzle in order to initiate the middleware.
72
     *
73
     * @param callable $handler Representation of an outgoing, client-side request.
74
     *
75
     * @return \Closure
76
     */
77
    public function __invoke($handler)
78
    {
79 2
        return function ($request, array $options) use ($handler) {
80 2
            $request = $this->onBefore($request);
81
82 2
            return $handler($request, $options);
83 2
        };
84
    }
85
86
    /**
87
     * Before executing the request, add the extra headers
88
     *
89
     * @param RequestInterface $request
90
     *
91
     * @return RequestInterface
92
     */
93 2
    private function onBefore(RequestInterface $request)
94
    {
95 2
        $headers = $this->createHeaders($request);
96 2
        foreach ($headers as $headerKey => $headerValue) {
97 2
            $request = $request->withHeader($headerKey, $headerValue);
98 2
        }
99
100 2
        return $request;
101
    }
102
103
    /**
104
     * Calculate/determine the required headers for the request.
105
     *
106
     * @param RequestInterface $request
107
     *
108
     * @return string[] The headers for the request
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
109
     */
110 2
    private function createHeaders(RequestInterface $request)
111
    {
112 2
        $nonce = $this->nonceGenerator->generate();
113 2
        $timestamp = $this->timestampGenerator->generate();
114
115
        /** @var array $data */
116 2
        $data = $this->createDataElement($request, $nonce, $timestamp);
117
118 2
        $payload = $request->getMethod() .
119 2
            '&' . rawurlencode((string)$request->getUri()) .
120 2
            '&' . implode(rawurlencode('&'), $data);
121
122 2
        $signkey = rawurlencode($this->key) . '&' . rawurlencode($this->secret);
123 2
        $hash = rawurlencode(base64_encode(hash_hmac('sha256', $payload, $signkey)));
124
125
        /** @var array $oauth_header */
126 2
        $oauth_header = $this->createOAuthHeaders($nonce, $timestamp, $hash);
127
128 2
        $header = [];
129 2
        $header['Content-type'] = 'application/json';
130 2
        $header['Authorization'] = 'OAuth ' . implode(', ', $oauth_header);
131
132 2
        return $header;
133
    }
134
135
    /**
136
     * Create the data array for the payload
137
     *
138
     * @param RequestInterface $request
139
     * @param string           $nonce The none which is generated
140
     * @param int              $timestamp The timestamp which is generated
141
     *
142
     * @return string[] The data-array
143
     */
144 2
    private function createDataElement(RequestInterface $request, $nonce, $timestamp)
145
    {
146 2
        $data = [];
147 2
        if ($request->getMethod() === 'POST') {
148 1
            $data[] = rawurlencode($request->getBody()->getContents());
149 1
        }
150 2
        $data[] = rawurlencode('oauth_consumer_key=' . $this->key);
151 2
        $data[] = rawurlencode('oauth_nonce=' . $nonce);
152 2
        $data[] = rawurlencode('oauth_signature_method=HMAC-SHA256');
153 2
        $data[] = rawurlencode('oauth_timestamp=' . $timestamp);
154 2
        $data[] = rawurlencode('oauth_version=1.0');
155
156 2
        return $data;
157
    }
158
159
    /**
160
     * Create the oAuth headers
161
     *
162
     * @param string $nonce The generated nonce
163
     * @param int    $timestamp The generated timestamp
164
     * @param string $hash The created hash
165
     *
166
     * @return string[] The headers for the request
167
     */
168 2
    private function createOAuthHeaders($nonce, $timestamp, $hash)
169
    {
170 2
        $oauth_header = [];
171 2
        $oauth_header[] = 'oauth_consumer_key="' . $this->key . '"';
172 2
        $oauth_header[] = 'oauth_nonce="' . $nonce . '"';
173 2
        $oauth_header[] = 'oauth_signature="' . $hash . '"';
174 2
        $oauth_header[] = 'oauth_signature_method="HMAC-SHA256"';
175 2
        $oauth_header[] = 'oauth_timestamp="' . $timestamp . '"';
176 2
        $oauth_header[] = 'oauth_version="1.0"';
177 2
        return $oauth_header;
178
    }
179
}
180