From 66add573c238307392a7e481a9151ae210193476 Mon Sep 17 00:00:00 2001 From: GuntherDebrauwer <22586858+GuntherDebrauwer@users.noreply.github.com> Date: Mon, 9 Sep 2019 18:22:36 +0200 Subject: [PATCH] Add 'actingAsClient' static method to 'Passport' class --- src/Passport.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Passport.php b/src/Passport.php index dfed50587..fed9d60a7 100644 --- a/src/Passport.php +++ b/src/Passport.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use DateTimeInterface; use Illuminate\Support\Facades\Route; +use League\OAuth2\Server\ResourceServer; class Passport { @@ -405,6 +406,29 @@ public static function actingAs($user, $scopes = [], $guard = 'api') return $user; } + /** + * Set the current client for the application with the given scopes. + * + * @param  \Laravel\Passport\Client  $client + * @param  array  $scopes + * @return \Laravel\Passport\Client + */ + public static function actingAsClient($client, $scopes = []) + { + $mock = Mockery::mock(ResourceServer::class); + + $mock->shouldReceive('validateAuthenticatedRequest') + ->andReturnUsing(function ($request) use ($client, $scopes) { + return $request + ->withAttribute('oauth_client_id', $client->id) + ->withAttribute('oauth_scopes', $scopes); + }); + + app()->instance(ResourceServer::class, $mock); + + return $client; + } + /** * Set the storage location of the encryption keys. *