Issues (2963)

LibreNMS/Authentication/HttpAuthAuthorizer.php (4 issues)

1
<?php
2
3
namespace LibreNMS\Authentication;
4
5
use LibreNMS\Config;
6
use LibreNMS\Exceptions\AuthenticationException;
7
8
class HttpAuthAuthorizer extends MysqlAuthorizer
9
{
10
    protected static $HAS_AUTH_USERMANAGEMENT = true;
11
    protected static $CAN_UPDATE_USER = true;
12
    protected static $CAN_UPDATE_PASSWORDS = false;
13
    protected static $AUTH_IS_EXTERNAL = true;
14
15
    public function authenticate($credentials)
16
    {
17
        if (isset($credentials['username']) && $this->userExists($credentials['username'])) {
18
            return true;
19
        }
20
21
        throw new AuthenticationException('No matching user found and http_auth_guest is not set');
22
    }
23
24
    public function userExists($username, $throw_exception = false)
25
    {
26
        if (parent::userExists($username)) {
27
            return true;
28
        }
29
30
        if (Config::has('http_auth_guest') && parent::userExists(Config::get('http_auth_guest'))) {
31
            return true;
32
        }
33
34
        return false;
35
    }
36
37
    public function getUserlevel($username)
38
    {
39
        $user_level = parent::getUserlevel($username);
40
41
        if ($user_level) {
0 ignored issues
show
$user_level is of type App\Models\User, thus it always evaluated to true.
Loading history...
42
            return $user_level;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user_level returns the type App\Models\User which is incompatible with the return type mandated by LibreNMS\Interfaces\Auth...horizer::getUserlevel() of integer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
43
        }
44
45
        if (Config::has('http_auth_guest')) {
46
            return parent::getUserlevel(Config::get('http_auth_guest'));
47
        }
48
49
        return 0;
50
    }
51
52
    public function getUserid($username)
53
    {
54
        $user_id = parent::getUserid($username);
55
56
        if ($user_id) {
0 ignored issues
show
$user_id is of type App\Models\User, thus it always evaluated to true.
Loading history...
57
            return $user_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user_id returns the type App\Models\User which is incompatible with the return type mandated by LibreNMS\Interfaces\Auth...Authorizer::getUserid() of integer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
58
        }
59
60
        if (Config::has('http_auth_guest')) {
61
            return parent::getUserid(Config::get('http_auth_guest'));
62
        }
63
64
        return -1;
65
    }
66
}
67