Skip to content

Commit 675f3f3

Browse files
author
Dmytro Voskoboinikov
committed
Merge branch 'MAGETWO-70725' into PR-22-2017-11-23
2 parents d35f8ad + 6fc2735 commit 675f3f3

File tree

2 files changed

+340
-24
lines changed

2 files changed

+340
-24
lines changed

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

Lines changed: 59 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,52 @@ 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
*
72+
* TokenUserContext constructor.
5373
* @param Request $request
5474
* @param TokenFactory $tokenFactory
5575
* @param IntegrationServiceInterface $integrationService
76+
* @param DateTime|null $dateTime
77+
* @param Date|null $date
78+
* @param OauthHelper|null $oauthHelper
5679
*/
5780
public function __construct(
5881
Request $request,
5982
TokenFactory $tokenFactory,
60-
IntegrationServiceInterface $integrationService
83+
IntegrationServiceInterface $integrationService,
84+
DateTime $dateTime = null,
85+
Date $date = null,
86+
OauthHelper $oauthHelper = null
6187
) {
6288
$this->request = $request;
6389
$this->tokenFactory = $tokenFactory;
6490
$this->integrationService = $integrationService;
91+
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(
92+
DateTime::class
93+
);
94+
$this->date = $date ?: ObjectManager::getInstance()->get(
95+
Date::class
96+
);
97+
$this->oauthHelper = $oauthHelper ?: ObjectManager::getInstance()->get(
98+
OauthHelper::class
99+
);
65100
}
66101

67102
/**
@@ -82,6 +117,28 @@ public function getUserType()
82117
return $this->userType;
83118
}
84119

120+
/**
121+
* Check if token is expired.
122+
*
123+
* @param Token $token
124+
* @return bool
125+
*/
126+
private function isTokenExpired(Token $token): bool
127+
{
128+
if ($token->getUserType() == UserContextInterface::USER_TYPE_ADMIN) {
129+
$tokenTtl = $this->oauthHelper->getAdminTokenLifetime();
130+
} elseif ($token->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
131+
$tokenTtl = $this->oauthHelper->getCustomerTokenLifetime();
132+
} else {
133+
// other user-type tokens are considered always valid
134+
return false;
135+
}
136+
if ($this->dateTime->strToTime($token->getCreatedAt()) < ($this->date->gmtTimestamp() - $tokenTtl * 3600)) {
137+
return true;
138+
}
139+
return false;
140+
}
141+
85142
/**
86143
* Finds the bearer token and looks up the value.
87144
*
@@ -114,7 +171,7 @@ 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;
119176
return;
120177
}

0 commit comments

Comments
 (0)