Skip to content

Commit 032e2e4

Browse files
driesvintslcobucci
andauthored
[8.x] PHP 8 Support (#1373)
* PHP 8 Support * Update JWT usage * Try out dev versions of jwt * Use vcs * Try composer token * Update phpunit xml * Fix test for JWT 3.4 * Try git url * Bind JWT Parser to container for 4.x * Add deprecation notice * Use configuration object * Use new token interface * Update composer.json * Update workflow Co-authored-by: Luís Cobucci <[email protected]>
1 parent 034b7ba commit 032e2e4

File tree

8 files changed

+43
-19
lines changed

8 files changed

+43
-19
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: true
1515
matrix:
16-
php: [7.3, 7.4]
16+
php: [7.3, 7.4, 8.0]
1717
laravel: [^8.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.3",
17+
"php": "^7.3|^8.0",
1818
"ext-json": "*",
1919
"firebase/php-jwt": "^5.0",
2020
"illuminate/auth": "^8.2",
@@ -26,7 +26,8 @@
2626
"illuminate/encryption": "^8.2",
2727
"illuminate/http": "^8.2",
2828
"illuminate/support": "^8.2",
29-
"league/oauth2-server": "^8.1",
29+
"league/oauth2-server": "^8.2",
30+
"lcobucci/jwt": "^3.4|^4.0",
3031
"nyholm/psr7": "^1.3",
3132
"phpseclib/phpseclib": "^2.0",
3233
"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/Http/Controllers/AccessTokenController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class AccessTokenController
3030
* The JWT parser instance.
3131
*
3232
* @var \Lcobucci\JWT\Parser
33+
*
34+
* @deprecated This property will be removed in a future Passport version.
3335
*/
3436
protected $jwt;
3537

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;
@@ -86,6 +88,7 @@ public function register()
8688

8789
$this->registerAuthorizationServer();
8890
$this->registerClientRepository();
91+
$this->registerJWTParser();
8992
$this->registerResourceServer();
9093
$this->registerGuard();
9194
}
@@ -227,6 +230,18 @@ protected function registerClientRepository()
227230
});
228231
}
229232

233+
/**
234+
* Register the JWT Parser.
235+
*
236+
* @return void
237+
*/
238+
protected function registerJWTParser()
239+
{
240+
$this->app->singleton(Parser::class, function () {
241+
return Configuration::forUnsecuredSigner()->parser();
242+
});
243+
}
244+
230245
/**
231246
* Register the resource server.
232247
*

src/PersonalAccessTokenFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class PersonalAccessTokenFactory
3535
* The JWT token parser instance.
3636
*
3737
* @var \Lcobucci\JWT\Parser
38+
*
39+
* @deprecated This property will be removed in a future Passport version.
3840
*/
3941
protected $jwt;
4042

@@ -127,7 +129,7 @@ protected function dispatchRequestToAuthorizationServer(ServerRequestInterface $
127129
protected function findAccessToken(array $response)
128130
{
129131
return $this->tokens->find(
130-
$this->jwt->parse($response['access_token'])->getClaim('jti')
132+
$this->jwt->parse($response['access_token'])->claims()->get('jti')
131133
);
132134
}
133135
}

tests/Feature/AccessTokenControllerTest.php

Lines changed: 8 additions & 8 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
{
@@ -77,10 +77,10 @@ public function testGettingAccessTokenWithClientCredentialsGrant()
7777
$expiresInSeconds = 31536000;
7878
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);
7979

80-
$jwtAccessToken = (new Parser())->parse($decodedResponse['access_token']);
81-
$this->assertTrue($this->app->make(ClientRepository::class)->findActive($jwtAccessToken->getClaim('aud'))->is($client));
80+
$jwtAccessToken = Configuration::forUnsecuredSigner()->parser()->parse($decodedResponse['access_token']);
81+
$this->assertTrue($this->app->make(ClientRepository::class)->findActive($jwtAccessToken->claims()->get('aud'))->is($client));
8282

83-
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti'));
83+
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->claims()->get('jti'));
8484
$this->assertInstanceOf(Token::class, $token);
8585
$this->assertTrue($token->client->is($client));
8686
$this->assertFalse($token->revoked);
@@ -170,11 +170,11 @@ public function testGettingAccessTokenWithPasswordGrant()
170170
$expiresInSeconds = 31536000;
171171
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);
172172

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

177-
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti'));
177+
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->claims()->get('jti'));
178178
$this->assertInstanceOf(Token::class, $token);
179179
$this->assertFalse($token->revoked);
180180
$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)