Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.x] Make client RFC compatible #1744

Merged
merged 31 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3563bc2
make client RFC compatible
hafezdivandari May 18, 2024
a521a78
formatting
hafezdivandari May 18, 2024
e906821
drop support for Laravel 9
hafezdivandari May 18, 2024
7b33d5d
update upgrade guide
hafezdivandari May 19, 2024
45c5143
Merge branch '13.x' into 13.x-refactor-client
hafezdivandari May 30, 2024
5bca853
wip
hafezdivandari May 30, 2024
7d7addc
Merge branch '13.x' into 13.x-refactor-client
hafezdivandari Jun 4, 2024
38a36be
Merge branch '13.x' into 13.x-refactor-client
hafezdivandari Jun 27, 2024
aae97b1
Merge branch '13.x' into 13.x-refactor-client
hafezdivandari Jun 27, 2024
da3edf0
revert some changes
hafezdivandari Jun 27, 2024
676b43c
Merge branch '13.x' into 13.x-refactor-client
hafezdivandari Jul 29, 2024
d8e89ca
fix tests
hafezdivandari Jul 29, 2024
3c66423
wip
hafezdivandari Jul 29, 2024
e4931d5
fix tests
hafezdivandari Jul 29, 2024
2784abf
wip
hafezdivandari Jul 29, 2024
7edb0ef
wip
hafezdivandari Jul 29, 2024
14df618
wip
hafezdivandari Jul 29, 2024
82bc8da
wip
hafezdivandari Jul 29, 2024
baa6395
wip
hafezdivandari Jul 29, 2024
a579f4f
wip
hafezdivandari Jul 30, 2024
5c7944a
wip
hafezdivandari Jul 30, 2024
1149e9c
wip
hafezdivandari Jul 30, 2024
405d559
formatting
hafezdivandari Jul 30, 2024
1fc5d18
formatting
hafezdivandari Jul 30, 2024
407082f
formatting
hafezdivandari Jul 30, 2024
dfdec66
wip
hafezdivandari Aug 2, 2024
7235890
revert unrelated changes
hafezdivandari Aug 4, 2024
d3136c4
fix backward compatibility
hafezdivandari Aug 4, 2024
6aeb388
formatting
hafezdivandari Aug 4, 2024
86694ee
fix bc on update
hafezdivandari Aug 4, 2024
bf7ce0d
add tests for clients without grant_types
hafezdivandari Aug 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ jobs:
fail-fast: true
matrix:
php: [8.1, 8.2, 8.3]
laravel: [9, 10, 11]
laravel: [10, 11]
exclude:
- php: 8.1
laravel: 11
- php: 8.3
laravel: 9

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
86 changes: 86 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,98 @@ PR: https://github.com/laravel/passport/pull/1734

PHP 8.1 is now the minimum required version.

### Minimum Laravel Version

PR: https://github.com/laravel/passport/pull/1744

Laravel 10.0 is now the minimum required version.

### OAuth2 Server

PR: https://github.com/laravel/passport/pull/1734

The `league/oauth2-server` Composer package which is utilized internally by Passport has been updated to 9.0, which adds additional types to method signatures. To ensure your application is compatible, you should review this package's complete [changelog](https://github.com/thephpleague/oauth2-server/blob/master/CHANGELOG.md#900---released-2024-05-13).

### Client Secret

PR: https://github.com/laravel/passport/pull/1744

Passport now always hashes clients' secret, you may call the following command if you need to hash your clients' secrets:

php artisan passport:hash

### Client ID

PR: https://github.com/laravel/passport/pull/1744

Passport now uses UUID to identify clients. If you were already using client with UUIDs you don't have to make any changes, but if you were not using client with UUIDs you must change the type of client ID columns to `char(36)`. Your previous client IDs won't change and you will get UUID for newly created clients from now on:

