Skip to content

Commit 4f05d76

Browse files
committed
MAGETWO-31999: oAuth issue [from github]
- Added isValidForTokenExchange to the consumer interface - Fixed expiry message for Oauth Consumer Settings
1 parent 3fd515a commit 4f05d76

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

app/code/Magento/Integration/Model/Oauth/Consumer.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ class Consumer extends \Magento\Framework\Model\AbstractModel implements Consume
3737
*/
3838
protected $_keyLengthFactory;
3939

40+
/**
41+
* @var \Magento\Integration\Helper\Oauth\Data
42+
*/
43+
protected $dataHelper;
44+
4045
/**
4146
* @param \Magento\Framework\Model\Context $context
4247
* @param \Magento\Framework\Registry $registry
4348
* @param \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory $keyLengthFactory
4449
* @param \Magento\Framework\Url\Validator $urlValidator
50+
* @param \Magento\Integration\Helper\Oauth\Data $dataHelper
4551
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
4652
* @param \Magento\Framework\Data\Collection\Db $resourceCollection
4753
* @param array $data
@@ -51,12 +57,14 @@ public function __construct(
5157
\Magento\Framework\Registry $registry,
5258
\Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory $keyLengthFactory,
5359
\Magento\Framework\Url\Validator $urlValidator,
60+
\Magento\Integration\Helper\Oauth\Data $dataHelper,
5461
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
5562
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
5663
array $data = []
5764
) {
5865
$this->_keyLengthFactory = $keyLengthFactory;
5966
$this->_urlValidator = $urlValidator;
67+
$this->dataHelper = $dataHelper;
6068
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
6169
}
6270

@@ -167,13 +175,11 @@ public function getCreatedAt()
167175
}
168176

169177
/**
170-
* Get time in seconds since consumer was created
171-
*
172-
* @param int $consumerId
173-
* @return int - time lapsed in seconds
178+
* {@inheritdoc}
174179
*/
175-
public function getTimeInSecondsSinceCreation($consumerId)
180+
public function isValidForTokenExchange()
176181
{
177-
return $this->getResource()->getTimeInSecondsSinceCreation($consumerId);
182+
$expiry = $this->dataHelper->getConsumerExpirationPeriod();
183+
return $this->getResource()->getTimeInSecondsSinceCreation($this->getId()) > $expiry;
178184
}
179185
}

app/code/Magento/Integration/Model/Oauth/Token/Provider.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,25 @@ class Provider implements TokenProviderInterface
2121
*/
2222
protected $_tokenFactory;
2323

24-
/**
25-
* @var \Magento\Integration\Helper\Oauth\Data
26-
*/
27-
protected $_dataHelper;
28-
2924
/**
3025
* @param \Magento\Integration\Model\Oauth\Consumer\Factory $consumerFactory
3126
* @param \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory
32-
* @param \Magento\Integration\Helper\Oauth\Data $dataHelper
3327
*/
3428
public function __construct(
3529
\Magento\Integration\Model\Oauth\Consumer\Factory $consumerFactory,
36-
\Magento\Integration\Model\Oauth\TokenFactory $tokenFactory,
37-
\Magento\Integration\Helper\Oauth\Data $dataHelper
30+
\Magento\Integration\Model\Oauth\TokenFactory $tokenFactory
3831
) {
3932
$this->_consumerFactory = $consumerFactory;
4033
$this->_tokenFactory = $tokenFactory;
41-
$this->_dataHelper = $dataHelper;
4234
}
4335

4436
/**
4537
* {@inheritdoc}
4638
*/
4739
public function validateConsumer($consumer)
4840
{
49-
$expiry = $this->_dataHelper->getConsumerExpirationPeriod();
5041
// Must use consumer within expiration period.
51-
if ($this->_consumerFactory->create()->getTimeInSecondsSinceCreation($consumer->getId()) > $expiry) {
42+
if (!$consumer->isValidForTokenExchange()) {
5243
throw new \Magento\Framework\Oauth\Exception(
5344
'Consumer key has expired'
5445
);

app/code/Magento/Integration/etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<label>Consumer Settings</label>
2626
<field id="expiration_period" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
2727
<label>Expiration Period</label>
28-
<comment>Disable consumer key/secret credentials if not used within X seconds.</comment>
28+
<comment>Consumer key/secret will expire if not used within X seconds after Oauth token exchange starts.</comment>
2929
</field>
3030
<field id="post_maxredirects" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
3131
<label>OAuth consumer credentials HTTP Post maxredirects</label>

dev/tests/unit/testsuite/Magento/Integration/Oauth/OauthTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function setUp()
6565
'getCallbackUrl',
6666
'save',
6767
'getData',
68-
'getTimeInSecondsSinceCreation',
68+
'isValidForTokenExchange',
6969
'__wakeup',
7070
]
7171
)
@@ -219,15 +219,8 @@ public function testGetRequestTokenOutdatedConsumerKey()
219219
$this->_setupConsumer();
220220
$this->_consumerMock
221221
->expects($this->any())
222-
->method('getTimeInSecondsSinceCreation')
223-
->will($this->returnValue(9999999999));
224-
$this->_dataHelperMock->expects(
225-
$this->once()
226-
)->method(
227-
'getConsumerExpirationPeriod'
228-
)->will(
229-
$this->returnValue(0)
230-
);
222+
->method('isValidForTokenExchange')
223+
->will($this->returnValue(false));
231224

232225
$this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
233226
}
@@ -271,12 +264,8 @@ protected function _makeValidExpirationPeriod()
271264
{
272265
$this->_consumerMock
273266
->expects($this->any())
274-
->method('getTimeInSecondsSinceCreation')
275-
->will($this->returnValue(0));
276-
$this->_dataHelperMock
277-
->expects($this->once())
278-
->method('getConsumerExpirationPeriod')
279-
->will($this->returnValue(300));
267+
->method('isValidForTokenExchange')
268+
->will($this->returnValue(true));
280269
}
281270

282271
/**

lib/internal/Magento/Framework/Oauth/ConsumerInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ public function getCallbackUrl();
5454
* @return string
5555
*/
5656
public function getCreatedAt();
57+
58+
/**
59+
* Check if the consumer key has not expired for Oauth token exchange usage
60+
*
61+
* @return bool
62+
*/
63+
public function isValidForTokenExchange();
5764
}

0 commit comments

Comments
 (0)