forked from nette/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionStorage.php
More file actions
158 lines (122 loc) · 3.83 KB
/
Copy pathSessionStorage.php
File metadata and controls
158 lines (122 loc) · 3.83 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\SecurityHttp;
use Nette;
use Nette\Http\Session;
use Nette\Http\SessionSection;
use Nette\Security\IIdentity;
/**
* Session storage for Nette\Security\User object.
*/
final class SessionStorage implements Nette\Security\UserStorage
{
use Nette\SmartObject;
private string $namespace = '';
private Session $sessionHandler;
private ?SessionSection $sessionSection = null;
private ?int $expireTime = null;
private bool $expireIdentity = false;
public function __construct(Session $sessionHandler)
{
$this->sessionHandler = $sessionHandler;
}
public function saveAuthentication(IIdentity $identity): void
{
$section = $this->getSessionSection();
$section->set('authenticated', true);
$section->set('reason', null);
$section->set('authTime', time()); // informative value
$section->set('identity', $identity);
$this->setupExpiration();
// Session Fixation defence
$this->sessionHandler->regenerateId();
}
public function clearAuthentication(bool $clearIdentity): void
{
$section = $this->getSessionSection();
$section->set('authenticated', false);
$section->set('reason', self::LogoutManual);
$section->set('authTime', null);
if ($clearIdentity === true) {
$section->set('identity', null);
}
// Session Fixation defence
$this->sessionHandler->regenerateId();
}
public function getState(): array
{
$section = $this->getSessionSection();
return $section
? [(bool) $section->get('authenticated'), $section->get('identity'), $section->get('reason')]
: [false, null, null];
}
public function setExpiration(?string $time, bool $clearIdentity = false): void
{
$this->expireTime = $time ? (int) Nette\Utils\DateTime::from($time)->format('U') : null;
$this->expireIdentity = $clearIdentity;
if ($this->sessionSection && $this->sessionSection->get('authenticated')) {
$this->setupExpiration();
}
}
private function setupExpiration(): void
{
$section = $this->sessionSection;
if ($this->expireTime) {
$section->set('expireTime', $this->expireTime);
$section->set('expireDelta', $this->expireTime - time());
} else {
$section->remove(['expireTime', 'expireDelta']);
}
$section->set('expireIdentity', $this->expireIdentity);
$section->setExpiration((string) $this->expireTime, 'foo'); // time check
}
/**
* Changes namespace; allows more users to share a session.
*/
public function setNamespace(string $namespace): static
{
if ($this->namespace !== $namespace) {
$this->namespace = $namespace;
$this->sessionSection = null;
}
return $this;
}
/**
* Returns current namespace.
*/
public function getNamespace(): string
{
return $this->namespace;
}
/**
* Returns and initializes $this->sessionSection.
*/
protected function getSessionSection(): ?SessionSection
{
if ($this->sessionSection !== null) {
return $this->sessionSection;
}
$this->sessionSection = $section = $this->sessionHandler->getSection('Nette.Http.UserStorage/' . $this->namespace);
if (!$section->get('identity') instanceof IIdentity || !is_bool($section->get('authenticated'))) {
$section->remove();
}
if ($section->get('authenticated') && $section->get('expireDelta') > 0) { // check time expiration
if ($section->get('expireTime') < time()) {
$section->set('reason', self::LogoutInactivity);
$section->set('authenticated', false);
if ($section->get('expireIdentity')) {
$section->remove('identity');
}
}
$section->set('expireTime', time() + $section->expireDelta); // sliding expiration
}
if (!$section->get('authenticated')) {
$section->remove(['expireTime', 'expireDelta', 'expireIdentity', 'authTime']);
}
return $this->sessionSection;
}
}