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

Fix exception will thrown if token belongs to first party clients #1040

Merged
merged 1 commit into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/Http/Middleware/CheckClientCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Zend\Diactoros\StreamFactory;
use Zend\Diactoros\ResponseFactory;
use Laravel\Passport\TokenRepository;
use Zend\Diactoros\UploadedFileFactory;
use League\OAuth2\Server\ResourceServer;
use Zend\Diactoros\ServerRequestFactory;
Expand All @@ -22,15 +23,24 @@ class CheckClientCredentials
*/
protected $server;

/**
* Token Repository.
*
* @var \Laravel\Passport\TokenRepository
*/
protected $repository;

/**
* Create a new middleware instance.
*
* @param \League\OAuth2\Server\ResourceServer $server
* @param \Laravel\Passport\TokenRepository $repository
* @return void
*/
public function __construct(ResourceServer $server)
public function __construct(ResourceServer $server, TokenRepository $repository)
{
$this->server = $server;
$this->repository = $repository;
}

/**
Expand All @@ -57,27 +67,33 @@ public function handle($request, Closure $next, ...$scopes)
throw new AuthenticationException;
}

$this->validateScopes($psr, $scopes);
$this->validate($psr, $scopes);

return $next($request);
}

/**
* Validate the scopes on the incoming request.
* Validate the scopes and token on the incoming request.
*
* @param \Psr\Http\Message\ServerRequestInterface $psr
* @param array $scopes
* @return void
* @throws \Laravel\Passport\Exceptions\MissingScopeException
* @throws \Laravel\Passport\Exceptions\MissingScopeException|\Illuminate\Auth\AuthenticationException
*/
protected function validateScopes($psr, $scopes)
protected function validate($psr, $scopes)
{
if (in_array('*', $tokenScopes = $psr->getAttribute('oauth_scopes'))) {
$token = $this->repository->find($psr->getAttribute('oauth_access_token_id'));

if (! $token || $token->client->firstParty()) {
throw new AuthenticationException;
}

if (in_array('*', $token->scopes)) {
return;
}

foreach ($scopes as $scope) {
if (! in_array($scope, $tokenScopes)) {
if ($token->cant($scope)) {
throw new MissingScopeException($scope);
}
}
Expand Down
29 changes: 23 additions & 6 deletions src/Http/Middleware/CheckClientCredentialsForAnyScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Zend\Diactoros\StreamFactory;
use Zend\Diactoros\ResponseFactory;
use Laravel\Passport\TokenRepository;
use Zend\Diactoros\UploadedFileFactory;
use League\OAuth2\Server\ResourceServer;
use Zend\Diactoros\ServerRequestFactory;
Expand All @@ -22,15 +23,24 @@ class CheckClientCredentialsForAnyScope
*/
private $server;

/**
* Token Repository.
*
* @var \Laravel\Passport\TokenRepository
*/
protected $repository;

/**
* Create a new middleware instance.
*
* @param \League\OAuth2\Server\ResourceServer $server
* @param \Laravel\Passport\TokenRepository $repository
* @return void
*/
public function __construct(ResourceServer $server)
public function __construct(ResourceServer $server, TokenRepository $repository)
{
$this->server = $server;
$this->repository = $repository;
}

/**
Expand All @@ -57,28 +67,35 @@ public function handle($request, Closure $next, ...$scopes)
throw new AuthenticationException;
}

if ($this->validateScopes($psr, $scopes)) {
if ($this->validate($psr, $scopes)) {
return $next($request);
}

throw new MissingScopeException($scopes);
}

/**
* Validate the scopes on the incoming request.
* Validate the scopes and token on the incoming request.
*
* @param \Psr\Http\Message\ServerRequestInterface $psr
* @param array $scopes
* @return bool
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function validateScopes($psr, $scopes)
protected function validate($psr, $scopes)
{
if (in_array('*', $tokenScopes = $psr->getAttribute('oauth_scopes'))) {
$token = $this->repository->find($psr->getAttribute('oauth_access_token_id'));

if (! $token || $token->client->firstParty()) {
throw new AuthenticationException;
}

if (in_array('*', $token->scopes)) {
return true;
}

foreach ($scopes as $scope) {
if (in_array($scope, $tokenScopes)) {
if ($token->can($scope)) {
return true;
}
}
Expand Down
77 changes: 73 additions & 4 deletions tests/CheckClientCredentialsForAnyScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Laravel\Passport\Tests;

use Mockery as m;
use Laravel\Passport\Token;
use Illuminate\Http\Request;
use Laravel\Passport\Client;
use PHPUnit\Framework\TestCase;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\ResourceServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
Expand All @@ -25,7 +28,17 @@ public function test_request_is_passed_along_if_token_is_valid()
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['*']);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
$client = m::mock(Client::class);
$client->shouldReceive('firstParty')->andReturnFalse();

$token = m::mock(Token::class);
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['*']);

$tokenRepository = m::mock(TokenRepository::class);
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -46,7 +59,19 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['foo', 'bar', 'baz']);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
$client = m::mock(Client::class);
$client->shouldReceive('firstParty')->andReturnFalse();

$token = m::mock(Token::class);
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['foo', 'bar', 'baz']);
$token->shouldReceive('can')->with('notfoo')->andReturnFalse();
$token->shouldReceive('can')->with('bar')->andReturnTrue();

$tokenRepository = m::mock(TokenRepository::class);
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -63,12 +88,13 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
*/
public function test_exception_is_thrown_when_oauth_throws_exception()
{
$tokenRepository = m::mock(TokenRepository::class);
$resourceServer = m::mock(ResourceServer::class);
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andThrow(
new OAuthServerException('message', 500, 'error type')
);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -90,7 +116,19 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['foo', 'bar']);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
$client = m::mock(Client::class);
$client->shouldReceive('firstParty')->andReturnFalse();

$token = m::mock(Token::class);
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['foo', 'bar']);
$token->shouldReceive('can')->with('baz')->andReturnFalse();
$token->shouldReceive('can')->with('notbar')->andReturnFalse();

$tokenRepository = m::mock(TokenRepository::class);
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -99,4 +137,35 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
return 'response';
}, 'baz', 'notbar');
}

/**
* @expectedException \Illuminate\Auth\AuthenticationException
*/
public function test_exception_is_thrown_if_token_belongs_to_first_party_client()
{
$resourceServer = m::mock(ResourceServer::class);
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['*']);

$client = m::mock(Client::class);
$client->shouldReceive('firstParty')->andReturnTrue();

$token = m::mock(Token::class);
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);

$tokenRepository = m::mock(TokenRepository::class);
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');

$response = $middleware->handle($request, function () {
return 'response';
});
}
}
Loading