Skip to content

Commit

Permalink
Fix breaking change introduced by #1682
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Sep 1, 2023
1 parent e225960 commit 10324b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Bridge/ScopeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class ScopeRepository implements ScopeRepositoryInterface
/**
* The client repository.
*
* @var \Laravel\Passport\ClientRepository
* @var \Laravel\Passport\ClientRepository|null
*/
protected ClientRepository $clients;
protected ?ClientRepository $clients;

/**
* Create a new scope repository.
*
* @param \Laravel\Passport\ClientRepository $clients
* @param \Laravel\Passport\ClientRepository|null $clients
* @return void
*/
public function __construct(ClientRepository $clients)
public function __construct(?ClientRepository $clients = null)
{
$this->clients = $clients;
}
Expand Down Expand Up @@ -50,11 +50,12 @@ public function finalizeScopes(
})->values()->all();
}

$client = $this->clients->findActive($clientEntity->getIdentifier());
$client = $this->clients?->findActive($clientEntity->getIdentifier());

return collect($scopes)->filter(function ($scope) use ($client) {
return Passport::hasScope($scope->getIdentifier())
&& $client->hasScope($scope->getIdentifier());
return collect($scopes)->filter(function ($scope) {
return Passport::hasScope($scope->getIdentifier());
})->when($client, function ($scopes, $client) {
return $scopes->filter(fn ($scope) => $client->hasScope($scope->getIdentifier()));
})->values()->all();
}
}
30 changes: 30 additions & 0 deletions tests/Unit/BridgeScopeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public function test_invalid_scopes_are_removed()
$this->assertEquals([$scope1], $scopes);
}

public function test_invalid_scopes_are_removed_without_a_client_repository()
{
Passport::tokensCan([
'scope-1' => 'description',
]);

$repository = new ScopeRepository();

$scopes = $repository->finalizeScopes(
[$scope1 = new Scope('scope-1'), new Scope('scope-2')], 'client_credentials', new Client('id', 'name', 'http://localhost'), 1
);

$this->assertEquals([$scope1], $scopes);
}

public function test_clients_do_not_restrict_scopes_by_default()
{
Passport::tokensCan([
Expand Down Expand Up @@ -126,4 +141,19 @@ public function test_superuser_scope_cant_be_applied_if_wrong_grant()

$this->assertEquals([], $scopes);
}

public function test_superuser_scope_cant_be_applied_if_wrong_grant_without_a_client_repository()
{
Passport::tokensCan([
'scope-1' => 'description',
]);

$repository = new ScopeRepository();

$scopes = $repository->finalizeScopes(
[$scope1 = new Scope('*')], 'refresh_token', new Client('id', 'name', 'http://localhost'), 1
);

$this->assertEquals([], $scopes);
}
}

0 comments on commit 10324b2

Please sign in to comment.