Authpost.php 4.3 KB

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