laravel / passport

Laravel Passport provides OAuth2 server support to Laravel.

Home Page:https://laravel.com/docs/passport

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`actingAsClient` method give error when guard provides `null`.

utsavsomaiya opened this issue · comments

Passport Version

11.8.7

Laravel Version

10.10.1

PHP Version

8.2.5

Database Driver & Version

MySQL mysql Ver 8.0.33-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

Description

When the scope and the guard are both null, the operation will inevitably fail.

test('it can fetch the products with variant', function (): void {
    $product = Product::factory()
        ->has(Variant::factory(5))
        ->create();

    Passport::actingAsClient(Client::factory()->create(), [], null);

    $response = $this->getJson('/api/product/'.$product->id);

    $response->assertStatus(Response::HTTP_OK);
});

If I execute the aforementioned test suit, it will pass successfully because I have included a specific code snippet in the vendor section. This code modification can be found at this GitHub link: https://github.com/laravel/passport/blob/11.x/src/Passport.php#L409.

- app('auth')->guard($guard)->setClient($client);
+ if ($guard) {
+  app('auth')->guard($guard)->setClient($client);
+ }

May be I am wrong. I don't know but it will get green and json has data.

Steps To Reproduce

// routes/api.php

Route::middleware('client')->group(function (): void {
    Route::get('product/{productId}', [ProductController::class, 'getProduct']);
});
  • I don't put HasApiToken in any model because I want a global level, And It has not any scopes.
  • In a fresh Laravel application just run the below command.
    • composer require laravel/passport
    • php artisan passport:install --uuid
    • php artisan passport:client --client Name: fake()->name()
    • Retrieving the token. https://laravel.com/docs/10.x/passport#retrieving-tokens
    • Below test gets green.
test('it can generate API token using laravel passport', function (): void {
    $client = Laravel\Passport\Client::factory()->create();

    $response = $this->postJson('/oauth/token', [
        'grant_type' => 'client_credentials',
        'client_id' => $client->id,
        'client_secret' => $client->secret,
    ]);

    $response->assertStatus(Response::HTTP_OK)
        ->assertJson(fn (AssertableJson $json) => $json->has('access_token')
            ->has('token_type')
            ->has('expires_in')
        );
});

Sorry, Maybe I am wrong.

test('it can fetch the products with variant', function (): void {
    $product = Product::factory()
        ->has(Variant::factory(5))
        ->create();

    Laravel\Passport\Passport::actingAsClient(Client::factory()->create(), [], null);

    $response = $this->getJson('/api/product/'.$product->id);

    $response->assertStatus(Response::HTTP_OK);
});

Here I attached the error screenshot.
image

Passing null as guard refers to the default guard, which is web most of the time. web guard is an instance of SessionGuard (uses session driver) that doesn't have setClient method as you see in the exception log.

You may pass api guard or any custom guard that is an instance of \Laravel\Passport\Guards\TokenGuard (uses passport driver).

Passport::actingAsClient(Client::factory()->create(), [], 'api');

// or simply
Passport::actingAsClient(Client::factory()->create());

Yes, you are right. I want to implement the functionality at a global level, applicable to all users. If I were to specify it explicitly, I would need to add a field called HasApiToken in the User Model. However, I need this functionality to be available globally, without any user-specific restrictions. What can I specify in the provider?

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
 
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

Hey there,

Unfortunately, I was not able to understand what you are trying to achieve here. However, seems to be also related to application development, so can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!

Hey @nunomaduro,

I just put it into the auth.php file it is working fine, Get green. But I don't know.

'api' => [
    'driver' => 'passport',
    'provider' => 'users',
],

Why user are in the provider!!?

Do you have any idea?