Skip to content

Commit 045e859

Browse files
authored
[9.x] Support PHP 8.0 (#1387)
* PHP 8 Support * Drop PHP 7.2 support * Bump OAuth2 Server * Re-add PHP 7.2 support
1 parent 11d95f5 commit 045e859

File tree

7 files changed

+37
-17
lines changed

7 files changed

+37
-17
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ on:
88

99
jobs:
1010
tests:
11-
1211
runs-on: ubuntu-latest
12+
1313
strategy:
1414
fail-fast: true
1515
matrix:
16-
php: [7.2, 7.3, 7.4]
16+
php: [7.2, 7.3, 7.4, 8.0]
1717
laravel: [^6.0, ^7.0]
1818

1919
name: P${{ matrix.php }} - L${{ matrix.laravel }}

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require": {
17-
"php": "^7.2",
17+
"php": "^7.2|^8.0",
1818
"ext-json": "*",
1919
"firebase/php-jwt": "^5.0",
2020
"illuminate/auth": "^6.18.31|^7.22.4",
@@ -27,7 +27,8 @@
2727
"illuminate/http": "^6.18.31|^7.22.4",
2828
"illuminate/support": "^6.18.31|^7.22.4",
2929
"laminas/laminas-diactoros": "^2.2",
30-
"league/oauth2-server": "^8.1",
30+
"lcobucci/jwt": "^3.4|^4.0",
31+
"league/oauth2-server": "^8.2.3",
3132
"nyholm/psr7": "^1.0",
3233
"phpseclib/phpseclib": "^2.0",
3334
"symfony/psr-http-message-bridge": "^2.0"

phpunit.xml.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
<directory suffix="Test.php">./tests/Feature</directory>
1919
</testsuite>
2020
</testsuites>
21-
<filter>
22-
<whitelist processUncoveredFilesFromWhitelist="true">
23-
<directory suffix=".php">./src/</directory>
24-
</whitelist>
25-
</filter>
2621
<php>
2722
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
2823
</php>

src/PassportServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Laravel\Passport\Bridge\PersonalAccessGrant;
1515
use Laravel\Passport\Bridge\RefreshTokenRepository;
1616
use Laravel\Passport\Guards\TokenGuard;
17+
use Lcobucci\JWT\Configuration;
18+
use Lcobucci\JWT\Parser;
1719
use League\OAuth2\Server\AuthorizationServer;
1820
use League\OAuth2\Server\CryptKey;
1921
use League\OAuth2\Server\Grant\AuthCodeGrant;
@@ -94,6 +96,7 @@ public function register()
9496

9597
$this->registerAuthorizationServer();
9698
$this->registerResourceServer();
99+
$this->registerJWTParser();
97100
$this->registerGuard();
98101
}
99102

@@ -252,6 +255,18 @@ protected function makeCryptKey($type)
252255
return new CryptKey($key, null, false);
253256
}
254257

258+
/**
259+
* Register the JWT Parser.
260+
*
261+
* @return void
262+
*/
263+
protected function registerJWTParser()
264+
{
265+
$this->app->singleton(Parser::class, function () {
266+
return Configuration::forUnsecuredSigner()->parser();
267+
});
268+
}
269+
255270
/**
256271
* Register the token guard.
257272
*

src/PersonalAccessTokenFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function dispatchRequestToAuthorizationServer(ServerRequest $request)
126126
protected function findAccessToken(array $response)
127127
{
128128
return $this->tokens->find(
129-
$this->jwt->parse($response['access_token'])->getClaim('jti')
129+
$this->jwt->parse($response['access_token'])->claims()->get('jti')
130130
);
131131
}
132132
}

tests/Feature/AccessTokenControllerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Laravel\Passport\HasApiTokens;
1313
use Laravel\Passport\Token;
1414
use Laravel\Passport\TokenRepository;
15-
use Lcobucci\JWT\Parser;
15+
use Lcobucci\JWT\Configuration;
1616

1717
class AccessTokenControllerTest extends PassportTestCase
1818
{
@@ -81,11 +81,11 @@ public function testGettingAccessTokenWithPasswordGrant()
8181
$expiresInSeconds = 31536000;
8282
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);
8383

84-
$jwtAccessToken = (new Parser())->parse($decodedResponse['access_token']);
85-
$this->assertTrue($this->app->make(ClientRepository::class)->findActive($jwtAccessToken->getClaim('aud'))->is($client));
86-
$this->assertTrue($this->app->make('auth')->createUserProvider()->retrieveById($jwtAccessToken->getClaim('sub'))->is($user));
84+
$jwtAccessToken = Configuration::forUnsecuredSigner()->parser()->parse($decodedResponse['access_token']);
85+
$this->assertTrue($this->app->make(ClientRepository::class)->findActive($jwtAccessToken->claims()->get('aud'))->is($client));
86+
$this->assertTrue($this->app->make('auth')->createUserProvider()->retrieveById($jwtAccessToken->claims()->get('sub'))->is($user));
8787

88-
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti'));
88+
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->claims()->get('jti'));
8989
$this->assertInstanceOf(Token::class, $token);
9090
$this->assertFalse($token->revoked);
9191
$this->assertTrue($token->user->is($user));

tests/Unit/PersonalAccessTokenFactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Laravel\Passport\Token;
99
use Laravel\Passport\TokenRepository;
1010
use Lcobucci\JWT\Parser;
11+
use Lcobucci\JWT\Token\DataSet;
12+
use Lcobucci\JWT\Token\Plain as PlainToken;
13+
use Lcobucci\JWT\Token\RegisteredClaims;
14+
use Lcobucci\JWT\Token\Signature;
1115
use League\OAuth2\Server\AuthorizationServer;
1216
use Mockery as m;
1317
use PHPUnit\Framework\TestCase;
@@ -34,8 +38,13 @@ public function test_access_token_can_be_created()
3438
'access_token' => 'foo',
3539
]));
3640

37-
$jwt->shouldReceive('parse')->with('foo')->andReturn($parsedToken = m::mock());
38-
$parsedToken->shouldReceive('getClaim')->with('jti')->andReturn('token');
41+
$parsedToken = new PlainToken(
42+
new DataSet([], ''),
43+
new DataSet([RegisteredClaims::ID => 'token'], ''),
44+
Signature::fromEmptyData()
45+
);
46+
47+
$jwt->shouldReceive('parse')->with('foo')->andReturn($parsedToken);
3948
$tokens->shouldReceive('find')
4049
->with('token')
4150
->andReturn($foundToken = new PersonalAccessTokenFactoryTestModelStub);

0 commit comments

Comments
 (0)