1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_address_handler\examples; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_address_handler\Handler; |
7
|
|
|
use kalanis\kw_address_handler\Sources\ServerRequest; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Certs |
12
|
|
|
* @package kalanis\kw_address_handler\examples |
13
|
|
|
* Authenticate via certificates |
14
|
|
|
* @codeCoverageIgnore because access external content |
15
|
|
|
* - public on server, private on client whom manage the site |
16
|
|
|
* |
17
|
|
|
* query: |
18
|
|
|
* http://localhost/web/u:debugger/?pass=asdfghjkl×tamp=123456&digest=hjkl |
19
|
|
|
* |
20
|
|
|
* makes following call: |
21
|
|
|
* 'hjkl' == md5( '/web/u:debugger/?pass=asdfghjkl×tamp=123456&salt=99999' . 'xyz' ) |
22
|
|
|
* |
23
|
|
|
* - it has removed digest value and added locally stored salt |
24
|
|
|
*/ |
25
|
|
|
class Certs |
26
|
|
|
{ |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $localKey = '99999'; |
29
|
|
|
/** @var string */ |
30
|
|
|
protected $localSalt = 'xyz'; |
31
|
|
|
/** @var Handler */ |
32
|
|
|
protected $uriHandler = null; |
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->uriHandler = new Handler(new ServerRequest()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \ArrayAccess $credentials |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
public function process(\ArrayAccess $credentials): void |
44
|
|
|
{ |
45
|
|
|
$stamp = $credentials->offsetExists('timestamp') ? $credentials->offsetGet('timestamp') : 0 ; |
46
|
|
|
if (!empty($stamp)) { |
47
|
|
|
// now we have public key and salt from our storage, so it's time to check it |
48
|
|
|
|
49
|
|
|
// digest out, salt in |
50
|
|
|
$digest = $this->uriHandler->getParams()->offsetGet('digest'); |
51
|
|
|
$this->uriHandler->getParams()->offsetUnset('digest'); |
52
|
|
|
$this->uriHandler->getParams()->offsetSet('salt', $this->localSalt); |
53
|
|
|
$data = $this->uriHandler->getAddress(); |
54
|
|
|
|
55
|
|
|
// verify |
56
|
|
|
$result = strval($digest) === md5(strval($data) . $this->localKey); |
57
|
|
|
if ($result) { |
58
|
|
|
// OK |
59
|
|
|
throw new \Exception('Passed?!'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|