Skip to content

Commit ca53922

Browse files
[13.x] Rename CheckClientCredentials middleware (#1792)
* rename CheckClientCredentials to CheckTokenScope * rename tests * formatting * formatting * formatting * update upgrade guide * Update UPGRADE.md --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d80b6dd commit ca53922

File tree

7 files changed

+38
-28
lines changed

7 files changed

+38
-28
lines changed

UPGRADE.md

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ PR: https://github.com/laravel/passport/pull/1755
6464

6565
When authenticating users via bearer tokens, the `User` model's `token` method now returns an instance of `Laravel\Passport\AccessToken` class instead of `Laravel\Passport\Token`.
6666

67+
### Renamed Middlewares
68+
69+
PR: https://github.com/laravel/passport/pull/1792
70+
71+
Passport's `CheckClientCredentials` and `CheckClientCredentialsForAnyScope` middleware have been renamed to better reflect their functionality:
72+
73+
* `Laravel\Passport\Http\Middleware\CheckClientCredentials` class has been renamed to `CheckToken`.
74+
* `Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope` class has been renamed to `CheckTokenForAnyScope`.
75+
* `Laravel\Passport\Http\Middleware\CheckCredentials` abstract class has been renamed to `ValidateToken`.
76+
6777
### Personal Access Client Table and Model Removal
6878

6979
PR: https://github.com/laravel/passport/pull/1749, https://github.com/laravel/passport/pull/1780

src/Http/Middleware/CheckClientCredentials.php src/Http/Middleware/CheckToken.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
use Laravel\Passport\AccessToken;
66
use Laravel\Passport\Exceptions\MissingScopeException;
77

8-
class CheckClientCredentials extends CheckCredentials
8+
class CheckToken extends ValidateToken
99
{
1010
/**
11-
* Validate token scopes.
11+
* Determine if the token has all the given scopes.
1212
*
1313
* @param string[] $scopes
1414
*
1515
* @throws \Laravel\Passport\Exceptions\MissingScopeException
1616
*/
17-
protected function validateScopes(AccessToken $token, array $scopes): void
17+
protected function hasScopes(AccessToken $token, array $scopes): void
1818
{
1919
if (in_array('*', $token->oauth_scopes)) {
2020
return;

src/Http/Middleware/CheckClientCredentialsForAnyScope.php src/Http/Middleware/CheckTokenForAnyScope.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
use Laravel\Passport\AccessToken;
66
use Laravel\Passport\Exceptions\MissingScopeException;
77

8-
class CheckClientCredentialsForAnyScope extends CheckCredentials
8+
class CheckTokenForAnyScope extends ValidateToken
99
{
1010
/**
11-
* Validate token scopes.
11+
* Determine if the token has at least one of the given scopes.
1212
*
1313
* @param string[] $scopes
1414
*
1515
* @throws \Laravel\Passport\Exceptions\MissingScopeException
1616
*/
17-
protected function validateScopes(AccessToken $token, array $scopes): void
17+
protected function hasScopes(AccessToken $token, array $scopes): void
1818
{
1919
if (in_array('*', $token->oauth_scopes)) {
2020
return;

src/Http/Middleware/CheckCredentials.php src/Http/Middleware/ValidateToken.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
1313
use Symfony\Component\HttpFoundation\Response;
1414

15-
abstract class CheckCredentials
15+
abstract class ValidateToken
1616
{
1717
/**
1818
* Create a new middleware instance.
@@ -59,17 +59,17 @@ public function handle(Request $request, Closure $next, string ...$scopes): Resp
5959
throw new AuthenticationException;
6060
}
6161

62-
$this->validateScopes(AccessToken::fromPsrRequest($psr), $scopes);
62+
$this->hasScopes(AccessToken::fromPsrRequest($psr), $scopes);
6363

6464
return $next($request);
6565
}
6666

6767
/**
68-
* Validate token scopes.
68+
* Determine if the token has the given scopes.
6969
*
7070
* @param string[] $scopes
7171
*
7272
* @throws \Laravel\Passport\Exceptions\MissingScopeException
7373
*/
74-
abstract protected function validateScopes(AccessToken $token, array $scopes): void;
74+
abstract protected function hasScopes(AccessToken $token, array $scopes): void;
7575
}

tests/Feature/ActingAsClientTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use Illuminate\Contracts\Routing\Registrar;
66
use Laravel\Passport\Client;
7-
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
8-
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
7+
use Laravel\Passport\Http\Middleware\CheckToken;
8+
use Laravel\Passport\Http\Middleware\CheckTokenForAnyScope;
99
use Laravel\Passport\Passport;
1010

1111
class ActingAsClientTest extends PassportTestCase
1212
{
13-
public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsMiddleware()
13+
public function testActingAsClientWhenTheRouteIsProtectedByCheckTokenMiddleware()
1414
{
1515
$this->withoutExceptionHandling();
1616

@@ -19,7 +19,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential
1919

2020
$router->get('/foo', function () {
2121
return 'bar';
22-
})->middleware(CheckClientCredentials::class);
22+
})->middleware(CheckToken::class);
2323

2424
Passport::actingAsClient(new Client());
2525

@@ -28,7 +28,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential
2828
$response->assertSee('bar');
2929
}
3030

31-
public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsForAnyScope()
31+
public function testActingAsClientWhenTheRouteIsProtectedByCheckTokenForAnyScope()
3232
{
3333
$this->withoutExceptionHandling();
3434

@@ -37,7 +37,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential
3737

3838
$router->get('/foo', function () {
3939
return 'bar';
40-
})->middleware(CheckClientCredentialsForAnyScope::class.':testFoo');
40+
})->middleware(CheckTokenForAnyScope::class.':testFoo');
4141

4242
Passport::actingAsClient(new Client(), ['testFoo']);
4343

tests/Unit/CheckClientCredentialsForAnyScopeTest.php tests/Unit/CheckTokenForAnyScopeTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Http\Response;
77
use Laravel\Passport\Exceptions\AuthenticationException;
8-
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
8+
use Laravel\Passport\Http\Middleware\CheckTokenForAnyScope;
99
use League\OAuth2\Server\Exception\OAuthServerException;
1010
use League\OAuth2\Server\ResourceServer;
1111
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1212
use Mockery as m;
1313
use PHPUnit\Framework\TestCase;
1414
use Psr\Http\Message\ServerRequestInterface;
1515

16-
class CheckClientCredentialsForAnyScopeTest extends TestCase
16+
class CheckTokenForAnyScopeTest extends TestCase
1717
{
1818
use MockeryPHPUnitIntegration;
1919

@@ -28,7 +28,7 @@ public function test_request_is_passed_along_if_token_is_valid()
2828
'oauth_scopes' => ['*'],
2929
]);
3030

31-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
31+
$middleware = new CheckTokenForAnyScope($resourceServer);
3232

3333
$request = Request::create('/');
3434
$request->headers->set('Authorization', 'Bearer token');
@@ -51,7 +51,7 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
5151
'oauth_scopes' => ['foo', 'bar', 'baz'],
5252
]);
5353

54-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
54+
$middleware = new CheckTokenForAnyScope($resourceServer);
5555

5656
$request = Request::create('/');
5757
$request->headers->set('Authorization', 'Bearer token');
@@ -72,7 +72,7 @@ public function test_exception_is_thrown_when_oauth_throws_exception()
7272
new OAuthServerException('message', 500, 'error type')
7373
);
7474

75-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
75+
$middleware = new CheckTokenForAnyScope($resourceServer);
7676

7777
$request = Request::create('/');
7878
$request->headers->set('Authorization', 'Bearer token');
@@ -95,7 +95,7 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
9595
'oauth_scopes' => ['foo', 'bar'],
9696
]);
9797

