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 Sessions |
13
|
|
|
* @package kalanis\kw_auth\AuthMethods |
14
|
|
|
* Authenticate via Session |
15
|
|
|
* @codeCoverageIgnore external resource, Cannot start session when headers already sent |
16
|
|
|
*/ |
17
|
|
|
class Sessions extends AMethods |
18
|
|
|
{ |
19
|
|
|
protected const SESSION_IP = 'acc_ip'; |
20
|
|
|
protected const SESSION_NAME = 'acc_name'; |
21
|
|
|
protected const SERVER_REMOTE = 'REMOTE_ADDR'; |
22
|
|
|
protected const INPUT_NAME = 'name'; |
23
|
|
|
protected const INPUT_NAME2 = 'user'; |
24
|
|
|
protected const INPUT_PASS = 'pass'; |
25
|
|
|
protected const INPUT_PASS2 = 'password'; |
26
|
|
|
|
27
|
|
|
/** @var ArrayAccess<string, string|int> */ |
28
|
|
|
protected ArrayAccess $session; |
29
|
|
|
/** @var ArrayAccess<string, string|int> */ |
30
|
|
|
protected ArrayAccess $server; |
31
|
|
|
protected ?SessionHandlerInterface $externalHandler = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param IAuth|null $authenticator |
35
|
|
|
* @param AMethods|null $nextOne |
36
|
|
|
* @param ArrayAccess<string, string|int> $session |
37
|
|
|
* @param ArrayAccess<string, string|int> $server |
38
|
|
|
* @param SessionHandlerInterface|null $externalHandler |
39
|
|
|
*/ |
40
|
|
|
public function __construct(?IAuth $authenticator, ?AMethods $nextOne, ArrayAccess $session, ArrayAccess $server, ?SessionHandlerInterface $externalHandler = null) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($authenticator, $nextOne); |
43
|
|
|
$this->session = $session; |
44
|
|
|
$this->server = $server; |
45
|
|
|
$this->externalHandler = $externalHandler; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function process(ArrayAccess $credentials): void |
49
|
|
|
{ |
50
|
|
|
if (PHP_SESSION_NONE == session_status()) { |
51
|
|
|
if ($this->externalHandler) { |
52
|
|
|
session_set_save_handler($this->externalHandler, true); |
53
|
|
|
} |
54
|
|
|
session_start(); |
55
|
|
|
} |
56
|
|
|
if ($this->tryLogged()) { |
57
|
|
|
/** @scrutinizer ignore-call */ |
58
|
|
|
$this->loggedUser = $this->authenticator->getDataOnly($this->nameFromSess()); |
59
|
|
|
} else { |
60
|
|
|
$name = $credentials->offsetExists(static::INPUT_NAME) ? strval($credentials->offsetGet(static::INPUT_NAME)) : '' ; |
61
|
|
|
$name = $credentials->offsetExists(static::INPUT_NAME2) ? strval($credentials->offsetGet(static::INPUT_NAME2)) : $name ; |
62
|
|
|
$pass = $credentials->offsetExists(static::INPUT_PASS) ? strval($credentials->offsetGet(static::INPUT_PASS)) : '' ; |
63
|
|
|
$pass = $credentials->offsetExists(static::INPUT_PASS2) ? strval($credentials->offsetGet(static::INPUT_PASS2)) : $pass ; |
64
|
|
|
if (!empty($name) && !empty($pass)) { |
65
|
|
|
/** @scrutinizer ignore-call */ |
66
|
|
|
$this->loggedUser = $this->authenticator->authenticate($name, ['password' => $pass]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
$this->clearSession(); |
70
|
|
|
if ($this->loggedUser) { |
71
|
|
|
$this->fillSession($this->loggedUser->getAuthName()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function remove(): void |
76
|
|
|
{ |
77
|
|
|
if (PHP_SESSION_ACTIVE == session_status()) { |
78
|
|
|
session_destroy(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function tryLogged(): bool |
83
|
|
|
{ |
84
|
|
|
return ( |
85
|
|
|
$this->session->offsetExists(static::SESSION_NAME) |
86
|
|
|
&& !empty($this->session->offsetGet(static::SESSION_NAME)) // user has name already set |
87
|
|
|
&& $this->session->offsetExists(static::SESSION_IP) |
88
|
|
|
&& !empty($this->session->offsetGet(static::SESSION_IP)) // user has already set known ip |
89
|
|
|
&& ($this->server->offsetGet(static::SERVER_REMOTE) == $this->session->offsetGet(static::SESSION_IP)) // against proxy attack - changed ip through work |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function nameFromSess(): string |
94
|
|
|
{ |
95
|
|
|
return strval($this->session->offsetGet(static::SESSION_NAME)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function fillSession(string $name): void |
99
|
|
|
{ |
100
|
|
|
$this->session->offsetSet(static::SESSION_NAME, $name); |
101
|
|
|
$this->session->offsetSet(static::SESSION_IP, strval($this->server->offsetGet(static::SERVER_REMOTE))); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function clearSession(): void |
105
|
|
|
{ |
106
|
|
|
$this->session->offsetSet(static::SESSION_NAME, ''); |
107
|
|
|
$this->session->offsetSet(static::SESSION_IP, ''); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|