userConfigManager = $userConfigManager; $this->curlFactory = $curlFactory; $this->service = $service; $this->decoder = $decoder; } /** * Request a token * @param UserInterface $user * @param string $via * @return true * @throws LocalizedException */ public function request(UserInterface $user, $via) { if (!in_array($via, ['call', 'sms'])) { throw new LocalizedException(__('Unsupported via method')); } $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE); if (!isset($providerInfo['user'])) { throw new LocalizedException(__('Missing user information')); } $url = $this->service->getProtectedApiEndpoint('' . $via . '/' . $providerInfo['user']) . '?force=true'; $curl = $this->curlFactory->create(); $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey()); $curl->get($url); $response = $this->decoder->decode($curl->getBody()); if ($errorMessage = $this->service->getErrorFromResponse($response)) { throw new LocalizedException(__($errorMessage)); } return true; } /** * Return true on token validation * @param UserInterface $user * @param DataObject $request * @return bool * @throws LocalizedException */ public function verify(UserInterface $user, DataObject $request) { $code = $request->getData('tfa_code'); if (!preg_match('/^\w+$/', $code)) { throw new LocalizedException(__('Invalid code format')); } $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE); if (!isset($providerInfo['user'])) { throw new LocalizedException(__('Missing user information')); } $url = $this->service->getProtectedApiEndpoint('verify/' . $code . '/' . $providerInfo['user']); $curl = $this->curlFactory->create(); $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey()); $curl->get($url); $response = $this->decoder->decode($curl->getBody()); if ($errorMessage = $this->service->getErrorFromResponse($response)) { throw new LocalizedException(__($errorMessage)); } return true; } }