|
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
|
|
|
|