mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-09 05:54:34 +02:00
Use GuardHelpers trait & Enhance RemoteUserProvider
This commit is contained in:
@ -13,23 +13,55 @@ use Exception;
|
||||
|
||||
class RemoteUserProvider implements UserProvider
|
||||
{
|
||||
// 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
|
||||
*
|
||||
* @return \App\Models\User
|
||||
*/
|
||||
protected function getInMemoryUser()
|
||||
{
|
||||
if (is_null($this->user)) {
|
||||
$this->user = new User;
|
||||
$this->user->name = 'Remote User';
|
||||
$this->user->email = 'fake.email@do.not.use';
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function retrieveById($identifier)
|
||||
{
|
||||
// 2FAuth is single user by design and domain data are not coupled to the user model.
|
||||
// So we provide a non-persisted user, dynamically instanciated using data
|
||||
// from the auth proxy.
|
||||
// This way no matter the user account used 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).
|
||||
$user = $this->getInMemoryUser();
|
||||
|
||||
$user = new User;
|
||||
$user->name = $identifier['user'];
|
||||
$user->email = Arr::has($identifier, 'email') ? $identifier['email'] : 'fake.email@do.not.use';
|
||||
if (Arr::has($identifier, 'user')) {
|
||||
$user->name = $identifier['user'];
|
||||
}
|
||||
if (Arr::has($identifier, 'email')) {
|
||||
$user->email = $identifier['email'];
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
@ -41,7 +73,7 @@ class RemoteUserProvider implements UserProvider
|
||||
*/
|
||||
public function retrieveByToken($identifier, $token)
|
||||
{
|
||||
throw new Exception(sprintf('No implementation for %s', __METHOD__));
|
||||
return $this->retrieveById($identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,7 +93,7 @@ class RemoteUserProvider implements UserProvider
|
||||
*/
|
||||
public function retrieveByCredentials(array $credentials)
|
||||
{
|
||||
throw new Exception(sprintf('No implementation for %s', __METHOD__));
|
||||
return $this->getInMemoryUser();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,6 +103,6 @@ class RemoteUserProvider implements UserProvider
|
||||
*/
|
||||
public function validateCredentials(Authenticatable $user, array $credentials)
|
||||
{
|
||||
throw new Exception(sprintf('No implementation for %s', __METHOD__));
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user