|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package cellcote/laravel-proxify |
|
5
|
|
|
* @author Michele Andreoli <michi.andreoli[at]gmail.com> |
|
6
|
|
|
* @copyright Copyright (c) Michele Andreoli |
|
7
|
|
|
* @author Rik Schreurs <rik.schreurs[at]mail.com> |
|
8
|
|
|
* @copyright Copyright (c) Rik Schreurs |
|
9
|
|
|
* @license http://mit-license.org/ |
|
10
|
|
|
* @link https://github.com/cellcote/laravel-proxify |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Cellcote\LaravelProxify\Managers; |
|
14
|
|
|
|
|
15
|
|
|
use Cellcote\LaravelProxify\ProxyAux; |
|
16
|
|
|
use Cellcote\LaravelProxify\Models\ProxyResponse; |
|
17
|
|
|
use GuzzleHttp\Client; |
|
18
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
19
|
|
|
use Cellcote\LaravelProxify\Exceptions\MissingClientSecretException; |
|
20
|
|
|
|
|
21
|
|
|
class RequestManager { |
|
22
|
|
|
|
|
23
|
|
|
private $uri = null; |
|
24
|
|
|
private $method = null; |
|
25
|
|
|
private $callMode = null; |
|
26
|
|
|
private $clientSecrets = null; |
|
27
|
|
|
private $cookieManager = null; |
|
28
|
|
|
private $useHeader = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $callMode |
|
32
|
|
|
* @param CookieManager $cookieManager |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($uri, $method, $clientSecrets, $callMode, $cookieManager) { |
|
35
|
|
|
$this->uri = $uri; |
|
36
|
|
|
$this->method = $method; |
|
37
|
|
|
$this->clientSecrets = $clientSecrets; |
|
38
|
|
|
$this->callMode = $callMode; |
|
39
|
|
|
$this->cookieManager = $cookieManager; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function enableHeader() { |
|
43
|
|
|
$this->useHeader = true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $inputs |
|
48
|
|
|
* @param $parsedCookie |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function executeRequest($inputs, $parsedCookie) { |
|
52
|
|
|
$cookie = null; |
|
53
|
|
|
switch ($this->callMode) { |
|
54
|
|
|
case ProxyAux::MODE_LOGIN: |
|
55
|
|
|
$inputs = $this->addLoginExtraParams($inputs); |
|
56
|
|
|
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
|
57
|
|
|
|
|
58
|
|
|
$clientId = (array_key_exists(ProxyAux::CLIENT_ID, $inputs)) ? $inputs[ProxyAux::CLIENT_ID] : null; |
|
59
|
|
|
$content = $proxyResponse->getContent(); |
|
60
|
|
|
$content = ProxyAux::addQueryValue($content, ProxyAux::COOKIE_URI, $this->uri); |
|
61
|
|
|
$content = ProxyAux::addQueryValue($content, ProxyAux::COOKIE_METHOD, $this->method); |
|
62
|
|
|
$content = ProxyAux::addQueryValue($content, ProxyAux::CLIENT_ID, $clientId); |
|
63
|
|
|
|
|
64
|
|
|
$cookie = $this->cookieManager->createCookie($content); |
|
65
|
|
|
break; |
|
66
|
|
|
case ProxyAux::MODE_TOKEN: |
|
67
|
|
|
$inputs = $this->addTokenExtraParams($inputs, $parsedCookie); |
|
68
|
|
|
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
|
69
|
|
|
|
|
70
|
|
|
//Get a new access token from refresh token if exists |
|
71
|
|
|
$cookie = null; |
|
72
|
|
|
if ($proxyResponse->getStatusCode() != 200) { |
|
73
|
|
|
if (array_key_exists(ProxyAux::REFRESH_TOKEN, $parsedCookie)) { |
|
74
|
|
|
$ret = $this->tryRefreshToken($inputs, $parsedCookie); |
|
75
|
|
|
} else { |
|
76
|
|
|
$cookie = $this->cookieManager->destroyCookie(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$proxyResponse = (isset($ret)) ? $ret['response'] : $proxyResponse; |
|
81
|
|
|
$cookie = (isset($ret)) ? $ret['cookie'] : $cookie; |
|
82
|
|
|
break; |
|
83
|
|
|
default: |
|
84
|
|
|
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return array( |
|
88
|
|
|
'response' => $proxyResponse, |
|
89
|
|
|
'cookie' => $cookie |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $inputs |
|
95
|
|
|
* @param $parsedCookie |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
private function tryRefreshToken($inputs, $parsedCookie) { |
|
99
|
|
|
$this->callMode = ProxyAux::MODE_REFRESH; |
|
100
|
|
|
|
|
101
|
|
|
//Get a new access token from refresh token |
|
102
|
|
|
$inputs = $this->removeTokenExtraParams($inputs); |
|
103
|
|
|
$params = $this->addRefreshExtraParams(array(), $parsedCookie); |
|
104
|
|
|
$proxyResponse = $this->replicateRequest($parsedCookie[ProxyAux::COOKIE_METHOD], $parsedCookie[ProxyAux::COOKIE_URI], $params); |
|
105
|
|
|
|
|
106
|
|
|
$content = $proxyResponse->getContent(); |
|
107
|
|
|
if ($proxyResponse->getStatusCode() === 200 && array_key_exists(ProxyAux::ACCESS_TOKEN, $content)) { |
|
108
|
|
|
$this->callMode = ProxyAux::MODE_TOKEN; |
|
109
|
|
|
$parsedCookie[ProxyAux::ACCESS_TOKEN] = $content[ProxyAux::ACCESS_TOKEN]; |
|
110
|
|
|
$parsedCookie[ProxyAux::REFRESH_TOKEN] = $content[ProxyAux::REFRESH_TOKEN]; |
|
111
|
|
|
|
|
112
|
|
|
$inputs = $this->addTokenExtraParams($inputs, $parsedCookie); |
|
113
|
|
|
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
|
114
|
|
|
|
|
115
|
|
|
//Set a new cookie with updated access token and refresh token |
|
116
|
|
|
$cookie = $this->cookieManager->createCookie($parsedCookie); |
|
117
|
|
|
} else { |
|
118
|
|
|
$cookie = $this->cookieManager->destroyCookie(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return array( |
|
122
|
|
|
'response' => $proxyResponse, |
|
123
|
|
|
'cookie' => $cookie |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $method |
|
129
|
|
|
* @param $uri |
|
130
|
|
|
* @param $inputs |
|
131
|
|
|
* @return ProxyResponse |
|
132
|
|
|
*/ |
|
133
|
|
|
private function replicateRequest($method, $uri, $inputs) { |
|
134
|
|
|
$guzzleResponse = $this->sendGuzzleRequest($method, $uri, $inputs); |
|
135
|
|
|
$proxyResponse = new ProxyResponse($guzzleResponse->getStatusCode(), $guzzleResponse->getReasonPhrase(), $guzzleResponse->getProtocolVersion(), $this->getResponseContent($guzzleResponse)); |
|
136
|
|
|
|
|
137
|
|
|
return $proxyResponse; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface $response |
|
142
|
|
|
* @return mixed |
|
143
|
|
|
*/ |
|
144
|
|
|
private function getResponseContent($response) { |
|
145
|
|
|
switch ($response->getHeader('content-type')) { |
|
146
|
|
|
case 'application/json': |
|
147
|
|
|
return $response->json(); |
|
148
|
|
|
case 'text/xml': |
|
149
|
|
|
case 'application/xml': |
|
150
|
|
|
return $response->xml(); |
|
151
|
|
|
default: |
|
152
|
|
|
return $response->getBody(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param $method |
|
158
|
|
|
* @param $uriVal |
|
159
|
|
|
* @param $inputs |
|
160
|
|
|
* @return \GuzzleHttp\Message\ResponseInterface |
|
161
|
|
|
*/ |
|
162
|
|
|
private function sendGuzzleRequest($method, $uriVal, $inputs) { |
|
163
|
|
|
$options = array(); |
|
164
|
|
|
$client = new Client(); |
|
165
|
|
|
|
|
166
|
|
|
if ($this->callMode === ProxyAux::MODE_TOKEN && $this->useHeader === true) { |
|
167
|
|
|
$accessToken = ProxyAux::getQueryValue($inputs, ProxyAux::ACCESS_TOKEN); |
|
|
|
|
|
|
168
|
|
|
$inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::ACCESS_TOKEN); |
|
169
|
|
|
$options = array_add($options, 'headers', [ProxyAux::HEADER_AUTH => 'Bearer ' . $accessToken]); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if ($method === 'GET') { |
|
173
|
|
|
$options = array_add($options, 'query', $inputs); |
|
174
|
|
|
} else { |
|
175
|
|
|
$options = array_add($options, 'body', $inputs); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$request = $client->createRequest($method, $uriVal, $options); |
|
179
|
|
|
|
|
180
|
|
|
try { |
|
181
|
|
|
$response = $client->send($request); |
|
182
|
|
|
} catch (ClientException $ex) { |
|
183
|
|
|
$response = $ex->getResponse(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $response; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param $clientId |
|
191
|
|
|
* @return array |
|
192
|
|
|
* @throws MissingClientSecretException |
|
193
|
|
|
*/ |
|
194
|
|
|
private function getClientInfo($clientId) { |
|
195
|
|
|
$info = ['id' => null, 'secret' => null]; |
|
196
|
|
|
|
|
197
|
|
|
if (isset($clientId)) { |
|
198
|
|
|
if (!array_key_exists($clientId, $this->clientSecrets)) { |
|
199
|
|
|
throw new MissingClientSecretException($clientId); |
|
200
|
|
|
} |
|
201
|
|
|
$info['id'] = $clientId; |
|
202
|
|
|
$info['secret'] = $this->clientSecrets[$clientId]; |
|
203
|
|
|
} else if (count($this->clientSecrets) >= 1) { |
|
204
|
|
|
$firstKey = key($this->clientSecrets); |
|
205
|
|
|
$info['id'] = $firstKey; |
|
206
|
|
|
$info['secret'] = $this->clientSecrets[$firstKey]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $info; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param $inputs |
|
214
|
|
|
* @return array |
|
215
|
|
|
*/ |
|
216
|
|
|
private function addLoginExtraParams($inputs) { |
|
217
|
|
|
//Get client secret key |
|
218
|
|
|
$clientId = (array_key_exists(ProxyAux::CLIENT_ID, $inputs)) ? $inputs[ProxyAux::CLIENT_ID] : null; |
|
219
|
|
|
$clientInfo = $this->getClientInfo($clientId); |
|
220
|
|
|
|
|
221
|
|
|
if (isset($clientInfo['id'])) { |
|
222
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_ID, $clientInfo['id']); |
|
223
|
|
|
} |
|
224
|
|
|
if (isset($clientInfo['secret'])) { |
|
225
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_SECRET, $clientInfo['secret']); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $inputs; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param $inputs |
|
233
|
|
|
* @param $parsedCookie |
|
234
|
|
|
* @return array |
|
235
|
|
|
*/ |
|
236
|
|
|
private function addTokenExtraParams($inputs, $parsedCookie) { |
|
237
|
|
|
if (isset($parsedCookie[ProxyAux::ACCESS_TOKEN])) { |
|
238
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::ACCESS_TOKEN, $parsedCookie[ProxyAux::ACCESS_TOKEN]); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $inputs; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param $inputs |
|
246
|
|
|
* @param $parsedCookie |
|
247
|
|
|
* @return array |
|
248
|
|
|
*/ |
|
249
|
|
|
private function addRefreshExtraParams($inputs, $parsedCookie) { |
|
250
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::GRANT_TYPE, ProxyAux::REFRESH_TOKEN); |
|
251
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::REFRESH_TOKEN, $parsedCookie[ProxyAux::REFRESH_TOKEN]); |
|
252
|
|
|
if (isset($parsedCookie[ProxyAux::CLIENT_ID])) { |
|
253
|
|
|
$clientInfo = $this->getClientInfo($parsedCookie[ProxyAux::CLIENT_ID]); |
|
254
|
|
|
if (isset($clientInfo['id'])) { |
|
255
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_ID, $clientInfo['id']); |
|
256
|
|
|
} |
|
257
|
|
|
if (isset($clientInfo['secret'])) { |
|
258
|
|
|
$inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_SECRET, $clientInfo['secret']); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return $inputs; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param $inputs |
|
267
|
|
|
* @return array |
|
268
|
|
|
*/ |
|
269
|
|
|
private function removeTokenExtraParams($inputs) { |
|
270
|
|
|
$inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::ACCESS_TOKEN); |
|
271
|
|
|
|
|
272
|
|
|
return $inputs; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.