Authy.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  21. use Magento\Framework\App\Config\ScopeConfigInterface;
  22. use Magento\Framework\DataObject;
  23. use Magento\Framework\Exception\LocalizedException;
  24. use Magento\Framework\HTTP\Client\CurlFactory;
  25. use Magento\Framework\Json\DecoderInterface;
  26. use Magento\User\Api\Data\UserInterface;
  27. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  28. use MSP\TwoFactorAuth\Api\EngineInterface;
  29. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy\Service;
  30. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy\Token;
  31. class Authy implements EngineInterface
  32. {
  33. const CODE = 'authy'; // Must be the same as defined in di.xml
  34. const XML_PATH_ENABLED = 'msp_securitysuite_twofactorauth/authy/enabled';
  35. const XML_PATH_ALLOW_TRUSTED_DEVICES = 'msp_securitysuite_twofactorauth/authy/allow_trusted_devices';
  36. /**
  37. * @var UserConfigManagerInterface
  38. */
  39. private $userConfigManager;
  40. /**
  41. * @var CurlFactory
  42. */
  43. private $curlFactory;
  44. /**
  45. * @var Service
  46. */
  47. private $service;
  48. /**
  49. * @var DecoderInterface
  50. */
  51. private $decoder;
  52. /**
  53. * @var ScopeConfigInterface
  54. */
  55. private $scopeConfig;
  56. /**
  57. * @var Token
  58. */
  59. private $token;
  60. /**
  61. * Authy constructor.
  62. * @param UserConfigManagerInterface $userConfigManager
  63. * @param DecoderInterface $decoder
  64. * @param ScopeConfigInterface $scopeConfig
  65. * @param Token $token
  66. * @param Service $service
  67. * @param CurlFactory $curlFactory
  68. */
  69. public function __construct(
  70. UserConfigManagerInterface $userConfigManager,
  71. DecoderInterface $decoder,
  72. ScopeConfigInterface $scopeConfig,
  73. Token $token,
  74. Service $service,
  75. CurlFactory $curlFactory
  76. ) {
  77. $this->userConfigManager = $userConfigManager;
  78. $this->curlFactory = $curlFactory;
  79. $this->service = $service;
  80. $this->decoder = $decoder;
  81. $this->scopeConfig = $scopeConfig;
  82. $this->token = $token;
  83. }
  84. /**
  85. * Enroll in Authy
  86. * @param UserInterface $user
  87. * @return bool
  88. * @throws LocalizedException
  89. */
  90. public function enroll(UserInterface $user)
  91. {
  92. $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE);
  93. if (!isset($providerInfo['country_code'])) {
  94. throw new LocalizedException(__('Missing phone information'));
  95. }
  96. $url = $this->service->getProtectedApiEndpoint('users/new');
  97. $curl = $this->curlFactory->create();
  98. $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey());
  99. $curl->post($url, [
  100. 'user[email]' => $user->getEmail(),
  101. 'user[cellphone]' => $providerInfo['phone_number'],
  102. 'user[country_code]' => $providerInfo['country_code'],
  103. ]);
  104. $response = $this->decoder->decode($curl->getBody());
  105. if ($errorMessage = $this->service->getErrorFromResponse($response)) {
  106. throw new LocalizedException(__($errorMessage));
  107. }
  108. $this->userConfigManager->addProviderConfig($user->getId(), Authy::CODE, [
  109. 'user' => $response['user']['id'],
  110. ]);
  111. $this->userConfigManager->activateProviderConfiguration($user->getId(), Authy::CODE);
  112. return true;
  113. }
  114. /**
  115. * Return true if this provider has been enabled by admin
  116. * @return boolean
  117. */
  118. public function isEnabled()
  119. {
  120. return
  121. !!$this->scopeConfig->getValue(static::XML_PATH_ENABLED) &&
  122. !!$this->service->getApiKey();
  123. }
  124. /**
  125. * Return true on token validation
  126. * @param UserInterface $user
  127. * @param DataObject $request
  128. * @return bool
  129. * @throws LocalizedException
  130. */
  131. public function verify(UserInterface $user, DataObject $request)
  132. {
  133. return $this->token->verify($user, $request);
  134. }
  135. /**
  136. * Return true if this provider allows trusted devices
  137. * @return boolean
  138. */
  139. public function isTrustedDevicesAllowed()
  140. {
  141. return !!$this->scopeConfig->getValue(static::XML_PATH_ALLOW_TRUSTED_DEVICES);
  142. }
  143. }