```php
Schema::table('oauth_clients', function (Blueprint $table) {
$table->char('id', 36)->change();
hafezdivandari marked this conversation as resolved.
Show resolved Hide resolved
});

Schema::table('oauth_auth_codes', function (Blueprint $table) {
$table->char('client_id', 36)->index()->change();
});

Schema::table('oauth_access_tokens', function (Blueprint $table) {
$table->char('client_id', 36)->index()->change();
});
```

### Clients Table

PR: https://github.com/laravel/passport/pull/1744

The `oauth_clients` table now requires `grant_types`, `scopes` and `redirect_uris` columns as JSON array and `personal_access_client` and `password_client` columns are removed:

```php
Schema::table('oauth_clients', function (Blueprint $table) {
$table->after('name', function (Blueprint $table) {
$table->text('grant_types');
$table->text('scopes');
$table->text('redirect_uris');
});
});

foreach (Passport::client()->cursor() as $client) {
Model::withoutTimestamps(fn () => $client->forceFill([
'grant_types' => match (true) {
(bool) $client->personal_access_client => ['personal_access'],
(bool) $client->password_client => ['password', 'refresh_token'],
empty($client->secret) && ! empty($client->redirect) => ['authorization_code', 'implicit', 'refresh_token'],
! empty($client->secret) && empty($client->redirect) => ['client_credentials'],
! empty($client->secret) && ! empty($client->redirect) => ['authorization_code', 'implicit', 'refresh_token', 'client_credentials'],
default => [],
},
'scopes' => ['*'],
'redirect_uris' => explode(',', $client->redirect),
])->save());
}

Schema::table('oauth_clients', function (Blueprint $table) {
$table->dropColumn(['redirect', 'personal_access_client', 'password_client']);
});
```

### Removed functionalities

PR: https://github.com/laravel/passport/pull/1744

The following list of properties and methods have been removed:

* `Passport::$clientUuids` property.
* `Passport::clientUuids()` method.
* `Passport::setClientUuids()` method.
* `'passport.client_uuids'` config property.
* `Passport::$hashesClientSecrets` property.
* `Passport::hashClientSecrets()` method.
* `Passport::$personalAccessClientModel` property.
* `Passport::usePersonalAccessClientModel()` method.
* `Passport::personalAccessClientModel()` method.
* `Passport::personalAccessClient()` method.

## Upgrading To 12.0 From 11.x

### Migration Changes
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"ext-json": "*",
"ext-openssl": "*",
"firebase/php-jwt": "^6.4",
"illuminate/auth": "^9.21|^10.0|^11.0",
"illuminate/console": "^9.21|^10.0|^11.0",
"illuminate/container": "^9.21|^10.0|^11.0",
"illuminate/contracts": "^9.21|^10.0|^11.0",
"illuminate/cookie": "^9.21|^10.0|^11.0",
"illuminate/database": "^9.21|^10.0|^11.0",
"illuminate/encryption": "^9.21|^10.0|^11.0",
"illuminate/http": "^9.21|^10.0|^11.0",
"illuminate/support": "^9.21|^10.0|^11.0",
"illuminate/auth": "^10.0|^11.0",
"illuminate/console": "^10.0|^11.0",
"illuminate/container": "^10.0|^11.0",
"illuminate/contracts": "^10.0|^11.0",
"illuminate/cookie": "^10.0|^11.0",
"illuminate/database": "^10.0|^11.0",
"illuminate/encryption": "^10.0|^11.0",
"illuminate/http": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"lcobucci/jwt": "^5.0",
"league/oauth2-server": "^9.0",
"nyholm/psr7": "^1.5",
Expand Down
13 changes: 0 additions & 13 deletions config/passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@

'connection' => env('PASSPORT_CONNECTION'),

