Skip to content

Commit b97c78f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-61867-API-Token' into 2.2-develop-pr-1
2 parents 6086595 + 2c555d2 commit b97c78f

File tree

8 files changed

+452
-20
lines changed

8 files changed

+452
-20
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Integration\Cron;
7+
8+
use Magento\Integration\Model\ResourceModel\Oauth\Token as TokenResourceModel;
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Integration\Helper\Oauth\Data as OauthHelper;
11+
12+
/**
13+
* Cron class for deleting expired OAuth tokens.
14+
*/
15+
class CleanExpiredTokens
16+
{
17+
/**
18+
* @var TokenResourceModel
19+
*/
20+
private $tokenResourceModel;
21+
22+
/**
23+
* @var OauthHelper
24+
*/
25+
private $oauthHelper;
26+
27+
/**
28+
* Initialize dependencies.
29+
*
30+
* @param TokenResourceModel $tokenResourceModel
31+
* @param OauthHelper $oauthHelper
32+
*/
33+
public function __construct(
34+
TokenResourceModel $tokenResourceModel,
35+
OauthHelper $oauthHelper
36+
) {
37+
$this->tokenResourceModel = $tokenResourceModel;
38+
$this->oauthHelper = $oauthHelper;
39+
}
40+
41+
/**
42+
* Delete expired customer and admin tokens.
43+
*
44+
* @return void
45+
*/
46+
public function execute()
47+
{
48+
$this->tokenResourceModel->deleteExpiredTokens(
49+
$this->oauthHelper->getAdminTokenLifetime(),
50+
[UserContextInterface::USER_TYPE_ADMIN]
51+
);
52+
$this->tokenResourceModel->deleteExpiredTokens(
53+
$this->oauthHelper->getCustomerTokenLifetime(),
54+
[UserContextInterface::USER_TYPE_CUSTOMER]
55+
);
56+
}
57+
}

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

Lines changed: 27 additions & 20 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,10 +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
}
113+
114+
/**
115+
* Get customer token lifetime from config.
116+
*
117+
* @return int hours
118+
*/
119+
public function getCustomerTokenLifetime()
120+
{
121+
$hours = (int)$this->_scopeConfig->getValue('oauth/access_token_lifetime/customer');
122+
return $hours > 0 ? $hours : 0;
123+
}
124+
125+
/**
126+
* Get customer token lifetime from config.
127+
*
128+
* @return int hours
129+
*/
130+
public function getAdminTokenLifetime()
131+
{
132+
$hours = (int)$this->_scopeConfig->getValue('oauth/access_token_lifetime/admin');
133+
return $hours > 0 ? $hours : 0;
134+
}
128135
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ public function deleteOldEntries($minutes)
104104
}
105105
}
106106

107+
/**
108+
* Delete expired tokens for the specified user types
109+
*
110+
* @param int $hours token lifetime
111+
* @param int[] $userTypes @see \Magento\Authorization\Model\UserContextInterface
112+
* @return int number of deleted tokens
113+
*/
114+
public function deleteExpiredTokens($hours, $userTypes)
115+
{
116+
if ($hours > 0) {
117+
$connection = $this->getConnection();
118+
119+
$userTypeCondition = $connection->quoteInto('user_type IN (?)', $userTypes);
120+
$createdAtCondition = $connection->quoteInto(
121+
'created_at <= ?',
122+
$this->_dateTime->formatDate($this->date->gmtTimestamp() - $hours * 60 * 60)
123+
);
124+
return $connection->delete(
125+
$this->getMainTable(),
126+
$userTypeCondition . ' AND ' . $createdAtCondition
127+
);
128+
} else {
129+
return 0;
130+
}
131+
}
132+
107133
/**
108134
* Select a single token of the specified type for the specified consumer.
109135
*

app/code/Magento/Integration/Test/Unit/Helper/Oauth/DataTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,44 @@ public function testGetConsumerPostTimeoutNonZero()
8181
$this->_scopeConfigMock->expects($this->once())->method('getValue')->will($this->returnValue(10));
8282
$this->assertEquals(10, $this->_dataHelper->getConsumerPostTimeout());
8383
}
84+
85+
public function testGetCustomerTokenLifetimeNotEmpty()
86+
{
87+
$this->_scopeConfigMock
88+
->expects($this->once())
89+
->method('getValue')
90+
->with('oauth/access_token_lifetime/customer')
91+
->will($this->returnValue(10));
92+
$this->assertEquals(10, $this->_dataHelper->getCustomerTokenLifetime());
93+
}
94+
95+
public function testGetCustomerTokenLifetimeEmpty()
96+
{
97+
$this->_scopeConfigMock
98+
->expects($this->once())
99+
->method('getValue')
100+
->with('oauth/access_token_lifetime/customer')
101+
->will($this->returnValue(null));
102+
$this->assertEquals(0, $this->_dataHelper->getCustomerTokenLifetime());
103+
}
104+
105+
public function testGetAdminTokenLifetimeNotEmpty()
106+
{
107+
$this->_scopeConfigMock
108+
->expects($this->once())
109+
->method('getValue')
110+
->with('oauth/access_token_lifetime/admin')
111+
->will($this->returnValue(10));
112+
$this->assertEquals(10, $this->_dataHelper->getAdminTokenLifetime());
113+
}
114+
115+
public function testGetAdminTokenLifetimeEmpty()
116+
{
117+
$this->_scopeConfigMock
118+
->expects($this->once())
119+
->method('getValue')
120+
->with('oauth/access_token_lifetime/admin')
121+
->will($this->returnValue(null));
122+
$this->assertEquals(0, $this->_dataHelper->getAdminTokenLifetime());
123+
}
84124
}

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

Lines changed: 11 additions & 0 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_lifetime" 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">

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<max_failures_count>6</max_failures_count>
2222
<timeout>1800</timeout>
2323
</authentication_lock>
24+
<access_token_lifetime>
25+
<customer>1</customer>
26+
<admin>4</admin>
27+
</access_token_lifetime>
2428
</oauth>
2529
</default>
2630
</config>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
<job name="outdated_authentication_failures_cleanup" instance="Magento\Integration\Cron\CleanExpiredAuthenticationFailures" method="execute">
1111
<schedule>* * * * *</schedule>
1212
</job>
13+
<job name="expired_tokens_cleanup" instance="Magento\Integration\Cron\CleanExpiredTokens" method="execute">
14+
<schedule>0 * * * *</schedule>
15+
</job>
1316
</group>
1417
</config>

0 commit comments

Comments
 (0)