Tfa.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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;
  21. use Magento\Framework\Api\SearchCriteriaBuilder;
  22. use Magento\Framework\App\Config\ScopeConfigInterface;
  23. use Magento\Framework\Exception\NoSuchEntityException;
  24. use MSP\TwoFactorAuth\Api\ProviderPoolInterface;
  25. use MSP\TwoFactorAuth\Api\TfaInterface;
  26. use MSP\TwoFactorAuth\Api\TrustedRepositoryInterface;
  27. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  28. /**
  29. * @SuppressWarnings(PHPMD.LongVariable)
  30. */
  31. class Tfa implements TfaInterface
  32. {
  33. private $forcedProviders = null;
  34. private $allowedUrls = null;
  35. private $enabledProviders = null;
  36. /**
  37. * @var ScopeConfigInterface
  38. */
  39. private $scopeConfig;
  40. /**
  41. * @var UserConfigManagerInterface
  42. */
  43. private $userConfigManager;
  44. /**
  45. * @var SearchCriteriaBuilder
  46. */
  47. private $searchCriteriaBuilder;
  48. /**
  49. * @var TrustedRepositoryInterface
  50. */
  51. private $trustedRepository;
  52. /**
  53. * @var ProviderPoolInterface
  54. */
  55. private $providerPool;
  56. public function __construct(
  57. ScopeConfigInterface $scopeConfig,
  58. TrustedRepositoryInterface $trustedRepository,
  59. SearchCriteriaBuilder $searchCriteriaBuilder,
  60. UserConfigManagerInterface $userConfigManager,
  61. ProviderPoolInterface $providerPool
  62. ) {
  63. $this->scopeConfig = $scopeConfig;
  64. $this->userConfigManager = $userConfigManager;
  65. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  66. $this->trustedRepository = $trustedRepository;
  67. $this->providerPool = $providerPool;
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function getAllProviders()
  73. {
  74. return array_values($this->providerPool->getProviders());
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function getProviderByCode($code)
  80. {
  81. if ($code) {
  82. try {
  83. return $this->providerPool->getProviderByCode($code);
  84. } catch (NoSuchEntityException $e) {
  85. return null;
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function getAllEnabledProviders()
  94. {
  95. if ($this->enabledProviders === null) {
  96. $this->enabledProviders = [];
  97. if ($this->isEnabled()) {
  98. $providers = $this->getAllProviders();
  99. foreach ($providers as $provider) {
  100. if ($provider->isEnabled()) {
  101. $this->enabledProviders[] = $provider;
  102. }
  103. }
  104. }
  105. }
  106. return $this->enabledProviders;
  107. }
  108. /**
  109. * @inheritdoc
  110. */
  111. public function getProvider($providerCode, $onlyEnabled = true)
  112. {
  113. $provider = $this->getProviderByCode($providerCode);
  114. if (!$provider) {
  115. return null;
  116. }
  117. if ($onlyEnabled && !$provider->isEnabled()) {
  118. return null;
  119. }
  120. return $provider;
  121. }
  122. /**
  123. * @inheritdoc
  124. */
  125. public function getForcedProviders()
  126. {
  127. if ($this->forcedProviders === null) {
  128. $forcedProvidersCodes =
  129. preg_split('/\s*,\s*/', $this->scopeConfig->getValue(TfaInterface::XML_PATH_FORCED_PROVIDERS));
  130. $this->forcedProviders = [];
  131. foreach ($forcedProvidersCodes as $forcedProviderCode) {
  132. $provider = $this->getProvider($forcedProviderCode);
  133. if ($provider) {
  134. $this->forcedProviders[] = $provider;
  135. }
  136. }
  137. }
  138. return $this->forcedProviders;
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function getUserProviders($userId)
  144. {
  145. $forcedProviders = $this->getForcedProviders();
  146. if (!empty($forcedProviders)) {
  147. return $forcedProviders;
  148. }
  149. $providersCodes = $this->userConfigManager->getProvidersCodes($userId);
  150. $res = [];
  151. foreach ($providersCodes as $providerCode) {
  152. $provider = $this->getProvider($providerCode);
  153. if ($provider) {
  154. $res[] = $provider;
  155. }
  156. }
  157. return $res;
  158. }
  159. /**
  160. * @inheritdoc
  161. */
  162. public function getTrustedDevices($userId)
  163. {
  164. $criteria = $this->searchCriteriaBuilder->addFilter('user_id', $userId)->create();
  165. $results = $this->trustedRepository->getList($criteria);
  166. return $results->getItems();
  167. }
  168. /**
  169. * @inheritdoc
  170. */
  171. public function getAllowedUrls()
  172. {
  173. if ($this->allowedUrls === null) {
  174. $this->allowedUrls = [
  175. 'adminhtml_auth_login',
  176. 'adminhtml_auth_logout',
  177. 'adminhtml_auth_forgotpassword',
  178. 'msp_twofactorauth_tfa_index'
  179. ];
  180. $providers = $this->getAllProviders();
  181. foreach ($providers as $provider) {
  182. $this->allowedUrls[] = str_replace('/', '_', $provider->getConfigureAction());
  183. $this->allowedUrls[] = str_replace('/', '_', $provider->getAuthAction());
  184. foreach (array_values($provider->getExtraActions()) as $extraAction) {
  185. $this->allowedUrls[] = str_replace('/', '_', $extraAction);
  186. }
  187. }
  188. }
  189. return $this->allowedUrls;
  190. }
  191. /**
  192. * @inheritdoc
  193. */
  194. public function getProvidersToActivate($userId)
  195. {
  196. $providers = $this->getUserProviders($userId);
  197. $res = [];
  198. foreach ($providers as $provider) {
  199. if (!$provider->isActive($userId)) {
  200. $res[] = $provider;
  201. }
  202. }
  203. return $res;
  204. }
  205. /**
  206. * @inheritdoc
  207. */
  208. public function getProviderIsAllowed($userId, $providerCode)
  209. {
  210. $providers = $this->getUserProviders($userId);
  211. foreach ($providers as $provider) {
  212. if ($provider->getCode() == $providerCode) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. /**
  219. * @inheritdoc
  220. */
  221. public function isEnabled()
  222. {
  223. return !!$this->scopeConfig->getValue(TfaInterface::XML_PATH_ENABLED);
  224. }
  225. /**
  226. * Return true if a provider code is allowed
  227. * @param int $userId
  228. * @param string $providerCode
  229. * @return bool
  230. * @throws NoSuchEntityException
  231. */
  232. private function checkAllowedProvider($userId, $providerCode)
  233. {
  234. if (!$this->getProviderIsAllowed($userId, $providerCode)) {
  235. throw new NoSuchEntityException(__('Unknown or not enabled provider %1 for this user', $providerCode));
  236. }
  237. return true;
  238. }
  239. /**
  240. * Get default provider code
  241. * @param int $userId
  242. * @return string
  243. */
  244. public function getDefaultProviderCode($userId)
  245. {
  246. return $this->userConfigManager->getDefaultProvider($userId);
  247. }
  248. /**
  249. * Set default provider code
  250. * @param int $userId
  251. * @param string $providerCode
  252. * @return boolean
  253. * @throws NoSuchEntityException
  254. */
  255. public function setDefaultProviderCode($userId, $providerCode)
  256. {
  257. $this->checkAllowedProvider($userId, $providerCode);
  258. return $this->userConfigManager->setDefaultProvider($userId, $providerCode);
  259. }
  260. /**
  261. * Reset default provider code
  262. * @param int $userId
  263. * @param string $providerCode
  264. * @return boolean
  265. * @throws NoSuchEntityException
  266. */
  267. public function resetProviderConfig($userId, $providerCode)
  268. {
  269. $this->checkAllowedProvider($userId, $providerCode);
  270. return $this->userConfigManager->resetProviderConfig($userId, $providerCode);
  271. }
  272. /**
  273. * Set providers
  274. * @param int $userId
  275. * @param string $providersCodes
  276. * @return boolean
  277. * @throws NoSuchEntityException
  278. */
  279. public function setProvidersCodes($userId, $providersCodes)
  280. {
  281. if (is_string($providersCodes)) {
  282. $providersCodes = preg_split('/\s*,\s*/', $providersCodes);
  283. }
  284. foreach ($providersCodes as $providerCode) {
  285. $this->checkAllowedProvider($userId, $providerCode);
  286. }
  287. return $this->userConfigManager->setProvidersCodes($userId, $providersCodes);
  288. }
  289. }