Completed
Push — master ( 5139b6...b16139 )
by Sebastian
02:13
created

Authenticate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 201
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isLogged() 0 4 1
A isNotLogged() 0 4 1
A getLoginData() 0 4 1
A login() 0 20 3
A logout() 0 11 1
A refresh() 0 21 3
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authentication;
13
14
use Linna\Session\Session;
15
16
/**
17
 * Provide methods for manage basic user authentication system. Checks for
18
 * correct login cover only the user name and the password.
19
 */
20
class Authenticate
21
{
22
    /**
23
     * @var array Login status
24
     */
25
    private $data = ['user_name'=>''];
26
27
    /**
28
     * @var bool Indicate login status, true or false
29
     */
30
    private $logged = false;
31
32
    /**
33
     * @var Session Session class
34
     */
35
    private $sessionInstance;
36
37
    /**
38
     * @var Password Password class
39
     */
40
    private $password;
41
42
    /**
43
     * Class constructor.
44
     * <pre><code class="php">use Linna\Session\Session;
45
     * use Linna\Auth\Password;
46
     *
47
     * $session = new Session();
48
     * $password = new Password();
49
     *
50
     * $auth = new Authenticate($session, $password);
51
     * </code></pre>
52
     *
53
     * @param Session  $session  Session class instance.
54
     * @param Password $password Password class instance.
55
     */
56 10
    public function __construct(Session $session, Password $password)
57
    {
58 10
        $this->password = $password;
59 10
        $this->sessionInstance = $session;
60 10
        $this->logged = $this->refresh();
61 10
    }
62
63
    /**
64
     * Utilize this method for check if an user in the current session,
65
     * is currently logged in.
66
     * <pre><code class="php">$auth = new Authenticate($session, $password);
67
     *
68
     * if ($auth->isLogged()) {
69
     *     //do actions
70
     * }
71
     * </code></pre>
72
     *
73
     * @return bool
74
     */
75 6
    public function isLogged() : bool
76
    {
77 6
        return $this->logged;
78
    }
79
80
    /**
81
     * Opposite to isLogged() method.
82
     *
83
     * Utilize this method for check if an user in the current session,
84
     * is currently not logged in.
85
     * <pre><code class="php">$auth = new Authenticate($session, $password);
86
     *
87
     * if ($auth->isNotLogged()) {
88
     *     //redirect or other action
89
     * }
90
     *
91
     * //do actions
92
     * </code></pre>
93
     *
94
     * @return bool
95
     */
96 3
    public function isNotLogged() : bool
97
    {
98 3
        return !$this->logged;
99
    }
100
101
    /**
102
     * Return array containing login data.
103
     * <pre><code class="php">$auth = new Authenticate($session, $password);
104
     *
105
     * $data = $auth->getLoginData();
106
     *
107
     * //var_dump result
108
     * //after session start and login, session data appear like below array:
109
     * //[
110
     * //    'time' => 1479641396
111
     * //    'expire' => 1800
112
     * //    'loginTime' => 1479641395
113
     * //    'login' => [
114
     * //        'login' => true
115
     * //        'user_id' => 1
116
     * //        'user_name' => 'root'
117
     * //    ]
118
     * //]
119
     * var_dump($data);
120
     * </code></pre>
121
     *
122
     * @return array
123
     */
124 4
    public function getLoginData() : array
125
    {
126 4
        return $this->data;
127
    }
128
129
    /**
130
     * Try to attemp login with the informations passed by param.
131
     *
132
     * <pre><code class="php">$user = ''; //user from login page form
133
     * $password = ''; //password from login page form
134
     *
135
     * $storedUser = ''; //user from stored informations
136
     * $storedPassword = ''; //password hash from stored informations
137
     * $storedId = ''; //user id from stored informations
138
     *
139
     * $auth = new Authenticate($session, $password);
140
     * $auth->login($user, $password, $storedUser, $storedPassword, $storedId);
141
     *
142
     * //other operation after login
143
     * </code></pre>
144
     *
145
     * @param string $userName
146
     * @param string $password
147
     * @param string $storedUserName
148
     * @param string $storedPassword
149
     * @param int    $storedId
150
     *
151
     * @return bool
152
     */
153 6
    public function login(string $userName, string $password, string $storedUserName = '', string $storedPassword = '', int $storedId = 0): bool
154
    {
155 6
        if ($userName === $storedUserName && $this->password->verify($password, $storedPassword)) {
156
            //write valid login on session
157 5
            $this->sessionInstance->loginTime = time();
158 5
            $this->sessionInstance->login = [
159 5
                'login'     => true,
160 5
                'user_id'   => $storedId,
161 5
                'user_name' => $storedUserName,
162
            ];
163
164
            //regenerate session id
165 5
            $this->sessionInstance->regenerate();
166 5
            $this->logged = true;
167
168 5
            return true;
169
        }
170
171 1
        return false;
172
    }
173
174
    /**
175
     * Do logout and delete login information from session.
176
     * <pre><code class="php">$auth = new Authenticate($session, $password);
177
     * $auth->logout();
178
     * </code></pre>
179
     *
180
     * @return bool
181
     */
182 2
    public function logout(): bool
183
    {
184
        //remove login data from session
185 2
        unset($this->sessionInstance->login, $this->sessionInstance->loginTime);
186
187
        //regenerate session id
188 2
        $this->sessionInstance->regenerate();
189 2
        $this->logged = false;
190
191 2
        return true;
192
    }
193
194
    /**
195
     * Check if user is logged, get login data from session and update it.
196
     *
197
     * @return bool
198
     */
199 10
    private function refresh(): bool
200
    {
201
        //check for login data on in current session
202 10
        if (!isset($this->sessionInstance->login)) {
203 10
            return false;
204
        }
205
206
        //take time
207 3
        $time = time();
208
209
        //check if login expired
210 3
        if (($this->sessionInstance->loginTime + $this->sessionInstance->expire) < $time) {
211 2
            return false;
212
        }
213
214
        //update login data
215 2
        $this->sessionInstance->loginTime = $time;
216 2
        $this->data = $this->sessionInstance->login;
217
218 2
        return true;
219
    }
220
}
221