Magento Extensions Rating 2024
EXTENSIONS BY CATEGORY
B2B (Business-To-Business)
Blog
Customer
ERP (Enterprise Resource Planning)
Mega Menu
One Step Checkout
Order
POS (Point Of Sale)
Search
Shopping Cart
Sitemap
SEO
Social
Stock & Inventory Management
EXTENSIONS BY DEVELOPER
aheadWorks
Amasty
Boost My Shop
BSS Commerce
Magestore
MageWorx
Mirasvit
Templates Master
Wyomind
XTENTO
Magento 2 Documentation
Magento 2 Documentation
2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
vendor
magento
module-webapi
Model
Authorization
TokenUserContext.php
Go to the documentation of this file.
1
<?php
7
namespace
Magento\Webapi\Model\Authorization
;
8
9
use
Magento\Authorization\Model\UserContextInterface
;
10
use
Magento\Framework\App\ObjectManager
;
11
use
Magento\Integration\Model\Oauth\Token
;
12
use Magento\Integration\Model\Oauth\TokenFactory;
13
use
Magento\Integration\Api\IntegrationServiceInterface
;
14
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;
18
22
class
TokenUserContext
implements
UserContextInterface
23
{
27
protected
$request
;
28
32
protected
$tokenFactory
;
33
37
protected
$userId
;
38
42
protected
$userType
;
43
47
protected
$isRequestProcessed
;
48
52
protected
$integrationService
;
53
57
private
$dateTime;
58
62
private
$date;
63
67
private
$oauthHelper;
68
79
public
function
__construct
(
80
Request
$request
,
81
TokenFactory
$tokenFactory
,
82
IntegrationServiceInterface
$integrationService
,
83
DateTime
$dateTime =
null
,
84
Date $date =
null
,
85
OauthHelper $oauthHelper =
null
86
) {
87
$this->request =
$request
;
88
$this->tokenFactory =
$tokenFactory
;
89
$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
);
99
}
100
104
public
function
getUserId
()
105
{
106
$this->
processRequest
();
107
return
$this->userId
;
108
}
109
113
public
function
getUserType
()
114
{
115
$this->
processRequest
();
116
return
$this->userType
;
117
}
118
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
136
if
(empty($tokenTtl)) {
137
return
false
;
138
}
139
140
if
($this->dateTime->strToTime(
$token
->getCreatedAt()) < ($this->date->gmtTimestamp() - $tokenTtl * 3600)) {
141
return
true
;
142
}
143
144
return
false
;
145
}
146
152
protected
function
processRequest
()
153
{
154
if
($this->isRequestProcessed) {
155
return
;
156
}
157
158
$authorizationHeaderValue = $this->request->getHeader(
'Authorization'
);
159
if
(!$authorizationHeaderValue) {
160
$this->isRequestProcessed =
true
;
161
return
;
162
}
163
164
$headerPieces = explode(
" "
, $authorizationHeaderValue);
165
if
(count($headerPieces) !== 2) {
166
$this->isRequestProcessed =
true
;
167
return
;
168
}
169
170
$tokenType = strtolower($headerPieces[0]);
171
if
($tokenType !==
'bearer'
) {
172
$this->isRequestProcessed =
true
;
173
return
;
174
}
175
176
$bearerToken = $headerPieces[1];
177
$token
= $this->tokenFactory->create()->loadByToken($bearerToken);
178
179
if
(!
$token
->getId() ||
$token
->getRevoked() || $this->isTokenExpired(
$token
)) {
180
$this->isRequestProcessed =
true
;
181
182
return
;
183
}
184
185
$this->
setUserDataViaToken
($token);
186
$this->isRequestProcessed =
true
;
187
}
188
193
protected
function
setUserDataViaToken
(
Token
$token
)
194
{
195
$this->userType =
$token
->getUserType();
196
switch
($this->userType) {
197
case
UserContextInterface::USER_TYPE_INTEGRATION
:
198
$this->userId = $this->integrationService->findByConsumerId(
$token
->getConsumerId())->getId();
199
$this->userType =
UserContextInterface::USER_TYPE_INTEGRATION
;
200
break
;
201
case
UserContextInterface::USER_TYPE_ADMIN
:
202
$this->userId =
$token
->getAdminId();
203
$this->userType =
UserContextInterface::USER_TYPE_ADMIN
;
204
break
;
205
case
UserContextInterface::USER_TYPE_CUSTOMER
:
206
$this->userId =
$token
->getCustomerId();
207
$this->userType =
UserContextInterface::USER_TYPE_CUSTOMER
;
208
break
;
209
default
:
210
/* this is an unknown user type so reset the cached user type */
211
$this->userType =
null
;
212
}
213
}
214
}
Magento\Integration\Api\IntegrationServiceInterface
Definition:
IntegrationServiceInterface.php:16
Magento\Integration\Model\Oauth\Token
Definition:
Provider.php:7
Magento\Framework\App\ObjectManager\getInstance
static getInstance()
Definition:
ObjectManager.php:33
elseif
elseif(isset( $params[ 'redirect_parent']))
Definition:
iframe.phtml:17
Magento\Webapi\Model\Authorization\TokenUserContext\processRequest
processRequest()
Definition:
TokenUserContext.php:152
Magento\Framework\Stdlib\DateTime\DateTime
Definition:
DateTime.php:15
Magento\Webapi\Model\Authorization
Definition:
GuestUserContext.php:7
Magento\Webapi\Model\Authorization\TokenUserContext\$userType
$userType
Definition:
TokenUserContext.php:42
Magento\Authorization\Model\UserContextInterface
Definition:
UserContextInterface.php:15
Magento\Framework\Stdlib\DateTime
Definition:
DateTime.php:7
$token
$token
Definition:
fake_payment_token.php:14
Magento\Framework\App\ObjectManager
Definition:
ConfigCache.php:8
Magento\Integration\Helper\Oauth\Data
Definition:
Data.php:11
Magento\Webapi\Model\Authorization\TokenUserContext\__construct
__construct(Request $request, TokenFactory $tokenFactory, IntegrationServiceInterface $integrationService, DateTime $dateTime=null, Date $date=null, OauthHelper $oauthHelper=null)
Definition:
TokenUserContext.php:79
Magento\Framework\Webapi\Request
Definition:
Request.php:18
Magento\Webapi\Model\Authorization\TokenUserContext\$request
$request
Definition:
TokenUserContext.php:27
Magento\Webapi\Model\Authorization\TokenUserContext\$isRequestProcessed
$isRequestProcessed
Definition:
TokenUserContext.php:47
Magento\Webapi\Model\Authorization\TokenUserContext\getUserId
getUserId()
Definition:
TokenUserContext.php:104
Magento\Webapi\Model\Authorization\TokenUserContext\$integrationService
$integrationService
Definition:
TokenUserContext.php:52
Magento\Authorization\Model\UserContextInterface\USER_TYPE_ADMIN
const USER_TYPE_ADMIN
Definition:
UserContextInterface.php:21
Magento\Webapi\Model\Authorization\TokenUserContext
Definition:
TokenUserContext.php:22
Magento\Webapi\Model\Authorization\TokenUserContext\$tokenFactory
$tokenFactory
Definition:
TokenUserContext.php:32
Magento\Authorization\Model\UserContextInterface\USER_TYPE_INTEGRATION
const USER_TYPE_INTEGRATION
Definition:
UserContextInterface.php:20
Magento\Webapi\Model\Authorization\TokenUserContext\setUserDataViaToken
setUserDataViaToken(Token $token)
Definition:
TokenUserContext.php:193
Magento\Webapi\Model\Authorization\TokenUserContext\$userId
$userId
Definition:
TokenUserContext.php:37
Magento\Authorization\Model\UserContextInterface\USER_TYPE_CUSTOMER
const USER_TYPE_CUSTOMER
Definition:
UserContextInterface.php:22
Magento\Webapi\Model\Authorization\TokenUserContext\getUserType
getUserType()
Definition:
TokenUserContext.php:113