Skip to content

Commit 4ffc542

Browse files
[13.x] Cleanup and add feature tests (#1767)
* cleanup * add test * fix tests * formatting
1 parent 5ab8904 commit 4ffc542

9 files changed

+61
-193
lines changed

src/ClientRepository.php

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,9 @@
33
namespace Laravel\Passport;
44

55
use Illuminate\Support\Str;
6-
use RuntimeException;
76

87
class ClientRepository
98
{
10-
/**
11-
* The personal access client ID.
12-
*
13-
* @var int|string|null
14-
*/
15-
protected $personalAccessClientId;
16-
17-
/**
18-
* The personal access client secret.
19-
*
20-
* @var string|null
21-
*/
22-
protected $personalAccessClientSecret;
23-
24-
/**
25-
* Create a new client repository.
26-
*
27-
* @param int|string|null $personalAccessClientId
28-
* @param string|null $personalAccessClientSecret
29-
* @return void
30-
*/
31-
public function __construct($personalAccessClientId = null, $personalAccessClientSecret = null)
32-
{
33-
$this->personalAccessClientId = $personalAccessClientId;
34-
$this->personalAccessClientSecret = $personalAccessClientSecret;
35-
}
36-
379
/**
3810
* Get a client by the given ID.
3911
*
@@ -103,22 +75,6 @@ public function activeForUser($userId)
10375
})->values();
10476
}
10577

106-
/**
107-
* Get the personal access token client for the application.
108-
*
109-
* @return \Laravel\Passport\Client
110-
*
111-
* @throws \RuntimeException
112-
*/
113-
public function personalAccessClient()
114-
{
115-
$client = $this->personalAccessClientId ? $this->find($this->personalAccessClientId) : null;
116-
117-
return $client ?? throw new RuntimeException(
118-
'Personal access client not found. Please create one and set the `PASSPORT_PERSONAL_ACCESS_CLIENT_ID` and `PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET` environment variables.'
119-
);
120-
}
121-
12278
/**
12379
* Store a new client.
12480
*
@@ -233,24 +189,4 @@ public function delete(Client $client)
233189

234190
$client->forceFill(['revoked' => true])->save();
235191
}
236-
237-
/**
238-
* Get the personal access client id.
239-
*
240-
* @return int|string|null
241-
*/
242-
public function getPersonalAccessClientId()
243-
{
244-
return $this->personalAccessClientId;
245-
}
246-
247-
/**
248-
* Get the personal access client secret.
249-
*
250-
* @return string|null
251-
*/
252-
public function getPersonalAccessClientSecret()
253-
{
254-
return $this->personalAccessClientSecret;
255-
}
256192
}

src/HasApiTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function tokenCan($scope)
6464
public function createToken($name, array $scopes = [])
6565
{
6666
return Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
67-
$this->getKey(), $name, $scopes
67+
$this->getAuthIdentifier(), $name, $scopes
6868
);
6969
}
7070

src/PassportServiceProvider.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ public function register()
131131
->give(fn () => Auth::guard(config('passport.guard', null)));
132132

133133
$this->registerAuthorizationServer();
134-
$this->registerClientRepository();
135134
$this->registerJWTParser();
136135
$this->registerResourceServer();
137136
$this->registerGuard();
@@ -265,20 +264,6 @@ public function makeAuthorizationServer()
265264
);
266265
}
267266

268-
/**
269-
* Register the client repository.
270-
*
271-
* @return void
272-
*/
273-
protected function registerClientRepository()
274-
{
275-
$this->app->singleton(ClientRepository::class, function ($container) {
276-
$config = $container->make('config')->get('passport.personal_access_client');
277-
278-
return new ClientRepository($config['id'] ?? null, $config['secret'] ?? null);
279-
});
280-
}
281-
282267
/**
283268
* Register the JWT Parser.
284269
*

src/PersonalAccessTokenFactory.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Nyholm\Psr7\Response;
88
use Nyholm\Psr7\ServerRequest;
99
use Psr\Http\Message\ServerRequestInterface;
10+
use RuntimeException;
1011

1112
class PersonalAccessTokenFactory
1213
{
@@ -17,13 +18,6 @@ class PersonalAccessTokenFactory
1718
*/
1819
protected $server;
1920

20-
/**
21-
* The client repository instance.
22-
*
23-
* @var \Laravel\Passport\ClientRepository
24-
*/
25-
protected $clients;
26-
2721
/**
2822
* The token repository instance.
2923
*
@@ -42,31 +36,26 @@ class PersonalAccessTokenFactory
4236
* Create a new personal access token factory instance.
4337
*
4438
* @param \League\OAuth2\Server\AuthorizationServer $server
45-
* @param \Laravel\Passport\ClientRepository $clients
4639
* @param \Laravel\Passport\TokenRepository $tokens
4740
* @param \Lcobucci\JWT\Parser $jwt
4841
* @return void
4942
*/
50-
public function __construct(AuthorizationServer $server,
51-
ClientRepository $clients,
52-
TokenRepository $tokens,
53-
JwtParser $jwt)
43+
public function __construct(AuthorizationServer $server, TokenRepository $tokens, JwtParser $jwt)
5444
{
5545
$this->jwt = $jwt;
5646
$this->tokens = $tokens;
5747
$this->server = $server;
58-
$this->clients = $clients;
5948
}
6049

