Authpost.php 4.0 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. * @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\Duo;
  21. use Magento\Backend\Model\Auth\Session;
  22. use Magento\Backend\App\Action;
  23. use Magento\Framework\DataObjectFactory;
  24. use Magento\Framework\View\Result\PageFactory;
  25. use MSP\TwoFactorAuth\Model\AlertInterface;
  26. use MSP\TwoFactorAuth\Api\TfaInterface;
  27. use MSP\TwoFactorAuth\Api\TfaSessionInterface;
  28. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  29. use MSP\TwoFactorAuth\Model\Provider\Engine\DuoSecurity;
  30. /**
  31. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  32. */
  33. class Authpost extends AbstractAction
  34. {
  35. /**
  36. * @var TfaInterface
  37. */
  38. private $tfa;
  39. /**
  40. * @var Session
  41. */
  42. private $session;
  43. /**
  44. * @var PageFactory
  45. */
  46. private $pageFactory;
  47. /**
  48. * @var TfaSessionInterface
  49. */
  50. private $tfaSession;
  51. /**
  52. * @var DuoSecurity
  53. */
  54. private $duoSecurity;
  55. /**
  56. * @var DataObjectFactory
  57. */
  58. private $dataObjectFactory;
  59. /**
  60. * @var AlertInterface
  61. */
  62. private $alert;
  63. /**
  64. * @var Action\Context
  65. */
  66. private $context;
  67. /**
  68. * Authpost constructor.
  69. * @param Action\Context $context
  70. * @param Session $session
  71. * @param PageFactory $pageFactory
  72. * @param DuoSecurity $duoSecurity
  73. * @param TfaSessionInterface $tfaSession
  74. * @param DataObjectFactory $dataObjectFactory
  75. * @param AlertInterface $alert
  76. * @param TfaInterface $tfa
  77. */
  78. public function __construct(
  79. Action\Context $context,
  80. Session $session,
  81. PageFactory $pageFactory,
  82. DuoSecurity $duoSecurity,
  83. TfaSessionInterface $tfaSession,
  84. DataObjectFactory $dataObjectFactory,
  85. AlertInterface $alert,
  86. TfaInterface $tfa
  87. ) {
  88. parent::__construct($context);
  89. $this->tfa = $tfa;
  90. $this->session = $session;
  91. $this->pageFactory = $pageFactory;
  92. $this->tfaSession = $tfaSession;
  93. $this->duoSecurity = $duoSecurity;
  94. $this->dataObjectFactory = $dataObjectFactory;
  95. $this->alert = $alert;
  96. $this->context = $context;
  97. }
  98. /**
  99. * Get current user
  100. * @return \Magento\User\Model\User|null
  101. */
  102. private function getUser()
  103. {
  104. return $this->session->getUser();
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function execute()
  110. {
  111. $user = $this->getUser();
  112. if ($this->duoSecurity->verify($user, $this->dataObjectFactory->create([
  113. 'data' => $this->getRequest()->getParams(),
  114. ]))) {
  115. $this->tfa->getProvider(DuoSecurity::CODE)->activate($user->getId());
  116. $this->tfaSession->grantAccess();
  117. return $this->_redirect($this->context->getBackendUrl()->getStartupPageUrl());
  118. } else {
  119. $this->alert->event(
  120. 'MSP_TwoFactorAuth',
  121. 'DuoSecurity invalid auth',
  122. AlertInterface::LEVEL_WARNING,
  123. $user->getUserName()
  124. );
  125. return $this->_redirect('*/*/auth');
  126. }
  127. }
  128. /**
  129. * Check if admin has permissions to visit related pages
  130. *
  131. * @return bool
  132. */
  133. protected function _isAllowed()
  134. {
  135. // Do not check for activation
  136. $user = $this->getUser();
  137. return
  138. $user &&
  139. $this->tfa->getProviderIsAllowed($user->getId(), DuoSecurity::CODE);
  140. }
  141. }