Skip to content

Commit fc7e2da

Browse files
hafezdivandaridriesvintstaylorotwell
authored
[13.x] Support OAuth2 Server v9 (#1734)
* support OAuth2 Server 9 * formatting * formatting * drop php 8.0 * use fqn * wip * wip * wip * formatting * formatting * wip * remove redundant attribute * fix tests * update upgrade guide * force re-run tests * wip * wip * add an entry on upgrade guide for oauth2 server * Update UPGRADE.md * Update UPGRADE.md * Update UPGRADE.md --------- Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8ea1dd4 commit fc7e2da

30 files changed

+173
-223
lines changed

.github/workflows/tests.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ jobs:
1616
strategy:
1717
fail-fast: true
1818
matrix:
19-
php: ['8.0', 8.1, 8.2, 8.3]
19+
php: [8.1, 8.2, 8.3]
2020
laravel: [9, 10, 11]
2121
exclude:
22-
- php: '8.0'
23-
laravel: 10
24-
- php: '8.0'
25-
laravel: 11
2622
- php: 8.1
2723
laravel: 11
2824
- php: 8.3

UPGRADE.md

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
## General Notes
44

5+
## Upgrading To 13.0 From 12.x
6+
7+
### Minimum PHP Version
8+
9+
PR: https://github.com/laravel/passport/pull/1734
10+
11+
PHP 8.1 is now the minimum required version.
12+
13+
### OAuth2 Server
14+
15+
PR: https://github.com/laravel/passport/pull/1734
16+
17+
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).
18+
519
## Upgrading To 12.0 From 11.x
620

721
### Migration Changes
@@ -14,6 +28,8 @@ php artisan vendor:publish --tag=passport-migrations
1428

1529
### Password Grant Type
1630

31+
PR: https://github.com/laravel/passport/pull/1715
32+
1733
The password grant type is disabled by default. You may enable it by calling the `enablePasswordGrant` method in the `boot` method of your application's `App\Providers\AppServiceProvider` class:
1834

