2020-01-18 17:50:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
namespace Shaarli\Front\Controller\Visitor;
|
2020-01-18 17:50:11 +01:00
|
|
|
|
2020-07-21 20:33:33 +02:00
|
|
|
use Shaarli\Front\Exception\CantLoginException;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Shaarli\Front\Exception\LoginBannedException;
|
2020-07-21 20:33:33 +02:00
|
|
|
use Shaarli\Front\Exception\WrongTokenException;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Render\TemplatePage;
|
2020-07-21 20:33:33 +02:00
|
|
|
use Shaarli\Security\CookieManager;
|
|
|
|
use Shaarli\Security\SessionManager;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class LoginController
|
|
|
|
*
|
|
|
|
* Slim controller used to render the login page.
|
|
|
|
*
|
|
|
|
* The login page is not available if the user is banned
|
|
|
|
* or if open shaarli setting is enabled.
|
|
|
|
*/
|
2020-05-22 13:20:31 +02:00
|
|
|
class LoginController extends ShaarliVisitorController
|
2020-01-18 17:50:11 +01:00
|
|
|
{
|
2020-07-21 20:33:33 +02:00
|
|
|
/**
|
|
|
|
* GET /login - Display the login page.
|
|
|
|
*/
|
2020-01-18 17:50:11 +01:00
|
|
|
public function index(Request $request, Response $response): Response
|
|
|
|
{
|
2020-07-21 20:33:33 +02:00
|
|
|
try {
|
|
|
|
$this->checkLoginState();
|
|
|
|
} catch (CantLoginException $e) {
|
2020-06-13 13:08:01 +02:00
|
|
|
return $this->redirect($response, '/');
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:33:33 +02:00
|
|
|
if ($request->getParam('login') !== null) {
|
|
|
|
$this->assignView('username', escape($request->getParam('login')));
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 20:33:33 +02:00
|
|
|
$returnUrl = $request->getParam('returnurl') ?? $this->container->environment['HTTP_REFERER'] ?? null;
|
2020-01-18 17:50:11 +01:00
|
|
|
|
|
|
|
$this
|
2020-07-21 20:33:33 +02:00
|
|
|
->assignView('returnurl', escape($returnUrl))
|
2020-01-26 09:06:13 +01:00
|
|
|
->assignView('remember_user_default', $this->container->conf->get('privacy.remember_user_default', true))
|
2020-09-22 20:25:47 +02:00
|
|
|
->assignView('pagetitle', t('Login') . ' - ' . $this->container->conf->get('general.title', 'Shaarli'))
|
2020-01-18 17:50:11 +01:00
|
|
|
;
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
return $response->write($this->render(TemplatePage::LOGIN));
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
2020-07-21 20:33:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /login - Process login
|
|
|
|
*/
|
|
|
|
public function login(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
if (!$this->container->sessionManager->checkToken($request->getParam('token'))) {
|
|
|
|
throw new WrongTokenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->checkLoginState();
|
|
|
|
} catch (CantLoginException $e) {
|
|
|
|
return $this->redirect($response, '/');
|
|
|
|
}
|
|
|
|
|
2020-09-22 20:25:47 +02:00
|
|
|
if (
|
|
|
|
!$this->container->loginManager->checkCredentials(
|
2020-07-21 20:33:33 +02:00
|
|
|
client_ip_id($this->container->environment),
|
|
|
|
$request->getParam('login'),
|
|
|
|
$request->getParam('password')
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$this->container->loginManager->handleFailedLogin($this->container->environment);
|
|
|
|
|
|
|
|
$this->container->sessionManager->setSessionParameter(
|
|
|
|
SessionManager::KEY_ERROR_MESSAGES,
|
|
|
|
[t('Wrong login/password.')]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Call controller directly instead of unnecessary redirection
|
|
|
|
return $this->index($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->container->loginManager->handleSuccessfulLogin($this->container->environment);
|
|
|
|
|
|
|
|
$cookiePath = $this->container->basePath . '/';
|
|
|
|
$expirationTime = $this->saveLongLastingSession($request, $cookiePath);
|
|
|
|
$this->renewUserSession($cookiePath, $expirationTime);
|
|
|
|
|
|
|
|
// Force referer from given return URL
|
|
|
|
$this->container->environment['HTTP_REFERER'] = $request->getParam('returnurl');
|
|
|
|
|
2020-07-27 12:34:17 +02:00
|
|
|
return $this->redirectFromReferer($request, $response, ['login', 'install']);
|
2020-07-21 20:33:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure that the user is allowed to login and/or displaying the login page:
|
|
|
|
* - not already logged in
|
|
|
|
* - not open shaarli
|
|
|
|
* - not banned
|
|
|
|
*/
|
|
|
|
protected function checkLoginState(): bool
|
|
|
|
{
|
2020-09-22 20:25:47 +02:00
|
|
|
if (
|
|
|
|
$this->container->loginManager->isLoggedIn()
|
2020-07-21 20:33:33 +02:00
|
|
|
|| $this->container->conf->get('security.open_shaarli', false)
|
|
|
|
) {
|
|
|
|
throw new CantLoginException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true !== $this->container->loginManager->canLogin($this->container->environment)) {
|
|
|
|
throw new LoginBannedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int Session duration in seconds
|
|
|
|
*/
|
|
|
|
protected function saveLongLastingSession(Request $request, string $cookiePath): int
|
|
|
|
{
|
|
|
|
if (empty($request->getParam('longlastingsession'))) {
|
|
|
|
// Standard session expiration (=when browser closes)
|
|
|
|
$expirationTime = 0;
|
|
|
|
} else {
|
|
|
|
// Keep the session cookie even after the browser closes
|
|
|
|
$this->container->sessionManager->setStaySignedIn(true);
|
|
|
|
$expirationTime = $this->container->sessionManager->extendSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->container->cookieManager->setCookieParameter(
|
|
|
|
CookieManager::STAY_SIGNED_IN,
|
|
|
|
$this->container->loginManager->getStaySignedInToken(),
|
|
|
|
$expirationTime,
|
|
|
|
$cookiePath
|
|
|
|
);
|
|
|
|
|
|
|
|
return $expirationTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renewUserSession(string $cookiePath, int $expirationTime): void
|
|
|
|
{
|
|
|
|
// Send cookie with the new expiration date to the browser
|
|
|
|
$this->container->sessionManager->destroy();
|
|
|
|
$this->container->sessionManager->cookieParameters(
|
|
|
|
$expirationTime,
|
|
|
|
$cookiePath,
|
|
|
|
$this->container->environment['SERVER_NAME']
|
|
|
|
);
|
|
|
|
$this->container->sessionManager->start();
|
|
|
|
$this->container->sessionManager->regenerateId(true);
|
|
|
|
}
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|