Skip to content

Commit

Permalink
[13.x] Rename CheckClientCredentials middleware (#1792)
Browse files Browse the repository at this point in the history
* rename CheckClientCredentials to CheckTokenScope

* rename tests

* formatting

* formatting

* formatting

* update upgrade guide

* Update UPGRADE.md

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
hafezdivandari and taylorotwell authored Oct 7, 2024
1 parent d80b6dd commit ca53922
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 28 deletions.
10 changes: 10 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions tests/Feature/ActingAsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -19,7 +19,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential

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

Passport::actingAsClient(new Client());

Expand All @@ -28,7 +28,7 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential
$response->assertSee('bar');
}

public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsForAnyScope()
public function testActingAsClientWhenTheRouteIsProtectedByCheckTokenForAnyScope()
{
$this->withoutExceptionHandling();

Expand All @@ -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']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

class CheckClientCredentialsForAnyScopeTest extends TestCase
class CheckTokenForAnyScopeTest extends TestCase
{
use MockeryPHPUnitIntegration;

Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

class CheckClientCredentialsTest extends TestCase
class CheckTokenTest extends TestCase
{
use MockeryPHPUnitIntegration;

Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit ca53922

Please sign in to comment.