Passed
Push — master ( c34495...8adf73 )
by Darío
03:13
created

Digest::httpDigestParse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 1
dl 0
loc 15
ccs 0
cts 11
cp 0
crap 20
rs 10
c 0
b 0
f 0
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']))
0 ignored issues
show
Bug introduced by
The method http_digest_parse() does not exist on Drone\Network\Rest\Digest. Did you maybe mean httpDigestParse()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        if (!($data = $this->/** @scrutinizer ignore-call */ http_digest_parse($_SERVER['PHP_AUTH_DIGEST']))

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.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The method httpDigestParse() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
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;
0 ignored issues
show
introduced by
$needed_parts is a non-empty array, thus is always true.
Loading history...
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