mirror of
https://github.com/flarum/core.git
synced 2025-10-15 08:55:53 +02:00
Overhaul sessions, tokens, and authentication
- Use cookies + CSRF token for API authentication in the default client. This mitigates potential XSS attacks by making the token unavailable to JavaScript. The Authorization header is still supported, but not used by default. - Make sensitive/destructive actions (editing a user, permanently deleting anything, visiting the admin CP) require the user to re-enter their password if they haven't entered it in the last 30 minutes. - Refactor and clean up the authentication middleware. - Add an `onhide` hook to the Modal component. (+1 squashed commit)
This commit is contained in:
81
src/Http/Middleware/StartSession.php
Normal file
81
src/Http/Middleware/StartSession.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Dflydev\FigCookies\FigResponseCookies;
|
||||
use Dflydev\FigCookies\SetCookie;
|
||||
use Dflydev\FigCookies\SetCookies;
|
||||
use Flarum\Http\Session;
|
||||
use Flarum\Core\Guest;
|
||||
use Flarum\Http\WriteSessionCookieTrait;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class StartSession implements MiddlewareInterface
|
||||
{
|
||||
use WriteSessionCookieTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
{
|
||||
$this->collectGarbage();
|
||||
|
||||
$session = $this->getSession($request);
|
||||
$actor = $this->getActor($session);
|
||||
|
||||
$request = $request
|
||||
->withAttribute('session', $session)
|
||||
->withAttribute('actor', $actor);
|
||||
|
||||
$response = $out ? $out($request, $response) : $response;
|
||||
|
||||
return $this->addSessionCookieToResponse($response, $session, 'flarum_session');
|
||||
}
|
||||
|
||||
private function getSession(Request $request)
|
||||
{
|
||||
$session = $request->getAttribute('session');
|
||||
|
||||
if (! $session) {
|
||||
$session = Session::generate();
|
||||
}
|
||||
|
||||
$session->extend()->save();
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
private function getActor(Session $session)
|
||||
{
|
||||
$actor = $session->user ?: new Guest;
|
||||
|
||||
if ($actor->exists) {
|
||||
$actor->updateLastSeen()->save();
|
||||
}
|
||||
|
||||
return $actor;
|
||||
}
|
||||
|
||||
private function collectGarbage()
|
||||
{
|
||||
if ($this->hitsLottery()) {
|
||||
Session::whereRaw('last_activity <= ? - duration * 60', [time()])->delete();
|
||||
}
|
||||
}
|
||||
|
||||
private function hitsLottery()
|
||||
{
|
||||
return mt_rand(1, 100) <= 1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user