Skip to content

Commit 7b8064f

Browse files
authored
Merge pull request #9722 from magento-gl/AC-14075
AC-14075::Replace carlos-mg89/oauth with PHP Native Functions
2 parents 768b444 + 81c33c2 commit 7b8064f

File tree

7 files changed

+360
-425
lines changed

7 files changed

+360
-425
lines changed

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
"dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0",
9898
"dg/bypass-finals": "^1.4",
9999
"friendsofphp/php-cs-fixer": "^3.22",
100-
"carlos-mg89/oauth": "^0.8.17",
101100
"magento/magento-coding-standard": "*",
102101
"magento/magento2-functional-testing-framework": "^5.0",
103102
"pdepend/pdepend": "3.x-dev",

composer.lock

+1-72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+6-45
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<?php
22
/**
3-
* Helper class for generating OAuth related credentials
4-
*
5-
* Copyright © Magento, Inc. All rights reserved.
6-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
75
*/
86
namespace Magento\TestFramework\Authentication;
97

108
use Magento\Framework\Exception\IntegrationException;
119
use Magento\Framework\Exception\LocalizedException;
1210
use Magento\Framework\Oauth\Exception;
13-
use Magento\TestFramework\Authentication\Rest\OauthClient;
1411
use Magento\TestFramework\Helper\Bootstrap;
15-
use OAuth\Common\Consumer\Credentials;
1612
use Laminas\Stdlib\Exception\LogicException;
1713
use Magento\Integration\Model\Integration;
14+
use Magento\TestFramework\Authentication\Rest\OauthClient;
1815

1916
/**
2017
* Authentication Oauth helper
@@ -67,41 +64,6 @@ public static function getConsumerCredentials($date = null)
6764
];
6865
}
6966

70-
/**
71-
* Create an access token to associated to a consumer to access APIs. No resources are available to this consumer.
72-
*
73-
* @return array comprising of token key and secret
74-
* <pre>
75-
* array (
76-
* 'key' => 'ajdsjashgdkahsdlkjasldkjals', //token key
77-
* 'secret' => 'alsjdlaskjdlaksjdlasjkdlas', //token secret
78-
* 'oauth_client' => $oauthClient // OauthClient instance used to fetch the access token
79-
* );
80-
* </pre>
81-
* @throws LocalizedException
82-
* @throws Exception
83-
* @throws \OAuth\Common\Http\Exception\TokenResponseException
84-
*/
85-
public static function getAccessToken()
86-
{
87-
$consumerCredentials = self::getConsumerCredentials();
88-
$credentials = new Credentials($consumerCredentials['key'], $consumerCredentials['secret'], TESTS_BASE_URL);
89-
$oAuthClient = new OauthClient($credentials);
90-
$requestToken = $oAuthClient->requestRequestToken();
91-
$accessToken = $oAuthClient->requestAccessToken(
92-
$requestToken->getRequestToken(),
93-
$consumerCredentials['verifier'],
94-
$requestToken->getRequestTokenSecret()
95-
);
96-
97-
/** TODO: Reconsider return format. It is not aligned with method name. */
98-
return [
99-
'key' => $accessToken->getAccessToken(),
100-
'secret' => $accessToken->getAccessTokenSecret(),
101-
'oauth_client' => $oAuthClient
102-
];
103-
}
104-
10567
/**
10668
* Create an access token, tied to integration which has permissions to all API resources in the system.
10769
*
@@ -132,14 +94,13 @@ public static function getApiAccessCredentials($resources = null, ?Integration $
13294
throw new LogicException('Access token was not created.');
13395
}
13496
$consumer = $oauthService->loadConsumer($integration->getConsumerId());
135-
$credentials = new Credentials($consumer->getKey(), $consumer->getSecret(), TESTS_BASE_URL);
136-
/** @var $oAuthClient OauthClient */
137-
$oAuthClient = new OauthClient($credentials);
97+
$oauthClientObj = $objectManager->create(OauthClient::class);
98+
$oauthClient = $oauthClientObj->create($consumer->getKey(), $consumer->getSecret());
13899

139100
self::$_apiCredentials = [
140101
'key' => $accessToken->getToken(),
141102
'secret' => $accessToken->getSecret(),
142-
'oauth_client' => $oAuthClient,
103+
'oauth_client' => $oauthClient,
143104
'integration' => $integration,
144105
];
145106
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\TestFramework\Authentication\Rest;
77

8-
use OAuth\Common\Http\Uri\UriInterface;
9-
108
/**
119
* Custom Client implementation for cURL
1210
*/
13-
class CurlClient extends \OAuth\Common\Http\Client\CurlClient
11+
class CurlClient extends \Magento\Framework\HTTP\ClientFactory
1412
{
15-
/**
16-
* @inheritdoc
17-
*/
18-
public function retrieveResponse(
19-
UriInterface $endpoint,
20-
$requestBody,
21-
array $extraHeaders = [],
22-
$method = 'POST'
23-
) {
24-
$this->setCurlParameters([CURLOPT_FAILONERROR => true]);
25-
return parent::retrieveResponse($endpoint, $requestBody, $extraHeaders, $method);
26-
}
2713

2814
/**
29-
* @inheritdoc
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
3022
*/
31-
public function normalizeHeaders($headers): array
32-
{
33-
$normalizeHeaders = [];
34-
foreach ($headers as $key => $val) {
35-
$val = ucfirst(strtolower($key)) . ': ' . $val;
36-
$normalizeHeaders[$key] = $val;
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);
3736
}
3837

39-
return $normalizeHeaders;
38+
return $httpClient->getBody();
4039
}
4140
}

0 commit comments

Comments
 (0)