Skip to content

Restrict grant types #725

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/Bridge/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function getClientEntity($clientIdentifier, $grantType = null,
*/
protected function handlesGrant($record, $grantType)
{
if (is_array($record->grant_types) && !in_array($grantType, $record->grant_types)) {
return false;
}

switch ($grantType) {
case 'authorization_code':
return ! $record->firstParty();
Expand Down
51 changes: 37 additions & 14 deletions tests/BridgeClientRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,56 @@

class BridgeClientRepositoryTest extends TestCase
{
public function setUp()
{
$clientModelRepository = Mockery::mock(Laravel\Passport\ClientRepository::class);
$clientModelRepository->shouldReceive('findActive')->with(1)->andReturn(new BridgeClientRepositoryTestClientStub);

$this->clientModelRepository = $clientModelRepository;
$this->repository = new Laravel\Passport\Bridge\ClientRepository($clientModelRepository);
}

public function tearDown()
{
Mockery::close();
}

public function test_can_get_client_for_auth_code_grant()
{
$clients = Mockery::mock('Laravel\Passport\ClientRepository');
$client = new BridgeClientRepositoryTestClientStub;
$clients->shouldReceive('findActive')->with(1)->andReturn($client);
$repository = new ClientRepository($clients);

$client = $repository->getClientEntity(1, 'authorization_code', 'secret', true);
$client = $this->repository->getClientEntity(1, 'authorization_code', 'secret', true);

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $client);
$this->assertNull($repository->getClientEntity(1, 'authorization_code', 'wrong-secret', true));
$this->assertNull($repository->getClientEntity(1, 'client_credentials', 'wrong-secret', true));
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'wrong-secret', true));
$this->assertNull($this->repository->getClientEntity(1, 'client_credentials', 'wrong-secret', true));
}

public function test_can_get_client_for_client_credentials_grant()
{
$clients = Mockery::mock('Laravel\Passport\ClientRepository');
$client = new BridgeClientRepositoryTestClientStub;
$client = $this->clientModelRepository->findActive(1);
$client->personal_access_client = true;
$clients->shouldReceive('findActive')->with(1)->andReturn($client);
$repository = new ClientRepository($clients);

$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $repository->getClientEntity(1, 'client_credentials', 'secret', true));
$this->assertNull($repository->getClientEntity(1, 'authorization_code', 'secret', true));
$this->assertInstanceOf('Laravel\Passport\Bridge\Client', $this->repository->getClientEntity(1, 'client_credentials', 'secret', true));
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'secret', true));
}

public function test_password_only_client_is_permitted()
{
$client = $this->clientModelRepository->findActive(1);
$client->password_client = true;
$client->grants = ['password'];

$client = $this->repository->getClientEntity(1, 'password', 'secret');
$this->assertEquals('Client', $client->getName());
}

public function test_password_only_client_is_prevented()
{
$client = $this->clientModelRepository->findActive(1);
$client->password_client = true;
$client->grant_types = ['password'];

$client = $this->repository->getClientEntity(1, 'client_credentials', 'secret');
$this->assertNull($client);
}
}

Expand All @@ -44,6 +65,8 @@ class BridgeClientRepositoryTestClientStub
public $secret = 'secret';
public $personal_access_client = false;
public $password_client = false;
public $grant_types;

public function firstParty()
{
return $this->personal_access_client || $this->password_client;
Expand Down