Passed
Push — master ( edb799...f409da )
by Petr
07:56
created

TAuthDigest::setCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
ccs 4
cts 4
cp 1
crap 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
    /** @var string */
18
    protected $username = '';
19
    /** @var string */
20
    protected $password = '';
21
    /** @var string */
22
    protected $realm = '';
23
    /** @var string */
24
    protected $remoteRandomNumber = '';
25
    /** @var string */
26
    protected $localRandomNumber = '';
27
    /** @var string */
28
    protected $returnToServer = '';
29
    /** @var string */
30
    protected $qualityOfProtection = '';
31
    /** @var string */
32
    protected $algorithm = 'md5';
33
    /** @var string */
34
    protected $requestCounter = '';
35
36
    /** @var int */
37
    protected static $queriesCounter = 1;
38
39 1
    public function setCredentials(string $username, string $password, string $realm): void
40
    {
41 1
        $this->username = $username;
42 1
        $this->password = $password;
43 1
        $this->realm = $realm;
44 1
    }
45
46 1
    public function setProperties(string $remoteRandomNumber, string $returnToServer, string $qualityOfProtection, string $algorithm = 'md5'): void
47
    {
48 1
        $this->localRandomNumber = $this->getRandomString();
49 1
        $this->remoteRandomNumber = $remoteRandomNumber;
50 1
        $this->returnToServer = $returnToServer;
51 1
        $this->qualityOfProtection = $qualityOfProtection;
52 1
        $this->algorithm = $algorithm;
53 1
        $this->requestCounter = sprintf('%08d', static::$queriesCounter);
54 1
        static::$queriesCounter++;
55 1
    }
56
57
    /**
58
     * @return string
59
     * @codeCoverageIgnore random generator
60
     */
61
    protected function getRandomString(): string
62
    {
63
        return substr(md5(strval(rand())), 0, 8);
64
    }
65
66 1
    public function authHeader(): void
67
    {
68 1
        $this->addHeader('Authorization',
69 1
            sprintf('%s username="%s", realm="%s", nonce="%s", uri="%s", qop="%s", nc="%s", cnonce="%s", response="%s", opaque="%s"',
70 1
                $this->authType(), $this->username, $this->realm, $this->remoteRandomNumber, $this->getPath(), $this->qualityOfProtection,
71 1
                $this->requestCounter, $this->localRandomNumber, $this->authKey(), $this->returnToServer));
72 1
    }
73
74 1
    protected function authType(): string
75
    {
76 1
        return 'Digest';
77
    }
78
79 1
    protected function authKey(): string
80
    {
81 1
        $A1 = $this->hash($this->username . ':' . $this->realm . ':' . $this->password);
82 1
        $A2 = $this->hash($this->getMethod() . ':' . $this->getPath());
83
//        return $this->hash($A1 . ':' . $this->remoteRandomNumber . ':' . $A2); // from rfc
84 1
        return $this->hash($A1 . ':' . $this->remoteRandomNumber . ':' . $this->requestCounter . ':' . $this->localRandomNumber . ':' . $this->qualityOfProtection . ':' . $A2); // from wiki
85
    }
86
87 1
    protected function hash(string $what): string
88
    {
89 1
        switch ($this->algorithm) {
90 1
            case 'md5':
91
            default:
92 1
                return md5($what);
93
        }
94
    }
95
96
    abstract public function getMethod(): string;
97
98
    abstract public function getPath(): string;
99
}
100