Created
May 29, 2017 07:48
-
-
Save phamngsinh/e7d2f5b0a4f404ea97ebc4a57d79b2b2 to your computer and use it in GitHub Desktop.
Magento 2 Decode token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Copyright © 2013-2017 Magento, Inc. All rights reserved. | |
| * See COPYING.txt for license details. | |
| */ | |
| namespace Magento\Webapi\Model\Authorization; | |
| use Magento\Authorization\Model\UserContextInterface; | |
| use Magento\Integration\Model\Oauth\Token; | |
| use Magento\Integration\Model\Oauth\TokenFactory; | |
| use Magento\Integration\Api\IntegrationServiceInterface; | |
| use Magento\Framework\Webapi\Request; | |
| /** | |
| * A user context determined by tokens in a HTTP request Authorization header. | |
| */ | |
| class TokenUserContext implements UserContextInterface | |
| { | |
| /** | |
| * @var Request | |
| */ | |
| protected $request; | |
| /** | |
| * @var Token | |
| */ | |
| protected $tokenFactory; | |
| /** | |
| * @var int | |
| */ | |
| protected $userId; | |
| /** | |
| * @var string | |
| */ | |
| protected $userType; | |
| /** | |
| * @var bool | |
| */ | |
| protected $isRequestProcessed; | |
| /** | |
| * @var IntegrationServiceInterface | |
| */ | |
| protected $integrationService; | |
| /** | |
| * Initialize dependencies. | |
| * | |
| * @param Request $request | |
| * @param TokenFactory $tokenFactory | |
| * @param IntegrationServiceInterface $integrationService | |
| */ | |
| public function __construct( | |
| Request $request, | |
| TokenFactory $tokenFactory, | |
| IntegrationServiceInterface $integrationService | |
| ) { | |
| $this->request = $request; | |
| $this->tokenFactory = $tokenFactory; | |
| $this->integrationService = $integrationService; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getUserId() | |
| { | |
| $this->processRequest(); | |
| return $this->userId; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getUserType() | |
| { | |
| $this->processRequest(); | |
| return $this->userType; | |
| } | |
| /** | |
| * Finds the bearer token and looks up the value. | |
| * | |
| * @return void | |
| */ | |
| protected function processRequest() | |
| { | |
| if ($this->isRequestProcessed) { | |
| return; | |
| } | |
| $authorizationHeaderValue = $this->request->getHeader('Authorization'); | |
| if (!$authorizationHeaderValue) { | |
| $this->isRequestProcessed = true; | |
| return; | |
| } | |
| $headerPieces = explode(" ", $authorizationHeaderValue); | |
| if (count($headerPieces) !== 2) { | |
| $this->isRequestProcessed = true; | |
| return; | |
| } | |
| $tokenType = strtolower($headerPieces[0]); | |
| if ($tokenType !== 'bearer') { | |
| $this->isRequestProcessed = true; | |
| return; | |
| } | |
| $bearerToken = $headerPieces[1]; | |
| $token = $this->tokenFactory->create()->loadByToken($bearerToken); | |
| if (!$token->getId() || $token->getRevoked()) { | |
| $this->isRequestProcessed = true; | |
| return; | |
| } | |
| $this->setUserDataViaToken($token); | |
| $this->isRequestProcessed = true; | |
| } | |
| /** | |
| * @param Token $token | |
| * @return void | |
| */ | |
| protected function setUserDataViaToken(Token $token) | |
| { | |
| $this->userType = $token->getUserType(); | |
| switch ($this->userType) { | |
| case UserContextInterface::USER_TYPE_INTEGRATION: | |
| $this->userId = $this->integrationService->findByConsumerId($token->getConsumerId())->getId(); | |
| $this->userType = UserContextInterface::USER_TYPE_INTEGRATION; | |
| break; | |
| case UserContextInterface::USER_TYPE_ADMIN: | |
| $this->userId = $token->getAdminId(); | |
| $this->userType = UserContextInterface::USER_TYPE_ADMIN; | |
| break; | |
| case UserContextInterface::USER_TYPE_CUSTOMER: | |
| $this->userId = $token->getCustomerId(); | |
| $this->userType = UserContextInterface::USER_TYPE_CUSTOMER; | |
| break; | |
| default: | |
| /* this is an unknown user type so reset the cached user type */ | |
| $this->userType = null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment