userConfigManager = $userConfigManager; $this->curlFactory = $curlFactory; $this->service = $service; $this->decoder = $decoder; $this->scopeConfig = $scopeConfig; $this->token = $token; } /** * Enroll in Authy * @param UserInterface $user * @return bool * @throws LocalizedException */ public function enroll(UserInterface $user) { $providerInfo = $this->userConfigManager->getProviderConfig($user->getId(), Authy::CODE); if (!isset($providerInfo['country_code'])) { throw new LocalizedException(__('Missing phone information')); } $url = $this->service->getProtectedApiEndpoint('users/new'); $curl = $this->curlFactory->create(); $curl->addHeader('X-Authy-API-Key', $this->service->getApiKey()); $curl->post($url, [ 'user[email]' => $user->getEmail(), 'user[cellphone]' => $providerInfo['phone_number'], 'user[country_code]' => $providerInfo['country_code'], ]); $response = $this->decoder->decode($curl->getBody()); if ($errorMessage = $this->service->getErrorFromResponse($response)) { throw new LocalizedException(__($errorMessage)); } $this->userConfigManager->addProviderConfig($user->getId(), Authy::CODE, [ 'user' => $response['user']['id'], ]); $this->userConfigManager->activateProviderConfiguration($user->getId(), Authy::CODE); return true; } /** * Return true if this provider has been enabled by admin * @return boolean */ public function isEnabled() { return !!$this->scopeConfig->getValue(static::XML_PATH_ENABLED) && !!$this->service->getApiKey(); } /** * Return true on token validation * @param UserInterface $user * @param DataObject $request * @return bool * @throws LocalizedException */ public function verify(UserInterface $user, DataObject $request) { return $this->token->verify($user, $request); } /** * Return true if this provider allows trusted devices * @return boolean */ public function isTrustedDevicesAllowed() { return !!$this->scopeConfig->getValue(static::XML_PATH_ALLOW_TRUSTED_DEVICES); } }