Digest::authenticate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 3
b 0
f 0
nc 3
nop 0
dl 0
loc 24
ccs 0
cts 17
cp 0
crap 20
rs 9.8333
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
55
            return false;
56
        }
57
58
        $A1 = md5($data['username'] . ':' . $this->realm . ':' . $this->whiteList[$data['username']]);
59
        $A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
60
        $valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
61
62
        if ($data['response'] != $valid_response) {
63
            $this->http->writeStatus($ht::HTTP_UNAUTHORIZED);
64
65
            return false;
66
        }
67
68
        $this->username = $data['username'];
69
70
        return true;
71
    }
72
73
    /**
74
     * Parse digest parameters
75
     *
76
     * @param string $txt
77
     *
78
     * @return boolean
79
     */
80
    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...
81
    {
82
        // protect against missing data
83
        $needed_parts = ['nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1];
84
        $data = [];
85
        $keys = implode('|', array_keys($needed_parts));
86
87
        preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
88
89
        foreach ($matches as $m) {
90
            $data[$m[1]] = $m[3] ? $m[3] : $m[4];
91
            unset($needed_parts[$m[1]]);
92
        }
93
94
        return $needed_parts ? false : $data;
0 ignored issues
show
introduced by
$needed_parts is a non-empty array, thus is always true.
Loading history...
95
    }
96
97
    /**
98
     * Shows the server response
99
     *
100
     * @return null
101
     */
102
    public function response()
103
    {
104
        $status = http_response_code();
105
        $this->response = 'Error ' . $status .' (' . $this->http->getStatusText($status) . ')!!';
106
        echo $this->response;
107
    }
108
}
109