Google.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Endroid\QrCode\QrCode;
  22. use Endroid\QrCode\Writer\PngWriter;
  23. use Magento\Framework\App\Config\ScopeConfigInterface;
  24. use Magento\Framework\DataObject;
  25. use Magento\Store\Model\StoreManagerInterface;
  26. use Magento\User\Api\Data\UserInterface;
  27. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  28. use MSP\TwoFactorAuth\Api\EngineInterface;
  29. use Base32\Base32;
  30. class Google implements EngineInterface
  31. {
  32. const XML_PATH_ENABLED = 'msp_securitysuite_twofactorauth/google/enabled';
  33. const XML_PATH_ALLOW_TRUSTED_DEVICES = 'msp_securitysuite_twofactorauth/google/allow_trusted_devices';
  34. const CODE = 'google'; // Must be the same as defined in di.xml
  35. private $totp = null;
  36. /**
  37. * @var UserConfigManagerInterface
  38. */
  39. private $configManager;
  40. /**
  41. * @var StoreManagerInterface
  42. */
  43. private $storeManager;
  44. /**
  45. * @var ScopeConfigInterface
  46. */
  47. private $scopeConfig;
  48. /**
  49. * Google constructor.
  50. * @param StoreManagerInterface $storeManager
  51. * @param ScopeConfigInterface $scopeConfig
  52. * @param UserConfigManagerInterface $configManager
  53. */
  54. public function __construct(
  55. StoreManagerInterface $storeManager,
  56. ScopeConfigInterface $scopeConfig,
  57. UserConfigManagerInterface $configManager
  58. ) {
  59. $this->configManager = $configManager;
  60. $this->storeManager = $storeManager;
  61. $this->scopeConfig = $scopeConfig;
  62. }
  63. /**
  64. * Generate random secret
  65. * @return string
  66. */
  67. private function generateSecret()
  68. {
  69. $secret = random_bytes(128);
  70. return preg_replace('/[^A-Za-z0-9]/', '', Base32::encode($secret));
  71. }
  72. /**
  73. * Get TOTP object
  74. * @param UserInterface $user
  75. * @return \OTPHP\TOTP
  76. * @throws \Magento\Framework\Exception\NoSuchEntityException
  77. */
  78. private function getTotp(UserInterface $user)
  79. {
  80. if ($this->totp === null) {
  81. $config = $this->configManager->getProviderConfig($user->getId(), static::CODE);
  82. // @codingStandardsIgnoreStart
  83. $this->totp = new \OTPHP\TOTP(
  84. $user->getEmail(),
  85. $config['secret']
  86. );
  87. // @codingStandardsIgnoreEnd
  88. }
  89. return $this->totp;
  90. }
  91. /**
  92. * Get TFA provisioning URL
  93. * @param UserInterface $user
  94. * @return string
  95. * @throws \Magento\Framework\Exception\NoSuchEntityException
  96. */
  97. private function getProvisioningUrl(UserInterface $user)
  98. {
  99. $config = $this->configManager->getProviderConfig($user->getId(), static::CODE);
  100. if (!isset($config['secret'])) {
  101. $config['secret'] = $this->generateSecret();
  102. $this->configManager->setProviderConfig($user->getId(), static::CODE, $config);
  103. }
  104. $baseUrl = $this->storeManager->getStore()->getBaseUrl();
  105. // @codingStandardsIgnoreStart
  106. $issuer = parse_url($baseUrl, PHP_URL_HOST);
  107. // @codingStandardsIgnoreEnd
  108. $totp = $this->getTotp($user);
  109. $totp->setIssuer($issuer);
  110. return $totp->getProvisioningUri();
  111. }
  112. /**
  113. * Return true on token validation
  114. * @param UserInterface $user
  115. * @param DataObject $request
  116. * @return bool
  117. * @throws \Magento\Framework\Exception\NoSuchEntityException
  118. */
  119. public function verify(UserInterface $user, DataObject $request)
  120. {
  121. $token = $request->getData('tfa_code');
  122. $totp = $this->getTotp($user);
  123. $totp->now();
  124. return $totp->verify($token);
  125. }
  126. /**
  127. * Render TFA QrCode
  128. * @param UserInterface $user
  129. * @return string
  130. * @throws \Magento\Framework\Exception\NoSuchEntityException
  131. * @throws \Endroid\QrCode\Exception\ValidationException
  132. */
  133. public function getQrCodeAsPng(UserInterface $user)
  134. {
  135. // @codingStandardsIgnoreStart
  136. $qrCode = new QrCode($this->getProvisioningUrl($user));
  137. $qrCode->setSize(400);
  138. $qrCode->setErrorCorrectionLevel('high');
  139. $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
  140. $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
  141. $qrCode->setLabelFontSize(16);
  142. $qrCode->setEncoding('UTF-8');
  143. $writer = new PngWriter();
  144. $pngData = $writer->writeString($qrCode);
  145. // @codingStandardsIgnoreEnd
  146. return $pngData;
  147. }
  148. /**
  149. * Return true if this provider has been enabled by admin
  150. * @return boolean
  151. */
  152. public function isEnabled()
  153. {
  154. return !!$this->scopeConfig->getValue(static::XML_PATH_ENABLED);
  155. }
  156. /**
  157. * Return true if this provider allows trusted devices
  158. * @return boolean
  159. */
  160. public function isTrustedDevicesAllowed()
  161. {
  162. return !!$this->scopeConfig->getValue(static::XML_PATH_ALLOW_TRUSTED_DEVICES);
  163. }
  164. }