Token.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Api\TfaInterface;
  25. use MSP\TwoFactorAuth\Controller\Adminhtml\AbstractAction;
  26. use MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  27. /**
  28. * @SuppressWarnings(PHPMD.CamelCaseMethodName)
  29. */
  30. class Token extends AbstractAction
  31. {
  32. /**
  33. * @var Session
  34. */
  35. private $session;
  36. /**
  37. * @var JsonFactory
  38. */
  39. private $jsonFactory;
  40. /**
  41. * @var TfaInterface
  42. */
  43. private $tfa;
  44. /**
  45. * @var Authy\Token
  46. */
  47. private $token;
  48. /**
  49. * Token constructor.
  50. * @param Action\Context $context
  51. * @param JsonFactory $jsonFactory
  52. * @param TfaInterface $tfa
  53. * @param Authy\Token $token
  54. * @param Session $session
  55. */
  56. public function __construct(
  57. Action\Context $context,
  58. JsonFactory $jsonFactory,
  59. TfaInterface $tfa,
  60. Authy\Token $token,
  61. Session $session
  62. ) {
  63. parent::__construct($context);
  64. $this->session = $session;
  65. $this->jsonFactory = $jsonFactory;
  66. $this->tfa = $tfa;
  67. $this->token = $token;
  68. }
  69. /**
  70. * Get current user
  71. * @return \Magento\User\Model\User|null
  72. */
  73. private function getUser()
  74. {
  75. return $this->session->getUser();
  76. }
  77. /**
  78. * @inheritdoc
  79. */
  80. public function execute()
  81. {
  82. $via = $this->getRequest()->getParam('via');
  83. $result = $this->jsonFactory->create();
  84. try {
  85. $this->token->request($this->getUser(), $via);
  86. $res = ['success' => true];
  87. } catch (\Exception $e) {
  88. $result->setHttpResponseCode(500);
  89. $res = ['success' => false, 'message' => $e->getMessage()];
  90. }
  91. $result->setData($res);
  92. return $result;
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. protected function _isAllowed()
  98. {
  99. $user = $this->getUser();
  100. return
  101. $user &&
  102. $this->tfa->getProviderIsAllowed($user->getId(), Authy::CODE) &&
  103. $this->tfa->getProvider(Authy::CODE)->isActive($user->getId());
  104. }
  105. }