1935
```php

composer.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
}
1515
],
1616
"require": {
17-
"php": "^8.0",
17+
"php": "^8.1",
1818
"ext-json": "*",
19+
"ext-openssl": "*",
1920
"firebase/php-jwt": "^6.4",
2021
"illuminate/auth": "^9.21|^10.0|^11.0",
2122
"illuminate/console": "^9.21|^10.0|^11.0",
@@ -26,18 +27,18 @@
2627
"illuminate/encryption": "^9.21|^10.0|^11.0",
2728
"illuminate/http": "^9.21|^10.0|^11.0",
2829
"illuminate/support": "^9.21|^10.0|^11.0",
29-
"lcobucci/jwt": "^4.3|^5.0",
30-
"league/oauth2-server": "^8.5.3",
30+
"lcobucci/jwt": "^5.0",
31+
"league/oauth2-server": "^9.0",
3132
"nyholm/psr7": "^1.5",
32-
"phpseclib/phpseclib": "^2.0|^3.0",
33+
"phpseclib/phpseclib": "^3.0",
3334
"symfony/console": "^6.0|^7.0",
34-
"symfony/psr-http-message-bridge": "^2.1|^6.0|^7.0"
35+
"symfony/psr-http-message-bridge": "^6.0|^7.0"
3536
},
3637
"require-dev": {
3738
"mockery/mockery": "^1.0",
3839
"orchestra/testbench": "^7.35|^8.14|^9.0",
3940
"phpstan/phpstan": "^1.10",
40-
"phpunit/phpunit": "^9.3|^10.5"
41+
"phpunit/phpunit": "^9.3|^10.5|^11.0"
4142
},
4243
"autoload": {
4344
"psr-4": {
@@ -66,6 +67,6 @@
6667
"post-autoload-dump": "@prepare",
6768
"prepare": "@php vendor/bin/testbench package:discover --ansi"
6869
},
69-
"minimum-stability": "dev",
70+
"minimum-stability": "stable",
7071
"prefer-stable": true
7172
}

database/factories/ClientFactory.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use Illuminate\Support\Str;
7-
use Laravel\Passport\Client;
87
use Laravel\Passport\Passport;
98

109
/**
@@ -13,11 +12,14 @@
1312
class ClientFactory extends Factory
1413
{
1514
/**
16-
* The name of the factory's corresponding model.
15+
* Get the name of the model that is generated by the factory.
1716
*
18-
* @var string
17+
* @return class-string<\Illuminate\Database\Eloquent\Model>
1918
*/
20-
protected $model = Client::class;
19+
public function modelName()
20+
{
21+
return $this->model ?? Passport::clientModel();
22+
}
2123

2224
/**
2325
* Define the model's default state.
@@ -46,7 +48,7 @@ public function definition()
4648
protected function ensurePrimaryKeyIsSet(array $data)
4749
{
4850
if (Passport::clientUuids()) {
49-
$keyName = (new $this->model)->getKeyName();
51+
$keyName = (new ($this->modelName()))->getKeyName();
5052

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

src/Bridge/AccessToken.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ class AccessToken implements AccessTokenEntityInterface
1515
/**
1616
* Create a new token instance.
1717
*
18-
* @param string $userIdentifier
19-
* @param array $scopes
20-
* @param \League\OAuth2\Server\Entities\ClientEntityInterface $client
21-
* @return void
18+
* @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes
2219
*/
23-
public function __construct($userIdentifier, array $scopes, ClientEntityInterface $client)
20+
public function __construct(string|null $userIdentifier, array $scopes, ClientEntityInterface $client)
2421
{
25-
$this->setUserIdentifier($userIdentifier);
22+
if (! is_null($userIdentifier)) {
23+
$this->setUserIdentifier($userIdentifier);
24+
}
2625

2726
foreach ($scopes as $scope) {
2827
$this->addScope($scope);

src/Bridge/AccessTokenRepository.php

+14-23
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface
1717

1818
/**
1919
* The token repository instance.
20-
*
21-
* @var \Laravel\Passport\TokenRepository
2220
*/
23-
protected $tokenRepository;
21+
protected TokenRepository $tokenRepository;
2422

2523
/**
2624
* The event dispatcher instance.
27-
*
28-
* @var \Illuminate\Contracts\Events\Dispatcher
2925
*/
30-
protected $events;
26+
protected Dispatcher $events;
3127

3228
/**
3329
* Create a new repository instance.
34-
*
35-
* @param \Laravel\Passport\TokenRepository $tokenRepository
36-
* @param \Illuminate\Contracts\Events\Dispatcher $events
37-
* @return void
3830
*/
3931
public function __construct(TokenRepository $tokenRepository, Dispatcher $events)
4032
{
@@ -45,46 +37,45 @@ public function __construct(TokenRepository $tokenRepository, Dispatcher $events
4537
/**
4638
* {@inheritdoc}
4739
*/
48-
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
49-
{
40+
public function getNewToken(
41+
ClientEntityInterface $clientEntity,
42+
array $scopes,
43+
string|null $userIdentifier = null
44+
): AccessTokenEntityInterface {
5045
return new Passport::$accessTokenEntity($userIdentifier, $scopes, $clientEntity);
5146
}
5247

5348
/**
5449
* {@inheritdoc}
5550
*/
56-
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
51+
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity): void
5752
{
5853
$this->tokenRepository->create([
59-
'id' => $accessTokenEntity->getIdentifier(),
60-
'user_id' => $accessTokenEntity->getUserIdentifier(),
61-
'client_id' => $accessTokenEntity->getClient()->getIdentifier(),
54+
'id' => $id = $accessTokenEntity->getIdentifier(),
55+
'user_id' => $userId = $accessTokenEntity->getUserIdentifier(),
56+
'client_id' => $clientId = $accessTokenEntity->getClient()->getIdentifier(),
6257
'scopes' => $this->scopesToArray($accessTokenEntity->getScopes()),
6358
'revoked' => false,
6459
'created_at' => new DateTime,
6560
'updated_at' => new DateTime,
6661
'expires_at' => $accessTokenEntity->getExpiryDateTime(),
6762
]);
6863

69-
$this->events->dispatch(new AccessTokenCreated(
70-
$accessTokenEntity->getIdentifier(),
71-
$accessTokenEntity->getUserIdentifier(),
72-
$accessTokenEntity->getClient()->getIdentifier()
73-
));
64+
$this->events->dispatch(new AccessTokenCreated($id, $userId, $clientId));
7465
}
7566

7667
/**
7768
* {@inheritdoc}
7869
*/
79-
public function revokeAccessToken($tokenId)
70+
public function revokeAccessToken(string $tokenId): void
8071
{
8172
$this->tokenRepository->revokeAccessToken($tokenId);
8273
}
8374

8475
/**
8576
* {@inheritdoc}
8677
*/
87-
public function isAccessTokenRevoked($tokenId)
78+
public function isAccessTokenRevoked(string $tokenId): bool
8879
{
8980
return $this->tokenRepository->isAccessTokenRevoked($tokenId);
9081
}

src/Bridge/AuthCodeRepository.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface
1313
/**
1414
* {@inheritdoc}
1515
*/
16-
public function getNewAuthCode()
16+
public function getNewAuthCode(): AuthCodeEntityInterface
1717
{
1818
return new AuthCode;
1919
}
2020

2121
/**
2222
* {@inheritdoc}
2323
*/
24-
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
24+
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity): void
2525
{
2626
$attributes = [
2727
'id' => $authCodeEntity->getIdentifier(),
@@ -38,15 +38,15 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
public function revokeAuthCode($codeId)
41+
public function revokeAuthCode(string $codeId): void
4242
{
4343
Passport::authCode()->where('id', $codeId)->update(['revoked' => true]);
4444
}
4545

4646
/**
4747
* {@inheritdoc}
4848
*/
49-
public function isAuthCodeRevoked($codeId)
49+
public function isAuthCodeRevoked(string $codeId): bool
5050
{
5151
return Passport::authCode()->where('id', $codeId)->where('revoked', 1)->exists();
5252
}

src/Bridge/Client.php

+11-42
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,32 @@
44

55
use League\OAuth2\Server\Entities\ClientEntityInterface;
66
use League\OAuth2\Server\Entities\Traits\ClientTrait;
7+
use League\OAuth2\Server\Entities\Traits\EntityTrait;
78

89
class Client implements ClientEntityInterface
910
{
10-
use ClientTrait;
11-
12-
/**
13-
* The client identifier.
14-
*
15-
* @var string
16-
*/
17-
protected $identifier;
11+
use ClientTrait, EntityTrait;
1812

1913
/**
2014
* The client's provider.
21-
*
22-
* @var string
2315
*/
24-
public $provider;
16+
public ?string $provider;
2517

2618
/**
2719
* Create a new client instance.
28-
*
29-
* @param string $identifier
30-
* @param string $name
31-
* @param string $redirectUri
32-
* @param bool $isConfidential
33-
* @param string|null $provider
34-
* @return void
3520
*/
36-
public function __construct($identifier, $name, $redirectUri, $isConfidential = false, $provider = null)
37-
{
38-
$this->setIdentifier((string) $identifier);
21+
public function __construct(
22+
string $identifier,
23+
string $name,
24+
string $redirectUri,
25+
bool $isConfidential = false,
26+
?string $provider = null
27+
) {
28+
$this->setIdentifier($identifier);
3929

4030
$this->name = $name;
4131
$this->isConfidential = $isConfidential;
4232
$this->redirectUri = explode(',', $redirectUri);
4333
$this->provider = $provider;
4434
}
45-
46-
/**
47-
* Get the client's identifier.
48-
*
49-
* @return string
50-
*/
51-
public function getIdentifier()
52-
{
53-
return (string) $this->identifier;
54-
}
55-
56-
/**
57-
* Set the client's identifier.
58-
*
59-
* @param string $identifier
60-
* @return void
61-
*/
62-
public function setIdentifier($identifier)
63-
{
64-
$this->identifier = $identifier;
65-
}
6635
}

0 commit comments

Comments
 (0)