Skip to content

Commit 93aaaae

Browse files
append plain_secret
1 parent ff6366b commit 93aaaae

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/Client.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ protected function secret(): Attribute
134134
);
135135
}
136136

137+
/**
138+
* Interact with the client's plain secret.
139+
*/
140+
protected function plainSecret(): Attribute
141+
{
142+
return Attribute::make(
143+
get: fn (): ?string => $this->plainSecret
144+
);
145+
}
146+
137147
/**
138148
* Interact with the client's redirect URIs.
139149
*/

src/Http/Controllers/ClientController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function forUser(Request $request): Collection
3838
/**
3939
* Store a new client.
4040
*/
41-
public function store(Request $request): array
41+
public function store(Request $request): Client
4242
{
4343
$this->validation->make($request->all(), [
4444
'name' => ['required', 'string', 'max:255'],
@@ -49,11 +49,11 @@ public function store(Request $request): array
4949
$client = $this->clients->createAuthorizationCodeGrantClient(
5050
$request->name,
5151
explode(',', $request->redirect),
52-
(bool) $request->input('confidential', true),
52+
$confidential = (bool) $request->input('confidential', true),
5353
$request->user(),
5454
);
5555

56-
return ['plainSecret' => $client->plainSecret] + $client->toArray();
56+
return $confidential ? $client->mergeAppends(['plain_secret']) : $client;
5757
}
5858

5959
/**

tests/Unit/ClientControllerTest.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Auth\Authenticatable;
66
use Illuminate\Contracts\Validation\Factory;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Hash;
89
use Laravel\Passport\Client;
910
use Laravel\Passport\ClientRepository;
1011
use Laravel\Passport\Http\Controllers\ClientController;
@@ -41,6 +42,9 @@ public function test_all_the_clients_for_the_current_user_can_be_retrieved()
4142

4243
public function test_clients_can_be_stored()
4344
{
45+
Hash::expects('isHashed')->once()->with('secret')->andReturn(false);
46+
Hash::expects('make')->once()->with('secret')->andReturn('hashed_secret');
47+
4448
$clients = m::mock(ClientRepository::class);
4549
$user = m::mock(Authenticatable::class);
4650
$user->shouldReceive('getAuthIdentifier')->andReturn(1);
@@ -51,7 +55,11 @@ public function test_clients_can_be_stored()
5155
$clients->shouldReceive('createAuthorizationCodeGrantClient')
5256
->once()
5357
->with('client name', ['http://localhost'], true, $user)
54-
->andReturn($client = new Client(['name' => 'client']));
58+
->andReturn($client = new Client([
59+
'name' => 'client name',
60+
'redirect' => 'http://localhost',
61+
'secret' => 'secret',
62+
]));
5563

5664
$redirectRule = m::mock(RedirectRule::class);
5765

@@ -70,10 +78,13 @@ public function test_clients_can_be_stored()
7078
$clients, $validator, $redirectRule
7179
);
7280

73-
$this->assertEquals([
74-
'name' => $client->name,
75-
'plainSecret' => $client->plainSecret,
76-
], $controller->store($request));
81+
$this->assertEquals($client, $controller->store($request));
82+
$this->assertSame('hashed_secret', $client->secret);
83+
$this->assertSame([
84+
'name' => 'client name',
85+
'redirect' => 'http://localhost',
86+
'plain_secret' => 'secret',
87+
], $client->toArray());
7788
}
7889

7990
public function test_public_clients_can_be_stored()
@@ -92,7 +103,11 @@ public function test_public_clients_can_be_stored()
92103
$clients->shouldReceive('createAuthorizationCodeGrantClient')
93104
->once()
94105
->with('client name', ['http://localhost'], false, $user)
95-
->andReturn($client = new Client(['name' => 'client']));
106+
->andReturn($client = new Client([
107+
'name' => 'client name',
108+
'redirect' => 'http://localhost',
109+
'secret' => null,
110+
]));
96111

97112
$redirectRule = m::mock(RedirectRule::class);
98113

@@ -112,10 +127,12 @@ public function test_public_clients_can_be_stored()
112127
$clients, $validator, $redirectRule
113128
);
114129

115-
$this->assertEquals([
116-
'name' => $client->name,
117-
'plainSecret' => $client->plainSecret,
118-
], $controller->store($request));
130+
$this->assertEquals($client, $controller->store($request));
131+
$this->assertNull($client->secret);
132+
$this->assertSame([
133+
'name' => 'client name',
134+
'redirect' => 'http://localhost',
135+
], $client->toArray());
119136
}
120137

121138
public function test_clients_can_be_updated()

0 commit comments

Comments
 (0)