The converted accounts */ public function migrate(mixed $migrationPayload) : Collection { $json = json_decode(htmlspecialchars_decode($migrationPayload), true); if (is_null($json) || Arr::has($json, 'services') == false) { Log::error('Aegis JSON migration data cannot be read'); throw new InvalidMigrationDataException('2FAS Auth'); } $twofaccounts = []; foreach ($json['services'] as $key => $otp_parameters) { $parameters = []; $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'] = strtolower($parameters['otp_type']) === 'hotp' && $otp_parameters['otp']['counter'] > 0 ? $otp_parameters['otp']['counter'] : null; $parameters['period'] = strtolower($parameters['otp_type']) === 'totp' && $otp_parameters['otp']['period'] > 0 ? $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::debug($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); } }