Skip to content

Commit 6896a17

Browse files
authored
[8.x] Automatic configuration of client uuids (laravel#1231)
Automatic configuration of client UUIDs via passport:install --uuids
1 parent dd4b1d9 commit 6896a17

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

config/passport.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@
1717

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

20+
/*
21+
|--------------------------------------------------------------------------
22+
| Client UUIDs
23+
|--------------------------------------------------------------------------
24+
|
25+
| By default, Passport uses auto-incrementing primary keys when assigning
26+
| IDs to clients. However, if Passport is installed using the provided
27+
| --uuids switch, this will be set to "true" and UUIDs will be used.
28+
|
29+
*/
30+
31+
'client_uuids' => false,
32+
2033
];

src/Client.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Passport;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Str;
67

78
class Client extends Model
89
{
@@ -41,6 +42,22 @@ class Client extends Model
4142
'revoked' => 'bool',
4243
];
4344

45+
/**
46+
* Bootstrap the model and its traits.
47+
*
48+
* @return void
49+
*/
50+
public static function boot()
51+
{
52+
parent::boot();
53+
54+
static::creating(function ($model) {
55+
if (config('passport.client_uuids')) {
56+
$model->{$model->getKeyName()} = $model->{$model->getKeyName()} ?: (string) Str::orderedUuid();
57+
}
58+
});
59+
}
60+
4461
/**
4562
* Get the user that the client belongs to.
4663
*
@@ -102,4 +119,24 @@ public function confidential()
102119
{
103120
return ! empty($this->secret);
104121
}
122+
123+
/**
124+
* Get the auto-incrementing key type.
125+
*
126+
* @return string
127+
*/
128+
public function getKeyType()
129+
{
130+
return Passport::clientUuids() ? 'string' : $this->keyType;
131+
}
132+
133+
/**
134+
* Get the value indicating whether the IDs are incrementing.
135+
*
136+
* @return bool
137+
*/
138+
public function getIncrementing()
139+
{
140+
return Passport::clientUuids() ? false : $this->incrementing;
141+
}
105142
}

src/Console/InstallCommand.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Passport\Console;
44

55
use Illuminate\Console\Command;
6+
use Laravel\Passport\Passport;
67

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

@@ -30,7 +32,54 @@ class InstallCommand extends Command
3032
public function handle()
3133
{
3234
$this->call('passport:keys', ['--force' => $this->option('force'), '--length' => $this->option('length')]);
35+
36+
if ($this->option('uuids')) {
37+
$this->configureUuids();
38+
}
39+
3340
$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
3441
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client']);
3542
}
43+
44+
/**
45+
* Configure Passport for client UUIDs.
46+
*
47+
* @return void
48+
*/
49+
protected function configureUuids()
50+
{
51+
$this->call('vendor:publish', ['--tag' => 'passport-config']);
52+
$this->call('vendor:publish', ['--tag' => 'passport-migrations']);
53+
54+
config(['passport.client_uuids' => true]);
55+
Passport::setClientUuids(true);
56+
57+
$this->replaceInFile(config_path('passport.php'), 'false', 'true');
58+
$this->replaceInFile(database_path('migrations/2016_06_01_000001_create_oauth_auth_codes_table.php'), '$table->unsignedBigInteger(\'client_id\');', '$table->uuid(\'client_id\');');
59+
$this->replaceInFile(database_path('migrations/2016_06_01_000002_create_oauth_access_tokens_table.php'), '$table->unsignedBigInteger(\'client_id\');', '$table->uuid(\'client_id\');');
60+
$this->replaceInFile(database_path('migrations/2016_06_01_000004_create_oauth_clients_table.php'), '$table->bigIncrements(\'id\');', '$table->uuid(\'id\')->primary();');
61+
$this->replaceInFile(database_path('migrations/2016_06_01_000005_create_oauth_personal_access_clients_table.php'), '$table->unsignedBigInteger(\'client_id\');', '$table->uuid(\'client_id\');');
62+
63+
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?')) {
64+
$this->call('migrate:rollback');
65+
$this->call('migrate');
66+
$this->line('');
67+
}
68+
}
69+
70+
/**
71+
* Replace a given string in a given file.
72+
*
73+
* @param string $path
74+
* @param string $search
75+
* @param string $replace
76+
* @return void
77+
*/
78+
protected function replaceInFile($path, $search, $replace)
79+
{
80+
file_put_contents(
81+
$path,
82+
str_replace($search, $replace, file_get_contents($path))
83+
);
84+
}
3685
}

src/Passport.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ class Passport
112112
*/
113113
public static $clientModel = 'Laravel\Passport\Client';
114114

115+
/**
116+
* Indicates if client's are identified by UUIDs.
117+
*
118+
* @var bool
119+
*/
120+
public static $clientUuids = false;
121+
115122
/**
116123
* The personal access client model class name.
117124
*
@@ -534,6 +541,27 @@ public static function client()
534541
return new static::$clientModel;
535542
}
536543

544+
/**
545+
* Determine if clients are identified using UUIDs.
546+
*
547+
* @return bool
548+
*/
549+
public static function clientUuids()
550+
{
551+
return static::$clientUuids;
552+
}
553+
554+
/**
555+
* Specify if clients are identified using UUIDs.
556+
*
557+
* @param bool $value
558+
* @return void
559+
*/
560+
public static function setClientUuids($value)
561+
{
562+
static::$clientUuids = $value;
563+
}
564+
537565
/**
538566
* Set the personal access client model class name.
539567
*

src/PassportServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function boot()
7575
*/
7676
protected function registerMigrations()
7777
{
78-
if (Passport::$runsMigrations) {
78+
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
7979
return $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
8080
}
8181
}
@@ -89,6 +89,8 @@ public function register()
8989
{
9090
$this->mergeConfigFrom(__DIR__.'/../config/passport.php', 'passport');
9191

92+
Passport::setClientUuids($this->app->make(Config::class)->get('passport.client_uuids', false));
93+
9294
$this->registerAuthorizationServer();
9395
$this->registerResourceServer();
9496
$this->registerGuard();

0 commit comments

Comments
 (0)