Skip to content

Commit 15b3f13

Browse files
author
Alex Paliarush
committed
MAGETWO-61867: API token does not expire after a time limit
1 parent 3cc77e6 commit 15b3f13

File tree

6 files changed

+35
-56
lines changed

6 files changed

+35
-56
lines changed

app/code/Magento/Integration/Cron/CleanExpiredTokens.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public function __construct(
4646
public function execute()
4747
{
4848
$this->tokenResourceModel->deleteExpiredTokens(
49-
$this->oauthHelper->getAdminTokenExpirationPeriod(),
49+
$this->oauthHelper->getAdminTokenLifetime(),
5050
[UserContextInterface::USER_TYPE_ADMIN]
5151
);
5252
$this->tokenResourceModel->deleteExpiredTokens(
53-
$this->oauthHelper->getCustomerTokenExpirationPeriod(),
53+
$this->oauthHelper->getCustomerTokenLifetime(),
5454
[UserContextInterface::USER_TYPE_CUSTOMER]
5555
);
5656
}

app/code/Magento/Integration/Helper/Oauth/Data.php

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $
6363
public function isCleanupProbability()
6464
{
6565
// Safe get cleanup probability value from system configuration
66-
$configValue = (int)$this->_scopeConfig->getValue(
67-
self::XML_PATH_CLEANUP_PROBABILITY,
68-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
69-
);
66+
$configValue = (int)$this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_PROBABILITY);
7067
return $configValue > 0 ? 1 == \Magento\Framework\Math\Random::getRandomNumber(1, $configValue) : false;
7168
}
7269

@@ -77,10 +74,7 @@ public function isCleanupProbability()
7774
*/
7875
public function getCleanupExpirationPeriod()
7976
{
80-
$minutes = (int)$this->_scopeConfig->getValue(
81-
self::XML_PATH_CLEANUP_EXPIRATION_PERIOD,
82-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
83-
);
77+
$minutes = (int)$this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_EXPIRATION_PERIOD);
8478
return $minutes > 0 ? $minutes : self::CLEANUP_EXPIRATION_PERIOD_DEFAULT;
8579
}
8680

@@ -91,10 +85,7 @@ public function getCleanupExpirationPeriod()
9185
*/
9286
public function getConsumerExpirationPeriod()
9387
{
94-
$seconds = (int)$this->_scopeConfig->getValue(
95-
self::XML_PATH_CONSUMER_EXPIRATION_PERIOD,
96-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
97-
);
88+
$seconds = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_EXPIRATION_PERIOD);
9889
return $seconds > 0 ? $seconds : self::CONSUMER_EXPIRATION_PERIOD_DEFAULT;
9990
}
10091

@@ -105,10 +96,7 @@ public function getConsumerExpirationPeriod()
10596
*/
10697
public function getConsumerPostMaxRedirects()
10798
{
108-
$redirects = (int)$this->_scopeConfig->getValue(
109-
self::XML_PATH_CONSUMER_POST_MAXREDIRECTS,
110-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
111-
);
99+
$redirects = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_POST_MAXREDIRECTS);
112100
return $redirects > 0 ? $redirects : 0;
113101
}
114102

@@ -119,38 +107,29 @@ public function getConsumerPostMaxRedirects()
119107
*/
120108
public function getConsumerPostTimeout()
121109
{
122-
$seconds = (int)$this->_scopeConfig->getValue(
123-
self::XML_PATH_CONSUMER_POST_TIMEOUT,
124-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
125-
);
110+
$seconds = (int)$this->_scopeConfig->getValue(self::XML_PATH_CONSUMER_POST_TIMEOUT);
126111
return $seconds > 0 ? $seconds : self::CONSUMER_POST_TIMEOUT_DEFAULT;
127112
}
128113

129114
/**
130-
* Get expiration period for customer tokens from config.
115+
* Get customer token lifetime from config.
131116
*
132-
* @return int minutes
117+
* @return int hours
133118
*/
134-
public function getCustomerTokenExpirationPeriod()
119+
public function getCustomerTokenLifetime()
135120
{
136-
$minutes = (int)$this->_scopeConfig->getValue(
137-
'oauth/access_token_expiration_period/customer',
138-
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE
139-
);
140-
return $minutes > 0 ? $minutes : 0;
121+
$hours = (int)$this->_scopeConfig->getValue('oauth/access_token_expiration_period/customer');
122+
return $hours > 0 ? $hours : 0;
141123
}
142124

