2017-12-03 20:29:14 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-25 20:29:04 +00:00
|
|
|
use Illuminate\Support\Str;
|
2017-12-03 20:29:14 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
|
|
|
|
|
|
class MigratePubPrivFormatToSingleKey extends Migration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the migrations.
|
|
|
|
*/
|
2022-11-25 20:29:04 +00:00
|
|
|
public function up(): void
|
2017-12-03 20:29:14 +00:00
|
|
|
{
|
|
|
|
DB::transaction(function () {
|
|
|
|
DB::table('api_keys')->get()->each(function ($item) {
|
|
|
|
try {
|
|
|
|
$decrypted = Crypt::decrypt($item->secret);
|
2022-11-25 20:29:04 +00:00
|
|
|
} catch (DecryptException) {
|
2017-12-03 20:29:14 +00:00
|
|
|
$decrypted = str_random(32);
|
|
|
|
} finally {
|
|
|
|
DB::table('api_keys')->where('id', $item->id)->update([
|
|
|
|
'secret' => $decrypted,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Schema::table('api_keys', function (Blueprint $table) {
|
|
|
|
$table->dropColumn('public');
|
2022-11-25 20:29:04 +00:00
|
|
|
$table->renameColumn('secret', 'token');
|
2017-12-03 20:29:14 +00:00
|
|
|
});
|
|
|
|
|
2022-11-25 20:29:04 +00:00
|
|
|
Schema::table('api_keys', function (Blueprint $table) {
|
|
|
|
$table->char('token', 32)->change();
|
|
|
|
$table->unique('token');
|
|
|
|
});
|
2017-12-03 20:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse the migrations.
|
|
|
|
*/
|
2022-11-25 20:29:04 +00:00
|
|
|
public function down(): void
|
2017-12-03 20:29:14 +00:00
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
Schema::table('api_keys', function (Blueprint $table) {
|
|
|
|
$table->dropUnique(['token']);
|
|
|
|
$table->renameColumn('token', 'secret');
|
|
|
|
});
|
2017-12-03 20:29:14 +00:00
|
|
|
|
|
|
|
Schema::table('api_keys', function (Blueprint $table) {
|
2022-11-25 20:29:04 +00:00
|
|
|
$table->dropUnique('token');
|
|
|
|
$table->text('token')->change();
|
|
|
|
});
|
|
|
|
|
|
|
|
Schema::table('api_keys', function (Blueprint $table) {
|
|
|
|
$table->renameColumn('token', 'secret');
|
|
|
|
|
|
|
|
$table->text('secret')->nullable()->change();
|
2017-12-03 20:29:14 +00:00
|
|
|
$table->char('public', 16)->after('user_id');
|
|
|
|
});
|
|
|
|
|
|
|
|
DB::transaction(function () {
|
|
|
|
DB::table('api_keys')->get()->each(function ($item) {
|
|
|
|
DB::table('api_keys')->where('id', $item->id)->update([
|
2022-11-25 20:29:04 +00:00
|
|
|
'public' => Str::random(16),
|
2017-12-03 20:29:14 +00:00
|
|
|
'secret' => Crypt::encrypt($item->secret),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|