Skip to content

Commit

Permalink
Fix getting/setting client scopes and grant types (#1717)
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon authored Feb 9, 2024
1 parent 121f030 commit 4d81207
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function validateClient($clientIdentifier, $clientSecret, $grantType)
*/
protected function handlesGrant($record, $grantType)
{
if (is_array($record->grant_types) && ! in_array($grantType, $record->grant_types)) {
if (! $record->hasGrantType($grantType)) {
return false;
}

Expand Down
48 changes: 16 additions & 32 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,37 +105,6 @@ public function tokens()
return $this->hasMany(Passport::tokenModel(), 'client_id');
}

/**
* Get the grant types the client can use.
*
* @return array|null
*/
public function getGrantTypesAttribute()
{
return $this->attributes['grant_types'] ?? null;
}

/**
* Get the scopes for the client.
*
* @return array|null
*/
public function getScopesAttribute()
{
return $this->attributes['scopes'] ?? null;
}

/**
* Set the scopes for the client.
*
* @param array|null $scopes
* @return void
*/
public function setScopesAttribute(?array $scopes)
{
$this->attributes['scopes'] = $scopes;
}

/**
* The temporary non-hashed client secret.
*
Expand Down Expand Up @@ -187,6 +156,21 @@ public function skipsAuthorization()
return false;
}

/**
* Determine if the client has the given grant type.
*
* @param string $grantType
* @return bool
*/
public function hasGrantType($grantType)
{
if (! isset($this->grant_types) || ! is_array($this->grant_types)) {
return true;
}

return in_array($grantType, $this->grant_types);
}

/**
* Determine whether the client has the given scope.
*
Expand All @@ -195,7 +179,7 @@ public function skipsAuthorization()
*/
public function hasScope($scope)
{
if (! is_array($this->scopes)) {
if (! isset($this->scopes) || ! is_array($this->scopes)) {
return true;
}

Expand Down
88 changes: 88 additions & 0 deletions tests/Feature/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Laravel\Passport\Tests\Feature;

use Illuminate\Database\Eloquent\Model;
use Laravel\Passport\Client;
use Orchestra\Testbench\TestCase;

final class ClientTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Model::preventAccessingMissingAttributes();
}

protected function tearDown(): void
{
Model::preventAccessingMissingAttributes(false);

parent::tearDown();
}

public function testScopesWhenClientDoesNotHaveScope(): void
{
$client = new Client(['scopes' => ['bar']]);
$client->exists = true;

$this->assertFalse($client->hasScope('foo'));
}

public function testScopesWhenClientHasScope(): void
{
$client = new Client(['scopes' => ['foo', 'bar']]);
$client->exists = true;

$this->assertTrue($client->hasScope('foo'));
}

public function testScopesWhenColumnDoesNotExist(): void
{
$client = new Client();
$client->exists = true;

$this->assertTrue($client->hasScope('foo'));
}

public function testScopesWhenColumnIsNull(): void
{
$client = new Client(['scopes' => null]);
$client->exists = true;

$this->assertTrue($client->hasScope('foo'));
}

public function testGrantTypesWhenClientDoesNotHaveGrantType(): void
{
$client = new Client(['grant_types' => ['bar']]);
$client->exists = true;

$this->assertFalse($client->hasGrantType('foo'));
}

public function testGrantTypesWhenClientHasGrantType(): void
{
$client = new Client(['grant_types' => ['foo', 'bar']]);
$client->exists = true;

$this->assertTrue($client->hasGrantType('foo'));
}

public function testGrantTypesWhenColumnDoesNotExist(): void
{
$client = new Client();
$client->exists = true;

$this->assertTrue($client->hasGrantType('foo'));
}

public function testGrantTypesWhenColumnIsNull(): void
{
$client = new Client(['scopes' => null]);
$client->exists = true;

$this->assertTrue($client->hasGrantType('foo'));
}
}
9 changes: 9 additions & 0 deletions tests/Unit/BridgeClientRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,13 @@ public function confidential()
{
return ! empty($this->secret);
}

public function hasGrantType($grantType)
{
if (! isset($this->grant_types) || ! is_array($this->grant_types)) {
return true;
}

return in_array($grantType, $this->grant_types);
}
}

0 comments on commit 4d81207

Please sign in to comment.