Passed
Push — master ( 06f2b4...a60608 )
by Petr
09:01
created

TAuthDigest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 76
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A authHeader() 0 6 1
A authType() 0 3 1
A authKey() 0 6 1
A setCredentials() 0 5 1
A hash() 0 6 2
A setProperties() 0 9 1
A getRandomString() 0 3 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Query;
4
5
6
/**
7
 * Trait TAuthDigest
8
 * @package kalanis\RemoteRequest\Protocols\Http\Query
9
 * Authorization header trait
10
 * @link https://datatracker.ietf.org/doc/html/rfc2069
11
 * @link https://en.wikipedia.org/wiki/Digest_access_authentication
12
 */
13
trait TAuthDigest
14
{
15
    use TAuth;
16
17
    protected string $username = '';
18
    protected string $password = '';
19
    protected string $realm = '';
20
    protected string $remoteRandomNumber = '';
21
    protected string $localRandomNumber = '';
22
    protected string $returnToServer = '';
23
    protected string $qualityOfProtection = '';
24
    protected string $algorithm = 'md5';
25
    protected string $requestCounter = '';
26
27
    protected static int $queriesCounter = 1;
28
29 1
    public function setCredentials(string $username, string $password, string $realm): void
30
    {
31 1
        $this->username = $username;
32 1
        $this->password = $password;
33 1
        $this->realm = $realm;
34
    }
35
36 1
    public function setProperties(string $remoteRandomNumber, string $returnToServer, string $qualityOfProtection, string $algorithm = 'md5'): void
37
    {
38 1
        $this->localRandomNumber = $this->getRandomString();
39 1
        $this->remoteRandomNumber = $remoteRandomNumber;
40 1
        $this->returnToServer = $returnToServer;
41 1
        $this->qualityOfProtection = $qualityOfProtection;
42 1
        $this->algorithm = $algorithm;
43 1
        $this->requestCounter = sprintf('%08d', static::$queriesCounter);
44 1
        static::$queriesCounter++;
45
    }
46
47
    /**
48
     * @return string
49
     * @codeCoverageIgnore random generator
50
     */
51
    protected function getRandomString(): string
52
    {
53
        return substr(md5(strval(rand())), 0, 8);
54
    }
55
56 1
    public function authHeader(): void
57
    {
58 1
        $this->addHeader('Authorization',
59 1
            sprintf('%s username="%s", realm="%s", nonce="%s", uri="%s", qop="%s", nc="%s", cnonce="%s", response="%s", opaque="%s"',
60 1
                $this->authType(), $this->username, $this->realm, $this->remoteRandomNumber, $this->getPath(), $this->qualityOfProtection,
61 1
                $this->requestCounter, $this->localRandomNumber, $this->authKey(), $this->returnToServer));
62
    }
63
64 1
    protected function authType(): string
65
    {
66 1
        return 'Digest';
67
    }
68
69 1
    protected function authKey(): string
70
    {
71 1
        $A1 = $this->hash($this->username . ':' . $this->realm . ':' . $this->password);
72 1
        $A2 = $this->hash($this->getMethod() . ':' . $this->getPath());
73
//        return $this->hash($A1 . ':' . $this->remoteRandomNumber . ':' . $A2); // from rfc
74 1
        return $this->hash($A1 . ':' . $this->remoteRandomNumber . ':' . $this->requestCounter . ':' . $this->localRandomNumber . ':' . $this->qualityOfProtection . ':' . $A2); // from wiki
75
    }
76
77 1
    protected function hash(string $what): string
78
    {
79 1
        switch ($this->algorithm) {
80 1
            case 'md5':
81
            default:
82 1
                return md5($what);
83
        }
84
    }
85
86
    abstract public function getMethod(): string;
87
88
    abstract public function getPath(): string;
89
}
90