98-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);
98+
$middleware = new CheckTokenForAnyScope($resourceServer);
9999

100100
$request = Request::create('/');
101101
$request->headers->set('Authorization', 'Bearer token');

tests/Unit/CheckClientCredentialsTest.php tests/Unit/CheckTokenTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Http\Response;
77
use Laravel\Passport\Exceptions\AuthenticationException;
8-
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
8+
use Laravel\Passport\Http\Middleware\CheckToken;
99
use League\OAuth2\Server\Exception\OAuthServerException;
1010
use League\OAuth2\Server\ResourceServer;
1111
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1212
use Mockery as m;
1313
use PHPUnit\Framework\TestCase;
1414
use Psr\Http\Message\ServerRequestInterface;
1515

16-
class CheckClientCredentialsTest extends TestCase
16+
class CheckTokenTest extends TestCase
1717
{
1818
use MockeryPHPUnitIntegration;
1919

@@ -28,7 +28,7 @@ public function test_request_is_passed_along_if_token_is_valid()
2828
'oauth_scopes' => ['*'],
2929
]);
3030

31-
$middleware = new CheckClientCredentials($resourceServer);
31+
$middleware = new CheckToken($resourceServer);
3232

3333
$request = Request::create('/');
3434
$request->headers->set('Authorization', 'Bearer token');
@@ -51,7 +51,7 @@ public function test_request_is_passed_along_if_token_and_scope_are_valid()
5151
'oauth_scopes' => ['see-profile'],
5252
]);
5353

54-
$middleware = new CheckClientCredentials($resourceServer);
54+
$middleware = new CheckToken($resourceServer);
5555

5656
$request = Request::create('/');
5757
$request->headers->set('Authorization', 'Bearer token');
@@ -72,7 +72,7 @@ public function test_exception_is_thrown_when_oauth_throws_exception()
7272
new OAuthServerException('message', 500, 'error type')
7373
);
7474

75-
$middleware = new CheckClientCredentials($resourceServer);
75+
$middleware = new CheckToken($resourceServer);
7676

7777
$request = Request::create('/');
7878
$request->headers->set('Authorization', 'Bearer token');
@@ -95,7 +95,7 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scopes(
9595
'oauth_scopes' => ['foo', 'notbar'],
9696
]);
9797

98-
$middleware = new CheckClientCredentials($resourceServer);
98+
$middleware = new CheckToken($resourceServer);
9999

100100
$request = Request::create('/');
101101
$request->headers->set('Authorization', 'Bearer token');

0 commit comments

Comments
 (0)