/
Authenticator.php
58 lines (42 loc) · 1.17 KB
/
Authenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
use Nette\Security as NS;
/**
* Users authenticator.
*/
class Authenticator extends Nette\Object implements NS\IAuthenticator
{
/** @var Nette\Database\Connection */
private $database;
public function __construct(Nette\Database\Connection $database)
{
$this->database = $database;
}
/**
* Performs an authentication
* @param array
* @return Nette\Security\Identity
* @throws Nette\Security\AuthenticationException
*/
public function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->database->table('users')->where('username', $username)->fetch();
if (!$row) {
throw new NS\AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
}
if ($row->password !== $this->calculateHash($password)) {
throw new NS\AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
}
unset($row->password);
return new NS\Identity($row->id, $row->role, $row->toArray());
}
/**
* Computes salted password hash.
* @param string
* @return string
*/
public function calculateHash($password)
{
return md5($password . str_repeat('*random salt*', 10));
}
}