Skip to content

Commit

Permalink
[8.x] Automatic configuration of client uuids (laravel#1231)
Browse files Browse the repository at this point in the history
Automatic configuration of client UUIDs via passport:install --uuids
  • Loading branch information
taylorotwell authored May 4, 2020
1 parent dd4b1d9 commit 6896a17
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
13 changes: 13 additions & 0 deletions config/passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@

'public_key' => env('PASSPORT_PUBLIC_KEY'),

/*
|--------------------------------------------------------------------------
| Client UUIDs
|--------------------------------------------------------------------------
|
| By default, Passport uses auto-incrementing primary keys when assigning
| IDs to clients. However, if Passport is installed using the provided
| --uuids switch, this will be set to "true" and UUIDs will be used.
|
*/

'client_uuids' => false,

];
37 changes: 37 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Passport;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Client extends Model
{
Expand Down Expand Up @@ -41,6 +42,22 @@ class Client extends Model
'revoked' => 'bool',
];

/**
* Bootstrap the model and its traits.
*
* @return void
*/
public static function boot()
{
parent::boot();

static::creating(function ($model) {
if (config('passport.client_uuids')) {
$model->{$model->getKeyName()} = $model->{$model->getKeyName()} ?: (string) Str::orderedUuid();
}
});
}

/**
* Get the user that the client belongs to.
*
Expand Down Expand Up @@ -102,4 +119,24 @@ public function confidential()
{
return ! empty($this->secret);
}

/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return Passport::clientUuids() ? 'string' : $this->keyType;
}

/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return Passport::clientUuids() ? false : $this->incrementing;
}
}
49 changes: 49 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Passport\Console;

use Illuminate\Console\Command;
use Laravel\Passport\Passport;

class InstallCommand extends Command
{
Expand All @@ -12,6 +13,7 @@ class InstallCommand extends Command
* @var string
*/
protected $signature = 'passport:install
{--uuids : Use UUIDs for all client IDs}
{--force : Overwrite keys they already exist}
{--length=4096 : The length of the private key}';

Expand All @@ -30,7 +32,54 @@ class InstallCommand extends Command
public function handle()
{
$this->call('passport:keys', ['--force' => $this->option('force'), '--length' => $this->option('length')]);

if ($this->option('uuids')) {
$this->configureUuids();
}

$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client']);
}

/**
* Configure Passport for client UUIDs.
*
* @return void
*/
protected function configureUuids()
{
$this->call('vendor:publish', ['--tag' => 'passport-config']);
$this->call('vendor:publish', ['--tag' => 'passport-migrations']);

config(['passport.client_uuids' => true]);
Passport::setClientUuids(true);

$this->replaceInFile(config_path('passport.php'), 'false', 'true');
$this->replaceInFile(database_path('migrations/2016_06_01_000001_create_oauth_auth_codes_table.php'), '$table->unsignedBigInteger(\'client_id\');', '$table->uuid(\'client_id\');');
$this->replaceInFile(database_path('migrations/2016_06_01_000002_create_oauth_access_tokens_table.php'), '$table->unsignedBigInteger(\'client_id\');', '$table->uuid(\'client_id\');');
$this->replaceInFile(database_path('migrations/2016_06_01_000004_create_oauth_clients_table.php'), '$table->bigIncrements(\'id\');', '$table->uuid(\'id\')->primary();');
$this->replaceInFile(database_path('migrations/2016_06_01_000005_create_oauth_personal_access_clients_table.php'), '$table->unsignedBigInteger(\'client_id\');', '$table->uuid(\'client_id\');');

if ($this->confirm('In order to finish configuring client UUIDs, we need to rebuild the Passport database tables. Would you like to rollback and re-run your last migration?')) {
$this->call('migrate:rollback');
$this->call('migrate');
$this->line('');
}
}

/**
* Replace a given string in a given file.
*
* @param string $path
* @param string $search
* @param string $replace
* @return void
*/
protected function replaceInFile($path, $search, $replace)
{
file_put_contents(
$path,
str_replace($search, $replace, file_get_contents($path))
);
}
}
28 changes: 28 additions & 0 deletions src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ class Passport
*/
public static $clientModel = 'Laravel\Passport\Client';

/**
* Indicates if client's are identified by UUIDs.
*
* @var bool
*/
public static $clientUuids = false;

/**
* The personal access client model class name.
*
Expand Down Expand Up @@ -534,6 +541,27 @@ public static function client()
return new static::$clientModel;
}

/**
* Determine if clients are identified using UUIDs.
*
* @return bool
*/
public static function clientUuids()
{
return static::$clientUuids;
}

/**
* Specify if clients are identified using UUIDs.
*
* @param bool $value
* @return void
*/
public static function setClientUuids($value)
{
static::$clientUuids = $value;
}

/**
* Set the personal access client model class name.
*
Expand Down
4 changes: 3 additions & 1 deletion src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function boot()
*/
protected function registerMigrations()
{
if (Passport::$runsMigrations) {
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
return $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}
Expand All @@ -89,6 +89,8 @@ public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/passport.php', 'passport');

Passport::setClientUuids($this->app->make(Config::class)->get('passport.client_uuids', false));

$this->registerAuthorizationServer();
$this->registerResourceServer();
$this->registerGuard();
Expand Down

0 comments on commit 6896a17

Please sign in to comment.