Access.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Integration\Controller\Token;
  8. use Magento\Framework\App\CsrfAwareActionInterface;
  9. use Magento\Framework\App\Request\InvalidRequestException;
  10. use Magento\Framework\App\RequestInterface;
  11. use Magento\Integration\Model\Integration as IntegrationModel;
  12. use Magento\Integration\Api\IntegrationServiceInterface as IntegrationService;
  13. use Magento\Integration\Api\OauthServiceInterface as IntegrationOauthService;
  14. use Magento\Framework\App\Action\Action;
  15. class Access extends Action implements CsrfAwareActionInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\Oauth\OauthInterface
  19. */
  20. protected $oauthService;
  21. /**
  22. * @var IntegrationOauthService
  23. */
  24. protected $intOauthService;
  25. /**
  26. * @var IntegrationService
  27. */
  28. protected $integrationService;
  29. /**
  30. * @var \Magento\Framework\Oauth\Helper\Request
  31. */
  32. protected $helper;
  33. /**
  34. * @param \Magento\Framework\App\Action\Context $context
  35. * @param \Magento\Framework\Oauth\OauthInterface $oauthService
  36. * @param IntegrationOauthService $intOauthService
  37. * @param IntegrationService $integrationService
  38. * @param \Magento\Framework\Oauth\Helper\Request $helper
  39. */
  40. public function __construct(
  41. \Magento\Framework\App\Action\Context $context,
  42. \Magento\Framework\Oauth\OauthInterface $oauthService,
  43. IntegrationOauthService $intOauthService,
  44. IntegrationService $integrationService,
  45. \Magento\Framework\Oauth\Helper\Request $helper
  46. ) {
  47. parent::__construct($context);
  48. $this->oauthService = $oauthService;
  49. $this->intOauthService = $intOauthService;
  50. $this->integrationService = $integrationService;
  51. $this->helper = $helper;
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. public function createCsrfValidationException(
  57. RequestInterface $request
  58. ): ?InvalidRequestException {
  59. return null;
  60. }
  61. /**
  62. * @inheritDoc
  63. */
  64. public function validateForCsrf(RequestInterface $request): ?bool
  65. {
  66. return true;
  67. }
  68. /**
  69. * Initiate AccessToken request operation
  70. *
  71. * @return void
  72. */
  73. public function execute()
  74. {
  75. try {
  76. $requestUrl = $this->helper->getRequestUrl($this->getRequest());
  77. $request = $this->helper->prepareRequest($this->getRequest(), $requestUrl);
  78. // Request access token in exchange of a pre-authorized token
  79. $response = $this->oauthService->getAccessToken($request, $requestUrl, $this->getRequest()->getMethod());
  80. //After sending the access token, update the integration status to active;
  81. $consumer = $this->intOauthService->loadConsumerByKey($request['oauth_consumer_key']);
  82. $integration = $this->integrationService->findByConsumerId($consumer->getId());
  83. $integration->setStatus(IntegrationModel::STATUS_ACTIVE);
  84. $integration->save();
  85. } catch (\Exception $exception) {
  86. $response = $this->helper->prepareErrorResponse($exception, $this->getResponse());
  87. }
  88. $this->getResponse()->setBody(http_build_query($response));
  89. }
  90. }