Passed
Push — master ( 141183...2b56eb )
by Petr
08:03
created

UrlHash::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_auth\Methods;
4
5
6
use kalanis\kw_address_handler\Handler;
7
use kalanis\kw_auth\Interfaces\IAuthCert;
8
9
10
/**
11
 * Class UrlHash
12
 * @package kalanis\kw_auth\AuthMethods
13
 * Authenticate via hashed values
14
 *
15
 * query:
16
 * //dummy/u:whoami/?pass=asdf123ghjk456&timestamp=123456&digest=poiuztrewq
17
 *
18
 * makes following call:
19
 * hash($algorithm = <md5 | sha256 | ...> , $key = 'mnbvcx987' . $data = '//dummy/u:whoami/?pass=asdf123ghjk456&timestamp=123456&salt=789' , $signature = 'poiuztrewq'
20
 *
21
 * - it removed digest value and added locally stored salt
22
 */
23
class UrlHash extends AMethods
24
{
25
    use TStamp;
26
27
    const INPUT_NAME = 'name';
28
    const INPUT_NAME2 = 'user';
29
    const INPUT_STAMP = 'timestamp';
30
    const INPUT_DIGEST = 'digest';
31
    const INPUT_SALT = 'salt';
32
33
    /** @var IAuthCert */
34
    protected $authenticator;
35
    /** @var Handler */
36
    protected $uriHandler = null;
37
    /** @var string */
38
    protected $algorithm = '';
39
40
    /**
41
     * @param IAuthCert $authenticator
42
     * @param AMethods|null $nextOne
43
     * @param Handler $uriHandler
44
     * @param string $algorithm for hash function
45
     * @link https://php.net/manual/en/function.hash.php
46
     */
47 5
    public function __construct(IAuthCert $authenticator, ?AMethods $nextOne, Handler $uriHandler, string $algorithm)
48
    {
49 5
        parent::__construct($authenticator, $nextOne);
50 5
        $this->uriHandler = $uriHandler;
51 5
        $this->algorithm = $algorithm;
52 5
    }
53
54 5
    public function process(\ArrayAccess $credentials): void
55
    {
56 5
        $name = $credentials->offsetExists(static::INPUT_NAME) ? strval($credentials->offsetGet(static::INPUT_NAME)) : '' ;
57 5
        $name = $credentials->offsetExists(static::INPUT_NAME2) ? strval($credentials->offsetGet(static::INPUT_NAME2) ): $name ;
58 5
        $stamp = $credentials->offsetExists(static::INPUT_STAMP) ? intval(strval($credentials->offsetGet(static::INPUT_STAMP))) : 0 ;
59
60 5
        $wantedUser = $this->authenticator->getCertData(strval($name));
61 5
        if ($wantedUser && !empty($stamp) && $this->checkStamp($stamp)) {
62
            // now we have private salt from our storage, so it's time to check it
63
64
            // digest out, salt in
65 1
            $digest = strval($this->uriHandler->getParams()->offsetGet(static::INPUT_DIGEST));
66 1
            $this->uriHandler->getParams()->offsetUnset(static::INPUT_DIGEST);
67 1
            $this->uriHandler->getParams()->offsetSet(static::INPUT_SALT, $wantedUser->getPubSalt());
68 1
            $data = strval($this->uriHandler->getAddress());
69
70
            // verify
71 1
            if (hash($this->algorithm, $wantedUser->getPubKey() . $data) == $digest) {
72
                // OK
73 1
                $this->loggedUser = $wantedUser;
74
            }
75
        }
76 5
    }
77
78 5
    public function remove(): void
79
    {
80 5
    }
81
}
82