UserAuthenticator   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 26
eloc 82
c 4
b 0
f 0
dl 0
loc 166
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserById() 0 5 1
A __construct() 0 3 1
A getUserByUsername() 0 7 1
A passwordReset() 0 10 1
A isLoggedIn() 0 16 3
A getLevel() 0 8 2
A login() 0 28 5
A randomPassword() 0 13 3
A getLoggedUser() 0 9 2
A logout() 0 13 4
A register() 0 22 3
1
<?php
2
3
namespace Lepton\Authenticator;
4
5
use Lepton\Core\Application;
6
7
class UserAuthenticator
8
{
9
    private $config;
10
11
    public function __construct()
12
    {
13
        $this->config = Application::getAuthConfig();
14
15
    }
16
17
    public function getUserByUsername($username)
18
    {
19
        $userModel = $this->config->auth_model;
20
        $userUsernameField = $this->config->username_field;
21
        $arguments = [$userUsernameField => $username];
22
        $user = $userModel::get(...$arguments);
23
        return $user;
24
    }
25
26
    public function getUserById($id)
27
    {
28
        $userModel = $this->config->auth_model;
29
        $user = $userModel::get($id);
30
        return $user;
31
    }
32
33
34
    public function login($username, $password)
35
    {
36
        $user = $this->getUserByUsername($username);
37
38
        if (!$user) {
39
            return false;
40
        }
41
42
        $passwordField = $this->config->password_field;
43
        if (password_verify($password, $user->$passwordField)) {
44
            $session_hash = bin2hex(random_bytes(32));
45
            $_SESSION['user_id'] = $user->getPk();
46
            $_SESSION['session_hash'] = $session_hash;
47
            if($this->config->login_use_unique_hash) {
48
                $hashField = $this->config->hash_field;
49
                $user->$hashField = $session_hash;
50
                $user->save();
51
            }
52
            if(isset($this->config->access_field)){
53
                $accessField = $this->config->access_field;
54
                date_default_timezone_set('Europe/Rome');
55
                $user->$accessField = date('Y-m-d H:i:s', time());;
56
                $user->save();
57
            }
58
            return true;
59
        }
60
61
        return false;
62
    }
63
64
    public function isLoggedIn()
65
    {
66
67
        if (!isset($_SESSION['user_id'])) {
68
            return false;
69
        }
70
71
        $user_id = $_SESSION['user_id'];
72
        $user = $this->getUserById($user_id);
73
74
        //$hashField = $this->config->hash_field;
75
        if (!$user) {// || $user->$hashField !== $_SESSION['session_hash']) {
76
            return false;
77
        }
78
79
        return true;
80
    }
81
82
83
    public function getLoggedUser()
84
    {
85
        if (!isset($_SESSION['user_id'])) {
86
            return false;
87
        }
88
89
        $user_id = $_SESSION['user_id'];
90
        $user = $this->getUserById($user_id);
91
        return $user;
92
    }
93
94
95
96
    public function register($username, $password = null, $password_length = 6)
97
    {
98
        // Check if username is already taken
99
        if ($this->getUserByUsername($username)) {
100
            return false;
101
        }
102
103
        // Hash the password
104
        if (! $password) {
105
            $password = $this->randomPassword(length: $password_length);
106
        }
107
108
        $password_hash = password_hash($password, PASSWORD_DEFAULT);
109
110
        $userModel = $this->config->auth_model;
111
        $usernameField = $this->config->username_field;
112
        $passwordField = $this->config->password_field;
113
114
        $user = new $userModel();
115
        $user->$usernameField = $username;
116
        $user->$passwordField = $password_hash;
117
        return $user;
118
    }
119
120
121
    public function passwordReset($id, $length = 6)
122
    {
123
        $user = $this->getUserById($id);
124
        $passwordField = $this->config->password_field;
125
126
        $password = $this->randomPassword(length: $length);
127
        $password_hash = password_hash($password, PASSWORD_DEFAULT);
128
        $user->$passwordField = $password_hash;
129
        $user->save();
130
        return $password;
131
    }
132
133
    public function logout()
134
    {
135
        if ($this->isLoggedIn()) {
136
            if (isset($_SESSION['user_id'])) {
137
                if(isset($this->config->hash_field)) {
138
                    $user = $this->getUserById($_SESSION['user_id']);
139
                    $hashField = $this->config->hash_field;
140
                    $user->$hashField = "";
141
                    $user->save();
142
                }
143
            }
144
            session_unset();
145
            session_destroy();
146
        }
147
    }
148
149
    public function getLevel()
150
    {
151
        if ($this->isLoggedIn()) {
152
            $user = $this->getUserById($_SESSION['user_id']);
153
            $levelField = $this->config->level_field;
154
            return $user->$levelField;
155
        }
156
        return 0;
157
    }
158
159
160
    public function randomPassword(
161
        $length,
162
        $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
163
    ) {
164
        $str = '';
165
        $max = mb_strlen($keyspace, '8bit') - 1;
166
        if ($max < 1) {
167
            throw new \Exception('$keyspace must be at least two characters long');
168
        }
169
        for ($i = 0; $i < $length; ++$i) {
170
            $str .= $keyspace[random_int(0, $max)];
171
        }
172
        return $str;
173
    }
174
}
175