Verifyonetouch.php 4.5 KB

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