misuzu/public/auth/password.php

151 lines
5.1 KiB
PHP
Raw Normal View History

2019-03-08 00:35:53 +00:00
<?php
namespace Misuzu;
2020-05-21 15:05:30 +00:00
use Misuzu\AuditLog;
use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException;
2020-05-25 14:15:17 +00:00
use Misuzu\Users\UserLoginAttempt;
2020-05-29 15:30:49 +00:00
use Misuzu\Users\UserRecoveryToken;
use Misuzu\Users\UserRecoveryTokenNotFoundException;
use Misuzu\Users\UserRecoveryTokenCreationFailedException;
2020-05-25 19:58:06 +00:00
use Misuzu\Users\UserSession;
2019-10-02 19:02:22 +00:00
2019-03-08 00:35:53 +00:00
require_once '../../misuzu.php';
2020-05-25 19:58:06 +00:00
if(UserSession::hasCurrent()) {
url_redirect('settings-account');
2019-03-08 00:35:53 +00:00
return;
}
2019-03-18 21:30:19 +00:00
$reset = !empty($_POST['reset']) && is_array($_POST['reset']) ? $_POST['reset'] : [];
$forgot = !empty($_POST['forgot']) && is_array($_POST['forgot']) ? $_POST['forgot'] : [];
$userId = !empty($reset['user']) ? (int)$reset['user'] : (
!empty($_GET['user']) ? (int)$_GET['user'] : 0
);
2019-03-08 00:35:53 +00:00
if($userId > 0)
try {
$userInfo = User::byId($userId);
} catch(UserNotFoundException $ex) {
url_redirect('auth-forgot');
return;
}
2019-03-08 00:35:53 +00:00
$notices = [];
$siteIsPrivate = Config::get('private.enable', Config::TYPE_BOOL);
$canResetPassword = $siteIsPrivate ? Config::get('private.allow_password_reset', Config::TYPE_BOOL, true) : true;
2020-05-25 14:15:17 +00:00
$remainingAttempts = UserLoginAttempt::remaining();
2019-03-08 00:35:53 +00:00
2019-06-10 17:04:53 +00:00
while($canResetPassword) {
if(!empty($reset) && $userId > 0) {
2019-12-11 18:10:54 +00:00
if(!CSRF::validateRequest()) {
2019-03-08 00:35:53 +00:00
$notices[] = 'Was unable to verify the request, please try again!';
break;
}
2019-03-18 21:30:19 +00:00
$verificationCode = !empty($reset['verification']) && is_string($reset['verification']) ? $reset['verification'] : '';
2019-03-08 00:35:53 +00:00
2020-05-29 15:30:49 +00:00
try {
$tokenInfo = UserRecoveryToken::byToken($verificationCode);
} catch(UserRecoveryTokenNotFoundException $ex) {
unset($tokenInfo);
}
if(empty($tokenInfo) || !$tokenInfo->isValid() || $tokenInfo->getUserId() !== $userInfo->getId()) {
2019-03-08 00:35:53 +00:00
$notices[] = 'Invalid verification code!';
break;
}
2019-03-18 21:30:19 +00:00
$password = !empty($reset['password']) && is_array($reset['password']) ? $reset['password'] : [];
$passwordNew = !empty($password['new']) && is_string($password['new']) ? $password['new'] : '';
$passwordConfirm = !empty($password['confirm']) && is_string($password['confirm']) ? $password['confirm'] : '';
2019-03-08 00:35:53 +00:00
2019-06-10 17:04:53 +00:00
if(empty($passwordNew) || empty($passwordConfirm)
2019-03-08 00:35:53 +00:00
|| $passwordNew !== $passwordConfirm) {
$notices[] = "Password confirmation failed!";
break;
}
if(User::validatePassword($passwordNew) !== '') {
2019-03-08 00:35:53 +00:00
$notices[] = 'Your password is too weak!';
break;
}
2020-06-07 20:37:03 +00:00
// also disables two factor auth to prevent getting locked out of account entirely
// this behaviour should really be replaced with recovery keys...
2020-06-07 20:37:03 +00:00
$userInfo->setPassword($passwordNew)
->removeTOTPKey()
->save();
AuditLog::create(AuditLog::PASSWORD_RESET, [], $userInfo);
2020-05-29 15:30:49 +00:00
$tokenInfo->invalidate();
2019-03-08 00:35:53 +00:00
url_redirect('auth-login', ['redirect' => '/']);
2019-03-08 00:35:53 +00:00
return;
}
2019-06-10 17:04:53 +00:00
if(!empty($forgot)) {
2019-12-11 18:10:54 +00:00
if(!CSRF::validateRequest()) {
2019-03-08 00:35:53 +00:00
$notices[] = 'Was unable to verify the request, please try again!';
break;
}
2019-06-10 17:04:53 +00:00
if(empty($forgot['email']) || !is_string($forgot['email'])) {
2019-03-08 00:35:53 +00:00
$notices[] = "You didn't supply an e-mail address.";
break;
}
2019-06-10 17:04:53 +00:00
if($remainingAttempts < 1) {
2019-03-08 00:35:53 +00:00
$notices[] = "There are too many failed login attempts from your IP address, please try again later.";
break;
}
try {
$forgotUser = User::byEMailAddress($forgot['email']);
} catch(UserNotFoundException $ex) {
unset($forgotUser);
}
2019-03-08 00:35:53 +00:00
if(empty($forgotUser) || $forgotUser->isDeleted()) {
2019-03-08 00:35:53 +00:00
$notices[] = "This e-mail address is not registered with us.";
break;
}
2020-05-29 15:30:49 +00:00
try {
$tokenInfo = UserRecoveryToken::byUserAndRemoteAddress($forgotUser);
} catch(UserRecoveryTokenNotFoundException $ex) {
$tokenInfo = UserRecoveryToken::create($forgotUser);
2019-03-08 00:35:53 +00:00
2019-12-03 19:09:18 +00:00
$recoveryMessage = Mailer::template('password-recovery', [
'username' => $forgotUser->getUsername(),
2020-05-29 15:30:49 +00:00
'token' => $tokenInfo->getToken(),
2019-12-03 19:09:18 +00:00
]);
2019-03-08 00:35:53 +00:00
2019-12-03 19:09:18 +00:00
$recoveryMail = Mailer::sendMessage(
[$forgotUser->getEMailAddress() => $forgotUser->getUsername()],
2019-12-03 19:09:18 +00:00
$recoveryMessage['subject'], $recoveryMessage['message']
2019-03-08 00:35:53 +00:00
);
2019-12-03 19:09:18 +00:00
if(!$recoveryMail) {
2019-03-08 00:35:53 +00:00
$notices[] = "Failed to send reset email, please contact the administrator.";
2020-05-29 15:30:49 +00:00
$tokenInfo->invalidate();
2019-03-08 00:35:53 +00:00
break;
}
}
url_redirect('auth-reset', ['user' => $forgotUser->getId()]);
2019-03-08 00:35:53 +00:00
return;
}
break;
}
Template::render(isset($userInfo) ? 'auth.password_reset' : 'auth.password_forgot', [
2019-03-08 00:35:53 +00:00
'password_notices' => $notices,
2019-03-18 21:30:19 +00:00
'password_email' => !empty($forget['email']) && is_string($forget['email']) ? $forget['email'] : '',
2019-03-08 00:35:53 +00:00
'password_attempts_remaining' => $remainingAttempts,
'password_user' => $userInfo ?? null,
2019-03-08 00:35:53 +00:00
'password_verification' => $verificationCode ?? '',
]);