Completed
Push — master ( 6adefe...4d2a94 )
by Sebastian
03:17
created

Authentication::isNotLogged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, 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 Authentication
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 Password Password class.
34
     */
35
    private $password;
36
37
    /**
38
     * @var Session Session class.
39
     */
40
    protected $session;
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 Authentication($session, $password);
51
     * </code></pre>
52
     *
53
     * @param Session  $session  Session class instance.
54
     * @param Password $password Password class instance.
55
     */
56 45
    public function __construct(Session $session, Password $password)
57
    {
58 45
        $this->password = $password;
59 45
        $this->session = $session;
60 45
        $this->logged = $this->refresh();
61 45
    }
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 Authentication($session, $password);
67
     *
68
     * if ($auth->isLogged()) {
69
     *     //do actions
70
     * }
71
     * </code></pre>
72
     *
73
     * @return bool True if logged false if no.
74
     */
75 13
    public function isLogged(): bool
76
    {
77 13
        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 Authentication($session, $password);
86
     *
87
     * if ($auth->isNotLogged()) {
88
     *     //redirect or other action
89
     * }
90
     *
91
     * //do actions
92
     * </code></pre>
93
     *
94
     * @return bool True if not logged false if logged.
95
     */
96 10
    public function isNotLogged(): bool
97
    {
98 10
        return !$this->logged;
99
    }
100
101
    /**
102
     * Return array containing login data.
103
     * <pre><code class="php">$auth = new Authentication($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 Login data.
123
     */
124 5
    public function getLoginData(): array
125
    {
126 5
        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 Authentication($session, $password);
140
     * $auth->login($user, $password, $storedUser, $storedPassword, $storedId);
141
     *
142
     * //other operation after login
143
     * </code></pre>
144
     *
145
     * @param string $userName       User name from browser input.
146
     * @param string $password       Password from browser input.
147
     * @param string $storedUserName User name from persistent storage.
148
     * @param string $storedPassword Password from persistent storage.
149
     * @param int    $storedId       User id from persistent storage.
150
     *
151
     * @return bool
152
     */
153 41
    public function login(string $userName, string $password, string $storedUserName = '', string $storedPassword = '', int $storedId): bool
154
    {
155 41
        if ($userName === $storedUserName && $this->password->verify($password, $storedPassword)) {
156
            //write valid login on session
157 12
            $this->session->loginTime = time();
158 12
            $this->session->login = [
159 12
                'login'     => true,
160 12
                'user_id'   => $storedId,
161 12
                'user_name' => $storedUserName,
162
            ];
163
164
            //update login data
165 12
            $this->data = $this->session->login;
166
167
            //regenerate session id
168 12
            $this->session->regenerate();
169 12
            $this->logged = true;
170
171 12
            return true;
172
        }
173
174 29
        return false;
175
    }
176
177
    /**
178
     * Do logout and delete login information from session.
179
     * <pre><code class="php">$auth = new Authentication($session, $password);
180
     * $auth->logout();
181
     * </code></pre>
182
     *
183
     * @return bool True if logout is done.
184
     */
185 2
    public function logout(): bool
186
    {
187
        //remove login data from session
188 2
        unset($this->session->login, $this->session->loginTime);
189
190
        //regenerate session id
191 2
        $this->session->regenerate();
192 2
        $this->logged = false;
193
194 2
        return true;
195
    }
196
197
    /**
198
     * Check if user is logged, get login data from session and update it.
199
     *
200
     * @return bool True if refresh is done false if no.
201
     */
202 45
    private function refresh(): bool
203
    {
204
        //check for login data on in current session
205 45
        if (empty($this->session->login)) {
206 45
            return false;
207
        }
208
209
        //take time
210 10
        $time = time();
211
212
        //check if login expired
213 10
        if (($this->session->loginTime + $this->session->expire) < $time) {
214 6
            return false;
215
        }
216
217
        //update login data
218 8
        $this->session->loginTime = $time;
219 8
        $this->data = $this->session->login;
220
221 8
        return true;
222
    }
223
}
224