Skip to content

Commit

Permalink
Merge pull request magento#7780 from magento-atwix-pyrrans/AC-5928-re…
Browse files Browse the repository at this point in the history
…place-zend-oauth

[Pyrrans] AC-5928: Replace Zend_Oauth with Laminas\Oauth
  • Loading branch information
Andrii Beziazychnyi authored Jul 25, 2022
2 parents d379b7f + 2f31e77 commit 2e45de7
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 25 deletions.
19 changes: 12 additions & 7 deletions app/code/Magento/Integration/Test/Unit/Oauth/OauthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\Integration\Test\Unit\Oauth;

use Laminas\OAuth\Http\Utility;
use Magento\Framework\DataObject;
use Magento\Framework\Math\Random;
use Magento\Framework\Oauth\Helper\Oauth;
Expand All @@ -31,6 +32,8 @@
*/
class OauthTest extends TestCase
{
private const TIMESTAMP_STUB = 1657789046;

/** @var ConsumerFactory */
private $_consumerFactory;

Expand Down Expand Up @@ -145,24 +148,26 @@ protected function setUp(): void
)
->addMethods(
[
'getType',
'getToken',
'getSecret',
'getConsumerId',
'getRevoked'
'getType',
'getToken',
'getSecret',
'getConsumerId',
'getRevoked'
]
)
->getMock();
$this->_tokenFactory->expects($this->any())->method('create')->willReturn($this->_tokenMock);
$this->_oauthHelperMock = $this->getMockBuilder(Oauth::class)
->setConstructorArgs([new Random()])
->getMock();
$this->_httpUtilityMock = $this->getMockBuilder(\Zend_Oauth_Http_Utility::class)
$this->_httpUtilityMock = $this->getMockBuilder(Utility::class)
->onlyMethods(['sign'])
->getMock();
$this->_dateMock = $this->getMockBuilder(DateTime::class)
->disableOriginalConstructor()
->getMock();
$this->_dateMock->method('timestamp')
->willReturn(self::TIMESTAMP_STUB);
$this->_loggerMock = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
Expand Down Expand Up @@ -821,7 +826,7 @@ public function testBuildAuthorizationHeader()
$oauthHeader = $this->_oauth->buildAuthorizationHeader($request, $requestUrl);

$expectedHeader = 'OAuth oauth_nonce="tyukmnjhgfdcvxstyuioplkmnhtfvert",' .
'oauth_timestamp="",' .
'oauth_timestamp="' . self::TIMESTAMP_STUB . '",' .
'oauth_version="1.0",oauth_consumer_key="edf957ef88492f0a32eb7e1731e85da2",' .
'oauth_consumer_secret="asdawwewefrtyh2f0a32eb7e1731e85d",' .
'oauth_token="7c0709f789e1f38a17aa4b9a28e1b06c",' .
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@
"laminas/laminas-mime": "^2.9.1",
"laminas/laminas-modulemanager": "^2.11.0",
"laminas/laminas-mvc": "^3.3.3",
"laminas/laminas-oauth": "^2.4",
"laminas/laminas-servicemanager": "^3.11.0",
"laminas/laminas-soap": "^2.10.0",
"laminas/laminas-stdlib": "^3.7.1",
"laminas/laminas-stdlib": "^3.10.0",
"laminas/laminas-uri": "^2.9.1",
"laminas/laminas-validator": "^2.17.0",
"league/flysystem": "~2.4.5",
Expand Down
228 changes: 221 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions lib/internal/Magento/Framework/Oauth/Oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Framework\Oauth;

use Laminas\OAuth\Http\Utility;
use Magento\Framework\Encryption\Helper\Security;
use Magento\Framework\Phrase;
use Magento\Framework\Oauth\Exception as AuthException;
Expand All @@ -21,7 +22,7 @@ class Oauth implements OauthInterface
protected $_oauthHelper;

/**
* @var \Zend_Oauth_Http_Utility
* @var Utility
*/
protected $_httpUtility;

Expand All @@ -45,13 +46,13 @@ public function __construct(
Helper\Oauth $oauthHelper,
NonceGeneratorInterface $nonceGenerator,
TokenProviderInterface $tokenProvider,
\Zend_Oauth_Http_Utility $httpUtility = null
Utility $httpUtility = null
) {
$this->_oauthHelper = $oauthHelper;
$this->_nonceGenerator = $nonceGenerator;
$this->_tokenProvider = $tokenProvider;
// null default to prevent ObjectManagerFactory from injecting, see MAGETWO-30809
$this->_httpUtility = $httpUtility ?: new \Zend_Oauth_Http_Utility();
$this->_httpUtility = $httpUtility ?: new Utility();
}

/**
Expand Down Expand Up @@ -249,12 +250,8 @@ protected function _validateProtocolParams($protocolParams, $requiredParams = []
}
$this->_checkRequiredParams($protocolParams, $requiredParams);

if (isset(
$protocolParams['oauth_token']
) && !$this->_tokenProvider->validateOauthToken(
$protocolParams['oauth_token']
)
) {
if (isset($protocolParams['oauth_token'])
&& !$this->_tokenProvider->validateOauthToken($protocolParams['oauth_token'])) {
throw new OauthInputException(new Phrase('The token length is invalid. Check the length and try again.'));
}

Expand Down
Loading

0 comments on commit 2e45de7

Please sign in to comment.