From ca53922dc6aefdec9b0b44eb7d2997be9525d3b4 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 7 Oct 2024 17:48:16 +0330 Subject: [PATCH] [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 --- UPGRADE.md | 10 ++++++++++ .../{CheckClientCredentials.php => CheckToken.php} | 6 +++--- ...ialsForAnyScope.php => CheckTokenForAnyScope.php} | 6 +++--- .../{CheckCredentials.php => ValidateToken.php} | 8 ++++---- tests/Feature/ActingAsClientTest.php | 12 ++++++------ ...nyScopeTest.php => CheckTokenForAnyScopeTest.php} | 12 ++++++------ ...kClientCredentialsTest.php => CheckTokenTest.php} | 12 ++++++------ 7 files changed, 38 insertions(+), 28 deletions(-) rename src/Http/Middleware/{CheckClientCredentials.php => CheckToken.php} (75%) rename src/Http/Middleware/{CheckClientCredentialsForAnyScope.php => CheckTokenForAnyScope.php} (73%) rename src/Http/Middleware/{CheckCredentials.php => ValidateToken.php} (88%) rename tests/Unit/{CheckClientCredentialsForAnyScopeTest.php => CheckTokenForAnyScopeTest.php} (88%) rename tests/Unit/{CheckClientCredentialsTest.php => CheckTokenTest.php} (90%) diff --git a/UPGRADE.md b/UPGRADE.md index fdf9f9e68..8a7186db7 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -64,6 +64,16 @@ PR: https://github.com/laravel/passport/pull/1755 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`. +### Renamed Middlewares + +PR: https://github.com/laravel/passport/pull/1792 + +Passport's `CheckClientCredentials` and `CheckClientCredentialsForAnyScope` middleware have been renamed to better reflect their functionality: + +* `Laravel\Passport\Http\Middleware\CheckClientCredentials` class has been renamed to `CheckToken`. +* `Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope` class has been renamed to `CheckTokenForAnyScope`. +* `Laravel\Passport\Http\Middleware\CheckCredentials` abstract class has been renamed to `ValidateToken`. + ### Personal Access Client Table and Model Removal PR: https://github.com/laravel/passport/pull/1749, https://github.com/laravel/passport/pull/1780 diff --git a/src/Http/Middleware/CheckClientCredentials.php b/src/Http/Middleware/CheckToken.php similarity index 75% rename from src/Http/Middleware/CheckClientCredentials.php rename to src/Http/Middleware/CheckToken.php index b29ab3f25..552890f79 100644 --- a/src/Http/Middleware/CheckClientCredentials.php +++ b/src/Http/Middleware/CheckToken.php @@ -5,16 +5,16 @@ use Laravel\Passport\AccessToken; use Laravel\Passport\Exceptions\MissingScopeException; -class CheckClientCredentials extends CheckCredentials +class CheckToken extends ValidateToken { /** - * Validate token scopes. + * Determine if the token has all the given scopes. * * @param string[] $scopes * * @throws \Laravel\Passport\Exceptions\MissingScopeException */ - protected function validateScopes(AccessToken $token, array $scopes): void + protected function hasScopes(AccessToken $token, array $scopes): void { if (in_array('*', $token->oauth_scopes)) { return; diff --git a/src/Http/Middleware/CheckClientCredentialsForAnyScope.php b/src/Http/Middleware/CheckTokenForAnyScope.php similarity index 73% rename from src/Http/Middleware/CheckClientCredentialsForAnyScope.php rename to src/Http/Middleware/CheckTokenForAnyScope.php index 9a3766b9f..b05e142a7 100644 --- a/src/Http/Middleware/CheckClientCredentialsForAnyScope.php +++ b/src/Http/Middleware/CheckTokenForAnyScope.php @@ -5,16 +5,16 @@ use Laravel\Passport\AccessToken; use Laravel\Passport\Exceptions\MissingScopeException; -class CheckClientCredentialsForAnyScope extends CheckCredentials +class CheckTokenForAnyScope extends ValidateToken { /** - * Validate token scopes. + * Determine if the token has at least one of the given scopes. * * @param string[] $scopes * * @throws \Laravel\Passport\Exceptions\MissingScopeException */ - protected function validateScopes(AccessToken $token, array $scopes): void + protected function hasScopes(AccessToken $token, array $scopes): void { if (in_array('*', $token->oauth_scopes)) { return; diff --git a/src/Http/Middleware/CheckCredentials.php b/src/Http/Middleware/ValidateToken.php similarity index 88% rename from src/Http/Middleware/CheckCredentials.php rename to src/Http/Middleware/ValidateToken.php index 3cb12c372..b7f8b7b76 100644 --- a/src/Http/Middleware/CheckCredentials.php +++ b/src/Http/Middleware/ValidateToken.php @@ -12,7 +12,7 @@ use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; use Symfony\Component\HttpFoundation\Response; -abstract class CheckCredentials +abstract class ValidateToken { /** * Create a new middleware instance. @@ -59,17 +59,17 @@ public function handle(Request $request, Closure $next, string ...$scopes): Resp throw new AuthenticationException; } - $this->validateScopes(AccessToken::fromPsrRequest($psr), $scopes); + $this->hasScopes(AccessToken::fromPsrRequest($psr), $scopes); return $next($request); } /** - * Validate token scopes. + * Determine if the token has the given scopes. * * @param string[] $scopes * * @throws \Laravel\Passport\Exceptions\MissingScopeException */ - abstract protected function validateScopes(AccessToken $token, array $scopes): void; + abstract protected function hasScopes(AccessToken $token, array $scopes): void; } diff --git a/tests/Feature/ActingAsClientTest.php b/tests/Feature/ActingAsClientTest.php index f0a92c7d7..c08c4d83c 100644 --- a/tests/Feature/ActingAsClientTest.php +++ b/tests/Feature/ActingAsClientTest.php @@ -4,13 +4,13 @@ use Illuminate\Contracts\Routing\Registrar; use Laravel\Passport\Client; -use Laravel\Passport\Http\Middleware\CheckClientCredentials; -use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope; +use Laravel\Passport\Http\Middleware\CheckToken; +use Laravel\Passport\Http\Middleware\CheckTokenForAnyScope; use Laravel\Passport\Passport; class ActingAsClientTest extends PassportTestCase { - public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsMiddleware() + public function testActingAsClientWhenTheRouteIsProtectedByCheckTokenMiddleware() { $this->withoutExceptionHandling(); @@ -19,7 +19,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential $router->get('/foo', function () { return 'bar'; - })->middleware(CheckClientCredentials::class); + })->middleware(CheckToken::class); Passport::actingAsClient(new Client()); @@ -28,7 +28,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential $response->assertSee('bar'); } - public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsForAnyScope() + public function testActingAsClientWhenTheRouteIsProtectedByCheckTokenForAnyScope() { $this->withoutExceptionHandling(); @@ -37,7 +37,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential $router->get('/foo', function () { return 'bar'; - })->middleware(CheckClientCredentialsForAnyScope::class.':testFoo'); + })->middleware(CheckTokenForAnyScope::class.':testFoo'); Passport::actingAsClient(new Client(), ['testFoo']); diff --git a/tests/Unit/CheckClientCredentialsForAnyScopeTest.php b/tests/Unit/CheckTokenForAnyScopeTest.php similarity index 88% rename from tests/Unit/CheckClientCredentialsForAnyScopeTest.php rename to tests/Unit/CheckTokenForAnyScopeTest.php index 1c32a15b1..da5ef7d33 100644 --- a/tests/Unit/CheckClientCredentialsForAnyScopeTest.php +++ b/tests/Unit/CheckTokenForAnyScopeTest.php @@ -5,7 +5,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Laravel\Passport\Exceptions\AuthenticationException; -use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope; +use Laravel\Passport\Http\Middleware\CheckTokenForAnyScope; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\ResourceServer; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; @@ -13,7 +13,7 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; -class CheckClientCredentialsForAnyScopeTest extends TestCase +class CheckTokenForAnyScopeTest extends TestCase { use MockeryPHPUnitIntegration; @@ -28,7 +28,7 @@ public function test_request_is_passed_along_if_token_is_valid() 'oauth_scopes' => ['*'], ]); - $middleware = new CheckClientCredentialsForAnyScope($resourceServer); + $middleware = new CheckTokenForAnyScope($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); @@ -51,7 +51,7 @@ public function test_request_is_passed_along_if_token_has_any_required_scope() 'oauth_scopes' => ['foo', 'bar', 'baz'], ]); - $middleware = new CheckClientCredentialsForAnyScope($resourceServer); + $middleware = new CheckTokenForAnyScope($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); @@ -72,7 +72,7 @@ public function test_exception_is_thrown_when_oauth_throws_exception() new OAuthServerException('message', 500, 'error type') ); - $middleware = new CheckClientCredentialsForAnyScope($resourceServer); + $middleware = new CheckTokenForAnyScope($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); @@ -95,7 +95,7 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope() 'oauth_scopes' => ['foo', 'bar'], ]); - $middleware = new CheckClientCredentialsForAnyScope($resourceServer); + $middleware = new CheckTokenForAnyScope($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); diff --git a/tests/Unit/CheckClientCredentialsTest.php b/tests/Unit/CheckTokenTest.php similarity index 90% rename from tests/Unit/CheckClientCredentialsTest.php rename to tests/Unit/CheckTokenTest.php index 6c69686e9..555293e01 100644 --- a/tests/Unit/CheckClientCredentialsTest.php +++ b/tests/Unit/CheckTokenTest.php @@ -5,7 +5,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Laravel\Passport\Exceptions\AuthenticationException; -use Laravel\Passport\Http\Middleware\CheckClientCredentials; +use Laravel\Passport\Http\Middleware\CheckToken; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\ResourceServer; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; @@ -13,7 +13,7 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; -class CheckClientCredentialsTest extends TestCase +class CheckTokenTest extends TestCase { use MockeryPHPUnitIntegration; @@ -28,7 +28,7 @@ public function test_request_is_passed_along_if_token_is_valid() 'oauth_scopes' => ['*'], ]); - $middleware = new CheckClientCredentials($resourceServer); + $middleware = new CheckToken($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); @@ -51,7 +51,7 @@ public function test_request_is_passed_along_if_token_and_scope_are_valid() 'oauth_scopes' => ['see-profile'], ]); - $middleware = new CheckClientCredentials($resourceServer); + $middleware = new CheckToken($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); @@ -72,7 +72,7 @@ public function test_exception_is_thrown_when_oauth_throws_exception() new OAuthServerException('message', 500, 'error type') ); - $middleware = new CheckClientCredentials($resourceServer); + $middleware = new CheckToken($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token'); @@ -95,7 +95,7 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scopes( 'oauth_scopes' => ['foo', 'notbar'], ]); - $middleware = new CheckClientCredentials($resourceServer); + $middleware = new CheckToken($resourceServer); $request = Request::create('/'); $request->headers->set('Authorization', 'Bearer token');