tfa = $tfa; $this->session = $session; $this->u2fKey = $u2fKey; $this->jsonFactory = $jsonFactory; $this->tfaSession = $tfaSession; $this->alert = $alert; } /** * Dispatch request * * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface */ public function execute() { $result = $this->jsonFactory->create(); try { $request = $this->getRequest()->getParam('request'); $response = $this->getRequest()->getParam('response'); $this->u2fKey->registerDevice($this->getUser(), $request, $response); $this->tfaSession->grantAccess(); $this->alert->event( 'MSP_TwoFactorAuth', 'U2F New device registered', AlertInterface::LEVEL_INFO, $this->getUser()->getUserName() ); $res = ['success' => true]; } catch (\Exception $e) { $this->alert->event( 'MSP_TwoFactorAuth', 'U2F error while adding device', AlertInterface::LEVEL_ERROR, $this->getUser()->getUserName(), AlertInterface::ACTION_LOG, $e->getMessage() ); $res = ['success' => false, 'message' => $e->getMessage()]; } $result->setData($res); return $result; } /** * @return \Magento\User\Model\User|null */ private function getUser() { return $this->session->getUser(); } /** * Check if admin has permissions to visit related pages * * @return bool */ protected function _isAllowed() { $user = $this->getUser(); return $user && $this->tfa->getProviderIsAllowed($user->getId(), U2fKey::CODE) && !$this->tfa->getProvider(U2fKey::CODE)->isActive($user->getId()); } }