OauthUserContext.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Model\Authorization;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. use Magento\Framework\Oauth\Helper\Request as OauthRequestHelper;
  9. use Magento\Framework\Oauth\OauthInterface as OauthService;
  10. use Magento\Integration\Api\IntegrationServiceInterface;
  11. use Magento\Framework\Webapi\Request;
  12. /**
  13. * A user context determined by OAuth headers in a HTTP request.
  14. */
  15. class OauthUserContext implements UserContextInterface
  16. {
  17. /**
  18. * @var Request
  19. */
  20. protected $request;
  21. /**
  22. * @var IntegrationServiceInterface
  23. */
  24. protected $integrationService;
  25. /**
  26. * @var OauthService
  27. */
  28. protected $oauthService;
  29. /**
  30. * @var OauthRequestHelper
  31. */
  32. protected $oauthHelper;
  33. /**
  34. * @var int
  35. */
  36. protected $integrationId;
  37. /**
  38. * Initialize dependencies.
  39. *
  40. * @param Request $request
  41. * @param IntegrationServiceInterface $integrationService
  42. * @param OauthService $oauthService
  43. * @param OauthRequestHelper $oauthHelper
  44. */
  45. public function __construct(
  46. Request $request,
  47. IntegrationServiceInterface $integrationService,
  48. OauthService $oauthService,
  49. OauthRequestHelper $oauthHelper
  50. ) {
  51. $this->request = $request;
  52. $this->integrationService = $integrationService;
  53. $this->oauthService = $oauthService;
  54. $this->oauthHelper = $oauthHelper;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getUserId()
  60. {
  61. if ($this->integrationId) {
  62. return $this->integrationId;
  63. }
  64. $oauthRequest = $this->oauthHelper->prepareRequest($this->request);
  65. //If its not a valid Oauth request no further processing is needed
  66. if (empty($oauthRequest)) {
  67. return null;
  68. }
  69. $consumerId = $this->oauthService->validateAccessTokenRequest(
  70. $oauthRequest,
  71. $this->oauthHelper->getRequestUrl($this->request),
  72. $this->request->getMethod()
  73. );
  74. $integration = $this->integrationService->findActiveIntegrationByConsumerId($consumerId);
  75. return $this->integrationId = ($integration->getId() ? (int)$integration->getId() : null);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getUserType()
  81. {
  82. return UserContextInterface::USER_TYPE_INTEGRATION;
  83. }
  84. }