Skip to content

Commit 97e3026

Browse files
committed
Upgrade to league/oauth2-server 8.0
1 parent 839b842 commit 97e3026

7 files changed

+73
-43
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"illuminate/encryption": "~5.8.0|~5.9.0",
2727
"illuminate/http": "~5.8.0|~5.9.0",
2828
"illuminate/support": "~5.8.0|~5.9.0",
29-
"league/oauth2-server": "^7.0",
29+
"league/oauth2-server": "^8.0",
3030
"phpseclib/phpseclib": "^2.0",
3131
"symfony/psr-http-message-bridge": "^1.0",
3232
"zendframework/zend-diactoros": "^2.0"

src/Bridge/AccessToken.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Passport\Bridge;
44

55
use League\OAuth2\Server\Entities\Traits\EntityTrait;
6+
use League\OAuth2\Server\Entities\ClientEntityInterface;
67
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
78
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
89
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
@@ -16,14 +17,17 @@ class AccessToken implements AccessTokenEntityInterface
1617
*
1718
* @param string $userIdentifier
1819
* @param array $scopes
20+
* @param ClientEntityInterface $client
1921
* @return void
2022
*/
21-
public function __construct($userIdentifier, array $scopes = [])
23+
public function __construct($userIdentifier, array $scopes, ClientEntityInterface $client)
2224
{
2325
$this->setUserIdentifier($userIdentifier);
2426

2527
foreach ($scopes as $scope) {
2628
$this->addScope($scope);
2729
}
30+
31+
$this->setClient($client);
2832
}
2933
}

src/Bridge/AccessTokenRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(TokenRepository $tokenRepository, Dispatcher $events
4545
*/
4646
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
4747
{
48-
return new AccessToken($userIdentifier, $scopes);
48+
return new AccessToken($userIdentifier, $scopes, $clientEntity);
4949
}
5050

5151
/**

src/Bridge/Client.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class Client implements ClientEntityInterface
1616
* @param string $identifier
1717
* @param string $name
1818
* @param string $redirectUri
19+
* @param bool $isConfidential
1920
* @return void
2021
*/
21-
public function __construct($identifier, $name, $redirectUri)
22+
public function __construct($identifier, $name, $redirectUri, $isConfidential = false)
2223
{
2324
$this->setIdentifier((string) $identifier);
2425

2526
$this->name = $name;
2627
$this->redirectUri = explode(',', $redirectUri);
28+
$this->isConfidential = $isConfidential;
2729
}
2830
}

