Skip to content

Commit 9c2c5c9

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-73030' into 2.3-develop-pr10-2
2 parents 1a66f3f + 83cb4eb commit 9c2c5c9

File tree

2 files changed

+353
-25
lines changed

2 files changed

+353
-25
lines changed

app/code/Magento/Webapi/Model/Authorization/TokenUserContext.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
namespace Magento\Webapi\Model\Authorization;
88

99
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Integration\Model\Oauth\Token;
1112
use Magento\Integration\Model\Oauth\TokenFactory;
1213
use Magento\Integration\Api\IntegrationServiceInterface;
1314
use Magento\Framework\Webapi\Request;
15+
use Magento\Framework\Stdlib\DateTime\DateTime as Date;
16+
use Magento\Framework\Stdlib\DateTime;
17+
use Magento\Integration\Helper\Oauth\Data as OauthHelper;
1418

1519
/**
1620
* A user context determined by tokens in a HTTP request Authorization header.
@@ -47,21 +51,51 @@ class TokenUserContext implements UserContextInterface
4751
*/
4852
protected $integrationService;
4953

54+
/**
55+
* @var DateTime
56+
*/
57+
private $dateTime;
58+
59+
/**
60+
* @var Date
61+
*/
62+
private $date;
63+
64+
/**
65+
* @var OauthHelper
66+
*/
67+
private $oauthHelper;
68+
5069
/**
5170
* Initialize dependencies.
5271
*
5372
* @param Request $request
5473
* @param TokenFactory $tokenFactory
5574
* @param IntegrationServiceInterface $integrationService
75+
* @param DateTime|null $dateTime
76+
* @param Date|null $date
77+
* @param OauthHelper|null $oauthHelper
5678
*/
5779
public function __construct(
5880
Request $request,
5981
TokenFactory $tokenFactory,
60-
IntegrationServiceInterface $integrationService
82+
IntegrationServiceInterface $integrationService,
83+
DateTime $dateTime = null,
84+
Date $date = null,
85+
OauthHelper $oauthHelper = null
6186
) {
6287
$this->request = $request;
6388
$this->tokenFactory = $tokenFactory;
6489
$this->integrationService = $integrationService;
90+
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(
91+
DateTime::class
92+
);
93+
$this->date = $date ?: ObjectManager::getInstance()->get(
94+
Date::class
95+
);
96+
$this->oauthHelper = $oauthHelper ?: ObjectManager::getInstance()->get(
97+
OauthHelper::class
98+
);
6599
}
66100

67101
/**
@@ -82,6 +116,29 @@ public function getUserType()
82116
return $this->userType;
83117
}
84118

119+
/**
120+
* Check if token is expired.
121+
*
122+
* @param Token $token
123+
* @return bool
124+
*/
125+
private function isTokenExpired(Token $token): bool
126+
{
127+
if ($token->getUserType() == UserContextInterface::USER_TYPE_ADMIN) {
128+
$tokenTtl = $this->oauthHelper->getAdminTokenLifetime();
129+
} elseif ($token->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
130+
$tokenTtl = $this->oauthHelper->getCustomerTokenLifetime();
131+
} else {
132+
// other user-type tokens are considered always valid
133+
return false;
134+
}
135+
if ($this->dateTime->strToTime($token->getCreatedAt()) < ($this->date->gmtTimestamp() - $tokenTtl * 3600)) {
136+
return true;
137+
}
138+
139+
return false;
140+
}
141+
85142
/**
86143
* Finds the bearer token and looks up the value.
87144
*
@@ -114,8 +171,9 @@ protected function processRequest()
114171
$bearerToken = $headerPieces[1];
115172
$token = $this->tokenFactory->create()->loadByToken($bearerToken);
116173

117-
if (!$token->getId() || $token->getRevoked()) {
174+
if (!$token->getId() || $token->getRevoked() || $this->isTokenExpired($token)) {
118175
$this->isRequestProcessed = true;
176+
119177
return;
120178
}
121179

0 commit comments

Comments
 (0)