/** * {@inheritdoc} */ public function createCustomerAccessToken($username, $password) { $this->validatorHelper->validate($username, $password); $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER); try { $customerDataObject = $this->accountManagement->authenticate($username, $password); } catch (\Exception $e) { $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER); throw new AuthenticationException(__('You did not sign in correctly or your account is temporarily disabled.')); } $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER); return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); }
/** * {@inheritdoc} */ public function createAdminAccessToken($username, $password) { $this->validatorHelper->validate($username, $password); $this->userModel->login($username, $password); if (!$this->userModel->getId()) { /* * This message is same as one thrown in \Magento\Backend\Model\Auth to keep the behavior consistent. * Constant cannot be created in Auth Model since it uses legacy translation that doesn't support it. * Need to make sure that this is refactored once exception handling is updated in Auth Model. */ throw new AuthenticationException(__('You did not sign in correctly or your account is temporarily disabled.')); } return $this->tokenModelFactory->create()->createAdminToken($this->userModel->getId())->getToken(); }
/** * {@inheritdoc} */ public function postToConsumer($consumerId, $endpointUrl) { try { $consumer = $this->loadConsumer($consumerId); $consumer->setUpdatedAt($this->getDateHelper()->gmtDate()); $consumer->save(); if (!$consumer->getId()) { throw new \Magento\Framework\Oauth\Exception(__('A consumer with ID %1 does not exist', $consumerId)); } $consumerData = $consumer->getData(); $verifier = $this->_tokenFactory->create()->createVerifierToken($consumerId); $storeBaseUrl = $this->_storeManager->getStore()->getBaseUrl(); $this->_httpClient->setUri($endpointUrl); $this->_httpClient->setParameterPost(['oauth_consumer_key' => $consumerData['key'], 'oauth_consumer_secret' => $consumerData['secret'], 'store_base_url' => $storeBaseUrl, 'oauth_verifier' => $verifier->getVerifier()]); $maxredirects = $this->_dataHelper->getConsumerPostMaxRedirects(); $timeout = $this->_dataHelper->getConsumerPostTimeout(); $this->_httpClient->setConfig(['maxredirects' => $maxredirects, 'timeout' => $timeout]); $this->_httpClient->request(\Magento\Framework\HTTP\ZendClient::POST); return $verifier->getVerifier(); } catch (\Magento\Framework\Exception\LocalizedException $exception) { throw $exception; } catch (\Magento\Framework\Oauth\Exception $exception) { throw $exception; } catch (\Exception $exception) { $this->_logger->critical($exception); throw new \Magento\Framework\Oauth\Exception(__('Unable to post data to consumer due to an unexpected error')); } }
/** * Load token object given a consumer Id. * * @param int $consumerId - The Id of the consumer. * @return Token * @throws \Magento\Framework\Oauth\Exception */ public function getIntegrationTokenByConsumerId($consumerId) { /** @var \Magento\Integration\Model\Oauth\Token $token */ $token = $this->_tokenFactory->create(); $token->loadByConsumerIdAndUserType($consumerId, UserContextInterface::USER_TYPE_INTEGRATION); if (!$token->getId()) { throw new \Magento\Framework\Oauth\Exception(__('A token with consumer ID %1 does not exist', [$consumerId])); } return $token; }
/** * {@inheritdoc} */ public function createAdminAccessToken($username, $password) { $this->validatorHelper->validateCredentials($username, $password); try { $this->userModel->login($username, $password); if (!$this->userModel->getId()) { /* * This message is same as one thrown in \Magento\Backend\Model\Auth to keep the behavior consistent. * Constant cannot be created in Auth Model since it uses legacy translation that doesn't support it. * Need to make sure that this is refactored once exception handling is updated in Auth Model. */ throw new AuthenticationException('Please correct the user name or password.'); } } catch (\Magento\Backend\Model\Auth\Exception $e) { throw new AuthenticationException($e->getMessage(), [], $e); } catch (\Magento\Framework\Model\Exception $e) { throw new LocalizedException($e->getMessage(), [], $e); } return $this->tokenModelFactory->create()->createAdminToken($this->userModel->getId())->getToken(); }
/** * {@inheritdoc} */ public function createCustomerAccessToken($username, $password) { $this->validatorHelper->validateCredentials($username, $password); $customerDataObject = $this->accountManagement->authenticate($username, $password); return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); }