6150
/**
6251
* Create a new personal access token.
6352
*
6453
* @param mixed $userId
6554
* @param string $name
66-
* @param array $scopes
55+
* @param string[] $scopes
6756
* @return \Laravel\Passport\PersonalAccessTokenResult
6857
*/
69-
public function make($userId, $name, array $scopes = [])
58+
public function make($userId, string $name, array $scopes = [])
7059
{
7160
$response = $this->dispatchRequestToAuthorizationServer(
7261
$this->createRequest($userId, $scopes)
@@ -88,15 +77,23 @@ public function make($userId, $name, array $scopes = [])
8877
* Create a request instance for the given client.
8978
*
9079
* @param mixed $userId
91-
* @param array $scopes
80+
* @param string[] $scopes
9281
* @return \Psr\Http\Message\ServerRequestInterface
9382
*/
9483
protected function createRequest($userId, array $scopes)
9584
{
85+
$config = config('passport.personal_access_client');
86+
87+
if (! $config) {
88+
throw new RuntimeException(
89+
'Personal access client not found. Please create one and set the `PASSPORT_PERSONAL_ACCESS_CLIENT_ID` and `PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET` environment variables.'
90+
);
91+
}
92+
9693
return (new ServerRequest('POST', 'not-important'))->withParsedBody([
9794
'grant_type' => 'personal_access',
98-
'client_id' => $this->clients->getPersonalAccessClientId(),
99-
'client_secret' => $this->clients->getPersonalAccessClientSecret(),
95+
'client_id' => $config['id'] ?? null,
96+
'client_secret' => $config['secret'] ?? null,
10097
'user_id' => $userId,
10198
'scope' => implode(' ', $scopes),
10299
]);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Laravel\Passport\Tests\Feature;
4+
5+
use Illuminate\Contracts\Hashing\Hasher;
6+
use Laravel\Passport\Client;
7+
use Laravel\Passport\Database\Factories\ClientFactory;
8+
use Laravel\Passport\Passport;
9+
use Laravel\Passport\PersonalAccessTokenResult;
10+
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
11+
use Workbench\Database\Factories\UserFactory;
12+
13+
class PersonalAccessTokenFactoryTest extends PassportTestCase
14+
{
15+
use WithLaravelMigrations;
16+
17+
public function testIssueToken()
18+
{
19+
$user = UserFactory::new()->create([
20+
'email' => '[email protected]',
21+
'password' => $this->app->make(Hasher::class)->make('foobar123'),
22+
]);
23+
24+
/** @var Client $client */
25+
$client = ClientFactory::new()->asPersonalAccessTokenClient()->create();
26+
27+
config([
28+
'passport.personal_access_client.id' => $client->getKey(),
29+
'passport.personal_access_client.secret' => $client->plainSecret,
30+
]);
31+
32+
Passport::tokensCan([
33+
'foo' => 'Do foo',
34+
'bar' => 'Do bar',
35+
]);
36+
37+
$result = $user->createToken('test', ['bar']);
38+
39+
$this->assertInstanceOf(PersonalAccessTokenResult::class, $result);
40+
$this->assertSame($client->getKey(), $result->token->client_id);
41+
$this->assertSame($user->getAuthIdentifier(), $result->token->user_id);
42+
$this->assertSame(['bar'], $result->token->scopes);
43+
}
44+
}

tests/Unit/BridgeClientRepositoryTest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,4 @@ class BridgeClientRepositoryTestClientStub extends \Laravel\Passport\Client
197197
public $password_client = false;
198198

199199
public $provider = null;
200-
201-
public $grant_types;
202-
203-
public function firstParty()
204-
{
205-
return $this->personal_access_client || $this->password_client;
206-
}
207-
208-
public function confidential()
209-
{
210-
return ! empty($this->secret);
211-
}
212-
213-
public function hasGrantType($grantType)
214-
{
215-
if (! isset($this->grant_types) || ! is_array($this->grant_types)) {
216-
return true;
217-
}
218-
219-
return in_array($grantType, $this->grant_types);
220-
}
221200
}

tests/Unit/HasApiTokensTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class HasApiTokensTestStub
4545
{
4646
use HasApiTokens;
4747

48-
public function getKey()
48+
public function getAuthIdentifier()
4949
{
5050
return 1;
5151
}

tests/Unit/PassportTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Laravel\Passport\AuthCode;
66
use Laravel\Passport\Client;
7-
use Laravel\Passport\ClientRepository;
87
use Laravel\Passport\Passport;
98
use Laravel\Passport\RefreshToken;
109
use Laravel\Passport\Token;
@@ -39,14 +38,6 @@ public function test_client_instance_can_be_created()
3938
$this->assertInstanceOf(Passport::clientModel(), $client);
4039
}
4140

42-
public function test_missing_personal_access_client_is_reported()
43-
{
44-
$this->expectException('RuntimeException');
45-
46-
$clientRepository = new ClientRepository;
47-
$clientRepository->personalAccessClient();
48-
}
49-
5041
public function test_token_instance_can_be_created()
5142
{
5243
$token = Passport::token();

tests/Unit/PersonalAccessTokenFactoryTest.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)