PHP
[Laravel] laravel Passport oauth uuid 사용하기
sjwiq200
2020. 6. 1. 15:07
728x90
반응형
Passport 관련 테이블을 int 가 아닌 uuid를 사용하는 방법에 대해서 포스팅하겠습니다.
해당 명령어를 통해 vendor 에 있는 oauth 관련 migration 들을 /app/database/migrations에 가져올 수 있습니다.
php artisan vendor:publish --tag=passport-migrations 를 사용하여 기본 마이그레이션을 가져올 수 있습니다.
[oauth_auth_codes] - table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOauthAuthCodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_auth_codes', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->bigInteger('user_id');
$table->uuid('client_id');
$table->text('scopes')->nullable();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_auth_codes');
}
}
[oauth_access_tokens] - table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOauthAccessTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->bigInteger('user_id')->index()->nullable();
$table->uuid('client_id');
$table->string('name')->nullable();
$table->text('scopes')->nullable();
$table->boolean('revoked');
$table->timestamps();
$table->dateTime('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_access_tokens');
}
}
[oauth_refresh_tokens] - table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOauthRefreshTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_refresh_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('access_token_id', 100)->index();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_refresh_tokens');
}
}
[oauth_clients] - table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOauthClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_clients', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
$table->bigInteger('user_id')->index()->nullable();
$table->string('name');
$table->string('secret', 100);
$table->text('redirect');
$table->boolean('personal_access_client');
$table->boolean('password_client');
$table->boolean('revoked');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_clients');
}
}
[oauth_personal_access_clients] - table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOauthPersonalAccessClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_personal_access_clients', function (Blueprint $table) {
$table->increments('id');
$table->uuid('client_id')->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_personal_access_clients');
}
}
/database/migrations/ 밑에 해당 테이블들의 코드를 추가하거나 바꿔 주세요!
그리고 /app/providers/AuthServiceProvider.php 파일에서
boot 메소드에 아래 코드를 추가해주세요~!
/* passport table id uuid*/
Client::creating(function (Client $client) {
$client->incrementing = false;
$client->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
});
Client::retrieved(function (Client $client) {
$client->incrementing = false;
});
그러면 기본 passport는 int로 키값을 지정하는데 uuid로 키값을 지정할 수 있습니다~!
감사합니다.
728x90
반응형