Skip to content

Commit 4d81207

Browse files
authored
Fix getting/setting client scopes and grant types (#1717)
1 parent 121f030 commit 4d81207

File tree

4 files changed

+114
-33
lines changed

4 files changed

+114
-33
lines changed

src/Bridge/ClientRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function validateClient($clientIdentifier, $clientSecret, $grantType)
7272
*/
7373
protected function handlesGrant($record, $grantType)
7474
{
75-
if (is_array($record->grant_types) && ! in_array($grantType, $record->grant_types)) {
75+
if (! $record->hasGrantType($grantType)) {
7676
return false;
7777
}
7878

src/Client.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,6 @@ public function tokens()
105105
return $this->hasMany(Passport::tokenModel(), 'client_id');
106106
}
107107

108-
/**
109-
* Get the grant types the client can use.
110-
*
111-
* @return array|null
112-
*/
113-
public function getGrantTypesAttribute()
114-
{
115-
return $this->attributes['grant_types'] ?? null;
116-
}
117-
118-
/**
119-
* Get the scopes for the client.
120-
*
121-
* @return array|null
122-
*/
123-
public function getScopesAttribute()
124-
{
125-
return $this->attributes['scopes'] ?? null;
126-
}
127-
128-
/**
129-
* Set the scopes for the client.
130-
*
131-
* @param array|null $scopes
132-
* @return void
133-
*/
134-
public function setScopesAttribute(?array $scopes)
135-
{
136-
$this->attributes['scopes'] = $scopes;
137-
}
138-
139108
/**
140109
* The temporary non-hashed client secret.
141110
*
@@ -187,6 +156,21 @@ public function skipsAuthorization()
187156
return false;
188157
}
189158

159+
/**
160+
* Determine if the client has the given grant type.
161+
*
162+
* @param string $grantType
163+
* @return bool
164+
*/
165+
public function hasGrantType($grantType)
166+
{
167+
if (! isset($this->grant_types) || ! is_array($this->grant_types)) {
168+
return true;
169+
}
170+
171+
return in_array($grantType, $this->grant_types);
172+
}
173+
190174
/**
191175
* Determine whether the client has the given scope.
192176
*
@@ -195,7 +179,7 @@ public function skipsAuthorization()
195179
*/
196180
public function hasScope($scope)
197181
{
198-
if (! is_array($this->scopes)) {
182+
if (! isset($this->scopes) || ! is_array($this->scopes)) {
199183
return true;
200184
}
201185

tests/Feature/ClientTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Laravel\Passport\Tests\Feature;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Laravel\Passport\Client;
7+
use Orchestra\Testbench\TestCase;
8+
9+
final class ClientTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
parent::setUp();
14+
15+
Model::preventAccessingMissingAttributes();
16+
}
17+
18+
protected function tearDown(): void
19+
{
20+
Model::preventAccessingMissingAttributes(false);
21+
22+
parent::tearDown();
23+
}
24+
25+
public function testScopesWhenClientDoesNotHaveScope(): void
26+
{
27+
$client = new Client(['scopes' => ['bar']]);
28+
$client->exists = true;
29+
30+
$this->assertFalse($client->hasScope('foo'));
31+
}
32+
33+
public function testScopesWhenClientHasScope(): void
34+
{
35+
$client = new Client(['scopes' => ['foo', 'bar']]);
36+
$client->exists = true;
37+
38+
$this->assertTrue($client->hasScope('foo'));
39+
}
40+
41+
public function testScopesWhenColumnDoesNotExist(): void
42+
{
43+
$client = new Client();
44+
$client->exists = true;
45+
46+
$this->assertTrue($client->hasScope('foo'));
47+
}
48+
49+
public function testScopesWhenColumnIsNull(): void
50+
{
51+
$client = new Client(['scopes' => null]);
52+
$client->exists = true;
53+
54+
$this->assertTrue($client->hasScope('foo'));
55+
}
56+
57+
public function testGrantTypesWhenClientDoesNotHaveGrantType(): void
58+
{
59+
$client = new Client(['grant_types' => ['bar']]);
60+
$client->exists = true;
61+
62+
$this->assertFalse($client->hasGrantType('foo'));
63+
}
64+
65+
public function testGrantTypesWhenClientHasGrantType(): void
66+
{
67+
$client = new Client(['grant_types' => ['foo', 'bar']]);
68+
$client->exists = true;
69+
70+
$this->assertTrue($client->hasGrantType('foo'));
71+
}
72+
73+
public function testGrantTypesWhenColumnDoesNotExist(): void
74+
{
75+
$client = new Client();
76+
$client->exists = true;
77+
78+
$this->assertTrue($client->hasGrantType('foo'));
79+
}
80+
81+
public function testGrantTypesWhenColumnIsNull(): void
82+
{
83+
$client = new Client(['scopes' => null]);
84+
$client->exists = true;
85+
86+
$this->assertTrue($client->hasGrantType('foo'));
87+
}
88+
}

tests/Unit/BridgeClientRepositoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,13 @@ public function confidential()
207207
{
208208
return ! empty($this->secret);
209209
}
210+
211+
public function hasGrantType($grantType)
212+
{
213+
if (! isset($this->grant_types) || ! is_array($this->grant_types)) {
214+
return true;
215+
}
216+
217+
return in_array($grantType, $this->grant_types);
218+
}
210219
}

0 commit comments

Comments
 (0)