HttpBasicAuth::authenticateAgainstArray()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 5
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Middleware;
6
7
class HttpBasicAuth extends \Suricate\Middleware
8
{
9
    const AUTHTYPE_ARRAY = 'array';
10
    const AUTHTYPE_DB = 'database';
11
    protected $options;
12
13
    public function __construct($options = null)
14
    {
15
        $this->options = [
16
            'users' => [],
17
            'type' => self::AUTHTYPE_ARRAY,
18
            'path' => '/',
19
            'realm' => 'restricted area',
20
            'db' => []
21
        ];
22
23
        if ($options !== null) {
24
            $this->options = array_merge($this->options, (array) $options);
25
        }
26
    }
27
28
    private function shouldAuthenticate($request)
29
    {
30
        $path = rtrim($this->options["path"], "/");
31
        $regex = "#" . $path . "(/.*)?$#";
32
33
        return preg_match($regex, $request->getRequestUri());
34
    }
35
36
    /**
37
     * Authenticate against backend dispatcher
38
     *
39
     * @param ?string $user     username
40
     * @param ?string $password password
41
     * @return bool
42
     */
43
    private function authenticate(?string $user, ?string $password): bool
44
    {
45
        switch ($this->options['type']) {
46
            case self::AUTHTYPE_ARRAY:
47
                return $this->authenticateAgainstArray($user, $password);
48
            case self::AUTHTYPE_DB:
49
                return $this->authenticateAgainstDatabase($user, $password);
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * Authenticate against array of usernames / passwords
57
     *
58
     * @param ?string $user     username
59
     * @param ?string $password password
60
     * @return bool
61
     */
62
    private function authenticateAgainstArray(
63
        ?string $user,
64
        ?string $password
65
    ): bool {
66
        if (
67
            isset($this->options['users'][$user]) &&
68
            $this->options['users'][$user] == $password
69
        ) {
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    private function authenticateAgainstDatabase(
77
        ?string $user,
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

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

77
        /** @scrutinizer ignore-unused */ ?string $user,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
        ?string $password
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed. ( Ignorable by Annotation )

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

78
        /** @scrutinizer ignore-unused */ ?string $password

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    ): bool {
80
        return false;
81
    }
82
83
    public function call(&$request, &$response)
84
    {
85
        if ($this->shouldAuthenticate($response)) {
86
            $user = dataGet($_SERVER, 'PHP_AUTH_USER');
87
            $password = dataGet($_SERVER, 'PHP_AUTH_PW');
88
89
            if (!$this->authenticate($user, $password)) {
90
                app()->abort('401', 'not aut', [
91
                    "WWW-Authenticate" => sprintf(
92
                        'Basic realm="%s"',
93
                        $this->options["realm"]
94
                    )
95
                ]);
96
            }
97
        }
98
    }
99
}
100