src/Bridge/ClientRepository.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,32 @@ public function __construct(ClientModelRepository $clients)
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function getClientEntity($clientIdentifier, $grantType = null,
32-
$clientSecret = null, $mustValidateSecret = true)
31+
public function getClientEntity($clientIdentifier)
3332
{
34-
// First, we will verify that the client exists and is authorized to create personal
35-
// access tokens. Generally personal access tokens are only generated by the user
36-
// from the main interface. We'll only let certain clients generate the tokens.
3733
$record = $this->clients->findActive($clientIdentifier);
3834

39-
if (! $record || ! $this->handlesGrant($record, $grantType)) {
35+
if (! $record) {
4036
return;
4137
}
4238

43-
// Once we have an existing client record we will create this actual client instance
44-
// and verify the secret if necessary. If the secret is valid we will be ready to
45-
// return this client instance back out to the consuming methods and finish up.
46-
$client = new Client(
47-
$clientIdentifier, $record->name, $record->redirect
39+
return new Client(
40+
$clientIdentifier, $record->name, $record->redirect, ! is_null($record->secret)
4841
);
42+
}
4943

50-
if ($mustValidateSecret &&
51-
! hash_equals($record->secret, (string) $clientSecret)) {
52-
return;
44+
public function validateClient($clientIdentifier, $clientSecret, $grantType)
45+
{
46+
// First, we will verify that the client exists and is authorized to create personal
47+
// access tokens. Generally personal access tokens are only generated by the user
48+
// from the main interface. We'll only let certain clients generate the tokens.
49+
$record = $this->clients->findActive($clientIdentifier);
50+
51+
if (! $record || ! $this->handlesGrant($record, $grantType)) {
52+
return false;
5353
}
5454

55-
return $client;
55+
// Once we have an existing client record we will verify the secret.
56+
return hash_equals($record->secret, (string) $clientSecret);
5657
}
5758

5859
/**

tests/BridgeAccessTokenRepositoryTest.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Laravel\Passport\Tests;
44

55
use Mockery as m;
6-
use Carbon\Carbon;
6+
use Carbon\CarbonImmutable;
77
use PHPUnit\Framework\TestCase;
88
use Laravel\Passport\Bridge\Scope;
99
use Laravel\Passport\Bridge\Client;
@@ -21,7 +21,7 @@ public function tearDown()
2121

2222
public function test_access_tokens_can_be_persisted()
2323
{
24-
$expiration = Carbon::now();
24+
$expiration = CarbonImmutable::now();
2525

2626
$tokenRepository = m::mock(TokenRepository::class);
2727
$events = m::mock(Dispatcher::class);
@@ -39,13 +39,29 @@ public function test_access_tokens_can_be_persisted()
3939

4040
$events->shouldReceive('dispatch')->once();
4141

42-
$accessToken = new AccessToken(2, [new Scope('scopes')]);
42+
$accessToken = new AccessToken(2, [new Scope('scopes')], new Client('client-id', 'name', 'redirect'));
4343
$accessToken->setIdentifier(1);
4444
$accessToken->setExpiryDateTime($expiration);
45-
$accessToken->setClient(new Client('client-id', 'name', 'redirect'));
4645

4746
$repository = new AccessTokenRepository($tokenRepository, $events);
4847

4948
$repository->persistNewAccessToken($accessToken);
5049
}
50+
51+
public function test_can_get_new_access_token()
52+
{
53+
$tokenRepository = m::mock(TokenRepository::class);
54+
$events = m::mock(Dispatcher::class);
55+
$repository = new AccessTokenRepository($tokenRepository, $events);
56+
$client = new Client('client-id', 'name', 'redirect');
57+
$scopes = [new Scope('place-orders'), new Scope('check-status')];
58+
$userIdentifier = 123;
59+
60+
$token = $repository->getNewToken($client, $scopes, $userIdentifier);
61+
62+
$this->assertInstanceOf(AccessToken::class, $token);
63+
$this->assertEquals($client, $token->getClient());
64+
$this->assertEquals($scopes, $token->getScopes());
65+
$this->assertEquals($userIdentifier, $token->getUserIdentifier());
66+
}
5167
}

tests/BridgeClientRepositoryTest.php

+27-20
Original file line numberDiff line numberDiff line change
@@ -38,93 +38,100 @@ public function tearDown()
3838
unset($this->clientModelRepository, $this->repository);
3939
}
4040

41-
public function test_can_get_client_for_auth_code_grant()
41+
public function test_can_get_client()
4242
{
43-
$client = $this->repository->getClientEntity(1, 'authorization_code', 'secret', true);
43+
$client = $this->repository->getClientEntity(1);
4444

4545
$this->assertInstanceOf(Client::class, $client);
46-
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'wrong-secret', true));
47-
$this->assertNull($this->repository->getClientEntity(1, 'client_credentials', 'wrong-secret', true));
46+
$this->assertEquals('1', $client->getIdentifier());
47+
$this->assertEquals('Client', $client->getName());
48+
$this->assertEquals(['http://localhost'], $client->getRedirectUri());
49+
$this->assertTrue($client->isConfidential());
4850
}
4951

50-
public function test_can_get_client_for_client_credentials_grant()
52+
public function test_can_validate_client_for_auth_code_grant()
53+
{
54+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'authorization_code'));
55+
$this->assertFalse($this->repository->validateClient(1, 'wrong-secret', 'authorization_code'));
56+
$this->assertFalse($this->repository->validateClient(1, 'wrong-secret', 'client_credentials'));
57+
}
58+
59+
public function test_can_validate_client_for_client_credentials_grant()
5160
{
5261
$client = $this->clientModelRepository->findActive(1);
5362
$client->personal_access_client = true;
5463

55-
$this->assertInstanceOf(
56-
Client::class,
57-
$this->repository->getClientEntity(1, 'client_credentials', 'secret', true)
58-
);
59-
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'secret', true));
64+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'client_credentials'));
65+
$this->assertFalse($this->repository->validateClient(1, 'wrong-secret', 'client_credentials'));
66+
$this->assertFalse($this->repository->validateClient(1, 'secret', 'authorization_code'));
6067
}
6168

6269
public function test_password_grant_is_permitted()
6370
{
6471
$client = $this->clientModelRepository->findActive(1);
6572
$client->password_client = true;
6673

67-
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'password', 'secret'));
74+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'password'));
6875
}
6976

7077
public function test_password_grant_is_prevented()
7178
{
72-
$this->assertNull($this->repository->getClientEntity(1, 'password', 'secret'));
79+
$this->assertFalse($this->repository->validateClient(1, 'secret', 'password'));
7380
}
7481

7582
public function test_authorization_code_grant_is_permitted()
7683
{
77-
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'authorization_code', 'secret'));
84+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'authorization_code'));
7885
}
7986

8087
public function test_authorization_code_grant_is_prevented()
8188
{
8289
$client = $this->clientModelRepository->findActive(1);
8390
$client->password_client = true;
8491

85-
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'secret'));
92+
$this->assertFalse($this->repository->validateClient(1, 'secret', 'authorization_code'));
8693
}
8794

8895
public function test_personal_access_grant_is_permitted()
8996
{
9097
$client = $this->clientModelRepository->findActive(1);
9198
$client->personal_access_client = true;
9299

93-
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'personal_access', 'secret'));
100+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'personal_access'));
94101
}
95102

96103
public function test_personal_access_grant_is_prevented()
97104
{
98-
$this->assertNull($this->repository->getClientEntity(1, 'personal_access', 'secret'));
105+
$this->assertFalse($this->repository->validateClient(1, 'secret', 'personal_access'));
99106
}
100107

101108
public function test_client_credentials_grant_is_permitted()
102109
{
103-
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'client_credentials', 'secret'));
110+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'client_credentials'));
104111
}
105112

106113
public function test_client_credentials_grant_is_prevented()
107114
{
108115
$client = $this->clientModelRepository->findActive(1);
109116
$client->secret = null;
110117

111-
$this->assertNull($this->repository->getClientEntity(1, 'client_credentials', 'secret'));
118+
$this->assertFalse($this->repository->validateClient(1, 'secret', 'client_credentials'));
112119
}
113120

114121
public function test_grant_types_allows_request()
115122
{
116123
$client = $this->clientModelRepository->findActive(1);
117124
$client->grant_types = ['client_credentials'];
118125

119-
$this->assertInstanceOf(Client::class, $this->repository->getClientEntity(1, 'client_credentials', 'secret'));
126+
$this->assertTrue($this->repository->validateClient(1, 'secret', 'client_credentials'));
120127
}
121128

122129
public function test_grant_types_disallows_request()
123130
{
124131
$client = $this->clientModelRepository->findActive(1);
125132
$client->grant_types = ['client_credentials'];
126133

127-
$this->assertNull($this->repository->getClientEntity(1, 'authorization_code', 'secret'));
134+
$this->assertFalse($this->repository->validateClient(1, 'secret', 'authorization_code'));
128135
}
129136
}
130137

0 commit comments

Comments
 (0)