diff --git a/.travis.yml b/.travis.yml index bb6e6e5e..61ea48bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ php: # - 8.0 services: - - mysql + # - mysql before_install: # - mysql -e 'CREATE DATABASE 2fauth_test;' @@ -20,12 +20,10 @@ before_script: - travis_retry composer install --no-interaction # no need to use a dedicated Travis .env file as phpunit # will use .env.testing by default - - php artisan cache:clear script: # - DATABASE=mysql vendor/bin/phpunit -c phpunit-mysql.xml - cp .env.travis .env - - php artisan cache:clear - php artisan config:clear - php artisan key:generate - DATABASE=sqlite vendor/bin/phpunit -c phpunit.xml --coverage-clover=coverage.xml diff --git a/database/migrations/2020_12_04_073605_alter_encrypted_columns_to_text_for_sqlite.php b/database/migrations/2020_12_04_073605_alter_encrypted_columns_to_text_for_sqlite.php index 10be2de1..ed874574 100644 --- a/database/migrations/2020_12_04_073605_alter_encrypted_columns_to_text_for_sqlite.php +++ b/database/migrations/2020_12_04_073605_alter_encrypted_columns_to_text_for_sqlite.php @@ -19,7 +19,6 @@ public function up() Schema::table('twofaccounts', function (Blueprint $table) { $table->text('account')->change(); - $table->text('uri')->change(); }); } } diff --git a/database/migrations/2020_12_04_073606_alter_encrypted_columns_to_text_for_sqlite_bis.php b/database/migrations/2020_12_04_073606_alter_encrypted_columns_to_text_for_sqlite_bis.php new file mode 100644 index 00000000..f5e54a60 --- /dev/null +++ b/database/migrations/2020_12_04_073606_alter_encrypted_columns_to_text_for_sqlite_bis.php @@ -0,0 +1,37 @@ +getConnection())->getConnection()->getDriverName(); + + if ('sqlite' === $driver) { + + Schema::table('twofaccounts', function (Blueprint $table) { + $table->text('uri')->change(); + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('twofaccounts', function (Blueprint $table) { + // + }); + } +} 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 aa6f3f4a..bc1db2a6 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,44 +16,17 @@ class SplitTwofaccountsUriInMultipleColumns extends Migration */ public function up() { + // 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 Schema::table('twofaccounts', function (Blueprint $table) { - $table->string('otp_type', 10); - $table->text('secret'); - $table->string('algorithm', 20); - $table->unsignedSmallInteger('digits'); + $table->string('otp_type', 10)->nullable(); + $table->text('secret')->nullable(); + $table->string('algorithm', 20)->nullable(); + $table->unsignedSmallInteger('digits')->nullable(); $table->unsignedInteger('period')->nullable(); $table->unsignedBigInteger('counter')->nullable(); }); - - 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()); - } - } } /** @@ -86,9 +59,5 @@ public function down() Schema::table('twofaccounts', function (Blueprint $table) { $table->dropColumn('counter'); }); - - Schema::table('twofaccounts', function (Blueprint $table) { - $table->renameColumn('legacy_uri', 'uri'); - }); } } diff --git a/database/migrations/2021_09_08_191140_rename_uri_to_legacy_uri.php b/database/migrations/2021_09_08_191140_rename_uri_to_legacy_uri.php new file mode 100644 index 00000000..dcb8196e --- /dev/null +++ b/database/migrations/2021_09_08_191140_rename_uri_to_legacy_uri.php @@ -0,0 +1,32 @@ +renameColumn('uri', 'legacy_uri'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('twofaccounts', function (Blueprint $table) { + $table->renameColumn('legacy_uri', 'uri'); + }); + } +} diff --git a/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php b/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php new file mode 100644 index 00000000..5958f51e --- /dev/null +++ b/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php @@ -0,0 +1,63 @@ +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(); + }); + + + $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()); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +}