Basic   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 62
ccs 0
cts 33
cp 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 8 2
A authenticate() 0 27 4
A response() 0 5 1
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
 * Basic class
15
 *
16
 * Class for Basic access authetication
17
 */
18
class Basic extends AbstractRest
19
{
20
    /**
21
     * Requests client authentication
22
     *
23
     * @return null
24
     */
25
    public function request()
26
    {
27
        if (empty($_SERVER['PHP_AUTH_USER'])) {
28
            $ht = $this->http;
29
30
            $this->http->writeStatus($ht::HTTP_UNAUTHORIZED);
31
            header('WWW-Authenticate: Basic realm="'.$this->realm.'"');
32
            die('Error ' . $ht::HTTP_UNAUTHORIZED .' (' . $this->http->getStatusText($ht::HTTP_UNAUTHORIZED) . ')!!');
33
        }
34
    }
35
36
    /**
37
     * Checks credentials
38
     *
39
     * @return boolean
40
     */
41
    public function authenticate()
42
    {
43
        $ht = $this->http;
44
45
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
46
            $this->http->writeStatus($ht::HTTP_UNAUTHORIZED);
47
48
            return false;
49
        }
50
51
        $username = $_SERVER['PHP_AUTH_USER'];
52
53
        if (!isset($this->whiteList[$username])) {
54
            $this->http->writeStatus($ht::HTTP_UNAUTHORIZED);
55
56
            return false;
57
        }
58
59
        if ($this->whiteList[$username] !== $_SERVER['PHP_AUTH_PW']) {
60
            $this->http->writeStatus($ht::HTTP_UNAUTHORIZED);
61
62
            return false;
63
        }
64
65
        $this->username = $username;
66
67
        return true;
68
    }
69
70
    /**
71
     * Shows the server response
72
     *
73
     * @return null
74
     */
75
    public function response()
76
    {
77
        $status = http_response_code();
78
        $this->response = 'Error ' . $status .' (' . $this->http->getStatusText($status) . ')!!';
79
        echo $this->response;
80
    }
81
}
82