TokensExchange.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Integration\Controller\Adminhtml\Integration;
  8. use Magento\Integration\Model\Integration as IntegrationModel;
  9. class TokensExchange extends \Magento\Integration\Controller\Adminhtml\Integration
  10. {
  11. /**
  12. * Let the admin know that integration has been sent for activation and token exchange is in process.
  13. *
  14. * @param bool $isReauthorize
  15. * @param string $integrationName
  16. * @return void
  17. */
  18. protected function _setActivationInProcessMsg($isReauthorize, $integrationName)
  19. {
  20. $integrationName = $this->escaper->escapeHtml($integrationName);
  21. $msg = $isReauthorize ? __(
  22. "Integration '%1' has been sent for re-authorization.",
  23. $integrationName
  24. ) : __(
  25. "Integration '%1' has been sent for activation.",
  26. $integrationName
  27. );
  28. $this->messageManager->addNotice($msg);
  29. }
  30. /**
  31. * Post consumer credentials for Oauth integration.
  32. *
  33. * @return void
  34. */
  35. public function execute()
  36. {
  37. try {
  38. $integrationId = $this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
  39. $isReauthorize = (bool)$this->getRequest()->getParam(self::PARAM_REAUTHORIZE, 0);
  40. $integration = $this->_integrationService->get($integrationId);
  41. if ($isReauthorize) {
  42. /** Remove existing token associated with consumer before issuing a new one. */
  43. $this->_oauthService->deleteIntegrationToken($integration->getConsumerId());
  44. $integration->setStatus(IntegrationModel::STATUS_INACTIVE)->save();
  45. }
  46. //Integration chooses to use Oauth for token exchange
  47. $this->_oauthService->postToConsumer($integration->getConsumerId(), $integration->getEndpoint());
  48. /** Generate JS popup content */
  49. $this->_view->loadLayout(false);
  50. // Activation or authorization is done only when the Oauth token exchange completes
  51. $this->_setActivationInProcessMsg($isReauthorize, $integration->getName());
  52. $this->_view->renderLayout();
  53. $popupContent = $this->_response->getBody();
  54. $consumer = $this->_oauthService->loadConsumer($integration->getConsumerId());
  55. if (!$consumer->getId()) {
  56. throw new \Magento\Framework\Oauth\Exception(
  57. __(
  58. 'A consumer with "%1" ID doesn\'t exist. Verify the ID and try again.',
  59. $integration->getConsumerId()
  60. )
  61. );
  62. }
  63. /** Initialize response body */
  64. $result = [
  65. IntegrationModel::IDENTITY_LINK_URL => $integration->getIdentityLinkUrl(),
  66. 'oauth_consumer_key' => $consumer->getKey(),
  67. 'popup_content' => $popupContent,
  68. ];
  69. $this->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
  70. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  71. $this->messageManager->addError($e->getMessage());
  72. $this->_redirect('*/*');
  73. return;
  74. } catch (\Exception $e) {
  75. $this->_logger->critical($e);
  76. $this->messageManager->addError(__('Internal error. Check exception log for details.'));
  77. $this->_redirect('*/*');
  78. return;
  79. }
  80. }
  81. }