diff --git a/database/migrations/2021_09_08_191139_split_twofaccounts_uri_in_multiple_columns.php b/database/migrations/2021_09_08_191139_split_twofaccounts_uri_in_multiple_columns.php index a3ce72cb..d2dfe0d6 100644 --- a/database/migrations/2021_09_08_191139_split_twofaccounts_uri_in_multiple_columns.php +++ b/database/migrations/2021_09_08_191139_split_twofaccounts_uri_in_multiple_columns.php @@ -16,8 +16,6 @@ class SplitTwofaccountsUriInMultipleColumns extends Migration */ public function up() { - $driver = Schema::connection($this->getConnection())->getConnection()->getDriverName(); - // as SQLITE disallow to add a not nullable column without default // value when altering a table we add all columns as nullable and // change them right after to not nullable column @@ -29,55 +27,10 @@ public function up() $table->unsignedInteger('period')->nullable(); $table->unsignedBigInteger('counter')->nullable(); }); - - // Schema::table('twofaccounts', function (Blueprint $table){ - // $table->string('otp_type', 10)->nullable(false)->change(); - // $table->text('secret')->nullable(false)->change(); - // $table->string('algorithm', 20)->nullable(false)->change(); - // $table->unsignedSmallInteger('digits')->nullable(false)->change(); - // }); - - // Apply previous migration 'AlterEncryptedColumnsToText' even to sqlite base - if ('sqlite' === $driver) { - - Schema::table('twofaccounts', function (Blueprint $table) { - $table->text('account')->change(); - }); - - Schema::table('twofaccounts', function (Blueprint $table) { - $table->text('uri')->change(); - }); - } Schema::table('twofaccounts', function (Blueprint $table) { $table->renameColumn('uri', 'legacy_uri'); }); - - - $twofaccounts = DB::table('twofaccounts')->select('id', 'legacy_uri')->get(); - $settingService = resolve('App\Services\SettingService'); - - foreach ($twofaccounts as $twofaccount) { - try { - $legacy_uri = $settingService->get('useEncryption') ? Crypt::decryptString($twofaccount->legacy_uri) : $twofaccount->legacy_uri; - $token = \OTPHP\Factory::loadFromProvisioningUri($legacy_uri); - - $affected = DB::table('twofaccounts') - ->where('id', $twofaccount->id) - ->update([ - 'otp_type' => get_class($token) === 'OTPHP\TOTP' ? 'totp' : 'hotp', - 'secret' => $settingService->get('useEncryption') ? Crypt::encryptString($token->getSecret()) : $token->getSecret(), - 'algorithm' => $token->getDigest(), - 'digits' => $token->getDigits(), - 'period' => $token->hasParameter('period') ? $token->getParameter('period') : null, - 'counter' => $token->hasParameter('counter') ? $token->getParameter('counter') : null - ]); - } - catch(Exception $ex) - { - Log::error($ex->getMessage()); - } - } } /** diff --git a/database/migrations/2021_09_14_195451_change_account_not_nullable_twofaccounts_table.php b/database/migrations/2021_09_14_195451_change_account_not_nullable_twofaccounts_table.php index 30c8d980..69a710b9 100644 --- a/database/migrations/2021_09_14_195451_change_account_not_nullable_twofaccounts_table.php +++ b/database/migrations/2021_09_14_195451_change_account_not_nullable_twofaccounts_table.php @@ -2,7 +2,10 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Crypt; +use Illuminate\Support\Facades\Log; class ChangeAccountNotNullableTwofaccountsTable extends Migration { @@ -13,10 +16,54 @@ class ChangeAccountNotNullableTwofaccountsTable extends Migration */ public function up() { + $driver = Schema::connection($this->getConnection())->getConnection()->getDriverName(); + Schema::table('twofaccounts', function (Blueprint $table) { $table->text('account')->nullable(false)->change(); $table->string('service')->nullable()->change(); + $table->string('otp_type', 10)->nullable(false)->change(); + $table->text('secret')->nullable(false)->change(); + $table->string('algorithm', 20)->nullable(false)->change(); + $table->unsignedSmallInteger('digits')->nullable(false)->change(); }); + + // Apply migration 'AlterEncryptedColumnsToText' even to sqlite base + if ('sqlite' === $driver) { + + Schema::table('twofaccounts', function (Blueprint $table) { + $table->text('account')->change(); + }); + + Schema::table('twofaccounts', function (Blueprint $table) { + $table->text('legacy_uri')->change(); + }); + } + + + $twofaccounts = DB::table('twofaccounts')->select('id', 'legacy_uri')->get(); + $settingService = resolve('App\Services\SettingService'); + + foreach ($twofaccounts as $twofaccount) { + try { + $legacy_uri = $settingService->get('useEncryption') ? Crypt::decryptString($twofaccount->legacy_uri) : $twofaccount->legacy_uri; + $token = \OTPHP\Factory::loadFromProvisioningUri($legacy_uri); + + $affected = DB::table('twofaccounts') + ->where('id', $twofaccount->id) + ->update([ + 'otp_type' => get_class($token) === 'OTPHP\TOTP' ? 'totp' : 'hotp', + 'secret' => $settingService->get('useEncryption') ? Crypt::encryptString($token->getSecret()) : $token->getSecret(), + 'algorithm' => $token->getDigest(), + 'digits' => $token->getDigits(), + 'period' => $token->hasParameter('period') ? $token->getParameter('period') : null, + 'counter' => $token->hasParameter('counter') ? $token->getParameter('counter') : null + ]); + } + catch(Exception $ex) + { + Log::error($ex->getMessage()); + } + } } /**