1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-08-04 15:17:28 +02:00

Re-use 'getUserDataByEmailAddress' in 'authenticateUserInternal'

This commit is contained in:
Marco
2017-02-16 08:56:44 +01:00
parent 8cc54473e3
commit f2561a1932

View File

@@ -772,59 +772,58 @@ class Auth {
private function authenticateUserInternal($password, $email, $rememberDuration = null) { private function authenticateUserInternal($password, $email, $rememberDuration = null) {
$email = self::validateEmailAddress($email); $email = self::validateEmailAddress($email);
// attempt to look up the account information using the specified email address
try { try {
$userData = $this->db->selectRow( $userData = $this->getUserDataByEmailAddress(
'SELECT id, email, password, verified, username FROM users WHERE email = ?', $email,
[ $email ] [ 'id', 'email', 'password', 'verified', 'username' ]
); );
} }
catch (Error $e) { // if there is no user with the specified email address
throw new DatabaseError(); catch (InvalidEmailException $e) {
// throttle this operation
$this->throttle(self::THROTTLE_ACTION_LOGIN);
$this->throttle(self::THROTTLE_ACTION_LOGIN, $email);
// and re-throw the exception
throw new InvalidEmailException();
} }
if (!empty($userData)) { $password = self::validatePassword($password);
$password = self::validatePassword($password);
if (password_verify($password, $userData['password'])) { if (password_verify($password, $userData['password'])) {
// if the password needs to be re-hashed to keep up with improving password cracking techniques // if the password needs to be re-hashed to keep up with improving password cracking techniques
if (password_needs_rehash($userData['password'], PASSWORD_DEFAULT)) { if (password_needs_rehash($userData['password'], PASSWORD_DEFAULT)) {
// create a new hash from the password and update it in the database // create a new hash from the password and update it in the database
$this->updatePassword($userData['id'], $password); $this->updatePassword($userData['id'], $password);
}
if ($userData['verified'] === 1) {
$this->onLoginSuccessful($userData['id'], $userData['email'], $userData['username'], false);
// continue to support the old parameter format
if ($rememberDuration === true) {
$rememberDuration = 60 * 60 * 24 * 28;
}
elseif ($rememberDuration === false) {
$rememberDuration = null;
} }
if ($userData['verified'] === 1) { if ($rememberDuration !== null) {
$this->onLoginSuccessful($userData['id'], $userData['email'], $userData['username'], false); $this->createRememberDirective($userData['id'], $rememberDuration);
// continue to support the old parameter format
if ($rememberDuration === true) {
$rememberDuration = 60 * 60 * 24 * 28;
}
elseif ($rememberDuration === false) {
$rememberDuration = null;
}
if ($rememberDuration !== null) {
$this->createRememberDirective($userData['id'], $rememberDuration);
}
return;
}
else {
throw new EmailNotVerifiedException();
} }
return;
} }
else { else {
$this->throttle(self::THROTTLE_ACTION_LOGIN); throw new EmailNotVerifiedException();
$this->throttle(self::THROTTLE_ACTION_LOGIN, $email);
throw new InvalidPasswordException();
} }
} }
else { else {
$this->throttle(self::THROTTLE_ACTION_LOGIN); $this->throttle(self::THROTTLE_ACTION_LOGIN);
$this->throttle(self::THROTTLE_ACTION_LOGIN, $email); $this->throttle(self::THROTTLE_ACTION_LOGIN, $email);
throw new InvalidEmailException(); throw new InvalidPasswordException();
} }
} }