Authpost.php 4.5 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\Google;
  21. use Magento\Backend\Model\Auth\Session;
  22. use Magento\Backend\App\Action;
  23. use Magento\Framework\Controller\Result\JsonFactory;
  24. use Magento\Framework\DataObjectFactory;
  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\Google;
  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 Google
  50. */
  51. private $google;
  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. /**
  69. * Authpost constructor.
  70. * @param Action\Context $context
  71. * @param Session $session
  72. * @param JsonFactory $jsonFactory
  73. * @param Google $google
  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. Google $google,
  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->google = $google;
  96. $this->tfaSession = $tfaSession;
  97. $this->trustedManager = $trustedManager;
  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. * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
  112. * @throws \Magento\Framework\Exception\NoSuchEntityException
  113. */
  114. public function execute()
  115. {
  116. $response = $this->jsonFactory->create();
  117. $user = $this->getUser();
  118. if ($this->google->verify($user, $this->dataObjectFactory->create([
  119. 'data' => $this->getRequest()->getParams(),
  120. ]))) {
  121. $this->trustedManager->handleTrustDeviceRequest(Google::CODE, $this->getRequest());
  122. $this->tfaSession->grantAccess();
  123. $response->setData(['success' => true]);
  124. } else {
  125. $this->alert->event(
  126. 'MSP_TwoFactorAuth',
  127. 'Google auth invalid token',
  128. AlertInterface::LEVEL_WARNING,
  129. $user->getUserName()
  130. );
  131. $response->setData(['success' => false, 'message' => 'Invalid code']);
  132. }
  133. return $response;
  134. }
  135. /**
  136. * Check if admin has permissions to visit related pages
  137. *
  138. * @return bool
  139. */
  140. protected function _isAllowed()
  141. {
  142. $user = $this->getUser();
  143. return
  144. $user &&
  145. $this->tfa->getProviderIsAllowed($user->getId(), Google::CODE) &&
  146. $this->tfa->getProvider(Google::CODE)->isActive($user->getId());
  147. }
  148. }