lundi 8 mars 2021

How to test the Password Grant Tokens flow in Laravel Passport?

I'm very lost with this, I want to make a test to test the Password Grant Tokens flow of Laravel Passport, but I don't know how.

I can't use the credentials generated by the php artisan client --password because they are saved in the original database, not in the testing database.

I tried to make a call to the JSON API provided by Passport and it returned me the credentials, but when I want to use them in a token generation call, it returns me "Invalid Credentials", this is my test code:

public function test_request_token() {

    # Creating the user
    $user = User::factory()->create();
    Passport::actingAs($user);

    # Creating credentials
    $credentials = $this->post("oauth/clients", [
        "name" => "Testa app",
        "redirect" => "http://127.0.0.1/callback"
    ]);

    # Until here all works fine, it returns me the credentials (I made a dd to check it), but now I want to ask for a token:

    $response = $this->post("oauth/token", [
        "grant_type" => "password",
        "client_id" => $credentials["id"],
        "client_secret" => $credentials["plainSecret"],
        "username" => $user->email,
        "password" => Hash::make("password"), # Factory always have this password by default
        "scope" => ""
    ]);

    dd($response); # It returns me the error "Invalid clients"
    
}

So, I don't know what else do to make this test, I don't know where I have to get the credentials to generate this token...

When I test it via CURL's it works fine with the credentials generated by Laravel for the install command.

Can you help me, please?

Aucun commentaire:

Enregistrer un commentaire