TimedSessions::tryLogged()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 9
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 72
rs 8.4444
1
<?php
2
3
namespace kalanis\kw_auth\Methods;
4
5
6
use ArrayAccess;
7
use kalanis\kw_accounts\Interfaces\IAuth;
8
use SessionHandlerInterface;
9
10
11
/**
12
 * Class TimedSessions
13
 * @package kalanis\kw_auth\AuthMethods
14
 * Authenticate via Session - timer for valid authentication
15
 * @codeCoverageIgnore external resource, Cannot start session when headers already sent
16
 */
17
class TimedSessions extends Sessions
18
{
19
    protected const INPUT_TIME = 'acc_time';
20
21
    protected int $loginTimeout = 0;
22
23
    /**
24
     * @param IAuth|null $authenticator
25
     * @param AMethods|null $nextOne
26
     * @param ArrayAccess<string, string|int> $session
27
     * @param ArrayAccess<string, string|int> $server
28
     * @param int $loginTimeout
29
     * @param SessionHandlerInterface|null $externalHandler
30
     */
31
    public function __construct(?IAuth $authenticator, ?AMethods $nextOne, ArrayAccess $session, ArrayAccess $server, int $loginTimeout = 86400, ?SessionHandlerInterface $externalHandler = null)
32
    {
33
        parent::__construct($authenticator, $nextOne, $session, $server, $externalHandler);
34
        $this->loginTimeout = $loginTimeout;
35
    }
36
37
    protected function tryLogged(): bool
38
    {
39
        return (
40
            $this->session->offsetExists(static::SESSION_NAME)
41
            && !empty($this->session->offsetGet(static::SESSION_NAME))// user has name already set
42
            && $this->session->offsetExists(static::SESSION_IP)
43
            && !empty($this->session->offsetGet(static::SESSION_IP)) // user has already set known ip
44
            && $this->session->offsetExists(static::INPUT_TIME)
45
            && !empty($this->session->offsetGet(static::INPUT_TIME)) // user has already set last used time
46
            && ($this->server->offsetGet(static::SERVER_REMOTE) == $this->session->offsetGet(static::SESSION_IP)) // against proxy attack - changed ip through work
47
            && ((intval(strval($this->session->offsetGet(static::INPUT_TIME))) + $this->loginTimeout) > time()) // kick-off on time delay
48
        );
49
    }
50
51
    protected function fillSession(string $name): void
52
    {
53
        parent::fillSession($name);
54
        $this->session->offsetSet(static::INPUT_TIME, time()); // set new timestamp
55
    }
56
57
    protected function clearSession(): void
58
    {
59
        parent::clearSession();
60
        $this->session->offsetSet(static::INPUT_TIME, 0);
61
    }
62
}
63