/*
|--------------------------------------------------------------------------
| 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,

/*
|--------------------------------------------------------------------------
| Personal Access Client
Expand Down
34 changes: 8 additions & 26 deletions database/factories/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,16 @@ public function modelName()
*/
public function definition()
{
return $this->ensurePrimaryKeyIsSet([
return [
'user_id' => null,
'name' => $this->faker->company(),
'grant_types' => [],
'scopes' => [],
'redirect_uris' => [$this->faker->url()],
'provider' => null,
'secret' => Str::random(40),
'redirect' => $this->faker->url(),
'personal_access_client' => false,
'password_client' => false,
'revoked' => false,
]);
}

/**
* Ensure the primary key is set on the model when using UUIDs.
*
* @param array $data
* @return array
*/
protected function ensurePrimaryKeyIsSet(array $data)
{
if (Passport::clientUuids()) {
$keyName = (new ($this->modelName()))->getKeyName();

$data[$keyName] = (string) Str::orderedUuid();
}

return $data;
];
}

/**
Expand All @@ -64,8 +48,7 @@ protected function ensurePrimaryKeyIsSet(array $data)
public function asPasswordClient()
{
return $this->state([
'personal_access_client' => false,
'password_client' => true,
'grant_types' => ['password', 'refresh_token'],
]);
}

Expand All @@ -77,8 +60,7 @@ public function asPasswordClient()
public function asClientCredentials()
{
return $this->state([
'personal_access_client' => false,
'password_client' => false,
'grant_types' => ['client_credentials'],
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
public function up(): void
{
Schema::create('oauth_auth_codes', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->unsignedBigInteger('user_id')->index();
$table->unsignedBigInteger('client_id');
$table->text('scopes')->nullable();
$table->char('id', 80)->primary();
$table->foreignId('user_id')->index();
$table->foreignUuid('client_id')->index();
$table->text('scopes');
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
Expand All @@ -28,4 +28,14 @@ public function down(): void
{
Schema::dropIfExists('oauth_auth_codes');
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return $this->connection ?? config('passport.connection');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
public function up(): void
{
Schema::create('oauth_access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->unsignedBigInteger('client_id');
$table->char('id', 80)->primary();
$table->foreignId('user_id')->nullable()->index();
$table->foreignUuid('client_id')->index();
$table->string('name')->nullable();
$table->text('scopes')->nullable();
$table->text('scopes');
$table->boolean('revoked');
$table->timestamps();
$table->dateTime('expires_at')->nullable();
Expand All @@ -30,4 +30,14 @@ public function down(): void
{
Schema::dropIfExists('oauth_access_tokens');
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return $this->connection ?? config('passport.connection');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public function up(): void
{
Schema::create('oauth_refresh_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('access_token_id', 100)->index();
$table->char('id', 80)->primary();
$table->char('access_token_id', 80)->index();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
Expand All @@ -26,4 +26,14 @@ public function down(): void
{
Schema::dropIfExists('oauth_refresh_tokens');
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return $this->connection ?? config('passport.connection');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
public function up(): void
{
Schema::create('oauth_clients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->nullable()->index();
$table->uuid('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('name');
$table->string('secret', 100)->nullable();
$table->text('grant_types');
$table->text('scopes');
$table->text('redirect_uris');
$table->string('provider')->nullable();
$table->text('redirect');
$table->boolean('personal_access_client');
$table->boolean('password_client');
$table->string('secret')->nullable();
$table->boolean('revoked');
$table->timestamps();
});
Expand All @@ -32,4 +32,14 @@ public function down(): void
{
Schema::dropIfExists('oauth_clients');
}

/**
* Get the migration connection name.
*
* @return string|null
*/
public function getConnection()
{
return $this->connection ?? config('passport.connection');
}
};

This file was deleted.

4 changes: 2 additions & 2 deletions src/Bridge/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class Client implements ClientEntityInterface
public function __construct(
string $identifier,
string $name,
string $redirectUri,
string|array $redirectUri,
bool $isConfidential = false,
?string $provider = null
) {
$this->setIdentifier($identifier);

$this->name = $name;
$this->isConfidential = $isConfidential;
$this->redirectUri = explode(',', $redirectUri);
$this->redirectUri = is_array($redirectUri) ? $redirectUri : [$redirectUri];
$this->provider = $provider;
}
}
Loading