Login.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Login\Controller;
  17. use Amazon\Core\Client\ClientFactoryInterface;
  18. use Amazon\Core\Api\Data\AmazonCustomerInterface;
  19. use Amazon\Core\Domain\AmazonCustomerFactory;
  20. use Amazon\Core\Helper\Data as AmazonCoreHelper;
  21. use Amazon\Login\Model\Validator\AccessTokenRequestValidator;
  22. use Amazon\Login\Model\Customer\Account\Redirect as AccountRedirect;
  23. use Amazon\Login\Helper\Session;
  24. use Magento\Customer\Model\Session as CustomerSession;
  25. use Magento\Customer\Model\Url;
  26. use Magento\Framework\App\Action\Action;
  27. use Magento\Framework\App\Action\Context;
  28. use Psr\Log\LoggerInterface;
  29. use Amazon\Login\Model\Customer\MatcherInterface;
  30. use Amazon\Login\Api\CustomerLinkManagementInterface;
  31. /**
  32. * Login with token controller
  33. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  34. */
  35. abstract class Login extends Action
  36. {
  37. /**
  38. * @var AmazonCustomerFactory
  39. */
  40. protected $amazonCustomerFactory;
  41. /**
  42. * @var ClientFactoryInterface
  43. */
  44. protected $clientFactory;
  45. /**
  46. * @var AmazonCoreHelper
  47. */
  48. protected $amazonCoreHelper;
  49. /**
  50. * @var Url
  51. */
  52. protected $customerUrl;
  53. /**
  54. * @var AccessTokenRequestValidator
  55. */
  56. protected $accessTokenRequestValidator;
  57. /**
  58. * @var AccountRedirect
  59. */
  60. protected $accountRedirect;
  61. /**
  62. * @var MatcherInterface
  63. */
  64. protected $matcher;
  65. /**
  66. * @var CustomerLinkManagementInterface
  67. */
  68. protected $customerLinkManagement;
  69. /**
  70. * @var CustomerSession
  71. */
  72. protected $customerSession;
  73. /**
  74. * @var Session
  75. */
  76. protected $session;
  77. /**
  78. * @var LoggerInterface
  79. */
  80. protected $logger;
  81. /**
  82. * Login constructor.
  83. * @param Context $context
  84. * @param AmazonCustomerFactory $amazonCustomerFactory
  85. * @param ClientFactoryInterface $clientFactory
  86. * @param LoggerInterface $logger
  87. * @param AmazonCoreHelper $amazonCoreHelper
  88. * @param Url $customerUrl
  89. * @param AccessTokenRequestValidator $accessTokenRequestValidator
  90. * @param AccountRedirect $accountRedirect
  91. * @param MatcherInterface $matcher
  92. * @param CustomerLinkManagementInterface $customerLinkManagement
  93. * @param CustomerSession $customerSession
  94. * @param Session $session
  95. * @param LoggerInterface $logger
  96. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  97. */
  98. public function __construct(
  99. Context $context,
  100. AmazonCustomerFactory $amazonCustomerFactory,
  101. ClientFactoryInterface $clientFactory,
  102. AmazonCoreHelper $amazonCoreHelper,
  103. Url $customerUrl,
  104. AccessTokenRequestValidator $accessTokenRequestValidator,
  105. AccountRedirect $accountRedirect,
  106. MatcherInterface $matcher,
  107. CustomerLinkManagementInterface $customerLinkManagement,
  108. CustomerSession $customerSession,
  109. Session $session,
  110. LoggerInterface $logger
  111. ) {
  112. $this->amazonCustomerFactory = $amazonCustomerFactory;
  113. $this->clientFactory = $clientFactory;
  114. $this->amazonCoreHelper = $amazonCoreHelper;
  115. $this->customerUrl = $customerUrl;
  116. $this->accessTokenRequestValidator = $accessTokenRequestValidator;
  117. $this->accountRedirect = $accountRedirect;
  118. $this->matcher = $matcher;
  119. $this->customerLinkManagement = $customerLinkManagement;
  120. $this->customerSession = $customerSession;
  121. $this->session = $session;
  122. $this->logger = $logger;
  123. parent::__construct($context);
  124. }
  125. /**
  126. * Load userinfo from access token
  127. *
  128. * @return AmazonCustomerInterface
  129. */
  130. protected function getAmazonCustomer()
  131. {
  132. try {
  133. $userInfo = $this->clientFactory
  134. ->create()
  135. ->getUserInfo($this->getRequest()->getParam('access_token'));
  136. if (is_array($userInfo) && isset($userInfo['user_id'])) {
  137. $data = [
  138. 'id' => $userInfo['user_id'],
  139. 'email' => $userInfo['email'],
  140. 'name' => $userInfo['name'],
  141. 'country' => $this->amazonCoreHelper->getRegion(),
  142. ];
  143. $amazonCustomer = $this->amazonCustomerFactory->create($data);
  144. return $amazonCustomer;
  145. }
  146. } catch (\Exception $e) {
  147. $this->logger->error($e);
  148. $this->messageManager->addErrorMessage(__('Error processing Amazon Login'));
  149. }
  150. return false;
  151. }
  152. /**
  153. * @return bool
  154. */
  155. protected function isValidToken()
  156. {
  157. return $this->accessTokenRequestValidator->isValid($this->getRequest());
  158. }
  159. /**
  160. * @return string
  161. */
  162. protected function getRedirectLogin()
  163. {
  164. return $this->_redirect($this->customerUrl->getLoginUrl());
  165. }
  166. /**
  167. * @return string
  168. */
  169. protected function getRedirectAccount()
  170. {
  171. return $this->accountRedirect->getRedirect();
  172. }
  173. }