Authpost.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Magento\Framework\DataObjectFactory;
  24. use MSP\TwoFactorAuth\Model\AlertInterface;
  25. use MSP\TwoFactorAuth\Api\TfaSessionInterface;
  26. use MSP\TwoFactorAuth\Api\TrustedManagerInterface;
  27. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  28. use MSP\TwoFactorAuth\Model\Provider\Engine\U2fKey;
  29. use MSP\TwoFactorAuth\Model\Tfa;
  30. /**
  31. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  32. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  33. */
  34. class Authpost extends AbstractAction
  35. {
  36. /**
  37. * @var Tfa
  38. */
  39. private $tfa;
  40. /**
  41. * @var Session
  42. */
  43. private $session;
  44. /**
  45. * @var U2fKey
  46. */
  47. private $u2fKey;
  48. /**
  49. * @var JsonFactory
  50. */
  51. private $jsonFactory;
  52. /**
  53. * @var TfaSessionInterface
  54. */
  55. private $tfaSession;
  56. /**
  57. * @var TrustedManagerInterface
  58. */
  59. private $trustedManager;
  60. /**
  61. * @var DataObjectFactory
  62. */
  63. private $dataObjectFactory;
  64. /**
  65. * @var AlertInterface
  66. */
  67. private $alert;
  68. public function __construct(
  69. Tfa $tfa,
  70. Session $session,
  71. JsonFactory $jsonFactory,
  72. TfaSessionInterface $tfaSession,
  73. TrustedManagerInterface $trustedManager,
  74. U2fKey $u2fKey,
  75. DataObjectFactory $dataObjectFactory,
  76. AlertInterface $alert,
  77. Action\Context $context
  78. ) {
  79. parent::__construct($context);
  80. $this->tfa = $tfa;
  81. $this->session = $session;
  82. $this->u2fKey = $u2fKey;
  83. $this->jsonFactory = $jsonFactory;
  84. $this->tfaSession = $tfaSession;
  85. $this->trustedManager = $trustedManager;
  86. $this->dataObjectFactory = $dataObjectFactory;
  87. $this->alert = $alert;
  88. }
  89. /**
  90. * Dispatch request
  91. *
  92. * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
  93. */
  94. public function execute()
  95. {
  96. $result = $this->jsonFactory->create();
  97. try {
  98. $this->u2fKey->verify($this->getUser(), $this->dataObjectFactory->create([
  99. 'data' => $this->getRequest()->getParams(),
  100. ]));
  101. $this->tfaSession->grantAccess();
  102. $this->trustedManager->handleTrustDeviceRequest(U2fKey::CODE, $this->getRequest());
  103. $res = ['success' => true];
  104. } catch (\Exception $e) {
  105. $this->alert->event(
  106. 'MSP_TwoFactorAuth',
  107. 'U2F error',
  108. AlertInterface::LEVEL_ERROR,
  109. $this->getUser()->getUserName(),
  110. AlertInterface::ACTION_LOG,
  111. $e->getMessage()
  112. );
  113. $res = ['success' => false, 'message' => $e->getMessage()];
  114. }
  115. $result->setData($res);
  116. return $result;
  117. }
  118. /**
  119. * @return \Magento\User\Model\User|null
  120. */
  121. private function getUser()
  122. {
  123. return $this->session->getUser();
  124. }
  125. /**
  126. * Check if admin has permissions to visit related pages
  127. *
  128. * @return bool
  129. */
  130. protected function _isAllowed()
  131. {
  132. $user = $this->getUser();
  133. return
  134. $user &&
  135. $this->tfa->getProviderIsAllowed($user->getId(), U2fKey::CODE) &&
  136. $this->tfa->getProvider(U2fKey::CODE)->isActive($user->getId());
  137. }
  138. }