|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Network\Rest; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Digest class |
|
15
|
|
|
* |
|
16
|
|
|
* Class for Digest access authetication |
|
17
|
|
|
*/ |
|
18
|
|
|
class Digest extends AbstractRest |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Requests client authentication |
|
22
|
|
|
* |
|
23
|
|
|
* @return null |
|
24
|
|
|
*/ |
|
25
|
|
|
public function request() |
|
26
|
|
|
{ |
|
27
|
|
|
if (empty($_SERVER['PHP_AUTH_DIGEST'])) { |
|
28
|
|
|
$ht = $this->http; |
|
29
|
|
|
|
|
30
|
|
|
$this->http->writeStatus($ht::HTTP_UNAUTHORIZED); |
|
31
|
|
|
header( |
|
32
|
|
|
'WWW-Authenticate: Digest realm="'. |
|
33
|
|
|
$this->realm. |
|
34
|
|
|
'",qop="auth",nonce="'. |
|
35
|
|
|
uniqid(). |
|
36
|
|
|
'",opaque="'.md5($this->realm).'"' |
|
37
|
|
|
); |
|
38
|
|
|
die('Error ' . $ht::HTTP_UNAUTHORIZED .' (' . $this->http->getStatusText($ht::HTTP_UNAUTHORIZED) . ')!!'); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Checks credentials |
|
44
|
|
|
* |
|
45
|
|
|
* @return boolean |
|
46
|
|
|
*/ |
|
47
|
|
|
public function authenticate() |
|
48
|
|
|
{ |
|
49
|
|
|
$ht = $this->http; |
|
50
|
|
|
|
|
51
|
|
|
if (!($data = $this->http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) |
|
|
|
|
|
|
52
|
|
|
|| !isset($this->whiteList[$data['username']])) { |
|
53
|
|
|
$this->http->writeStatus($ht::HTTP_UNAUTHORIZED); |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$A1 = md5($data['username'] . ':' . $this->realm . ':' . $this->whiteList[$data['username']]); |
|
58
|
|
|
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']); |
|
59
|
|
|
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2); |
|
60
|
|
|
|
|
61
|
|
|
if ($data['response'] != $valid_response) { |
|
62
|
|
|
$this->http->writeStatus($ht::HTTP_UNAUTHORIZED); |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->username = $data['username']; |
|
67
|
|
|
|
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Parse digest parameters |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $txt |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean |
|
77
|
|
|
*/ |
|
78
|
|
|
private function httpDigestParse($txt) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
// protect against missing data |
|
81
|
|
|
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1); |
|
82
|
|
|
$data = array(); |
|
83
|
|
|
$keys = implode('|', array_keys($needed_parts)); |
|
84
|
|
|
|
|
85
|
|
|
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($matches as $m) { |
|
88
|
|
|
$data[$m[1]] = $m[3] ? $m[3] : $m[4]; |
|
89
|
|
|
unset($needed_parts[$m[1]]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $needed_parts ? false : $data; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Shows the server response |
|
97
|
|
|
* |
|
98
|
|
|
* @return null |
|
99
|
|
|
*/ |
|
100
|
|
|
public function response() |
|
101
|
|
|
{ |
|
102
|
|
|
$status = http_response_code(); |
|
103
|
|
|
$this->response = 'Error ' . $status .' (' . $this->http->getStatusText($status) . ')!!'; |
|
104
|
|
|
echo $this->response; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.