robsontenorio / laravel-keycloak-guard

🔑 Simple Keycloak Guard for Laravel
MIT License
419 stars 137 forks source link

Acting as during tests #96

Closed Ashk2a closed 1 year ago

Ashk2a commented 1 year ago

Hello,

I was using Laravel Passport before, the package proposes something like Passport::actingAs to simplify the generation of a token and authentication during tests.

I haven't found an equivalent, but I'd like to know if there's a simple way to get the same behavior ?

robsontenorio commented 1 year ago

@Ashk2a We don't have equivalent. I will accept a MR for this.

lotyp commented 1 year ago

Probably no code changes are needed, just use built in functions:

    public function setUp(): void
    {
        parent::setUp();

        $user = $this->createUser();
        $this->actingAs($user, 'api');
    }

https://laravel.com/docs/10.x/http-tests#session-and-authentication

Ashk2a commented 1 year ago

@robsontenorio I'll try to do something like thast @lotyp Yes you can do that (and set key env variable as empty) but you can't retrieve information

Ashk2a commented 1 year ago

I close the ticket, for the moment we can use actingAs

tuytoosh commented 1 year ago

I do not have a user table in reality because all information related to users is being handled in Keycloak. In order to pass the middleware, I have created a redundant user table to be able to use actingAs(), which seems pretty odd. I think it would be better to have a mechanism to pass authentication without requiring the creation of a user. Something like this: $this->actingWithKeycloakToken().

I will try to delve into the Laravel source code and this package as well to find out how to accomplish this.