2020-02-01 06:42:22 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Auth\Access\Guards;
|
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
use BookStack\Auth\Access\RegistrationService;
|
2020-02-01 06:42:22 -05:00
|
|
|
use Illuminate\Auth\GuardHelpers;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\StatefulGuard;
|
|
|
|
use Illuminate\Contracts\Auth\UserProvider;
|
|
|
|
use Illuminate\Contracts\Session\Session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BaseSessionGuard
|
|
|
|
* A base implementation of a session guard. Is a copy of the default Laravel
|
|
|
|
* guard with 'remember' functionality removed. Basic auth and event emission
|
|
|
|
* has also been removed to keep this simple. Designed to be extended by external
|
|
|
|
* Auth Guards.
|
|
|
|
*/
|
|
|
|
class ExternalBaseSessionGuard implements StatefulGuard
|
|
|
|
{
|
|
|
|
use GuardHelpers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the Guard. Typically "session".
|
|
|
|
*
|
|
|
|
* Corresponds to guard name in authentication configuration.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user we last attempted to retrieve.
|
|
|
|
*
|
|
|
|
* @var \Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
|
|
|
protected $lastAttempted;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The session used by the guard.
|
|
|
|
*
|
|
|
|
* @var \Illuminate\Contracts\Session\Session
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if the logout method has been called.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $loggedOut = false;
|
|
|
|
|
2020-02-02 05:59:03 -05:00
|
|
|
/**
|
2020-02-02 12:31:00 -05:00
|
|
|
* Service to handle common registration actions.
|
2020-02-02 05:59:03 -05:00
|
|
|
*
|
2020-02-02 12:31:00 -05:00
|
|
|
* @var RegistrationService
|
2020-02-02 05:59:03 -05:00
|
|
|
*/
|
2020-02-02 12:31:00 -05:00
|
|
|
protected $registrationService;
|
2020-02-02 05:59:03 -05:00
|
|
|
|
2020-02-01 06:42:22 -05:00
|
|
|
/**
|
|
|
|
* Create a new authentication guard.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-02-02 12:31:00 -05:00
|
|
|
public function __construct(string $name, UserProvider $provider, Session $session, RegistrationService $registrationService)
|
2020-02-01 06:42:22 -05:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->provider = $provider;
|
2020-02-02 12:31:00 -05:00
|
|
|
$this->registrationService = $registrationService;
|
2020-02-01 06:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the currently authenticated user.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
if ($this->loggedOut) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already retrieved the user for the current request we can just
|
|
|
|
// return it back immediately. We do not want to fetch the user data on
|
|
|
|
// every call to this method because that would be tremendously slow.
|
2021-06-26 11:23:15 -04:00
|
|
|
if (!is_null($this->user)) {
|
2020-02-01 06:42:22 -05:00
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->session->get($this->getName());
|
|
|
|
|
|
|
|
// First we will try to load the user using the
|
|
|
|
// identifier in the session if one exists.
|
2021-06-26 11:23:15 -04:00
|
|
|
if (!is_null($id)) {
|
2020-02-01 06:42:22 -05:00
|
|
|
$this->user = $this->provider->retrieveById($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID for the currently authenticated user.
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function id()
|
|
|
|
{
|
|
|
|
if ($this->loggedOut) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user()
|
|
|
|
? $this->user()->getAuthIdentifier()
|
|
|
|
: $this->session->get($this->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a user into the application without sessions or cookies.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param array $credentials
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function once(array $credentials = [])
|
|
|
|
{
|
|
|
|
if ($this->validate($credentials)) {
|
|
|
|
$this->setUser($this->lastAttempted);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the given user ID into the application without sessions or cookies.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param mixed $id
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|false
|
|
|
|
*/
|
|
|
|
public function onceUsingId($id)
|
|
|
|
{
|
2021-06-26 11:23:15 -04:00
|
|
|
if (!is_null($user = $this->provider->retrieveById($id))) {
|
2020-02-01 06:42:22 -05:00
|
|
|
$this->setUser($user);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a user's credentials.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param array $credentials
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate(array $credentials = [])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to authenticate a user using the given credentials.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param array $credentials
|
|
|
|
* @param bool $remember
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function attempt(array $credentials = [], $remember = false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the given user ID into the application.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param mixed $id
|
|
|
|
* @param bool $remember
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|false
|
|
|
|
*/
|
|
|
|
public function loginUsingId($id, $remember = false)
|
|
|
|
{
|
2021-07-17 12:45:00 -04:00
|
|
|
// Always return false as to disable this method,
|
|
|
|
// Logins should route through LoginService.
|
2020-02-01 06:42:22 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log a user into the application.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
* @param bool $remember
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function login(AuthenticatableContract $user, $remember = false)
|
|
|
|
{
|
|
|
|
$this->updateSession($user->getAuthIdentifier());
|
|
|
|
|
|
|
|
$this->setUser($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the session with the given ID.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param string $id
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function updateSession($id)
|
|
|
|
{
|
|
|
|
$this->session->put($this->getName(), $id);
|
|
|
|
|
|
|
|
$this->session->migrate(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the user out of the application.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
$this->clearUserDataFromStorage();
|
|
|
|
|
|
|
|
// Now we will clear the users out of memory so they are no longer available
|
|
|
|
// as the user is no longer considered as being signed into this
|
|
|
|
// application and should not be available here.
|
|
|
|
$this->user = null;
|
|
|
|
|
|
|
|
$this->loggedOut = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the user data from the session and cookies.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function clearUserDataFromStorage()
|
|
|
|
{
|
|
|
|
$this->session->remove($this->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last user we attempted to authenticate.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
|
|
|
public function getLastAttempted()
|
|
|
|
{
|
|
|
|
return $this->lastAttempted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a unique identifier for the auth session value.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
2021-06-26 11:23:15 -04:00
|
|
|
return 'login_' . $this->name . '_' . sha1(static::class);
|
2020-02-01 06:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the user was authenticated via "remember me" cookie.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function viaRemember()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the currently cached user.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
|
*/
|
|
|
|
public function getUser()
|
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current user.
|
|
|
|
*
|
2021-06-26 11:23:15 -04:00
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
|
*
|
2020-02-01 06:42:22 -05:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUser(AuthenticatableContract $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
|
|
|
|
$this->loggedOut = false;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|