Verification.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_NoSpam
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  21. use Magento\Framework\Exception\LocalizedException;
  22. use Magento\Framework\HTTP\Client\CurlFactory;
  23. use Magento\Framework\Json\DecoderInterface;
  24. use Magento\Framework\Stdlib\DateTime\DateTime;
  25. use Magento\User\Api\Data\UserInterface;
  26. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  27. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  28. class Verification
  29. {
  30. /**
  31. * @var CurlFactory
  32. */
  33. private $curlFactory;
  34. /**
  35. * @var Service
  36. */
  37. private $service;
  38. /**
  39. * @var UserConfigManagerInterface
  40. */
  41. private $userConfigManager;
  42. /**
  43. * @var DecoderInterface
  44. */
  45. private $decoder;
  46. /**
  47. * @var DateTime
  48. */
  49. private $dateTime;
  50. /**
  51. * Verification constructor.
  52. * @param CurlFactory $curlFactory
  53. * @param DecoderInterface $decoder
  54. * @param DateTime $dateTime
  55. * @param UserConfigManagerInterface $userConfigManager
  56. * @param Service $service
  57. */
  58. public function __construct(
  59. CurlFactory $curlFactory,
  60. DecoderInterface $decoder,
  61. DateTime $dateTime,
  62. UserConfigManagerInterface $userConfigManager,
  63. Service $service
  64. ) {
  65. $this->curlFactory = $curlFactory;
  66. $this->service = $service;
  67. $this->userConfigManager = $userConfigManager;
  68. $this->decoder = $decoder;
  69. $this->dateTime = $dateTime;
  70. }
  71. /**
  72. * Verify phone number
  73. * @param UserInterface $user
  74. * @param string $country
  75. * @param string $phoneNumber
  76. * @param string $method
  77. * @param array &$response
  78. * @return true
  79. * @throws LocalizedException
  80. */
  81. public function request(UserInterface $user, $country, $phoneNumber, $method, &$response)
  82. {
  83. $url = $this->service->getProtectedApiEndpoint('phones/verification/start');
  84. $curl = $this->curlFactory->create();
  85. $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey());
  86. $curl->post($url, [
  87. 'via' => $method,
  88. 'country_code' => $country,
  89. 'phone_number' => $phoneNumber
  90. ]);
  91. $response = $this->decoder->decode($curl->getBody());
  92. if ($errorMessage = $this->service->getErrorFromResponse($response)) {
  93. throw new LocalizedException(__($errorMessage));
  94. }
  95. $this->userConfigManager->addProviderConfig($user->getId(), Authy::CODE, [
  96. 'country_code' => $country,
  97. 'phone_number' => $phoneNumber,
  98. 'carrier' => $response['carrier'],
  99. 'mobile' => $response['is_cellphone'],
  100. 'verify' => [
  101. 'uuid' => $response['uuid'],
  102. 'via' => $method,
  103. 'expires' => $this->dateTime->timestamp() + $response['seconds_to_expire'],
  104. 'seconds_to_expire' => $response['seconds_to_expire'],
  105. 'message' => $response['message'],
  106. ],
  107. 'phone_confirmed' => false,
  108. ]);
  109. return true;
  110. }
  111. /**
  112. * Verify phone number
  113. * @param UserInterface $user
  114. * @param string $verificationCode
  115. * @return true
  116. * @throws LocalizedException
  117. */
  118. public function verify(UserInterface $user, $verificationCode)
  119. {
  120. $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE);
  121. if (!isset($providerInfo['country_code'])) {
  122. throw new LocalizedException(__('Missing verify request information'));
  123. }
  124. $url = $this->service->getProtectedApiEndpoint('phones/verification/check');
  125. $curl = $this->curlFactory->create();
  126. $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey());
  127. $curl->get($url . '?' . http_build_query([
  128. 'country_code' => $providerInfo['country_code'],
  129. 'phone_number' => $providerInfo['phone_number'],
  130. 'verification_code' => $verificationCode,
  131. ]));
  132. $response = $this->decoder->decode($curl->getBody());
  133. if ($errorMessage = $this->service->getErrorFromResponse($response)) {
  134. throw new LocalizedException(__($errorMessage));
  135. }
  136. $this->userConfigManager->addProviderConfig($user->getId(), Authy::CODE, [
  137. 'phone_confirmed' => true,
  138. ]);
  139. $this->userConfigManager->activateProviderConfiguration($user->getId(), Authy::CODE);
  140. return true;
  141. }
  142. }