Skip to content

Commit 5b1f44d

Browse files
committed
MAGETWO-31999: oAuth issue [from github]
- Fixed consumer expiry calculation to be more deterministic and removed use of Stdlib\DateTime\DateTime from Token provider
1 parent f62afbb commit 5b1f44d

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,15 @@ public function getCreatedAt()
165165
{
166166
return $this->getData('created_at');
167167
}
168+
169+
/**
170+
* Get time in seconds since consumer was created
171+
*
172+
* @param int $consumerId
173+
* @return int - time lapsed in seconds
174+
*/
175+
public function getTimeInSecondsSinceCreation($consumerId)
176+
{
177+
return $this->getResource()->getTimeInSecondsSinceCreation($consumerId);
178+
}
168179
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ class Provider implements TokenProviderInterface
2626
*/
2727
protected $_dataHelper;
2828

29-
/**
30-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
31-
*/
32-
protected $_date;
33-
3429
/**
3530
* @var Token
3631
*/
@@ -40,20 +35,17 @@ class Provider implements TokenProviderInterface
4035
* @param \Magento\Integration\Model\Oauth\Consumer\Factory $consumerFactory
4136
* @param \Magento\Integration\Model\Oauth\Token\Factory $tokenFactory
4237
* @param \Magento\Integration\Helper\Oauth\Data $dataHelper
43-
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
4438
* @param Token $token
4539
*/
4640
public function __construct(
4741
\Magento\Integration\Model\Oauth\Consumer\Factory $consumerFactory,
4842
\Magento\Integration\Model\Oauth\Token\Factory $tokenFactory,
4943
\Magento\Integration\Helper\Oauth\Data $dataHelper,
50-
\Magento\Framework\Stdlib\DateTime\DateTime $date,
5144
Token $token
5245
) {
5346
$this->_consumerFactory = $consumerFactory;
5447
$this->_tokenFactory = $tokenFactory;
5548
$this->_dataHelper = $dataHelper;
56-
$this->_date = $date;
5749
$this->token = $token;
5850
}
5951

@@ -62,10 +54,9 @@ public function __construct(
6254
*/
6355
public function validateConsumer($consumer)
6456
{
65-
// Must use consumer within expiration period.
66-
$consumerTS = strtotime($consumer->getCreatedAt());
6757
$expiry = $this->_dataHelper->getConsumerExpirationPeriod();
68-
if ($this->_date->timestamp() - $consumerTS > $expiry) {
58+
// Must use consumer within expiration period.
59+
if ($this->_consumerFactory->create()->getTimeInSecondsSinceCreation($consumer->getId()) > $expiry) {
6960
throw new \Magento\Framework\Oauth\Exception(
7061
'Consumer key has expired'
7162
);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,22 @@ public function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
5656
$adapter->delete($this->getTable('oauth_token'), ['consumer_id' => $object->getId()]);
5757
return parent::_afterDelete($object);
5858
}
59+
60+
/**
61+
* Compute time in seconds since consumer was created.
62+
*
63+
* @param int $consumerId - The consumer id
64+
* @return int - time lapsed in seconds
65+
*/
66+
public function getTimeInSecondsSinceCreation($consumerId)
67+
{
68+
$adapter = $this->_getReadAdapter();
69+
$select = $adapter->select()
70+
->from($this->getMainTable())
71+
->reset(\Zend_Db_Select::COLUMNS)
72+
->columns('CURRENT_TIMESTAMP() - created_at')
73+
->where('entity_id = ?', $consumerId);
74+
75+
return $adapter->fetchOne($select);
76+
}
5977
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function setUp()
6565
'getCallbackUrl',
6666
'save',
6767
'getData',
68+
'getTimeInSecondsSinceCreation',
6869
'__wakeup',
6970
]
7071
)
@@ -122,7 +123,6 @@ public function setUp()
122123
$this->_consumerFactory,
123124
$this->_tokenFactory,
124125
$this->_dataHelperMock,
125-
$this->_dateMock,
126126
$this->_tokenMock
127127
);
128128
$this->_oauth = new \Magento\Framework\Oauth\Oauth(
@@ -218,7 +218,10 @@ public function testGetRequestTokenConsumerKeyNotFound()
218218
public function testGetRequestTokenOutdatedConsumerKey()
219219
{
220220
$this->_setupConsumer();
221-
$this->_dateMock->expects($this->any())->method('timestamp')->will($this->returnValue(9999999999));
221+
$this->_consumerMock
222+
->expects($this->any())
223+
->method('getTimeInSecondsSinceCreation')
224+
->will($this->returnValue(9999999999));
222225
$this->_dataHelperMock->expects(
223226
$this->once()
224227
)->method(
@@ -267,14 +270,14 @@ protected function _setupConsumer($isLoadable = true)
267270

268271
protected function _makeValidExpirationPeriod()
269272
{
270-
$this->_dateMock->expects($this->any())->method('timestamp')->will($this->returnValue(0));
271-
$this->_dataHelperMock->expects(
272-
$this->once()
273-
)->method(
274-
'getConsumerExpirationPeriod'
275-
)->will(
276-
$this->returnValue(300)
277-
);
273+
$this->_consumerMock
274+
->expects($this->any())
275+
->method('getTimeInSecondsSinceCreation')
276+
->will($this->returnValue(0));
277+
$this->_dataHelperMock
278+
->expects($this->once())
279+
->method('getConsumerExpirationPeriod')
280+
->will($this->returnValue(300));
278281
}
279282

280283
/**

0 commit comments

Comments
 (0)