Skip to content

Commit bc39485

Browse files
committed
Merge branch '8.x'
# Conflicts: # CHANGELOG.md # src/Client.php
2 parents 11d8533 + 6affa6e commit bc39485

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Release Notes
22

3-
## [Unreleased](https://github.com/laravel/passport/compare/v8.4.4...master)
3+
## [Unreleased](https://github.com/laravel/passport/compare/v8.5.0...master)
4+
5+
6+
## [v8.5.0 (2020-05-05)](https://github.com/laravel/passport/compare/v8.4.4...v8.5.0)
7+
8+
### Added
9+
- Automatic configuration of client UUIDs ([#1231](https://github.com/laravel/passport/pull/1231))
410

511

612
## [v8.4.4 (2020-04-21)](https://github.com/laravel/passport/compare/v8.4.3...v8.4.4)

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
{
@@ -48,6 +49,22 @@ class Client extends Model
4849
*/
4950
protected $plainSecret;
5051

52+
/**
53+
* Bootstrap the model and its traits.
54+
*
55+
* @return void
56+
*/
57+
public static function boot()
58+
{
59+
parent::boot();
60+
61+
static::creating(function ($model) {
62+
if (config('passport.client_uuids')) {
63+
$model->{$model->getKeyName()} = $model->{$model->getKeyName()} ?: (string) Str::orderedUuid();
64+
}
65+
});
66+
}
67+
5168
/**
5269
* Get the user that the client belongs to.
5370
*
@@ -142,4 +159,24 @@ public function confidential()
142159
{
143160
return ! empty($this->secret);
144161
}
162+
163+
/**
164+
* Get the auto-incrementing key type.
165+
*
166+
* @return string
167+
*/
168+
public function getKeyType()
169+
{
170+
return Passport::clientUuids() ? 'string' : $this->keyType;
171+
}
172+
173+
/**
174+
* Get the value indicating whether the IDs are incrementing.
175+
*
176+
* @return bool
177+
*/
178+
public function getIncrementing()
179+
{
180+
return Passport::clientUuids() ? false : $this->incrementing;
181+
}
145182
}

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

@@ -32,7 +34,54 @@ public function handle()
3234
$provider = in_array('users', array_keys(config('auth.providers'))) ? 'users' : null;
3335

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

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
*
@@ -539,6 +546,27 @@ public static function client()
539546
return new static::$clientModel;
540547
}
541548

549+
/**
550+
* Determine if clients are identified using UUIDs.
551+
*
552+
* @return bool
553+
*/
554+
public static function clientUuids()
555+
{
556+
return static::$clientUuids;
557+
}
558+
559+
/**
560+
* Specify if clients are identified using UUIDs.
561+
*
562+
* @param bool $value
563+
* @return void
564+
*/
565+
public static function setClientUuids($value)
566+
{
567+
static::$clientUuids = $value;
568+
}
569+
542570
/**
543571
* Set the personal access client model class name.
544572
*

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)