Token.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\DataObject;
  22. use Magento\Framework\Exception\LocalizedException;
  23. use Magento\Framework\HTTP\Client\CurlFactory;
  24. use Magento\Framework\Json\DecoderInterface;
  25. use Magento\User\Api\Data\UserInterface;
  26. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  27. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  28. class Token
  29. {
  30. /**
  31. * @var UserConfigManagerInterface
  32. */
  33. private $userConfigManager;
  34. /**
  35. * @var CurlFactory
  36. */
  37. private $curlFactory;
  38. /**
  39. * @var Service
  40. */
  41. private $service;
  42. /**
  43. * @var DecoderInterface
  44. */
  45. private $decoder;
  46. /**
  47. * Token constructor.
  48. * @param UserConfigManagerInterface $userConfigManager
  49. * @param Service $service
  50. * @param DecoderInterface $decoder
  51. * @param CurlFactory $curlFactory
  52. */
  53. public function __construct(
  54. UserConfigManagerInterface $userConfigManager,
  55. Service $service,
  56. DecoderInterface $decoder,
  57. CurlFactory $curlFactory
  58. ) {
  59. $this->userConfigManager = $userConfigManager;
  60. $this->curlFactory = $curlFactory;
  61. $this->service = $service;
  62. $this->decoder = $decoder;
  63. }
  64. /**
  65. * Request a token
  66. * @param UserInterface $user
  67. * @param string $via
  68. * @return true
  69. * @throws LocalizedException
  70. */
  71. public function request(UserInterface $user, $via)
  72. {
  73. if (!in_array($via, ['call', 'sms'])) {
  74. throw new LocalizedException(__('Unsupported via method'));
  75. }
  76. $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE);
  77. if (!isset($providerInfo['user'])) {
  78. throw new LocalizedException(__('Missing user information'));
  79. }
  80. $url = $this->service->getProtectedApiEndpoint('' . $via . '/' . $providerInfo['user']) . '?force=true';
  81. $curl = $this->curlFactory->create();
  82. $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey());
  83. $curl->get($url);
  84. $response = $this->decoder->decode($curl->getBody());
  85. if ($errorMessage = $this->service->getErrorFromResponse($response)) {
  86. throw new LocalizedException(__($errorMessage));
  87. }
  88. return true;
  89. }
  90. /**
  91. * Return true on token validation
  92. * @param UserInterface $user
  93. * @param DataObject $request
  94. * @return bool
  95. * @throws LocalizedException
  96. */
  97. public function verify(UserInterface $user, DataObject $request)
  98. {
  99. $code = $request->getData('tfa_code');
  100. if (!preg_match('/^\w+$/', $code)) {
  101. throw new LocalizedException(__('Invalid code format'));
  102. }
  103. $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE);
  104. if (!isset($providerInfo['user'])) {
  105. throw new LocalizedException(__('Missing user information'));
  106. }
  107. $url = $this->service->getProtectedApiEndpoint('verify/' . $code . '/' . $providerInfo['user']);
  108. $curl = $this->curlFactory->create();
  109. $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey());
  110. $curl->get($url);
  111. $response = $this->decoder->decode($curl->getBody());
  112. if ($errorMessage = $this->service->getErrorFromResponse($response)) {
  113. throw new LocalizedException(__($errorMessage));
  114. }
  115. return true;
  116. }
  117. }