1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth\Methods; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use kalanis\kw_accounts\Interfaces\IAuth; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class HttpUser |
12
|
|
|
* @package kalanis\kw_auth\AuthMethods |
13
|
|
|
* Authenticate via Http User |
14
|
|
|
* @link https://www.php.net/manual/en/features.http-auth.php |
15
|
|
|
*/ |
16
|
|
|
class HttpUser extends AMethods |
17
|
|
|
{ |
18
|
|
|
public const INPUT_NAME = 'PHP_AUTH_USER'; |
19
|
|
|
public const INPUT_PASS = 'PHP_AUTH_PW'; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected string $realm = 'KWCMS_Http_User'; |
23
|
|
|
/** @var ArrayAccess<string, string|int> */ |
24
|
|
|
protected ArrayAccess $server; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param IAuth|null $authenticator |
28
|
|
|
* @param AMethods|null $nextOne |
29
|
|
|
* @param ArrayAccess<string, string|int> $server |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct(?IAuth $authenticator, ?AMethods $nextOne, ArrayAccess $server) |
32
|
|
|
{ |
33
|
3 |
|
parent::__construct($authenticator, $nextOne); |
34
|
3 |
|
$this->server = $server; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
3 |
|
public function process(ArrayAccess $credentials): void |
38
|
|
|
{ |
39
|
3 |
|
$name = $this->server->offsetExists(static::INPUT_NAME) ? strval($this->server->offsetGet(static::INPUT_NAME)) : '' ; |
40
|
3 |
|
$pass = $this->server->offsetExists(static::INPUT_PASS) ? strval($this->server->offsetGet(static::INPUT_PASS)) : '' ; |
41
|
|
|
|
42
|
3 |
|
if (!empty($name) && !empty($pass)) { |
43
|
|
|
/** @scrutinizer ignore-call */ |
44
|
1 |
|
$this->loggedUser = $this->authenticator->authenticate($name, ['password' => $pass]); |
45
|
|
|
} |
46
|
3 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @codeCoverageIgnore headers |
50
|
|
|
*/ |
51
|
|
|
public function remove(): void |
52
|
|
|
{ |
53
|
|
|
$this->authNotExists(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @codeCoverageIgnore headers |
58
|
|
|
*/ |
59
|
|
|
public function authNotExists(): void |
60
|
|
|
{ |
61
|
|
|
header('HTTP/1.1 401 Unauthorized'); |
62
|
|
|
header('WWW-Authenticate: Basic realm="' . $this->realm . '"'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|