1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ghc\Rosetta\Connectors; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Ghc\Rosetta\Messages\HttpResponse; |
7
|
|
|
use gomes81\GuzzleHttp\Subscriber\CookieAuth; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\HandlerStack; |
10
|
|
|
use GuzzleHttp\Subscriber\Oauth\Oauth1; |
11
|
|
|
use kamermans\OAuth2\OAuth2Middleware; |
12
|
|
|
|
13
|
|
|
class Http extends Connector |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Basic HTTP auth. |
17
|
|
|
* Config example: ['type' => self::AUTH_BASIC, 'username' => 'USER', 'password' => 'PASSWD']. |
18
|
|
|
*/ |
19
|
|
|
const AUTH_BASIC = 'basic'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Digest HTTP auth. |
23
|
|
|
* Config example: ['type' => self::AUTH_DIGEST, 'username' => 'USER', 'password' => 'PASSWD']. |
24
|
|
|
*/ |
25
|
|
|
const AUTH_DIGEST = 'digest'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Microsoft NTLM HTTP auth. |
29
|
|
|
* Config example: ['type' => self::AUTH_NTLM, 'username' => 'USER', 'password' => 'PASSWD']. |
30
|
|
|
*/ |
31
|
|
|
const AUTH_NTLM = 'ntlm'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Oauth1 auth. |
35
|
|
|
* Config example: ['type' => self::AUTH_OAUTH1, 'consumer_key' => 'my_key', 'consumer_secret' => 'my_secret', 'token' => 'my_token', 'token_secret' => 'my_token_secret']. |
36
|
|
|
* |
37
|
|
|
* @link https://github.com/guzzle/oauth-subscriber |
38
|
|
|
*/ |
39
|
|
|
const AUTH_OAUTH1 = 'oauth1'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Oauth2 auth. |
43
|
|
|
* Config example: ['type' => self::AUTH_OAUTH2, "uri" => "access_token_uri", "grant_type" => "client_credentials", "client_id" => "my_client_id", "client_secret" => "my_client_secret",] |
44
|
|
|
* Grant types: authorization_code, client_credentials, password_credentials, refresh_token. |
45
|
|
|
* |
46
|
|
|
* @link https://github.com/kamermans/guzzle-oauth2-subscriber |
47
|
|
|
*/ |
48
|
|
|
const AUTH_OAUTH2 = 'oauth2'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Cookie auth. |
52
|
|
|
* Config example: ['type' => self::AUTH_COOKIE, 'uri' => 'form_uri', 'fields' => ['username' => 'USER', 'password' => 'PASSWD', 'foo' => 'bar'], 'method' => 'POST', 'cookies' => 'cookie_string_or_cookie_string_array_or_cookie_jar']. |
53
|
|
|
*/ |
54
|
|
|
const AUTH_COOKIE = 'cookie'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Custom handler stack auth. |
58
|
|
|
* Config example: ['type' => self::AUTH_CUSTOM, 'handler' => $handlerStack', 'auth' => 'authName']. |
59
|
|
|
*/ |
60
|
|
|
const AUTH_CUSTOM = 'custom'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* HTTP client. |
64
|
|
|
* |
65
|
|
|
* @var Client |
66
|
|
|
*/ |
67
|
|
|
protected $client; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Boot Connector. |
71
|
|
|
*/ |
72
|
|
|
protected function boot() |
73
|
|
|
{ |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Client $client |
78
|
|
|
* |
79
|
|
|
* @return Http |
80
|
|
|
*/ |
81
|
|
|
public function setClient($client) |
82
|
|
|
{ |
83
|
|
|
$this->client = $client; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Client |
90
|
|
|
*/ |
91
|
|
|
public function getClient() |
92
|
|
|
{ |
93
|
|
|
if (!$this->client) { |
94
|
|
|
$this->bootClient(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->client; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Client |
102
|
|
|
*/ |
103
|
|
|
public function bootClient() |
104
|
|
|
{ |
105
|
|
|
if ($authConfig = $this->config->get('auth_config')) { |
106
|
|
|
$this->config->set('auth_config', null); |
107
|
|
|
$this->setAuth($authConfig); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->setClient(new Client($this->config->all())); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $config |
115
|
|
|
* |
116
|
|
|
* @throws Exception |
117
|
|
|
* |
118
|
|
|
* @uses getClientConfigAuthBasic |
119
|
|
|
* @uses getClientConfigAuthDigest |
120
|
|
|
* @uses getClientConfigAuthNtlm |
121
|
|
|
* @uses getClientConfigAuthOauth1 |
122
|
|
|
* @uses getClientConfigAuthOauth2 |
123
|
|
|
* @uses getClientConfigAuthCookie |
124
|
|
|
* @uses getClientConfigAuthCustom |
125
|
|
|
*/ |
126
|
|
|
public function setAuth($config = []) |
127
|
|
|
{ |
128
|
|
|
$type = $config['type']; |
129
|
|
|
unset($config['type']); |
130
|
|
|
|
131
|
|
|
$method = 'getClientConfigAuth'.ucfirst($type); |
132
|
|
|
if (!method_exists($this, $method)) { |
133
|
|
|
throw new Exception("Invalid auth type: $type"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** @var array $clientConfig */ |
137
|
|
|
$clientConfig = call_user_func([$this, $method], $config); |
138
|
|
|
|
139
|
|
|
foreach ($clientConfig as $key => $value) { |
140
|
|
|
$this->setConfig($key, $value); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $config |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
private function getClientConfigAuthBasic($config = []) |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
'auth' => [ |
153
|
|
|
$config['username'], |
154
|
|
|
$config['password'], |
155
|
|
|
], |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param array $config |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
private function getClientConfigAuthDigest($config = []) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
return [ |
167
|
|
|
'auth' => [ |
168
|
|
|
$config['username'], |
169
|
|
|
$config['password'], |
170
|
|
|
'digest', |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $config |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
private function getClientConfigAuthNtlm($config = []) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
|
|
'auth' => [ |
184
|
|
|
$config['username'], |
185
|
|
|
$config['password'], |
186
|
|
|
'ntlm', |
187
|
|
|
], |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $config |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
private function getClientConfigAuthOauth1($config = []) |
197
|
|
|
{ |
198
|
|
|
$stack = HandlerStack::create(); |
199
|
|
|
$middleware = new Oauth1($config); |
200
|
|
|
$stack->push($middleware); |
201
|
|
|
$clientConfig['handler'] = $stack; |
|
|
|
|
202
|
|
|
$clientConfig['auth'] = 'oauth'; |
203
|
|
|
|
204
|
|
|
return [ |
205
|
|
|
'auth' => 'oauth', |
206
|
|
|
'handler' => $stack, |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $config |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
private function getClientConfigAuthOauth2($config = []) |
216
|
|
|
{ |
217
|
|
|
$authClient = new Client(['base_uri' => $config['uri']]); |
218
|
|
|
$grantType = '\\kamermans\\OAuth2\\GrantType\\'.studly_case($config['grant_type']); |
219
|
|
|
unset($config['uri']); |
220
|
|
|
unset($config['grant_type']); |
221
|
|
|
/** @var \kamermans\OAuth2\GrantType\GrantTypeInterface $grant_type */ |
222
|
|
|
$grant_type = new $grantType($authClient, $config); |
223
|
|
|
$oauth = new OAuth2Middleware($grant_type); |
224
|
|
|
$stack = HandlerStack::create(); |
225
|
|
|
$stack->push($oauth); |
226
|
|
|
|
227
|
|
|
return [ |
228
|
|
|
'auth' => 'oauth', |
229
|
|
|
'handler' => $stack, |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $config |
235
|
|
|
* |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
|
private function getClientConfigAuthCookie($config = []) |
239
|
|
|
{ |
240
|
|
|
$stack = HandlerStack::create(); |
241
|
|
|
if (!isset($config['cookies'])) { |
242
|
|
|
$config['cookies'] = null; |
243
|
|
|
} |
244
|
|
|
if (!isset($config['method'])) { |
245
|
|
|
$config['method'] = 'POST'; |
246
|
|
|
} |
247
|
|
|
$middleware = new CookieAuth( |
248
|
|
|
$config['uri'], |
249
|
|
|
$config['fields'], |
250
|
|
|
$config['method'], |
251
|
|
|
$config['cookies'] |
252
|
|
|
); |
253
|
|
|
$stack->push($middleware); |
254
|
|
|
|
255
|
|
|
return [ |
256
|
|
|
'auth' => 'cookie', |
257
|
|
|
'handler' => $stack, |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param array $config |
263
|
|
|
* |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
private function getClientConfigAuthCustom($config = []) |
267
|
|
|
{ |
268
|
|
|
return [ |
269
|
|
|
'auth' => $config['auth'], |
270
|
|
|
'handler' => $config['handler'], |
271
|
|
|
]; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param string $uri |
276
|
|
|
* @param array $options |
277
|
|
|
* |
278
|
|
|
* @return mixed |
279
|
|
|
*/ |
280
|
|
|
public function show($uri, $options = []) |
281
|
|
|
{ |
282
|
|
|
return new HttpResponse($this->getClient()->request('GET', $uri, $options)); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param string $uri |
287
|
|
|
* @param null|mixed $data |
288
|
|
|
* @param array $options |
289
|
|
|
* |
290
|
|
|
* @return mixed |
291
|
|
|
*/ |
292
|
|
View Code Duplication |
public function create($uri, $data = null, $options = []) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
if (!is_null($data)) { |
295
|
|
|
$options['form_params'] = $data; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return new HttpResponse($this->getClient()->request('POST', $uri, $options)); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $uri |
303
|
|
|
* @param null|mixed $data |
304
|
|
|
* @param array $options |
305
|
|
|
* |
306
|
|
|
* @return mixed |
307
|
|
|
*/ |
308
|
|
View Code Duplication |
public function update($uri, $data = null, $options = []) |
|
|
|
|
309
|
|
|
{ |
310
|
|
|
if (!is_null($data)) { |
311
|
|
|
$options['form_params'] = $data; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return new HttpResponse($this->getClient()->request('PATCH', $uri, $options)); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param string $uri |
319
|
|
|
* @param null|mixed $data |
320
|
|
|
* @param array $options |
321
|
|
|
* |
322
|
|
|
* @return mixed |
323
|
|
|
*/ |
324
|
|
View Code Duplication |
public function replace($uri, $data = null, $options = []) |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
if (!is_null($data)) { |
327
|
|
|
$options['form_params'] = $data; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return new HttpResponse($this->getClient()->request('PUT', $uri, $options)); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param string $uri |
335
|
|
|
* @param array $options |
336
|
|
|
* |
337
|
|
|
* @return mixed |
338
|
|
|
*/ |
339
|
|
|
public function delete($uri, $options = []) |
340
|
|
|
{ |
341
|
|
|
return new HttpResponse($this->getClient()->request('DELETE', $uri, $options)); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
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.