Skip to content

Commit de00e47

Browse files
authored
Allow setting the client with Passport::actingAs() (#1876)
1 parent 8f3dfb3 commit de00e47

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Passport.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,14 @@ public static function ignoreCsrfToken(bool $ignoreCsrfToken = true): void
351351
* @param string[] $scopes
352352
* @return \Laravel\Passport\Contracts\OAuthenticatable
353353
*/
354-
public static function actingAs(Authenticatable $user, array $scopes = [], ?string $guard = 'api'): Authenticatable
355-
{
354+
public static function actingAs(
355+
Authenticatable $user,
356+
array $scopes = [],
357+
?string $guard = 'api',
358+
?Client $client = null,
359+
): Authenticatable {
356360
$token = new AccessToken([
361+
'oauth_client_id' => $client?->getKey(),
357362
'oauth_user_id' => $user->getAuthIdentifier(),
358363
'oauth_scopes' => $scopes,
359364
]);

tests/Feature/ActingAsTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Laravel\Passport\Tests\Feature;
44

55
use Illuminate\Contracts\Routing\Registrar;
6+
use Illuminate\Http\Request;
67
use Illuminate\Support\Facades\Route;
8+
use Laravel\Passport\Client;
79
use Laravel\Passport\Http\Middleware\CheckToken;
810
use Laravel\Passport\Http\Middleware\CheckTokenForAnyScope;
911
use Laravel\Passport\Passport;
@@ -18,11 +20,18 @@ public function testActingAsWhenTheRouteIsProtectedByAuthMiddleware()
1820
/** @var Registrar $router */
1921
$router = $this->app->make(Registrar::class);
2022

21-
$router->get('/foo', function () {
23+
$router->get('/foo', function (Request $request) {
24+
$this->assertSame('client-1234', $request->user()->token()->oauth_client_id);
25+
$this->assertSame(1234, $request->user()->token()->oauth_user_id);
26+
$this->assertSame(['admin'], $request->user()->token()->oauth_scopes);
27+
2228
return 'bar';
2329
})->middleware('auth:api');
2430

25-
Passport::actingAs(new User());
31+
$client = new Client(['id' => 'client-1234']);
32+
$user = (new User())->forceFill(['id' => 1234]);
33+
34+
Passport::actingAs($user, ['admin'], client: $client);
2635

2736
$response = $this->get('/foo');
2837
$response->assertSuccessful();

0 commit comments

Comments
 (0)