I’m trying to sign into my laravel app which uses passport and added guzzle
to post to my oath/token
route so the user can sign in without me having to expose my secret_key
However I’m running into an error which I can’t seem to solve
Problem
I’ve updated my laravel app with passport and tested my backend end locally which worked fine. After I deployed my laravel app to my live servers test environment I started to get errors while trying to sign in.
At first guzzle couldn’t verify the SSL of my current live environment. So I turned verify to false. Now I’ve started to get a different which states my url oath/token
route is wrong. Which is wrong because directly posting to my `oath/token’ seems to work with postmen.
I’ve also checked if the route exists with php artisan route:list
summarise: guzzle is stating the url I’m posting to doesn’t exist, but it does
My get token function
public function getToken(& $username, $password)
{
$http = new GuzzleHttpClient;
try {
$response = $http->post(config('services.passport.login_endpoint'), [
//'verify' => false,
'verify' => ( env( 'APP_ENV' ) === 'local' ) ? false : true,
'form_params' => [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'scope' => '',
'username' => $username,
'password' => $password,
]
]);
return $response->getBody();
} catch (GuzzleHttpExceptionBadResponseException $e) {
if ($e->getCode() === 400) {
return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
} else if ($e->getCode() === 401) {
return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
}
return $e;
}
}
Response
GuzzleHttpExceptionClientException: Client error: `POST https: //my.domain.com/oauth/token` resulted in a `404 Not Found` response:
Expectation
Whenever Guzzle
posts something to my existing route it returns response from my laravel app.
ADDED DEBUG
* Trying {my ip adres}...
* TCP_NODELAY set
* Connected to {mydomain} {my ip adres} port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=CH; L=Schaffhausen; O=Plesk; CN=Plesk; [email protected]
* start date: Feb 9 14: 54: 36 2020 GMT
* expire date: Feb 8 14: 54: 36 2021 GMT
* issuer: C=CH; L=Schaffhausen; O=Plesk; CN=Plesk; [email protected]
* SSL certificate verify result: self signed certificate (18), continuing anyway.
> POST /oauth/token HTTP/1.1
Host: {mydomain}
User-Agent: GuzzleHttp/6.3.3 curl/7.58.0 PHP/7.1.33
Content-Type: application/x-www-form-urlencoded
Content-Length: 144
* upload completely sent off: 144 out of 144 bytes
< HTTP/1.1 404 Not Found
< Server: nginx
< Date: Wed,
01 Apr 2020 17: 31: 26 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 266
< Connection: keep-alive
<
* Connection #0 to host {mydomain} left intact
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache Server at my.domain.com Port 443</address>
</body></html>
Routes
| | POST | oauth/token | passport.token | LaravelPassportHttpControllersAccessTokenController@issueToken | throttle |
| | GET|HEAD | oauth/tokens | passport.tokens.index | LaravelPassportHttpControllersAuthorizedAccessTokenController@forUser | web,auth |
| | DELETE | oauth/tokens/{token_id} | passport.tokens.destroy | LaravelPassportHttpControllersAuthorizedAccessTokenController@destroy | web,auth |
2
Answers
I've found the solution in this post when I run
php artisan serve
I can call'base_uri' => "127.0.0.1:8000"
. So I added it to supervisor to runphp artisan serve
and I can now make a call to my own server.Apparently I can't make a call to my own server over static PHP.
Have you tried to prepend your configured login.endpoint with the base url, so something like: