2022-03-19 00:14:20 +01:00
|
|
|
<?php
|
|
|
|
|
2022-03-24 14:58:30 +01:00
|
|
|
// Largely inspired by Firefly III remote user implementation (https://github.com/firefly-iii)
|
2022-03-19 00:14:20 +01:00
|
|
|
// see https://github.com/firefly-iii/firefly-iii/blob/main/app/Support/Authentication/RemoteUserProvider.php
|
|
|
|
|
|
|
|
namespace App\Extensions;
|
|
|
|
|
|
|
|
use App\Models\User;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Exception;
|
2022-03-19 00:14:20 +01:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
|
|
use Illuminate\Contracts\Auth\UserProvider;
|
2022-03-24 14:58:30 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2022-03-19 00:14:20 +01:00
|
|
|
|
|
|
|
class RemoteUserProvider implements UserProvider
|
|
|
|
{
|
2022-09-07 17:58:34 +02:00
|
|
|
// 2FAuth is single user by design and domain data are not coupled to the user model.
|
|
|
|
// So the RemoteUserProvider provides a non-persisted user, dynamically instanciated using data
|
|
|
|
// from the auth proxy.
|
|
|
|
//
|
|
|
|
// This way no matter the user data set at proxy level, 2FAuth will always
|
|
|
|
// authenticate a request from the proxy and will return domain data without restriction.
|
|
|
|
//
|
|
|
|
// The downside of this approach is that we have to be sure that no change that needs
|
|
|
|
// to be persisted will be made to the user instance afterward (i.e through middlewares).
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently authenticated user.
|
|
|
|
*
|
|
|
|
* @var \App\Models\User|null
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the In-memory user
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2022-09-07 17:58:34 +02:00
|
|
|
* @return \App\Models\User
|
|
|
|
*/
|
|
|
|
protected function getInMemoryUser()
|
|
|
|
{
|
|
|
|
if (is_null($this->user)) {
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->user = new User;
|
|
|
|
$this->user->name = 'Remote User';
|
2022-09-07 17:58:34 +02:00
|
|
|
$this->user->email = 'fake.email@do.not.use';
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2022-09-07 17:58:34 +02:00
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:14:20 +01:00
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* {@inheritDoc}
|
2022-03-19 00:14:20 +01:00
|
|
|
*/
|
2022-03-24 14:58:30 +01:00
|
|
|
public function retrieveById($identifier)
|
2022-03-19 00:14:20 +01:00
|
|
|
{
|
2022-09-07 17:58:34 +02:00
|
|
|
$user = $this->getInMemoryUser();
|
|
|
|
|
|
|
|
if (Arr::has($identifier, 'user')) {
|
|
|
|
$user->name = $identifier['user'];
|
|
|
|
}
|
|
|
|
if (Arr::has($identifier, 'email')) {
|
|
|
|
$user->email = $identifier['email'];
|
|
|
|
}
|
2022-03-19 00:14:20 +01:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2022-03-31 08:38:35 +02:00
|
|
|
* @codeCoverageIgnore
|
2022-03-19 00:14:20 +01:00
|
|
|
*/
|
|
|
|
public function retrieveByToken($identifier, $token)
|
|
|
|
{
|
2022-09-07 17:58:34 +02:00
|
|
|
return $this->retrieveById($identifier);
|
2022-03-19 00:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2022-03-31 08:38:35 +02:00
|
|
|
* @codeCoverageIgnore
|
2022-03-19 00:14:20 +01:00
|
|
|
*/
|
|
|
|
public function updateRememberToken(Authenticatable $user, $token)
|
|
|
|
{
|
|
|
|
throw new Exception(sprintf('No implementation for %s', __METHOD__));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2022-03-31 08:38:35 +02:00
|
|
|
* @codeCoverageIgnore
|
2022-03-19 00:14:20 +01:00
|
|
|
*/
|
|
|
|
public function retrieveByCredentials(array $credentials)
|
|
|
|
{
|
2022-09-07 17:58:34 +02:00
|
|
|
return $this->getInMemoryUser();
|
2022-03-19 00:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2022-03-31 08:38:35 +02:00
|
|
|
* @codeCoverageIgnore
|
2022-03-19 00:14:20 +01:00
|
|
|
*/
|
|
|
|
public function validateCredentials(Authenticatable $user, array $credentials)
|
|
|
|
{
|
2022-09-07 17:58:34 +02:00
|
|
|
return true;
|
2022-03-19 00:14:20 +01:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|