curlFactory = $curlFactory; $this->service = $service; $this->userConfigManager = $userConfigManager; $this->decoder = $decoder; $this->dateTime = $dateTime; } /** * Verify phone number * @param UserInterface $user * @param string $country * @param string $phoneNumber * @param string $method * @param array &$response * @return true * @throws LocalizedException */ public function request(UserInterface $user, $country, $phoneNumber, $method, &$response) { $url = $this->service->getProtectedApiEndpoint('phones/verification/start'); $curl = $this->curlFactory->create(); $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey()); $curl->post($url, [ 'via' => $method, 'country_code' => $country, 'phone_number' => $phoneNumber ]); $response = $this->decoder->decode($curl->getBody()); if ($errorMessage = $this->service->getErrorFromResponse($response)) { throw new LocalizedException(__($errorMessage)); } $this->userConfigManager->addProviderConfig($user->getId(), Authy::CODE, [ 'country_code' => $country, 'phone_number' => $phoneNumber, 'carrier' => $response['carrier'], 'mobile' => $response['is_cellphone'], 'verify' => [ 'uuid' => $response['uuid'], 'via' => $method, 'expires' => $this->dateTime->timestamp() + $response['seconds_to_expire'], 'seconds_to_expire' => $response['seconds_to_expire'], 'message' => $response['message'], ], 'phone_confirmed' => false, ]); return true; } /** * Verify phone number * @param UserInterface $user * @param string $verificationCode * @return true * @throws LocalizedException */ public function verify(UserInterface $user, $verificationCode) { $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE); if (!isset($providerInfo['country_code'])) { throw new LocalizedException(__('Missing verify request information')); } $url = $this->service->getProtectedApiEndpoint('phones/verification/check'); $curl = $this->curlFactory->create(); $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey()); $curl->get($url . '?' . http_build_query([ 'country_code' => $providerInfo['country_code'], 'phone_number' => $providerInfo['phone_number'], 'verification_code' => $verificationCode, ])); $response = $this->decoder->decode($curl->getBody()); if ($errorMessage = $this->service->getErrorFromResponse($response)) { throw new LocalizedException(__($errorMessage)); } $this->userConfigManager->addProviderConfig($user->getId(), Authy::CODE, [ 'phone_confirmed' => true, ]); $this->userConfigManager->activateProviderConfiguration($user->getId(), Authy::CODE); return true; } }