Skip to content

Commit 6093edc

Browse files
committed
AC-14075::Replace carlos-mg89/oauth with PHP Native Functions
1 parent 6232a23 commit 6093edc

File tree

3 files changed

+129
-78
lines changed

3 files changed

+129
-78
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
5+
*/
6+
namespace Magento\TestFramework\Authentication\Rest;
7+
8+
/**
9+
* Custom Client implementation for cURL
10+
*/
11+
class CurlClient extends \Magento\Framework\HTTP\ClientFactory
12+
{
13+
14+
/**
15+
* Fetch api response using curl client factory
16+
*
17+
* @param string $url
18+
* @param array $requestBody
19+
* @param array $headers
20+
* @param string $method
21+
* @return string
22+
*/
23+
public function retrieveResponse(
24+
string $url,
25+
array $requestBody,
26+
array $headers,
27+
string $method = 'POST'
28+
): string {
29+
$httpClient = $this->create();
30+
$httpClient->setHeaders($headers);
31+
$httpClient->setOption(CURLOPT_FAILONERROR, true);
32+
if ($method === 'GET') {
33+
$httpClient->get($url);
34+
} else {
35+
$httpClient->post($url, $requestBody);
36+
}
37+
38+
return $httpClient->getBody();
39+
}
40+
}

dev/tests/api-functional/framework/Magento/TestFramework/Authentication/Rest/OauthClient.php

Lines changed: 27 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,38 @@
1010
use Magento\Framework\Oauth\Helper\Utility;
1111
use Magento\TestFramework\Helper\Bootstrap;
1212
use 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
*/
1818
class 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
*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
namespace Magento\TestFramework\Authentication\Rest\OauthClient;
8+
9+
use Magento\Framework\Oauth\Helper\Utility;
10+
11+
/**
12+
* Signature class for Magento REST API.
13+
*/
14+
class Signature
15+
{
16+
/**
17+
* @param Utility $helper
18+
*/
19+
public function __construct(private readonly Utility $helper)
20+
{
21+
}
22+
23+
/**
24+
* Get the signature
25+
*
26+
* @param array $params
27+
* @param string $signatureMethod
28+
* @param string $consumerSecret
29+
* @param string|null $tokenSecret
30+
* @param string $httpMethod
31+
* @param string $requestUrl
32+
* @return string
33+
*/
34+
public function getSignature(
35+
array $params,
36+
string $signatureMethod,
37+
string $consumerSecret,
38+
?string $tokenSecret,
39+
string $httpMethod,
40+
string $requestUrl
41+
): string {
42+
$data = parse_url($requestUrl);
43+
$queryStringData = !isset($data['query']) ? [] : array_reduce(
44+
explode('&', $data['query']),
45+
function ($carry, $item) {
46+
list($key, $value) = explode('=', $item, 2);
47+
$carry[rawurldecode($key)] = rawurldecode($value);
48+
return $carry;
49+
},
50+
[]
51+
);
52+
53+
return $this->helper->sign(
54+
array_merge($queryStringData, $params),
55+
$signatureMethod,
56+
$consumerSecret,
57+
$tokenSecret,
58+
$httpMethod,
59+
$requestUrl
60+
);
61+
}
62+
}

0 commit comments

Comments
 (0)