Skip to content

Commit 3cc77e6

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

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
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->getAdminTokenExpirationPeriod(),
50+
[UserContextInterface::USER_TYPE_ADMIN]
51+
);
52+
$this->tokenResourceModel->deleteExpiredTokens(
53+
$this->oauthHelper->getCustomerTokenExpirationPeriod(),
54+
[UserContextInterface::USER_TYPE_CUSTOMER]
55+
);
56+
}
57+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,32 @@ public function getConsumerPostTimeout()
125125
);
126126
return $seconds > 0 ? $seconds : self::CONSUMER_POST_TIMEOUT_DEFAULT;
127127
}
128+
129+
/**
130+
* Get expiration period for customer tokens from config.
131+
*
132+
* @return int minutes
133+
*/
134+
public function getCustomerTokenExpirationPeriod()
135+
{
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;
141+
}
142+
143+
/**
144+
* Get expiration period for admin tokens from config.
145+
*
146+
* @return int minutes
147+
*/
148+
public function getAdminTokenExpirationPeriod()
149+
{
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;
155+
}
128156
}

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 $minutes expiration period
111+
* @param int[] $userTypes @see \Magento\Authorization\Model\UserContextInterface
112+
* @return int number of deleted tokens
113+
*/
114+
public function deleteExpiredTokens($minutes, $userTypes)
115+
{
116+
if ($minutes > 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() - $minutes * 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/etc/adminhtml/system.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
<comment>Timeout for OAuth consumer credentials Post request within X seconds.</comment>
3838
</field>
3939
</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>
4051
</section>
4152
</system>
4253
</config>

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_expiration_period>
25+
<customer>259200</customer>
26+
<admin>43200</admin>
27+
</access_token_expiration_period>
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>* * * * *</schedule>
15+
</job>
1316
</group>
1417
</config>

0 commit comments

Comments
 (0)