public function testGetCookie() { $key = "cookieName"; $default = "defaultValue"; $this->cookieManager->expects($this->once())->method('getCookie')->with($key, $default); $this->request->getCookie($key, $default); }
/** * {@inheritdoc} */ public function getUserId() { if ($this->integrationId) { return $this->integrationId; } $oauthRequest = $this->oauthHelper->prepareRequest($this->request); //If its not a valid Oauth request no further processing is needed if (empty($oauthRequest)) { return null; } $consumerId = $this->oauthService->validateAccessTokenRequest($oauthRequest, $this->oauthHelper->getRequestUrl($this->request), $this->request->getMethod()); $integration = $this->integrationService->findActiveIntegrationByConsumerId($consumerId); return $this->integrationId = $integration->getId() ? (int) $integration->getId() : null; }
/** * @dataProvider getValidTokenData */ public function testValidToken($userType, $userId, $expectedUserType, $expectedUserId) { $bearerToken = 'bearer1234'; $this->request->expects($this->once())->method('getHeader')->with('Authorization')->will($this->returnValue("Bearer {$bearerToken}")); $token = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token')->disableOriginalConstructor()->setMethods(['loadByToken', 'getId', 'getUserType', 'getCustomerId', 'getAdminId', '__wakeup'])->getMock(); $this->tokenFactory->expects($this->once())->method('create')->will($this->returnValue($token)); $token->expects($this->once())->method('loadByToken')->with($bearerToken)->will($this->returnSelf()); $token->expects($this->once())->method('getId')->will($this->returnValue(1)); $token->expects($this->once())->method('getUserType')->will($this->returnValue($userType)); $integration = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock(); switch ($userType) { case UserContextInterface::USER_TYPE_INTEGRATION: $integration->expects($this->once())->method('getId')->will($this->returnValue($userId)); $this->integrationService->expects($this->once())->method('findByConsumerId')->will($this->returnValue($integration)); break; case UserContextInterface::USER_TYPE_ADMIN: $token->expects($this->once())->method('getAdminId')->will($this->returnValue($userId)); break; case UserContextInterface::USER_TYPE_CUSTOMER: $token->expects($this->once())->method('getCustomerId')->will($this->returnValue($userId)); break; } $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType()); $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId()); /* check again to make sure that the above methods were only called once */ $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType()); $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId()); }
/** * 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; }
/** * {@inheritdoc} */ public function getUserId() { $consumerId = $this->_request->getConsumerId(); $integration = $this->_integrationFactory->create()->loadByConsumerId($consumerId); return $integration->getId() ? (int) $integration->getId() : 0; }
/** * Initialize dependencies * * @param \Magento\Framework\App\AreaList $areaList * @param \Magento\Framework\Config\ScopeInterface $configScope * @param \Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader * @param \Magento\Webapi\Controller\Rest\Request\Deserializer\Factory $deserializerFactory * @param null|string $uri */ public function __construct(\Magento\Framework\App\AreaList $areaList, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader, \Magento\Webapi\Controller\Rest\Request\Deserializer\Factory $deserializerFactory, $uri = null) { parent::__construct($areaList, $configScope, $cookieReader, $uri); $this->_deserializerFactory = $deserializerFactory; }
/** * Matches a Request with parts defined by a map. Assigns and * returns an array of variables on a successful match. * * @param \Magento\Webapi\Controller\Request $request * @param boolean $partial Partial path matching * @return array|bool An array of assigned values or a boolean false on a mismatch */ public function match($request, $partial = false) { return parent::match(strtolower(ltrim($request->getPathInfo(), $this->_urlDelimiter)), $partial); }