Laravel passport 500 internal server error when using subdomain - laravel

Using laravel passport on my local machine and domain http://myapp.test, I have no problem.
When I push the code to my server on my main domain https://myapp.com, again no problem.
However, I have a sudomain used for my live test (pre-production) before to push to the main domain in production. If I use https://dev.myapp.com, then I get a 500 internal server error.
Any idea how to fix it?
This is the guzzle call I'm doing:
$http = new GuzzleHttp\Client;
$response = $http->post('https://dev.myapp.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client_id',
'client_secret' => 'client_secret_key',
'username' => 'john#example.com',
'password' => '123456',
'scope' => '',
],
]);
return $response;
If I change the url to my local or production website (changing the passport key and making sure the same user exists), it works properly.

My bad. Just forgot to run that command on the server for that specific domain as specified in the documentation. This command generates the encryption keys Passport needs in order to generate access token.
php artisan passport:keys

Related

How to login with laravel passport in seeder file?

On laravel backend API site with passport I use service for data crud operations
and inside of this service I use Auth::user() to fill creator of data.
Testing this service in POSTMAN I use oauth/token with client_id, client-secret, username, password
parameters and it works ok.
I need to add some init data in seeders using the same service
Looking how oauth/token at docs https://laravel.com/docs/9.x/passport
I added to my seeder:
use Illuminate\Support\Facades\Http;
...
$response = Http::asForm()->post(url('oauth/token'), [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'username',
'password' => 'password',
'scope' => '',
]);
\Log::info($response->json()); // I can see valid response
if(Auth::user()) { // that condition did not true
\Log::info(Auth::user());
}
Which action have I to take to login with valid token?
When I refer Auth::user()->id in service I got error :
Attempt to read property "id" on null
I tried to debug by routes outpout:
.token › Laravel\Passport › AccessTokenController#issueToken
POST oauth/token/refresh ........................................................................................................................ passport.token
file src/Http/Controllers/AccessTokenController.php
but I see only token functionality...
"laravel/framework": "^9.2",
"laravel/passport": "^10.3",
Thanks in advance!
You can use Auth::loginUsingId() for user information that you want get

How I make phpunit auth tests for laravel/passport?

I make backend API with laravel 9 (based on laravel/passport 10.3) and
I init app with command :
php artisan passport:install
which generates personal access client and client secret code
I use these codes in login request and recieve Bearer Token, which is used in all datas raeding/updating data with request.
Now I make phpunit tests for these requests and wonder in which wat these register/login requests must be tested as
usuallyin tests controllers I use
$this->actingAs($user, 'api');
Where $user is created with factory...
How that can be implemented ? Please link to example...
Thanks!
Well, you can use :
Passport::actingAs($user)
and usually we don't test third party code, so don't bother testing passport authentication routes, it's already well tested
but if you really want to do that, you can for example with $this->postJson
$this->postJson('api/oauth/token', [
'username' => 'username',
'password' => Str::random(),
'client_id' => 'client_id',
'client_secret' => 'secret',
'grant_type' => 'password',
'scope' => '*',
])->assertStatus(400)->assertJson([
'error' => 'invalid_grant',
]);

I got Unable to resolve PSR request error with GuzzleHttp request

Implementing in my Laravel 7.6 app(pt hosts at http://local-votes.com of my local OS) api for
external api I want to make a test request from my other
Laravel 6.17.1 app and reading here : https://laravel.com/docs/7.x/passport
Converting Authorization Codes To Access Tokens
I try to use this example:
$http = new \GuzzleHttp\Client;
$client_id= 3; // client ID I made in my host app
$client_secret= 'XXX'; // client secret I made in my host app
$host= 'http://local-votes.com';
$response = $http->post($host.'/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $host.'/votes',
'code' => 'code',
// 'code' => $request->code,
],
]);
But I got error :
Illuminate\Contracts\Container\BindingResolutionException: Unable to resolve PSR request. Please i (truncated...)
{"userId":5,"exception":"[object] (GuzzleHttp\\Exception\\ServerException(code: 500): Server error: `POST http://local-votes.com/oauth/token` resulted in a `500 Internal Server Error` response:
Also I am not sure which values have I provide for fields 'client_secret' and 'code' ?
Can it be the issue ?
How to fix this error ?
Thanks!
Installing package nyholm/psr7
on provider app salved the problem!

401 Unauthorized when creating Password Grant Tokens with laravel passport

I have created an api with passport and want to use password grant tokens because it is more of application to application interactions. But i get below error when i try to access it.
GuzzleHttp\Exception\ClientException
Client error: `POST http://127.0.0.1:8000/oauth/token` resulted in a `401 Unauthorized` response: {"error":"invalid_client","error_description":"Client authentication failed","message":"Client authentication failed"}
my route
Route::get('/get_token_by_password', function (Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://192.168.0.103:8000/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 13,
'client_secret' => 'f37AwQGsVMiZDVu786KiRdbpn4MXYSBWCvqNcqiC',
'username' => 'developer#ymail.com',
'password' => '123456789',
'scope' => '*',
],
]);
return json_decode((string) $response->getBody(), true);
})->name('get_token_by_password');
What might I be doing wrong?
Also, how do I use postman to get the token?
How do i get a link to be used in other applications that want to consume my api
The request seems correct. Check if you have correct keys and client. After, check in DB the new value of client_secret.
php artisan passport:keys
php artisan passport:client --password
Your client id and/or client secret is not correct, since the error is "invalid_client".

Guzzle and docker

I am using laravel as my framework with passport and guzzle installed. I am trying to get a password grant token from the server for my iPhone app that I am developing. This is how I am calling the api for the grant token
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor#test',
'password' => 'my-password',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
When I call this with all the correct data I get this error from the console.
GuzzleHttp\Exception\ConnectException: cURL error 7:
Failed to connect to studious.test port 80: Connection refused (see
http://curl.haxx.se/libcurl/c/libcurl-errors.html) in file /var/www/your-app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 185
I am also using Laradock for development and I have both your-app.com and www.your-app.com in my hosts file. From my research it seems that this could be a error due to some dns stuff. Not sure what in the world is going on with this.
In my host file I tried both putting my url with and with out the www.
You need to replace the request url with http://{nameofservice}/oauth/token, for example http://nginx/oauth/token, then it should work

Resources