123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- /**
- * MageSpecialist
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Open Software License (OSL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/osl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to info@magespecialist.it so we can send you a copy immediately.
- *
- * @category MSP
- * @package MSP_TwoFactorAuth
- * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- */
- namespace MSP\TwoFactorAuth\Controller\Adminhtml\Google;
- use Magento\Backend\Model\Auth\Session;
- use Magento\Backend\App\Action;
- use Magento\Framework\Controller\Result\JsonFactory;
- use Magento\Framework\DataObjectFactory;
- use MSP\TwoFactorAuth\Model\AlertInterface;
- use MSP\TwoFactorAuth\Api\TfaInterface;
- use MSP\TwoFactorAuth\Api\TfaSessionInterface;
- use MSP\TwoFactorAuth\Api\TrustedManagerInterface;
- use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
- use MSP\TwoFactorAuth\Model\Provider\Engine\Google;
- /**
- * @SuppressWarnings(PHPMD.CamelCaseMethodName)
- */
- class Authpost extends AbstractAction
- {
- /**
- * @var TfaInterface
- */
- private $tfa;
- /**
- * @var Session
- */
- private $session;
- /**
- * @var JsonFactory
- */
- private $jsonFactory;
- /**
- * @var Google
- */
- private $google;
- /**
- * @var TfaSessionInterface
- */
- private $tfaSession;
- /**
- * @var TrustedManagerInterface
- */
- private $trustedManager;
- /**
- * @var DataObjectFactory
- */
- private $dataObjectFactory;
- /**
- * @var AlertInterface
- */
- private $alert;
- /**
- * Authpost constructor.
- * @param Action\Context $context
- * @param Session $session
- * @param JsonFactory $jsonFactory
- * @param Google $google
- * @param TfaSessionInterface $tfaSession
- * @param TrustedManagerInterface $trustedManager
- * @param TfaInterface $tfa
- * @param AlertInterface $alert
- * @param DataObjectFactory $dataObjectFactory
- */
- public function __construct(
- Action\Context $context,
- Session $session,
- JsonFactory $jsonFactory,
- Google $google,
- TfaSessionInterface $tfaSession,
- TrustedManagerInterface $trustedManager,
- TfaInterface $tfa,
- AlertInterface $alert,
- DataObjectFactory $dataObjectFactory
- ) {
- parent::__construct($context);
- $this->tfa = $tfa;
- $this->session = $session;
- $this->jsonFactory = $jsonFactory;
- $this->google = $google;
- $this->tfaSession = $tfaSession;
- $this->trustedManager = $trustedManager;
- $this->dataObjectFactory = $dataObjectFactory;
- $this->alert = $alert;
- }
- /**
- * Get current user
- * @return \Magento\User\Model\User|null
- */
- private function getUser()
- {
- return $this->session->getUser();
- }
- /**
- * @inheritdoc
- * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- */
- public function execute()
- {
- $response = $this->jsonFactory->create();
- $user = $this->getUser();
- if ($this->google->verify($user, $this->dataObjectFactory->create([
- 'data' => $this->getRequest()->getParams(),
- ]))) {
- $this->trustedManager->handleTrustDeviceRequest(Google::CODE, $this->getRequest());
- $this->tfaSession->grantAccess();
- $response->setData(['success' => true]);
- } else {
- $this->alert->event(
- 'MSP_TwoFactorAuth',
- 'Google auth invalid token',
- AlertInterface::LEVEL_WARNING,
- $user->getUserName()
- );
- $response->setData(['success' => false, 'message' => 'Invalid code']);
- }
- return $response;
- }
- /**
- * Check if admin has permissions to visit related pages
- *
- * @return bool
- */
- protected function _isAllowed()
- {
- $user = $this->getUser();
- return
- $user &&
- $this->tfa->getProviderIsAllowed($user->getId(), Google::CODE) &&
- $this->tfa->getProvider(Google::CODE)->isActive($user->getId());
- }
- }
|