Skip to content

Commit

Permalink
[11.x] Remove deprecated functionality and simplify some feature tests (
Browse files Browse the repository at this point in the history
#1559)

* Drop JWT v3

* wip
  • Loading branch information
driesvints authored Aug 19, 2022
1 parent a57723e commit dd00df7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 34 deletions.
15 changes: 1 addition & 14 deletions src/Http/Controllers/AccessTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Passport\Http\Controllers;

use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser as JwtParser;
use League\OAuth2\Server\AuthorizationServer;
use Nyholm\Psr7\Response as Psr7Response;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -26,28 +25,16 @@ class AccessTokenController
*/
protected $tokens;

/**
* The JWT parser instance.
*
* @var \Lcobucci\JWT\Parser
*
* @deprecated This property will be removed in a future Passport version.
*/
protected $jwt;

/**
* Create a new controller instance.
*
* @param \League\OAuth2\Server\AuthorizationServer $server
* @param \Laravel\Passport\TokenRepository $tokens
* @param \Lcobucci\JWT\Parser $jwt
* @return void
*/
public function __construct(AuthorizationServer $server,
TokenRepository $tokens,
JwtParser $jwt)
TokenRepository $tokens)
{
$this->jwt = $jwt;
$this->server = $server;
$this->tokens = $tokens;
}
Expand Down
4 changes: 1 addition & 3 deletions src/PersonalAccessTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ 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 @@ -126,7 +124,7 @@ protected function dispatchRequestToAuthorizationServer(ServerRequestInterface $
* @param array $response
* @return \Laravel\Passport\Token
*/
protected function findAccessToken(array $response)
public function findAccessToken(array $response)
{
return $this->tokens->find(
$this->jwt->parse($response['access_token'])->claims()->get('jti')
Expand Down
15 changes: 3 additions & 12 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Laravel\Passport\Client;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Database\Factories\ClientFactory;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Passport;
use Laravel\Passport\PersonalAccessTokenFactory;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Configuration;

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

$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->claims()->get('jti'));
$token = $this->app->make(PersonalAccessTokenFactory::class)->findAccessToken($decodedResponse);
$this->assertInstanceOf(Token::class, $token);
$this->assertTrue($token->client->is($client));
$this->assertFalse($token->revoked);
Expand Down Expand Up @@ -171,11 +166,7 @@ public function testGettingAccessTokenWithPasswordGrant()
$expiresInSeconds = 31536000;
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);

$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->claims()->get('jti'));
$token = $this->app->make(PersonalAccessTokenFactory::class)->findAccessToken($decodedResponse);
$this->assertInstanceOf(Token::class, $token);
$this->assertFalse($token->revoked);
$this->assertTrue($token->user->is($user));
Expand Down
7 changes: 2 additions & 5 deletions tests/Unit/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Laravel\Passport\Exceptions\OAuthServerException;
use Laravel\Passport\Http\Controllers\AccessTokenController;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException as LeagueException;
use Mockery as m;
Expand All @@ -26,7 +25,6 @@ public function test_a_token_can_be_issued()
$request = m::mock(ServerRequestInterface::class);
$response = m::type(ResponseInterface::class);
$tokens = m::mock(TokenRepository::class);
$jwt = m::mock(Parser::class);

$psrResponse = new Response();
$psrResponse->getBody()->write(json_encode(['access_token' => 'access-token']));
Expand All @@ -36,22 +34,21 @@ public function test_a_token_can_be_issued()
->with($request, $response)
->andReturn($psrResponse);

$controller = new AccessTokenController($server, $tokens, $jwt);
$controller = new AccessTokenController($server, $tokens);

$this->assertSame('{"access_token":"access-token"}', $controller->issueToken($request)->getContent());
}

public function test_exceptions_are_handled()
{
$tokens = m::mock(TokenRepository::class);
$jwt = m::mock(Parser::class);

$server = m::mock(AuthorizationServer::class);
$server->shouldReceive('respondToAccessTokenRequest')->with(
m::type(ServerRequestInterface::class), m::type(ResponseInterface::class)
)->andThrow(LeagueException::invalidCredentials());

$controller = new AccessTokenController($server, $tokens, $jwt);
$controller = new AccessTokenController($server, $tokens);

$this->expectException(OAuthServerException::class);

Expand Down

0 comments on commit dd00df7

Please sign in to comment.