1010use Magento \Framework \Oauth \Helper \Utility ;
1111use Magento \TestFramework \Helper \Bootstrap ;
1212use Magento \TestFramework \Inspection \Exception ;
13- use Magento \Framework \ HTTP \ ClientFactory ;
13+ use Magento \TestFramework \ Authentication \ Rest \ OauthClient \ Signature ;
1414
1515/**
1616 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1717 */
1818class OauthClient
1919{
2020 /**
21- * @var \Magento\Framework\ Url
21+ * @var Url
2222 */
2323 protected Url $ urlProvider ;
2424
2525 /**
26- * @var \Magento\Framework\HTTP\ClientFactory
26+ * @var CurlClient
2727 */
28- protected ClientFactory $ clientFactory ;
28+ protected CurlClient $ curlClient ;
2929
3030 /**
31- * @var \Magento\Framework\Oauth\ NonceGeneratorInterface
31+ * @var NonceGeneratorInterface
3232 */
3333 protected NonceGeneratorInterface $ _nonceGenerator ;
3434
3535 /**
36- * @var \Magento\Framework\Oauth\Helper\ Utility
36+ * @var Utility
3737 */
3838 private Utility $ _httpUtility ;
3939
40+ /**
41+ * @var Signature
42+ */
43+ private Signature $ signature ;
44+
4045 /**
4146 * @var string
4247 */
@@ -49,20 +54,23 @@ class OauthClient
4954
5055 /**
5156 * @param Url $urlProvider
52- * @param ClientFactory $clientFactory
57+ * @param CurlClient $curlClient
5358 * @param NonceGeneratorInterface $nonceGenerator
5459 * @param Utility $utility
60+ * @param Signature $signature
5561 */
5662 public function __construct (
5763 Url $ urlProvider ,
58- ClientFactory $ clientFactory ,
64+ CurlClient $ curlClient ,
5965 NonceGeneratorInterface $ nonceGenerator ,
60- Utility $ utility
66+ Utility $ utility ,
67+ Signature $ signature
6168 ) {
6269 $ this ->urlProvider = $ urlProvider ;
63- $ this ->clientFactory = $ clientFactory ;
70+ $ this ->curlClient = $ curlClient ;
6471 $ this ->_nonceGenerator = $ nonceGenerator ;
6572 $ this ->_httpUtility = $ utility ;
73+ $ this ->signature = $ signature ;
6674 }
6775
6876 /**
@@ -116,7 +124,7 @@ public function getRequestToken(): array
116124 )
117125 ];
118126
119- $ responseBody = $ this ->fetchResponse ($ requestUrl , [], $ headers );
127+ $ responseBody = $ this ->curlClient -> retrieveResponse ($ requestUrl , [], $ headers );
120128 return $ this ->parseResponseBody ($ responseBody );
121129 }
122130
@@ -137,7 +145,7 @@ public function buildAuthorizationHeaderToRequestToken(
137145 string $ signatureMethod = \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
138146 string $ httpMethod = 'POST '
139147 ): string {
140- $ params ['oauth_signature ' ] = $ this ->getSignature (
148+ $ params ['oauth_signature ' ] = $ this ->signature -> getSignature (
141149 $ params ,
142150 $ signatureMethod ,
143151 $ consumerSecret ,
@@ -175,7 +183,11 @@ public function getAccessToken(array $token, string $verifier): array
175183 $ bodyParams
176184 ),
177185 ];
178- $ responseBody = $ this ->fetchResponse ($ this ->getAccessTokenEndpoint (), $ bodyParams , $ authorizationHeader );
186+ $ responseBody = $ this ->curlClient ->retrieveResponse (
187+ $ this ->getAccessTokenEndpoint (),
188+ $ bodyParams ,
189+ $ authorizationHeader
190+ );
179191 return $ this ->parseResponseBody ($ responseBody );
180192 }
181193
@@ -207,7 +219,7 @@ public function validateAccessToken(array $token, string $method = 'GET'): array
207219
208220 $ headers = array_merge ($ authorizationHeader , $ extraAuthenticationHeaders );
209221
210- $ responseBody = $ this ->fetchResponse ($ this ->getTestApiEndpoint (), [], $ headers , $ method );
222+ $ responseBody = $ this ->curlClient -> retrieveResponse ($ this ->getTestApiEndpoint (), [], $ headers , $ method );
211223
212224 return json_decode ($ responseBody );
213225 }
@@ -241,7 +253,7 @@ public function buildAuthorizationHeaderForAPIRequest(
241253 $ params = array_merge ($ params , ['oauth_token ' => $ token ['oauth_token ' ]]);
242254 $ params = array_merge ($ params , $ bodyParams );
243255
244- $ params ['oauth_signature ' ] = $ this ->getSignature (
256+ $ params ['oauth_signature ' ] = $ this ->signature -> getSignature (
245257 $ params ,
246258 $ signatureMethod ,
247259 $ consumerSecret ,
@@ -253,46 +265,6 @@ public function buildAuthorizationHeaderForAPIRequest(
253265 return $ this ->_httpUtility ->toAuthorizationHeader ($ params );
254266 }
255267
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-
296268 /**
297269 * Request token endpoint.
298270 *
@@ -327,29 +299,6 @@ public function getTestApiEndpoint(): string
327299 return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/rest/ ' . $ defaultStoreCode . '/V1/testmodule1 ' );
328300 }
329301
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-
353302 /**
354303 * Builds the bearer token authorization header
355304 *
0 commit comments