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

[10.x] PHP 8 Support #1373

Merged
merged 14 commits into from
Nov 26, 2020
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4]
php: [7.3, 7.4, 8.0]
laravel: [^8.0]

name: P${{ matrix.php }} - L${{ matrix.laravel }}
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": "^7.3",
"php": "^7.3|^8.0",
"ext-json": "*",
"firebase/php-jwt": "^5.0",
"illuminate/auth": "^8.2",
Expand All @@ -26,7 +26,8 @@
"illuminate/encryption": "^8.2",
"illuminate/http": "^8.2",
"illuminate/support": "^8.2",
"league/oauth2-server": "^8.1",
"league/oauth2-server": "^8.2",
"lcobucci/jwt": "^3.4|^4.0",
"nyholm/psr7": "^1.3",
"phpseclib/phpseclib": "^2.0",
"symfony/psr-http-message-bridge": "^2.0"
Expand Down
5 changes: 0 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
</php>
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Controllers/AccessTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class AccessTokenController
* The JWT parser instance.
*
* @var \Lcobucci\JWT\Parser
*
* @deprecated This property will be removed in a future Passport version.
*/
protected $jwt;

Expand Down
15 changes: 15 additions & 0 deletions src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Laravel\Passport\Bridge\PersonalAccessGrant;
use Laravel\Passport\Bridge\RefreshTokenRepository;
use Laravel\Passport\Guards\TokenGuard;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Parser;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Grant\AuthCodeGrant;
Expand Down Expand Up @@ -86,6 +88,7 @@ public function register()

$this->registerAuthorizationServer();
$this->registerClientRepository();
$this->registerJWTParser();
$this->registerResourceServer();
$this->registerGuard();
}
Expand Down Expand Up @@ -227,6 +230,18 @@ protected function registerClientRepository()
});
}

/**
* Register the JWT Parser.
*
* @return void
*/
protected function registerJWTParser()
{
$this->app->singleton(Parser::class, function () {
return Configuration::forUnsecuredSigner()->parser();
});
}

/**
* Register the resource server.
*
Expand Down
4 changes: 3 additions & 1 deletion src/PersonalAccessTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class PersonalAccessTokenFactory
* The JWT token parser instance.
*
* @var \Lcobucci\JWT\Parser
*
* @deprecated This property will be removed in a future Passport version.
*/
protected $jwt;

Expand Down Expand Up @@ -127,7 +129,7 @@ protected function dispatchRequestToAuthorizationServer(ServerRequestInterface $
protected function findAccessToken(array $response)
{
return $this->tokens->find(
$this->jwt->parse($response['access_token'])->getClaim('jti')
$this->jwt->parse($response['access_token'])->claims()->get('jti')
);
}
}
16 changes: 8 additions & 8 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Configuration;

class AccessTokenControllerTest extends PassportTestCase
{
Expand Down Expand Up @@ -77,10 +77,10 @@ public function testGettingAccessTokenWithClientCredentialsGrant()
$expiresInSeconds = 31536000;
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);

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

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

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

$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti'));
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->claims()->get('jti'));
$this->assertInstanceOf(Token::class, $token);
$this->assertFalse($token->revoked);
$this->assertTrue($token->user->is($user));
Expand Down
13 changes: 11 additions & 2 deletions tests/Unit/PersonalAccessTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Token\DataSet;
use Lcobucci\JWT\Token\Plain as PlainToken;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\Token\Signature;
use League\OAuth2\Server\AuthorizationServer;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand All @@ -34,8 +38,13 @@ public function test_access_token_can_be_created()
'access_token' => 'foo',
]));

$jwt->shouldReceive('parse')->with('foo')->andReturn($parsedToken = m::mock());
$parsedToken->shouldReceive('getClaim')->with('jti')->andReturn('token');
$parsedToken = new PlainToken(
new DataSet([], ''),
new DataSet([RegisteredClaims::ID => 'token'], ''),
Signature::fromEmptyData()
);

$jwt->shouldReceive('parse')->with('foo')->andReturn($parsedToken);
$tokens->shouldReceive('find')
->with('token')
->andReturn($foundToken = new PersonalAccessTokenFactoryTestModelStub);
Expand Down