oxtotp/src/Modules/Application/Controller/Admin/d3_totp_LoginController.php

171 lines
4.2 KiB
PHP
Raw Normal View History

2018-10-21 20:54:58 +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
2018-10-21 20:54:58 +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
2018-10-21 20:54:58 +02:00
*/
namespace D3\Totp\Modules\Application\Controller\Admin;
use D3\Totp\Application\Model\d3totp;
use D3\Totp\Application\Model\d3backupcodelist;
2018-10-21 20:54:58 +02:00
use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException;
use Doctrine\DBAL\DBALException;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Registry;
2019-08-07 23:51:48 +02:00
use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\UtilsView;
2018-10-21 20:54:58 +02:00
class d3_totp_LoginController extends d3_totp_LoginController_parent
{
/**
* @return string
* @throws DBALException
* @throws DatabaseConnectionException
*/
public function render()
{
2019-08-07 23:51:48 +02:00
$auth = $this->d3GetSession()->getVariable("auth");
2018-10-21 20:54:58 +02:00
$return = parent::render();
2019-08-07 23:51:48 +02:00
$totp = $this->d3GetTotpObject();
2018-10-21 20:54:58 +02:00
$totp->loadByUserId($auth);
if ($auth
&& $totp->isActive()
2019-08-07 23:51:48 +02:00
&& false == $this->d3GetSession()->getVariable(d3totp::TOTP_SESSION_VARNAME)
2018-10-21 20:54:58 +02:00
) {
// set auth as secured parameter;
2019-08-07 23:51:48 +02:00
$this->d3GetSession()->setVariable("auth", $auth);
2018-10-21 20:54:58 +02:00
$this->addTplParam('request_totp', true);
}
return $return;
}
2019-08-07 23:51:48 +02:00
/**
* @return d3totp
*/
public function d3GetTotpObject()
{
return oxNew(d3totp::class);
}
/**
* @return d3backupcodelist
*/
public function d3GetBackupCodeListObject()
{
return oxNew(d3backupcodelist::class);
}
/**
* @return UtilsView
*/
public function d3GetUtilsView()
{
return Registry::getUtilsView();
}
/**
* @return Session
*/
public function d3GetSession()
{
return Registry::getSession();
}
2018-10-21 20:54:58 +02:00
/**
* @return mixed|string
* @throws DBALException
* @throws DatabaseConnectionException
*/
public function checklogin()
{
$sTotp = Registry::getRequest()->getRequestEscapedParameter('d3totp', true);
2019-08-07 23:51:48 +02:00
$totp = $this->d3GetTotpObject();
2018-10-21 20:54:58 +02:00
$totp->loadByUserId(Registry::getSession()->getVariable("auth"));
$return = 'login';
try {
if ($this->isNoTotpOrNoLogin($totp)) {
$return = parent::checklogin();
} elseif ($this->hasValidTotp($sTotp, $totp)) {
2019-08-07 23:51:48 +02:00
$this->d3GetSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, $sTotp);
2018-10-21 20:54:58 +02:00
$return = "admin_start";
}
} catch (d3totp_wrongOtpException $oEx) {
2019-08-07 23:51:48 +02:00
$this->d3GetUtilsView()->addErrorToDisplay($oEx);
2018-10-21 20:54:58 +02:00
}
return $return;
}
/**
* @return string|void
* @throws DatabaseConnectionException
*/
public function getBackupCodeCountMessage()
{
2019-08-07 23:51:48 +02:00
$oBackupCodeList = $this->d3GetBackupCodeListObject();
$iCount = $oBackupCodeList->getAvailableCodeCount(Registry::getSession()->getVariable("auth"));
if ($iCount < 4) {
return sprintf(
Registry::getLang()->translateString('D3_TOTP_AVAILBACKUPCODECOUNT'),
$iCount
);
};
return;
}
2018-10-21 20:54:58 +02:00
/**
* @param d3totp $totp
* @return bool
*/
public function isNoTotpOrNoLogin($totp)
{
2019-08-07 23:51:48 +02:00
return false == $this->d3GetSession()->getVariable("auth")
2018-10-21 20:54:58 +02:00
|| false == $totp->isActive();
}
/**
* @param string $sTotp
* @param d3totp $totp
* @return bool
2019-07-28 00:07:16 +02:00
* @throws DatabaseConnectionException
2018-10-21 20:54:58 +02:00
* @throws d3totp_wrongOtpException
*/
public function hasValidTotp($sTotp, $totp)
{
return Registry::getSession()->getVariable(d3totp::TOTP_SESSION_VARNAME) ||
(
$sTotp && $totp->verify($sTotp)
);
}
public function d3CancelLogin()
{
2019-08-07 23:51:48 +02:00
$oUser = $this->d3GetUserObject();
2018-10-21 20:54:58 +02:00
$oUser->logout();
}
2019-08-07 23:51:48 +02:00
/**
* @return User
*/
public function d3GetUserObject()
{
return oxNew(User::class);
}
2018-10-17 12:50:10 +02:00
}