U2fKey.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\Store\Model\Store;
  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 u2flib_server\U2F;
  30. class U2fKey implements EngineInterface
  31. {
  32. const XML_PATH_ENABLED = 'msp_securitysuite_twofactorauth/u2fkey/enabled';
  33. const XML_PATH_ALLOW_TRUSTED_DEVICES = 'msp_securitysuite_twofactorauth/u2fkey/allow_trusted_devices';
  34. const CODE = 'u2fkey'; // Must be the same as defined in di.xml
  35. /**
  36. * @var UserConfigManagerInterface
  37. */
  38. private $userConfigManager;
  39. /**
  40. * @var StoreManagerInterface
  41. */
  42. private $storeManager;
  43. /**
  44. * @var ScopeConfigInterface
  45. */
  46. private $scopeConfig;
  47. /**
  48. * U2fKey constructor.
  49. * @param StoreManagerInterface $storeManager
  50. * @param ScopeConfigInterface $scopeConfig
  51. * @param UserConfigManagerInterface $userConfigManager
  52. */
  53. public function __construct(
  54. StoreManagerInterface $storeManager,
  55. ScopeConfigInterface $scopeConfig,
  56. UserConfigManagerInterface $userConfigManager
  57. ) {
  58. $this->userConfigManager = $userConfigManager;
  59. $this->storeManager = $storeManager;
  60. $this->scopeConfig = $scopeConfig;
  61. }
  62. /**
  63. * Converts array to object
  64. * @param array $hash
  65. * @return \stdClass
  66. */
  67. private function hashToObject(array $hash)
  68. {
  69. // @codingStandardsIgnoreStart
  70. $object = new \stdClass();
  71. // @codingStandardsIgnoreEnd
  72. foreach ($hash as $key => $value) {
  73. $object->$key = $value;
  74. }
  75. return $object;
  76. }
  77. /**
  78. * Return true on token validation
  79. * @param UserInterface $user
  80. * @param DataObject $request
  81. * @return true
  82. * @throws LocalizedException
  83. * @throws \u2flib_server\Error
  84. */
  85. public function verify(UserInterface $user, DataObject $request)
  86. {
  87. $u2f = $this->getU2f();
  88. $registration = $this->getRegistration($user);
  89. if ($registration === null) {
  90. throw new LocalizedException(__('Missing registration data'));
  91. }
  92. $requests = [$this->hashToObject($request->getData('request')[0])];
  93. $registrations = [$this->hashToObject($registration)];
  94. $response = $this->hashToObject($request->getData('response'));
  95. // it triggers an error in case of auth failure
  96. $u2f->doAuthenticate($requests, $registrations, $response);
  97. return true;
  98. }
  99. /**
  100. * Create the registration challenge
  101. * @return array
  102. * @throws LocalizedException
  103. * @throws \u2flib_server\Error
  104. */
  105. public function getRegisterData()
  106. {
  107. $u2f = $this->getU2f();
  108. return $u2f->getRegisterData();
  109. }
  110. /**
  111. * Get authenticate data
  112. * @param UserInterface $user
  113. * @return array
  114. * @throws LocalizedException
  115. * @throws \u2flib_server\Error
  116. */
  117. public function getAuthenticateData(UserInterface $user)
  118. {
  119. $u2f = $this->getU2f();
  120. $registration = $this->getRegistration($user);
  121. if ($registration === null) {
  122. throw new LocalizedException(__('Missing registration data'));
  123. }
  124. return $u2f->getAuthenticateData([$this->hashToObject($registration)]);
  125. }
  126. /**
  127. * Get registration information
  128. * @param UserInterface $user
  129. * @return array
  130. * @throws \Magento\Framework\Exception\NoSuchEntityException
  131. */
  132. private function getRegistration(UserInterface $user)
  133. {
  134. $providerConfig = $this->userConfigManager->getProviderConfig($user->getId(), static::CODE);
  135. if (!isset($providerConfig['registration'])) {
  136. return null;
  137. }
  138. return $providerConfig['registration'];
  139. }
  140. /**
  141. * Register a new key
  142. * @param UserInterface $user
  143. * @param array $request
  144. * @param array $response
  145. * @return \u2flib_server\Registration
  146. * @throws LocalizedException
  147. * @throws \Magento\Framework\Exception\NoSuchEntityException
  148. * @throws \u2flib_server\Error
  149. */
  150. public function registerDevice(UserInterface $user, array $request, array $response)
  151. {
  152. // Must convert to object
  153. $request = $this->hashToObject($request);
  154. $response = $this->hashToObject($response);
  155. $u2f = $this->getU2f();
  156. $res = $u2f->doRegister($request, $response);
  157. $this->userConfigManager->addProviderConfig($user->getId(), static::CODE, [
  158. 'registration' => [
  159. 'certificate' => $res->certificate,
  160. 'keyHandle' => $res->keyHandle,
  161. 'publicKey' => $res->publicKey,
  162. 'counter' => $res->counter,
  163. ]
  164. ]);
  165. $this->userConfigManager->activateProviderConfiguration($user->getId(), static::CODE);
  166. return $res;
  167. }
  168. /**
  169. * Return true if this provider has been enabled by admin
  170. * @return boolean
  171. */
  172. public function isEnabled()
  173. {
  174. return !!$this->scopeConfig->getValue(static::XML_PATH_ENABLED);
  175. }
  176. /**
  177. * Return true if this provider allows trusted devices
  178. * @return boolean
  179. */
  180. public function isTrustedDevicesAllowed()
  181. {
  182. return !!$this->scopeConfig->getValue(static::XML_PATH_ALLOW_TRUSTED_DEVICES);
  183. }
  184. /**
  185. * @return U2F
  186. * @throws LocalizedException
  187. * @throws \u2flib_server\Error
  188. */
  189. private function getU2f()
  190. {
  191. /** @var Store $store */
  192. $store = $this->storeManager->getStore(Store::ADMIN_CODE);
  193. $baseUrl = $store->getBaseUrl();
  194. if (preg_match('/^(https?:\/\/.+?)\//', $baseUrl, $matches)) {
  195. $domain = $matches[1];
  196. } else {
  197. throw new LocalizedException(__('Unexpected error while parsing domain name'));
  198. }
  199. /** @var U2F $u2f */
  200. // @codingStandardsIgnoreStart
  201. return new U2F($domain);
  202. // @codingStandardsIgnoreEnd
  203. }
  204. }