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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Login;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Session\SessionNamespace;
use Piwik\Url;
class PasswordVerifier
{
public const VERIFY_VALID_FOR_MINUTES = 30;
public const VERIFY_REVALIDATE_X_MINUTES_LEFT = 15;
/**
* @var Date|null
*/
private $now;
private $enableRedirect = true;
/**
* @ignore
* tests only
*/
public function setDisableRedirect()
{
$this->enableRedirect = false;
}
private function getLoginSession()
{
return new SessionNamespace('Login');
}
public function isPasswordCorrect(
$userLogin,
#[\SensitiveParameter]
$password
) {
/**
* @ignore
* @internal
*/
Piwik::postEvent('Login.beforeLoginCheckAllowed');
/** @var \Piwik\Auth $authAdapter */
$authAdapter = StaticContainer::get('Piwik\Auth');
$authAdapter->setLogin($userLogin);
$authAdapter->setPasswordHash(null);// ensure authentication happens on password
$authAdapter->setPassword($password);
$authAdapter->setTokenAuth(null);// ensure authentication happens on password
$authResult = $authAdapter->authenticate();
if ($authResult->wasAuthenticationSuccessful()) {
return true;
}
/**
* @ignore
* @internal
*/
Piwik::postEvent('Login.recordFailedLoginAttempt');
return false;
}
public function hasPasswordVerifyBeenRequested()
{
$sessionNamespace = $this->getLoginSession();
return !empty($sessionNamespace->redirectParams);
}
public function forgetVerifiedPassword()
{
// call this method if you want the user to enter the password again after some action was finished which needed
// the password
$sessionNamespace = $this->getLoginSession();
unset($sessionNamespace->lastPasswordAuth);
unset($sessionNamespace->redirectParams);
}
/**
* @ignore
* tests only
*/
public function setNow(Date $now)
{
$this->now = $now;
}
private function getNow()
{
if ($this->now) {
return $this->now;
}
return Date::now();
}
public function setPasswordVerifiedCorrectly()
{
$sessionNamespace = $this->getLoginSession();
$sessionNamespace->lastPasswordAuth = $this->getNow()->getDatetime();
$sessionNamespace->setExpirationSeconds(self::VERIFY_VALID_FOR_MINUTES * 60, 'lastPasswordAuth');
$sessionNamespace->setExpirationSeconds(self::VERIFY_VALID_FOR_MINUTES * 60, 'redirectParams');
if ($this->enableRedirect) {
Url::redirectToUrl('index.php' . Url::getCurrentQueryStringWithParametersModified(
$sessionNamespace->redirectParams
));
}
}
public function hasBeenVerified()
{
$lastAuthValidTo = $this->getPasswordVerifyValidUpToDateIfVerified();
$now = $this->getNow();
if ($lastAuthValidTo && $now->isEarlier($lastAuthValidTo)) {
return true;
}
return false;
}
private function getPasswordVerifyValidUpToDateIfVerified()
{
$sessionNamespace = $this->getLoginSession();
if (!empty($sessionNamespace->lastPasswordAuth) && !empty($sessionNamespace->redirectParams)) {
$lastAuthValidTo = Date::factory($sessionNamespace->lastPasswordAuth)->addPeriod(self::VERIFY_VALID_FOR_MINUTES, 'minute');
return $lastAuthValidTo;
}
}
protected function hasBeenVerifiedAndHalfTimeValid()
{
$lastAuthValidTo = $this->getPasswordVerifyValidUpToDateIfVerified();
$now = $this->getNow()->addPeriod(self::VERIFY_REVALIDATE_X_MINUTES_LEFT, 'minute');
if ($lastAuthValidTo && $now->isEarlier($lastAuthValidTo)) {
return true;
}
return false;
}
/**
* Checks if the user has verified the password within the last 15 minutes. If not, the user will be redirected.
* The password verify will be valid for at least another 15 minutes giving the user some time to perform an action.
* See {@link requirePasswordVerified}
*
* @param $redirectParams
* @return null|true if password has been verified recently, will redirect if not
* @throws \Zend_Session_Exception
*/
public function requirePasswordVerifiedRecently($redirectParams)
{
if ($this->hasBeenVerifiedAndHalfTimeValid()) {
return true;
}
$this->initiatePasswordVerifyRedirect($redirectParams);
}
/**
* Checks if the user has verified the password within the last 30 minutes. If not, the user will be redirected.
* Please note that if the user performs an action afterwards, the password verify could be valid for only few more
* seconds or minutes and by the time the user confirms a certain action, the password verify may no longer be valid.
* If you want to ensure the password will be still valid for eg 15 minutes before the user performs some action,
* consider using {@link requirePasswordVerifiedRecently}.
*
* @param $redirectParams
* @return null|true if password has been verified, will redirect if not
* @throws \Zend_Session_Exception
*/
public function requirePasswordVerified($redirectParams)
{
if ($this->hasBeenVerified()) {
return true;
}
$this->initiatePasswordVerifyRedirect($redirectParams);
}
private function initiatePasswordVerifyRedirect($redirectParams)
{
$sessionNamespace = $this->getLoginSession();
$sessionNamespace->redirectParams = $redirectParams;
$sessionNamespace->setExpirationSeconds(self::VERIFY_VALID_FOR_MINUTES * 60 * 5, 'redirectParams');
if ($this->enableRedirect) {
Piwik::redirectToModule(Piwik::getLoginPluginName(), 'confirmPassword');
}
}
}
|