|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Auth; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A listener which will log out a session if it's idle or has been active for too long. |
|
25
|
|
|
* |
|
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
27
|
|
|
* @license Apache-2.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class TimeoutListener implements \Caridea\Event\Listener |
|
30
|
|
|
{ |
|
31
|
|
|
use \Psr\Log\LoggerAwareTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int Session timeout length in seconds |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $timeout; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var int Session expiration length in seconds |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $expire; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Creates a new timeout listener. |
|
44
|
|
|
* |
|
45
|
|
|
* @param int $timeout The number of seconds until a session should be |
|
46
|
|
|
* considered idle. If omitted, the default is 20 minutes. |
|
47
|
|
|
* @param int $expire The number of seconds until a session should be |
|
48
|
|
|
* considered expired. If omitted, the default is 24 hours. |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function __construct(int $timeout = 1200, int $expire = 86400) |
|
51
|
|
|
{ |
|
52
|
3 |
|
$this->timeout = (int)$timeout; |
|
53
|
3 |
|
$this->expire = (int)$expire; |
|
54
|
3 |
|
$this->logger = new \Psr\Log\NullLogger(); |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function notify(\Caridea\Event\Event $event): void |
|
61
|
|
|
{ |
|
62
|
3 |
|
if ($event instanceof Event\Resume) { |
|
63
|
3 |
|
$now = microtime(true); |
|
64
|
3 |
|
if (($this->timeout + $event->getLastActive()) < $now) { |
|
65
|
1 |
|
$this->logger->info( |
|
66
|
1 |
|
"Authentication for {user} has timed out", |
|
67
|
1 |
|
['user' => $event->getPrincipal()] |
|
68
|
|
|
); |
|
69
|
1 |
|
$event->getSource()->logout(); |
|
70
|
3 |
|
} elseif (($this->expire + $event->getFirstActive()) < $now) { |
|
71
|
1 |
|
$this->logger->info( |
|
72
|
1 |
|
"Authentication for {user} has expired", |
|
73
|
1 |
|
['user' => $event->getPrincipal()] |
|
74
|
|
|
); |
|
75
|
1 |
|
$event->getSource()->logout(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
3 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|