1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth\Methods; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use kalanis\kw_accounts\Interfaces\IAuthCert; |
8
|
|
|
use kalanis\kw_address_handler\Handler; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class HttpCerts |
13
|
|
|
* @package kalanis\kw_auth\AuthMethods |
14
|
|
|
* Authenticate via http certificates |
15
|
|
|
* - public on server, private on client whom manage the site |
16
|
|
|
*/ |
17
|
|
|
class HttpCerts extends AMethods |
18
|
|
|
{ |
19
|
|
|
public const INPUT_NAME = 'PHP_AUTH_USER'; |
20
|
|
|
public const INPUT_PASS = 'PHP_AUTH_DIGEST'; |
21
|
|
|
public const INPUT_SALT = 'salt'; |
22
|
|
|
|
23
|
|
|
protected IAuthCert $certAuthenticator; |
24
|
|
|
protected Handler $uriHandler; |
25
|
|
|
/** @var ArrayAccess<string, string|int> */ |
26
|
|
|
protected ArrayAccess $server; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param IAuthCert $authenticator |
30
|
|
|
* @param AMethods|null $nextOne |
31
|
|
|
* @param Handler $uriHandler |
32
|
|
|
* @param ArrayAccess<string, string|int> $server |
33
|
|
|
*/ |
34
|
1 |
|
public function __construct(IAuthCert $authenticator, ?AMethods $nextOne, Handler $uriHandler, ArrayAccess $server) |
35
|
|
|
{ |
36
|
1 |
|
parent::__construct($authenticator, $nextOne); |
37
|
1 |
|
$this->certAuthenticator = $authenticator; |
38
|
1 |
|
$this->uriHandler = $uriHandler; |
39
|
1 |
|
$this->server = $server; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
1 |
|
public function process(\ArrayAccess $credentials): void |
43
|
|
|
{ |
44
|
1 |
|
$name = $this->server->offsetExists(static::INPUT_NAME) ? strval($this->server->offsetGet(static::INPUT_NAME)) : '' ; |
45
|
1 |
|
$digest = $this->server->offsetExists(static::INPUT_PASS) ? strval($this->server->offsetGet(static::INPUT_PASS)) : '' ; |
46
|
1 |
|
$wantedUser = $this->certAuthenticator->getDataOnly(strval($name)); |
47
|
1 |
|
$wantedCert = $this->certAuthenticator->getCertData(strval($name)); |
48
|
1 |
|
if ($wantedUser && $wantedCert && $digest) { |
49
|
|
|
// now we have public key and salt from our storage, so it's time to check it |
50
|
|
|
|
51
|
|
|
// salt in |
52
|
1 |
|
$this->uriHandler->getParams()->offsetSet(static::INPUT_SALT, $wantedCert->getSalt()); |
53
|
1 |
|
$data = strval($this->uriHandler->getAddress()); |
54
|
|
|
|
55
|
|
|
// verify |
56
|
1 |
|
$result = openssl_verify($data, base64_decode(rawurldecode(strval($digest))), $wantedCert->getPubKey(), OPENSSL_ALGO_SHA256); |
57
|
1 |
|
if (1 === $result) { |
58
|
|
|
// OK |
59
|
1 |
|
$this->loggedUser = $wantedUser; |
60
|
|
|
} |
61
|
|
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @codeCoverageIgnore headers |
66
|
|
|
*/ |
67
|
|
|
public function remove(): void |
68
|
|
|
{ |
69
|
|
|
$this->authNotExists(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @codeCoverageIgnore headers |
74
|
|
|
*/ |
75
|
|
|
public function authNotExists(): void |
76
|
|
|
{ |
77
|
|
|
header('HTTP/1.1 401 Unauthorized'); |
78
|
|
|
header('WWW-Authenticate: DigestCert'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|