/**
* Performs an authentication.
* @param array $credentials
* @return Identity
* @throws AuthenticationException
*/
public function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->db->select('*')->from(self::TABLE_NAME)->where(self::COLUMN_NAME . ' = %s', $username)->fetch();
if (!$row) {
throw new AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
} elseif (!Passwords::verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
throw new AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
} elseif (Passwords::needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
$row[self::COLUMN_PASSWORD_HASH] = Passwords::hash($password);
}
$arr = $row->toArray();
unset($arr[self::COLUMN_PASSWORD_HASH]);
return new Identity($row[self::COLUMN_ID], $row[self::COLUMN_ROLE], $arr);
}