Configureverifypost.php 4.2 KB

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