Skip to content

Commit 7bf426b

Browse files
Cari Spruiellisitnikov
authored andcommitted
MAGETWO-51292: [GITHUB] OAuth Token Exchange Expiration Period Is Not Calculated Correctly #3449
- improved how updated date is compared against expiration limit
1 parent b769b7b commit 7bf426b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class Consumer extends \Magento\Framework\Model\AbstractModel implements Consume
4343
*/
4444
protected $dataHelper;
4545

46+
/**
47+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
48+
*/
49+
private $_dateHelper;
50+
4651
/**
4752
* @param \Magento\Framework\Model\Context $context
4853
* @param \Magento\Framework\Registry $registry
@@ -80,6 +85,22 @@ protected function _construct()
8085
$this->_init('Magento\Integration\Model\ResourceModel\Oauth\Consumer');
8186
}
8287

88+
/**
89+
* The getter function to get the new DateTime dependency
90+
*
91+
* @return \Magento\Framework\Stdlib\DateTime\DateTime
92+
*
93+
* @deprecated
94+
*/
95+
private function getDateHelper()
96+
{
97+
if ($this->_dateHelper === null) {
98+
$this->_dateHelper = \Magento\Framework\App\ObjectManager::getInstance()
99+
->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
100+
}
101+
return $this->_dateHelper;
102+
}
103+
83104
/**
84105
* BeforeSave actions
85106
*
@@ -176,6 +197,8 @@ public function getCreatedAt()
176197
public function isValidForTokenExchange()
177198
{
178199
$expiry = $this->dataHelper->getConsumerExpirationPeriod();
179-
return $expiry > $this->getResource()->getTimeInSecondsSinceTokenExchangeStarted($this->getId());
200+
$currentTimestamp = $this->getDateHelper()->gmtTimestamp();
201+
$updatedTimestamp = $this->getDateHelper()->gmtTimestamp($this->getUpdatedAt());
202+
return $expiry > ($currentTimestamp - $updatedTimestamp);
180203
}
181204
}

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,7 @@ public function getTimeInSecondsSinceCreation($consumerId)
5656
$select = $connection->select()
5757
->from($this->getMainTable())
5858
->reset(\Magento\Framework\DB\Select::COLUMNS)
59-
->columns(new \Zend_Db_Expr('UTC_TIMESTAMP() - created_at'))
60-
->where('entity_id = ?', $consumerId);
61-
62-
return $connection->fetchOne($select);
63-
}
64-
65-
/**
66-
* Compute time in seconds since token exchange started.
67-
*
68-
* @param int $consumerId - The consumer id
69-
* @return int - time lapsed in seconds
70-
*/
71-
public function getTimeInSecondsSinceTokenExchangeStarted($consumerId)
72-
{
73-
$connection = $this->getConnection();
74-
$select = $connection->select()
75-
->from($this->getMainTable())
76-
->reset(\Magento\Framework\DB\Select::COLUMNS)
77-
->columns(new \Zend_Db_Expr('UTC_TIMESTAMP() - updated_at'))
59+
->columns(new \Zend_Db_Expr('CURRENT_TIMESTAMP() - created_at'))
7860
->where('entity_id = ?', $consumerId);
7961

8062
return $connection->fetchOne($select);

0 commit comments

Comments
 (0)