Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for Laravel 9.x set the client on the TokenGuard when mocking the client #1680

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Guards/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
8 changes: 7 additions & 1 deletion src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down