/
StartSession.php
95 lines (80 loc) · 2.57 KB
/
StartSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* This file is part of Flarum.
*
* For detailed 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 Flarum\Http\CookieFactory;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Session\Session;
use Illuminate\Session\Store;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use SessionHandlerInterface;
class StartSession implements Middleware
{
/**
* @var SessionHandlerInterface
*/
protected $handler;
/**
* @var CookieFactory
*/
protected $cookie;
/**
* @var array
*/
protected $config;
/**
* @param SessionHandlerInterface $handler
* @param CookieFactory $cookie
* @param ConfigRepository $config
*/
public function __construct(SessionHandlerInterface $handler, CookieFactory $cookie, ConfigRepository $config)
{
$this->handler = $handler;
$this->cookie = $cookie;
$this->config = $config->get('session');
}
public function process(Request $request, Handler $handler): Response
{
$request = $request->withAttribute(
'session',
$session = $this->makeSession($request)
);
$session->start();
$response = $handler->handle($request);
$session->save();
$response = $this->withCsrfTokenHeader($response, $session);
return $this->withSessionCookie($response, $session);
}
private function makeSession(Request $request): Session
{
return new Store(
$this->config['cookie'],
$this->handler,
Arr::get($request->getCookieParams(), $this->cookie->getName($this->config['cookie']))
);
}
private function withCsrfTokenHeader(Response $response, Session $session): Response
{
return $response->withHeader('X-CSRF-Token', $session->token());
}
private function withSessionCookie(Response $response, Session $session): Response
{
return FigResponseCookies::set(
$response,
$this->cookie->make($session->getName(), $session->getId(), $this->getSessionLifetimeInSeconds())
);
}
private function getSessionLifetimeInSeconds(): int
{
return $this->config['lifetime'] * 60;
}
}