143125
/**
144-
* Get expiration period for admin tokens from config.
126+
* Get customer token lifetime from config.
145127
*
146-
* @return int minutes
128+
* @return int hours
147129
*/
148-
public function getAdminTokenExpirationPeriod()
130+
public function getAdminTokenLifetime()
149131
{
150-
$minutes = (int)$this->_scopeConfig->getValue(
151-
'oauth/access_token_expiration_period/admin',
152-
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE
153-
);
154-
return $minutes > 0 ? $minutes : 0;
132+
$hours = (int)$this->_scopeConfig->getValue('oauth/access_token_expiration_period/admin');
133+
return $hours > 0 ? $hours : 0;
155134
}
156135
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,19 @@ public function deleteOldEntries($minutes)
107107
/**
108108
* Delete expired tokens for the specified user types
109109
*
110-
* @param int $minutes expiration period
110+
* @param int $hours token lifetime
111111
* @param int[] $userTypes @see \Magento\Authorization\Model\UserContextInterface
112112
* @return int number of deleted tokens
113113
*/
114-
public function deleteExpiredTokens($minutes, $userTypes)
114+
public function deleteExpiredTokens($hours, $userTypes)
115115
{
116-
if ($minutes > 0) {
116+
if ($hours > 0) {
117117
$connection = $this->getConnection();
118118

119119
$userTypeCondition = $connection->quoteInto('user_type IN (?)', $userTypes);
120120
$createdAtCondition = $connection->quoteInto(
121121
'created_at <= ?',
122-
$this->_dateTime->formatDate($this->date->gmtTimestamp() - $minutes * 60)
122+
$this->_dateTime->formatDate($this->date->gmtTimestamp() - $hours * 60 * 60)
123123
);
124124
return $connection->delete(
125125
$this->getMainTable(),

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
<label>OAuth</label>
1212
<tab>service</tab>
1313
<resource>Magento_Integration::config_oauth</resource>
14+
<group id="access_token_expiration_period" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0">
15+
<label>Access Token Expiration</label>
16+
<field id="customer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
17+
<label>Customer Token Lifetime (hours)</label>
18+
<comment>We will disable this feature if the value is empty.</comment>
19+
</field>
20+
<field id="admin" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
21+
<label>Admin Token Lifetime (hours)</label>
22+
<comment>We will disable this feature if the value is empty.</comment>
23+
</field>
24+
</group>
1425
<group id="cleanup" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="0" showInStore="0">
1526
<label>Cleanup Settings</label>
1627
<field id="cleanup_probability" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
@@ -37,17 +48,6 @@
3748
<comment>Timeout for OAuth consumer credentials Post request within X seconds.</comment>
3849
</field>
3950
</group>
40-
<group id="access_token_expiration_period" translate="label" type="text" sortOrder="600" showInDefault="1" showInWebsite="0" showInStore="0">
41-
<label>Access Tokens Expiration Period</label>
42-
<field id="customer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
43-
<label>Customer Tokens</label>
44-
<comment>Customer access tokens will expire after X minutes after generation. Specify 0 to disable expiration (not recommended)</comment>
45-
</field>
46-
<field id="admin" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
47-
<label>Admin Tokens</label>
48-
<comment>Admin access tokens will expire after X minutes after generation. Specify 0 to disable expiration (not recommended)</comment>
49-
</field>
50-
</group>
5151
</section>
5252
</system>
5353
</config>

app/code/Magento/Integration/etc/config.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<timeout>1800</timeout>
2323
</authentication_lock>
2424
<access_token_expiration_period>
25-
<customer>259200</customer>
26-
<admin>43200</admin>
25+
<customer>1</customer>
26+
<admin>4</admin>
2727
</access_token_expiration_period>
2828
</oauth>
2929
</default>

app/code/Magento/Integration/etc/crontab.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<schedule>* * * * *</schedule>
1212
</job>
1313
<job name="expired_tokens_cleanup" instance="Magento\Integration\Cron\CleanExpiredTokens" method="execute">
14-
<schedule>* * * * *</schedule>
14+
<schedule>0 * * * *</schedule>
1515
</job>
1616
</group>
1717
</config>

0 commit comments

Comments
 (0)