From ab5ad1e506cdb55fd7d4802b018c1b3445dbfeaa Mon Sep 17 00:00:00 2001 From: Choraimy Kroonstuiver <3661474+axlon@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:57:28 +0100 Subject: [PATCH 1/3] Allow token guard client to be set programmatically --- src/Guards/TokenGuard.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Guards/TokenGuard.php b/src/Guards/TokenGuard.php index 640c6961e..a10a1fed5 100644 --- a/src/Guards/TokenGuard.php +++ b/src/Guards/TokenGuard.php @@ -13,6 +13,7 @@ use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Http\Request; use Illuminate\Support\Traits\Macroable; +use Laravel\Passport\Client; use Laravel\Passport\ClientRepository; use Laravel\Passport\Passport; use Laravel\Passport\PassportUserProvider; @@ -353,4 +354,17 @@ public static function serialized() { return EncryptCookies::serialized('XSRF-TOKEN'); } + + /** + * Set the client for the current request. + * + * @param \Laravel\Passport\Client $client + * @return $this + */ + public function setClient(Client $client) + { + $this->client = $client; + + return $this; + } } From b975168debcef1ee11dc8a5b258936fc808f2568 Mon Sep 17 00:00:00 2001 From: Choraimy Kroonstuiver <3661474+axlon@users.noreply.github.com> Date: Tue, 18 Jan 2022 19:19:41 +0100 Subject: [PATCH 2/3] Set the client on the TokenGuard when mocking the client --- src/Passport.php | 7 ++++++- tests/Feature/ActingAsClientTest.php | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Passport.php b/src/Passport.php index c12618571..d73189953 100644 --- a/src/Passport.php +++ b/src/Passport.php @@ -381,9 +381,10 @@ public static function actingAs($user, $scopes = [], $guard = 'api') * * @param \Laravel\Passport\Client $client * @param array $scopes + * @param string $guard * @return \Laravel\Passport\Client */ - public static function actingAsClient($client, $scopes = []) + public static function actingAsClient($client, $scopes = [], $guard = 'api') { $token = app(self::tokenModel()); @@ -407,6 +408,10 @@ public static function actingAsClient($client, $scopes = []) app()->instance(TokenRepository::class, $mock); + app('auth')->guard($guard)->setClient($client); + + app('auth')->shouldUse($guard); + return $client; } diff --git a/tests/Feature/ActingAsClientTest.php b/tests/Feature/ActingAsClientTest.php index 8fc99b777..595890ffa 100644 --- a/tests/Feature/ActingAsClientTest.php +++ b/tests/Feature/ActingAsClientTest.php @@ -9,7 +9,7 @@ use Laravel\Passport\Passport; use Orchestra\Testbench\TestCase; -class ActingAsClientTest extends TestCase +class ActingAsClientTest extends PassportTestCase { public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredentialsMiddleware() { @@ -46,4 +46,11 @@ public function testActingAsClientWhenTheRouteIsProtectedByCheckClientCredential $response->assertSuccessful(); $response->assertSee('bar'); } + + public function testActingAsClientSetsTheClientOnTheGuard() + { + Passport::actingAsClient($client = new Client()); + + $this->assertSame($client, app('auth')->client()); + } } From 6bed7b40e139cda3f831d2af4385258ed8bb929d Mon Sep 17 00:00:00 2001 From: Choraimy Kroonstuiver <3661474+axlon@users.noreply.github.com> Date: Fri, 21 Jan 2022 19:03:26 +0100 Subject: [PATCH 3/3] Apply fixes from StyleCI --- src/Passport.php | 2 +- tests/Feature/ActingAsClientTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Passport.php b/src/Passport.php index d73189953..38c6c8542 100644 --- a/src/Passport.php +++ b/src/Passport.php @@ -381,7 +381,7 @@ public static function actingAs($user, $scopes = [], $guard = 'api') * * @param \Laravel\Passport\Client $client * @param array $scopes - * @param string $guard + * @param string $guard * @return \Laravel\Passport\Client */ public static function actingAsClient($client, $scopes = [], $guard = 'api') diff --git a/tests/Feature/ActingAsClientTest.php b/tests/Feature/ActingAsClientTest.php index 595890ffa..f0a92c7d7 100644 --- a/tests/Feature/ActingAsClientTest.php +++ b/tests/Feature/ActingAsClientTest.php @@ -7,7 +7,6 @@ use Laravel\Passport\Http\Middleware\CheckClientCredentials; use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope; use Laravel\Passport\Passport; -use Orchestra\Testbench\TestCase; class ActingAsClientTest extends PassportTestCase {