I'm not so good at PHP and Laravel , and I have the following problem: I came from Java .
I follow this guide to implement a custom user provider :
https://blog.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers/
I am using Larave version 5.3 .
I will briefly talk about what I need: my Laravel application is for an external application only . All business logic, including user authentication, runs > Java back end application , which provides REST web services .
Making a call:
http:
and passing the username and password as basic authentication, I get a JSON response that represents the registered user:
{
"userName": "Painkiller",
"email": "painkiller@gmail.com",
"enabled": true
}
So, in my Laravel application, I have to make this call and then parse the previous returned JSON object to generate an authenticated object in the foreground application session.
(, , ), UserProvider, Laravel IlluminateUserProvider:
<?php
namespace App\Authentication;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
use GuzzleHttp\Client;
use function GuzzleHttp\json_encode;
use function GuzzleHttp\json_decode;
use Illuminate\Support\Facades\Log;
class UserProvider implements IlluminateUserProvider
{
public function retrieveById($identifier)
{
\Log::info('retrieveById START');
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
return $user;
}
public function retrieveByToken($identifier, $token)
{
\Log::info('retrieveByToken START');
}
public function updateRememberToken(Authenticatable $user, $token)
{
\Log::info('updateRememberToken START');
}
public function retrieveByCredentials(array $credentials) {
\Log::info('retrieveByCredentials START');
\Log::info('INSERTED USER CREDENTIAL: '.$credentials['email'] . ' ' .$credentials['password']);
$client = new Client();
$response = $client->get('http://localhost:8080/Extranet/login',
[
'auth' => [
'nobili.andrea@gmail.com',
'pswd'
]
]);
$dettagliLogin = json_decode($response->getBody());
\Log::info('response: '.(json_encode($dettagliLogin)));
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
\Log::info('USER: '.(json_encode($user)));
return $user;
}
public function validateCredentials(Authenticatable $user, array $credentials)
{
\Log::info('validateCredentials START');
return true;
}
}
, .
:
1) (http://localhost:8000/login), retrieveByCredentials():
public function retrieveByCredentials(array $credentials) {
\Log::info('retrieveByCredentials START');
\Log::info('INSERTED USER CREDENTIAL: '.$credentials['email'] . ' ' .$credentials['password']);
$client = new Client();
$response = $client->get('http://localhost:8080/Extranet/login',
[
'auth' => [
'nobili.andrea@gmail.com',
'pswd'
]
]);
$dettagliLogin = json_decode($response->getBody());
\Log::info('response: '.(json_encode($dettagliLogin)));
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
\Log::info('USER: '.(json_encode($user)));
return $user;
}
- , . validateCredentials() ( true ). Finnaly GenericUser, ( , , JSON, -.
, ( ), , retrieveById ($ identifier) , :
public function retrieveById($identifier)
{
\Log::info('retrieveById START');
$attributes = array(
'id' => 123,
'username' => 'nobili.andrea@gmail.com',
'password' => \Hash::make('SuperSecret'),
'name' => 'Dummy User',
);
$user = new GenericUser($attributes);
return $user;
}
, - , GenericUser , , . .
, , .
, : retrieveByCredentials ( $credentials), , GenericUser .
retrieveById() .
- ? , ? \ ( PHP )