From 52a8296fd019304730d81a15a72bb4d6c5d8f08b Mon Sep 17 00:00:00 2001 From: systemsolutionweb <88460896+systemsolutionweb@users.noreply.github.com> Date: Mon, 24 Jul 2023 12:47:14 -0500 Subject: [PATCH] Set the client on the TokenGuard when mocking the client --- src/Guards/TokenGuard.php | 25 +++++++++++++++++++++ src/Passport.php | 8 ++++++- tests/Feature/AccessTokenControllerTest.php | 4 ++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Guards/TokenGuard.php b/src/Guards/TokenGuard.php index 921223edd..1b6da72a9 100644 --- a/src/Guards/TokenGuard.php +++ b/src/Guards/TokenGuard.php @@ -11,6 +11,7 @@ use Illuminate\Cookie\CookieValuePrefix; use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Http\Request; +use Laravel\Passport\Client; use Laravel\Passport\ClientRepository; use Laravel\Passport\Passport; use Laravel\Passport\PassportUserProvider; @@ -58,6 +59,13 @@ class TokenGuard */ protected $encrypter; + /** + * The currently authenticated client. + * + * @var \Laravel\Passport\Client|null + */ + protected $client; + /** * Create a new token guard instance. * @@ -122,6 +130,10 @@ public function user(Request $request) */ public function client(Request $request) { + if (! is_null($this->client)) { + return $this->client; + } + if ($request->bearerToken()) { if (! $psr = $this->getPsrRequestViaBearerToken($request)) { return; @@ -314,4 +326,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; + } } diff --git a/src/Passport.php b/src/Passport.php index fd5f7c8a8..e92af586f 100644 --- a/src/Passport.php +++ b/src/Passport.php @@ -414,9 +414,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 = null) { $token = app(self::tokenModel()); @@ -440,6 +441,11 @@ public static function actingAsClient($client, $scopes = []) app()->instance(TokenRepository::class, $mock); + if ($guard) { + app('auth')->guard($guard)->setClient($client); + app('auth')->shouldUse($guard); + } + return $client; } diff --git a/tests/Feature/AccessTokenControllerTest.php b/tests/Feature/AccessTokenControllerTest.php index 451107d30..cd9d669ba 100644 --- a/tests/Feature/AccessTokenControllerTest.php +++ b/tests/Feature/AccessTokenControllerTest.php @@ -75,7 +75,7 @@ public function testGettingAccessTokenWithClientCredentialsGrant() $this->assertArrayHasKey('expires_in', $decodedResponse); $this->assertArrayHasKey('access_token', $decodedResponse); $this->assertSame('Bearer', $decodedResponse['token_type']); - $expiresInSeconds = 31536000; + $expiresInSeconds = 31622400; $this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5); $jwtAccessToken = Configuration::forUnsecuredSigner()->parser()->parse($decodedResponse['access_token']); @@ -168,7 +168,7 @@ public function testGettingAccessTokenWithPasswordGrant() $this->assertArrayHasKey('access_token', $decodedResponse); $this->assertArrayHasKey('refresh_token', $decodedResponse); $this->assertSame('Bearer', $decodedResponse['token_type']); - $expiresInSeconds = 31536000; + $expiresInSeconds = 31622400; $this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5); $jwtAccessToken = Configuration::forUnsecuredSigner()->parser()->parse($decodedResponse['access_token']);