2019-07-31 22:43:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2022-09-26 15:22:26 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* https://www.d3data.de
|
2019-07-31 22:43:34 +02:00
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
2022-09-26 15:22:26 +02:00
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
2019-07-31 22:43:34 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-07-31 22:43:34 +02:00
|
|
|
namespace D3\Totp\Modules\Application\Component;
|
|
|
|
|
|
|
|
use D3\Totp\Application\Model\d3totp;
|
2022-11-10 00:00:50 +01:00
|
|
|
use D3\Totp\Application\Model\d3totp_conf;
|
2019-07-31 22:43:34 +02:00
|
|
|
use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException;
|
2022-11-10 11:34:05 +01:00
|
|
|
use D3\Totp\Modules\Application\Model\d3_totp_user;
|
2019-07-31 22:43:34 +02:00
|
|
|
use Doctrine\DBAL\DBALException;
|
2022-11-09 12:03:16 +01:00
|
|
|
use InvalidArgumentException;
|
2019-07-31 22:43:34 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2019-08-07 00:15:54 +02:00
|
|
|
use OxidEsales\Eshop\Core\Session;
|
2022-11-09 10:18:31 +01:00
|
|
|
use OxidEsales\Eshop\Core\Utils;
|
2019-08-07 00:15:54 +02:00
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
2019-07-31 22:43:34 +02:00
|
|
|
|
|
|
|
class d3_totp_UserComponent extends d3_totp_UserComponent_parent
|
|
|
|
{
|
|
|
|
/**
|
2022-11-09 12:03:16 +01:00
|
|
|
* @param User $oUser
|
|
|
|
*
|
|
|
|
* @return string
|
2019-07-31 22:43:34 +02:00
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
2022-11-09 12:03:16 +01:00
|
|
|
protected function _afterLogin($oUser)
|
2019-07-31 22:43:34 +02:00
|
|
|
{
|
2022-11-09 12:03:16 +01:00
|
|
|
if (!$oUser instanceof User) {
|
|
|
|
throw oxNew( InvalidArgumentException::class, 'user argument must an instance of User class');
|
|
|
|
}
|
2019-07-31 22:43:34 +02:00
|
|
|
|
2022-11-09 12:03:16 +01:00
|
|
|
if ($oUser->getId()) {
|
2019-08-07 00:15:54 +02:00
|
|
|
$totp = $this->d3GetTotpObject();
|
2019-07-31 22:43:34 +02:00
|
|
|
$totp->loadByUserId($oUser->getId());
|
|
|
|
|
|
|
|
if ($totp->isActive()
|
2022-11-10 00:00:50 +01:00
|
|
|
&& !$this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_AUTH)
|
2019-07-31 22:43:34 +02:00
|
|
|
) {
|
2022-11-10 00:00:50 +01:00
|
|
|
$this->d3TotpGetSession()->setVariable(
|
|
|
|
d3totp_conf::SESSION_CURRENTCLASS,
|
2022-09-30 21:06:30 +02:00
|
|
|
$this->getParent()->getClassKey() != 'd3totplogin' ? $this->getParent()->getClassKey() : 'start'
|
|
|
|
);
|
2022-11-09 10:18:31 +01:00
|
|
|
|
2022-11-10 00:00:50 +01:00
|
|
|
$oUser->logout();
|
|
|
|
|
|
|
|
$this->d3TotpGetSession()->setVariable(d3totp_conf::SESSION_CURRENTUSER, $oUser->getId());
|
|
|
|
$this->d3TotpGetSession()->setVariable(
|
|
|
|
d3totp_conf::SESSION_NAVFORMPARAMS,
|
2019-07-31 22:43:34 +02:00
|
|
|
$this->getParent()->getViewConfig()->getNavFormParams()
|
|
|
|
);
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
$sUrl = Registry::getConfig()->getShopHomeUrl() . 'cl=d3totplogin';
|
2022-11-10 00:00:50 +01:00
|
|
|
$this->d3TotpGetUtils()->redirect($sUrl, false);
|
2019-07-31 22:43:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 12:03:16 +01:00
|
|
|
|
|
|
|
return parent::_afterLogin($oUser);
|
2019-07-31 22:43:34 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 00:15:54 +02:00
|
|
|
/**
|
|
|
|
* @return d3totp
|
|
|
|
*/
|
2022-11-09 10:18:31 +01:00
|
|
|
public function d3GetTotpObject(): d3totp
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
|
|
|
return oxNew(d3totp::class);
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:43:34 +02:00
|
|
|
/**
|
|
|
|
* @throws DBALException
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpCheckTotpLogin()
|
2019-07-31 22:43:34 +02:00
|
|
|
{
|
2022-11-14 00:25:20 +01:00
|
|
|
$sTotp = implode('', Registry::getRequest()->getRequestEscapedParameter('d3totp'));
|
2019-07-31 22:43:34 +02:00
|
|
|
|
2022-11-10 11:34:05 +01:00
|
|
|
/** @var d3_totp_user $oUser */
|
2019-07-31 22:43:34 +02:00
|
|
|
$oUser = oxNew(User::class);
|
2022-11-10 11:34:05 +01:00
|
|
|
$sUserId = Registry::getSession()->getVariable(d3totp_conf::SESSION_CURRENTUSER);
|
2019-07-31 22:43:34 +02:00
|
|
|
$oUser->load($sUserId);
|
|
|
|
|
2019-08-07 00:15:54 +02:00
|
|
|
$totp = $this->d3GetTotpObject();
|
2019-07-31 22:43:34 +02:00
|
|
|
$totp->loadByUserId($sUserId);
|
|
|
|
|
|
|
|
try {
|
2022-11-10 00:00:50 +01:00
|
|
|
if (!$this->d3TotpIsNoTotpOrNoLogin($totp) && $this->d3TotpHasValidTotp($sTotp, $totp)) {
|
2022-11-09 10:18:31 +01:00
|
|
|
// relogin, don't extract from this try block
|
2022-11-10 11:34:05 +01:00
|
|
|
$this->d3TotpGetSession()->setVariable(d3totp_conf::SESSION_AUTH, $oUser->getId());
|
|
|
|
$this->d3TotpGetSession()->setVariable(d3totp_conf::OXID_FRONTEND_AUTH, $oUser->getId());
|
2022-11-09 10:18:31 +01:00
|
|
|
$this->setUser(null);
|
|
|
|
$this->setLoginStatus(USER_LOGIN_SUCCESS);
|
|
|
|
$this->_afterLogin($oUser);
|
|
|
|
|
2019-07-31 22:43:34 +02:00
|
|
|
$this->d3TotpClearSessionVariables();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (d3totp_wrongOtpException $oEx) {
|
2022-11-10 00:00:50 +01:00
|
|
|
$this->d3TotpGetUtilsView()->addErrorToDisplay($oEx, false, false, "", 'd3totplogin');
|
2019-07-31 22:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'd3totplogin';
|
|
|
|
}
|
|
|
|
|
2019-08-07 00:15:54 +02:00
|
|
|
/**
|
|
|
|
* @return UtilsView
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpGetUtilsView()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
|
|
|
return Registry::getUtilsView();
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:18:31 +01:00
|
|
|
/**
|
|
|
|
* @return Utils
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpGetUtils()
|
2022-11-09 10:18:31 +01:00
|
|
|
{
|
|
|
|
return Registry::getUtils();
|
|
|
|
}
|
|
|
|
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpCancelTotpLogin()
|
2019-08-02 00:23:21 +02:00
|
|
|
{
|
|
|
|
$this->d3TotpClearSessionVariables();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:43:34 +02:00
|
|
|
/**
|
|
|
|
* @param d3totp $totp
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpIsNoTotpOrNoLogin($totp)
|
2019-07-31 22:43:34 +02:00
|
|
|
{
|
2022-11-10 00:00:50 +01:00
|
|
|
return false == Registry::getSession()->getVariable(d3totp_conf::SESSION_CURRENTUSER)
|
2019-07-31 22:43:34 +02:00
|
|
|
|| false == $totp->isActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sTotp
|
|
|
|
* @param d3totp $totp
|
|
|
|
* @return bool
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws d3totp_wrongOtpException
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpHasValidTotp($sTotp, $totp)
|
2019-07-31 22:43:34 +02:00
|
|
|
{
|
2022-11-10 00:00:50 +01:00
|
|
|
return Registry::getSession()->getVariable(d3totp_conf::SESSION_AUTH) ||
|
2022-11-13 23:40:54 +01:00
|
|
|
$totp->verify($sTotp);
|
2019-07-31 22:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function d3TotpClearSessionVariables()
|
|
|
|
{
|
2022-11-10 00:00:50 +01:00
|
|
|
$this->d3TotpGetSession()->deleteVariable(d3totp_conf::SESSION_CURRENTCLASS);
|
|
|
|
$this->d3TotpGetSession()->deleteVariable(d3totp_conf::SESSION_CURRENTUSER);
|
|
|
|
$this->d3TotpGetSession()->deleteVariable(d3totp_conf::SESSION_NAVFORMPARAMS);
|
2019-08-07 00:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Session
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function d3TotpGetSession()
|
2019-08-07 00:15:54 +02:00
|
|
|
{
|
|
|
|
return Registry::getSession();
|
2019-07-31 22:43:34 +02:00
|
|
|
}
|
2022-09-30 21:06:30 +02:00
|
|
|
}
|