10
10
use Magento \Framework \Oauth \Helper \Utility ;
11
11
use Magento \TestFramework \Helper \Bootstrap ;
12
12
use Magento \TestFramework \Inspection \Exception ;
13
- use Magento \Framework \ HTTP \ ClientFactory ;
13
+ use Magento \TestFramework \ Authentication \ Rest \ OauthClient \ Signature ;
14
14
15
15
/**
16
16
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
17
*/
18
18
class OauthClient
19
19
{
20
20
/**
21
- * @var \Magento\Framework\ Url
21
+ * @var Url
22
22
*/
23
23
protected Url $ urlProvider ;
24
24
25
25
/**
26
- * @var \Magento\Framework\HTTP\ClientFactory
26
+ * @var CurlClient
27
27
*/
28
- protected ClientFactory $ clientFactory ;
28
+ protected CurlClient $ curlClient ;
29
29
30
30
/**
31
- * @var \Magento\Framework\Oauth\ NonceGeneratorInterface
31
+ * @var NonceGeneratorInterface
32
32
*/
33
33
protected NonceGeneratorInterface $ _nonceGenerator ;
34
34
35
35
/**
36
- * @var \Magento\Framework\Oauth\Helper\ Utility
36
+ * @var Utility
37
37
*/
38
38
private Utility $ _httpUtility ;
39
39
40
+ /**
41
+ * @var Signature
42
+ */
43
+ private Signature $ signature ;
44
+
40
45
/**
41
46
* @var string
42
47
*/
@@ -49,20 +54,23 @@ class OauthClient
49
54
50
55
/**
51
56
* @param Url $urlProvider
52
- * @param ClientFactory $clientFactory
57
+ * @param CurlClient $curlClient
53
58
* @param NonceGeneratorInterface $nonceGenerator
54
59
* @param Utility $utility
60
+ * @param Signature $signature
55
61
*/
56
62
public function __construct (
57
63
Url $ urlProvider ,
58
- ClientFactory $ clientFactory ,
64
+ CurlClient $ curlClient ,
59
65
NonceGeneratorInterface $ nonceGenerator ,
60
- Utility $ utility
66
+ Utility $ utility ,
67
+ Signature $ signature
61
68
) {
62
69
$ this ->urlProvider = $ urlProvider ;
63
- $ this ->clientFactory = $ clientFactory ;
70
+ $ this ->curlClient = $ curlClient ;
64
71
$ this ->_nonceGenerator = $ nonceGenerator ;
65
72
$ this ->_httpUtility = $ utility ;
73
+ $ this ->signature = $ signature ;
66
74
}
67
75
68
76
/**
@@ -116,7 +124,7 @@ public function getRequestToken(): array
116
124
)
117
125
];
118
126
119
- $ responseBody = $ this ->fetchResponse ($ requestUrl , [], $ headers );
127
+ $ responseBody = $ this ->curlClient -> retrieveResponse ($ requestUrl , [], $ headers );
120
128
return $ this ->parseResponseBody ($ responseBody );
121
129
}
122
130
@@ -137,7 +145,7 @@ public function buildAuthorizationHeaderToRequestToken(
137
145
string $ signatureMethod = \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
138
146
string $ httpMethod = 'POST '
139
147
): string {
140
- $ params ['oauth_signature ' ] = $ this ->getSignature (
148
+ $ params ['oauth_signature ' ] = $ this ->signature -> getSignature (
141
149
$ params ,
142
150
$ signatureMethod ,
143
151
$ consumerSecret ,
@@ -175,7 +183,11 @@ public function getAccessToken(array $token, string $verifier): array
175
183
$ bodyParams
176
184
),
177
185
];
178
- $ responseBody = $ this ->fetchResponse ($ this ->getAccessTokenEndpoint (), $ bodyParams , $ authorizationHeader );
186
+ $ responseBody = $ this ->curlClient ->retrieveResponse (
187
+ $ this ->getAccessTokenEndpoint (),
188
+ $ bodyParams ,
189
+ $ authorizationHeader
190
+ );
179
191
return $ this ->parseResponseBody ($ responseBody );
180
192
}
181
193
@@ -207,7 +219,7 @@ public function validateAccessToken(array $token, string $method = 'GET'): array
207
219
208
220
$ headers = array_merge ($ authorizationHeader , $ extraAuthenticationHeaders );
209
221
210
- $ responseBody = $ this ->fetchResponse ($ this ->getTestApiEndpoint (), [], $ headers , $ method );
222
+ $ responseBody = $ this ->curlClient -> retrieveResponse ($ this ->getTestApiEndpoint (), [], $ headers , $ method );
211
223
212
224
return json_decode ($ responseBody );
213
225
}
@@ -241,7 +253,7 @@ public function buildAuthorizationHeaderForAPIRequest(
241
253
$ params = array_merge ($ params , ['oauth_token ' => $ token ['oauth_token ' ]]);
242
254
$ params = array_merge ($ params , $ bodyParams );
243
255
244
- $ params ['oauth_signature ' ] = $ this ->getSignature (
256
+ $ params ['oauth_signature ' ] = $ this ->signature -> getSignature (
245
257
$ params ,
246
258
$ signatureMethod ,
247
259
$ consumerSecret ,
@@ -253,46 +265,6 @@ public function buildAuthorizationHeaderForAPIRequest(
253
265
return $ this ->_httpUtility ->toAuthorizationHeader ($ params );
254
266
}
255
267
256
- /**
257
- * Get the signature
258
- *
259
- * @param array $params
260
- * @param string $signatureMethod
261
- * @param string $consumerSecret
262
- * @param string|null $tokenSecret
263
- * @param string $httpMethod
264
- * @param string $requestUrl
265
- * @return string
266
- */
267
- public function getSignature (
268
- array $ params ,
269
- string $ signatureMethod ,
270
- string $ consumerSecret ,
271
- ?string $ tokenSecret ,
272
- string $ httpMethod ,
273
- string $ requestUrl
274
- ): string {
275
- $ data = parse_url ($ requestUrl );
276
- $ queryStringData = !isset ($ data ['query ' ]) ? [] : array_reduce (
277
- explode ('& ' , $ data ['query ' ]),
278
- function ($ carry , $ item ) {
279
- list ($ key , $ value ) = explode ('= ' , $ item , 2 );
280
- $ carry [rawurldecode ($ key )] = rawurldecode ($ value );
281
- return $ carry ;
282
- },
283
- []
284
- );
285
-
286
- return $ this ->_httpUtility ->sign (
287
- array_merge ($ queryStringData , $ params ),
288
- $ signatureMethod ,
289
- $ consumerSecret ,
290
- $ tokenSecret ,
291
- $ httpMethod ,
292
- $ requestUrl
293
- );
294
- }
295
-
296
268
/**
297
269
* Request token endpoint.
298
270
*
@@ -327,29 +299,6 @@ public function getTestApiEndpoint(): string
327
299
return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/rest/ ' . $ defaultStoreCode . '/V1/testmodule1 ' );
328
300
}
329
301
330
- /**
331
- * Fetch api response using curl client factory
332
- *
333
- * @param string $url
334
- * @param array $requestBody
335
- * @param array $headers
336
- * @param string $method
337
- * @return string
338
- */
339
- public function fetchResponse (string $ url , array $ requestBody , array $ headers , string $ method = 'POST ' ): string
340
- {
341
- $ httpClient = $ this ->clientFactory ->create ();
342
- $ httpClient ->setHeaders ($ headers );
343
- $ httpClient ->setOption (CURLOPT_FAILONERROR , true );
344
- if ($ method === 'GET ' ) {
345
- $ httpClient ->get ($ url );
346
- } else {
347
- $ httpClient ->post ($ url , $ requestBody );
348
- }
349
-
350
- return $ httpClient ->getBody ();
351
- }
352
-
353
302
/**
354
303
* Builds the bearer token authorization header
355
304
*
0 commit comments