$otp_parameters) { $parameters = array(); $parameters['otp_type'] = $otp_parameters['otp']['tokenType']; $parameters['service'] = $otp_parameters['name']; $parameters['account'] = $otp_parameters['otp']['account'] ?? $parameters['service']; $parameters['secret'] = $this->padToValidBase32Secret($otp_parameters['secret']); $parameters['algorithm'] = $otp_parameters['otp']['algorithm']; $parameters['digits'] = $otp_parameters['otp']['digits']; $parameters['counter'] = $otp_parameters['otp']['counter'] ?? null; $parameters['period'] = $otp_parameters['otp']['period'] ?? null; try { $twofaccounts[$key] = new TwoFAccount; $twofaccounts[$key]->fillWithOtpParameters($parameters); } catch (\Exception $exception) { Log::error(sprintf('Cannot instanciate a TwoFAccount object with 2FAS imported item #%s', $key)); Log::error($exception->getMessage()); // The token failed to generate a valid account so we create a fake account to be returned. $fakeAccount = new TwoFAccount(); $fakeAccount->id = TwoFAccount::FAKE_ID; $fakeAccount->otp_type = $otp_parameters['otp']['tokenType'] ?? TwoFAccount::TOTP; // Only basic fields are filled to limit the risk of another exception. $fakeAccount->account = $otp_parameters['otp']['account'] ?? __('twofaccounts.import.invalid_account'); $fakeAccount->service = $otp_parameters['name'] ?? __('twofaccounts.import.invalid_service'); // The secret field is used to pass the error, not very clean but will do the job for now. $fakeAccount->secret = $exception->getMessage(); $twofaccounts[$key] = $fakeAccount; } } return collect($twofaccounts); } }