AuthDigest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 57
ccs 23
cts 23
cp 1
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataToReturn() 0 3 1
A getRemoteRandomNumber() 0 3 1
A getAlgorithm() 0 3 2
A getAuthRealm() 0 3 1
A getAuthType() 0 3 1
A getQualitiesOfProtection() 0 3 1
A getAuthHeader() 0 3 1
A processContent() 0 13 3
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Http\Answer;
4
5
6
use kalanis\RemoteRequest\Protocols\Http;
7
8
9
/**
10
 * Class AuthDigest
11
 * @package kalanis\RemoteRequest\Protocols\Http\Answer
12
 * Message from the remote server compilation - protocol http - parse digest info
13
 *
14
 * How it works:
15
 * - ask for data without auth
16
 * - server returns 401 with WWW-Authenticate header
17
 * - pass that data to this class to parse header
18
 * - fill auth query with data from this class and original query
19
 * - ask for data again
20
 */
21
class AuthDigest extends Http\Answer
22
{
23
    protected string $authType = '';
24
    /** @var string[] */
25
    protected array $authHeader = [];
26
27 3
    public function processContent(): self
28
    {
29 3
        if (401 == $this->getCode()) {
30
            // unauth
31 3
            $headerData = strval($this->getHeader('WWW-Authenticate'));
32 3
            preg_match('#^\s?([^\s]+)\s#i', $headerData, $types);
33 3
            preg_match_all('#([^\s]+)="([^"]+)"#i', $headerData, $matches);
34 3
            $this->authType = $types[1] ?? 'Basic';
35 3
            foreach ($matches[0] as $key => $info) {
36 3
                $this->authHeader[$matches[1][$key]] = $matches[2][$key];
37
            }
38
        }
39 3
        return $this;
40
    }
41
42 3
    public function getAuthType(): string
43
    {
44 3
        return $this->authType;
45
    }
46
47 3
    public function getAuthRealm(): ?string
48
    {
49 3
        return $this->getAuthHeader('realm');
50
    }
51
52
    /**
53
     * @return string[]
54
     */
55 3
    public function getQualitiesOfProtection(): array
56
    {
57 3
        return explode(',', strval($this->getAuthHeader('qop')));
58
    }
59
60 3
    public function getRemoteRandomNumber(): ?string
61
    {
62 3
        return $this->getAuthHeader('nonce');
63
    }
64
65 3
    public function getDataToReturn(): ?string
66
    {
67 3
        return $this->getAuthHeader('opaque');
68
    }
69
70 3
    public function getAlgorithm(): string
71
    {
72 3
        return $this->getAuthHeader('algorithm') ?: 'md5';
73
    }
74
75 3
    protected function getAuthHeader(string $key): ?string
76
    {
77 3
        return $this->authHeader[$key] ?? null;
78
    }
79
}
80