Configurepost.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. */
  18. namespace MSP\TwoFactorAuth\Controller\Adminhtml\U2f;
  19. use Magento\Backend\App\Action;
  20. use Magento\Backend\Model\Auth\Session;
  21. use Magento\Framework\App\ResponseInterface;
  22. use Magento\Framework\Controller\Result\JsonFactory;
  23. use MSP\TwoFactorAuth\Model\AlertInterface;
  24. use MSP\TwoFactorAuth\Api\TfaSessionInterface;
  25. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  26. use MSP\TwoFactorAuth\Model\Provider\Engine\U2fKey;
  27. use MSP\TwoFactorAuth\Model\Tfa;
  28. /**
  29. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  30. */
  31. class Configurepost extends AbstractAction
  32. {
  33. /**
  34. * @var Tfa
  35. */
  36. private $tfa;
  37. /**
  38. * @var Session
  39. */
  40. private $session;
  41. /**
  42. * @var U2fKey
  43. */
  44. private $u2fKey;
  45. /**
  46. * @var JsonFactory
  47. */
  48. private $jsonFactory;
  49. /**
  50. * @var TfaSessionInterface
  51. */
  52. private $tfaSession;
  53. /**
  54. * @var AlertInterface
  55. */
  56. private $alert;
  57. public function __construct(
  58. Tfa $tfa,
  59. Session $session,
  60. JsonFactory $jsonFactory,
  61. TfaSessionInterface $tfaSession,
  62. U2fKey $u2fKey,
  63. AlertInterface $alert,
  64. Action\Context $context
  65. ) {
  66. parent::__construct($context);
  67. $this->tfa = $tfa;
  68. $this->session = $session;
  69. $this->u2fKey = $u2fKey;
  70. $this->jsonFactory = $jsonFactory;
  71. $this->tfaSession = $tfaSession;
  72. $this->alert = $alert;
  73. }
  74. /**
  75. * Dispatch request
  76. *
  77. * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
  78. */
  79. public function execute()
  80. {
  81. $result = $this->jsonFactory->create();
  82. try {
  83. $request = $this->getRequest()->getParam('request');
  84. $response = $this->getRequest()->getParam('response');
  85. $this->u2fKey->registerDevice($this->getUser(), $request, $response);
  86. $this->tfaSession->grantAccess();
  87. $this->alert->event(
  88. 'MSP_TwoFactorAuth',
  89. 'U2F New device registered',
  90. AlertInterface::LEVEL_INFO,
  91. $this->getUser()->getUserName()
  92. );
  93. $res = ['success' => true];
  94. } catch (\Exception $e) {
  95. $this->alert->event(
  96. 'MSP_TwoFactorAuth',
  97. 'U2F error while adding device',
  98. AlertInterface::LEVEL_ERROR,
  99. $this->getUser()->getUserName(),
  100. AlertInterface::ACTION_LOG,
  101. $e->getMessage()
  102. );
  103. $res = ['success' => false, 'message' => $e->getMessage()];
  104. }
  105. $result->setData($res);
  106. return $result;
  107. }
  108. /**
  109. * @return \Magento\User\Model\User|null
  110. */
  111. private function getUser()
  112. {
  113. return $this->session->getUser();
  114. }
  115. /**
  116. * Check if admin has permissions to visit related pages
  117. *
  118. * @return bool
  119. */
  120. protected function _isAllowed()
  121. {
  122. $user = $this->getUser();
  123. return
  124. $user &&
  125. $this->tfa->getProviderIsAllowed($user->getId(), U2fKey::CODE) &&
  126. !$this->tfa->getProvider(U2fKey::CODE)->isActive($user->getId());
  127. }
  128. }