-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSessionListener.php
More file actions
130 lines (111 loc) · 3.47 KB
/
Copy pathSessionListener.php
File metadata and controls
130 lines (111 loc) · 3.47 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Bolt\Session;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* This connects the client to the session using cookies through the HttpKernel.
*
* @author Carson Full <carsonfull@gmail.com>
*/
class SessionListener implements EventSubscriberInterface
{
/** @var SessionInterface */
protected $session;
/** @var OptionsBag */
protected $options;
/**
* Constructor.
*
* @param SessionInterface $session
* @param OptionsBag $options
*/
public function __construct(SessionInterface $session, OptionsBag $options)
{
$this->session = $session;
$this->options = $options;
}
/**
* Set the session ID from request cookies.
*
* @param GetResponseEvent $event
*/
public function onRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$request->setSession($this->session);
$this->prependBasePathToCookie($request);
$this->appendRealmToName($request);
$name = $this->session->getName();
if ($request->cookies->has($name)) {
$this->session->setId($request->cookies->get($name));
$this->session->start();
}
}
/**
* Add the session cookie to the response if it is started.
*
* @param FilterResponseEvent $event
*/
public function onResponse(FilterResponseEvent $event)
{
if (!$event->isMasterRequest() || !$this->session->isStarted()) {
return;
}
$this->session->save();
$cookie = $this->generateCookie();
$event->getResponse()->headers->setCookie($cookie);
}
protected function prependBasePathToCookie(Request $request)
{
if (!$path = $this->options['cookie_path']) {
return;
}
$this->options['cookie_path'] = $request->getBasePath() . $path;
}
protected function appendRealmToName(Request $request)
{
if (!$this->options->getBoolean('restrict_realm')) {
return;
}
$name = $this->session->getName();
$realm = '_' . md5($request->getHttpHost() . $request->getBasePath());
if (substr($name, -strlen($realm)) === $realm) { // name ends with realm
return;
}
$this->session->setName($name . $realm);
}
protected function generateCookie()
{
$lifetime = $this->options->getInt('cookie_lifetime');
if ($lifetime !== 0) {
$lifetime += time();
}
return new Cookie(
$this->session->getName(),
$this->session->getId(),
$lifetime,
$this->options['cookie_path'],
$this->options['cookie_domain'] ?: null,
$this->options->getBoolean('cookie_secure'),
$this->options->getBoolean('cookie_httponly')
);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['onRequest', 128],
KernelEvents::RESPONSE => ['onResponse', -128],
];
}
}