Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TokenUserContext.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\Integration\Model\Oauth\TokenFactory;
17 use Magento\Integration\Helper\Oauth\Data as OauthHelper;
18 
23 {
27  protected $request;
28 
32  protected $tokenFactory;
33 
37  protected $userId;
38 
42  protected $userType;
43 
48 
53 
57  private $dateTime;
58 
62  private $date;
63 
67  private $oauthHelper;
68 
79  public function __construct(
81  TokenFactory $tokenFactory,
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();
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) {
198  $this->userId = $this->integrationService->findByConsumerId($token->getConsumerId())->getId();
200  break;
202  $this->userId = $token->getAdminId();
203  $this->userType = UserContextInterface::USER_TYPE_ADMIN;
204  break;
206  $this->userId = $token->getCustomerId();
208  break;
209  default:
210  /* this is an unknown user type so reset the cached user type */
211  $this->userType = null;
212  }
213  }
214 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(Request $request, TokenFactory $tokenFactory, IntegrationServiceInterface $integrationService, DateTime $dateTime=null, Date $date=null, OauthHelper $oauthHelper=null)