2022-11-10 00:00:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* https://www.d3data.de
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace D3\Totp\Application\Controller\Admin;
|
|
|
|
|
|
|
|
use D3\Totp\Application\Model\d3backupcodelist;
|
|
|
|
use D3\Totp\Application\Model\d3totp;
|
|
|
|
use D3\Totp\Application\Model\d3totp_conf;
|
|
|
|
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;
|
2022-11-10 00:00:50 +01:00
|
|
|
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
|
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2022-11-10 16:45:57 +01:00
|
|
|
use OxidEsales\Eshop\Core\Session;
|
2022-11-10 00:00:50 +01:00
|
|
|
use OxidEsales\Eshop\Core\Utils;
|
2022-11-10 16:45:57 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-11-10 00:00:50 +01:00
|
|
|
|
|
|
|
class d3totpadminlogin extends AdminController
|
|
|
|
{
|
|
|
|
protected $_sThisTemplate = 'd3totpadminlogin.tpl';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function _authorize(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:45:57 +01:00
|
|
|
/**
|
|
|
|
* @return d3totp|mixed
|
|
|
|
*/
|
|
|
|
public function d3TotpGetTotpObject()
|
|
|
|
{
|
|
|
|
return oxNew(d3totp::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
|
|
|
protected function isTotpIsNotRequired(): bool
|
|
|
|
{
|
|
|
|
$user = $this->d3TotpGetUserObject();
|
|
|
|
$userId = $user->d3TotpGetCurrentUser();
|
|
|
|
|
|
|
|
$totp = $this->d3TotpGetTotpObject();
|
|
|
|
$totp->loadByUserId($userId);
|
|
|
|
|
|
|
|
return $this->d3TotpGetSession()->hasVariable(d3totp_conf::SESSION_AUTH) ||
|
|
|
|
!$totp->isActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isTotpLoginNotPossible(): bool
|
|
|
|
{
|
|
|
|
return !$this->d3TotpGetSession()->hasVariable(d3totp_conf::OXID_ADMIN_AUTH) &&
|
|
|
|
!$this->d3TotpGetSession()->hasVariable(d3totp_conf::SESSION_CURRENTUSER);
|
|
|
|
}
|
|
|
|
|
2022-11-10 00:00:50 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
2022-11-10 16:45:57 +01:00
|
|
|
* @throws DatabaseConnectionException
|
2022-11-10 00:00:50 +01:00
|
|
|
*/
|
|
|
|
public function render(): string
|
|
|
|
{
|
2022-11-10 16:45:57 +01:00
|
|
|
if ($this->isTotpIsNotRequired()) {
|
|
|
|
$this->d3TotpGetUtils()->redirect('index.php?cl=admin_start');
|
|
|
|
} elseif ($this->isTotpLoginNotPossible()) {
|
|
|
|
$this->d3TotpGetUtils()->redirect('index.php?cl=login');
|
2022-11-10 00:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent::render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return d3backupcodelist
|
|
|
|
*/
|
2022-11-10 16:45:57 +01:00
|
|
|
public function d3GetBackupCodeListObject(): d3backupcodelist
|
2022-11-10 00:00:50 +01:00
|
|
|
{
|
|
|
|
return oxNew(d3backupcodelist::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|void
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
|
|
|
public function getBackupCodeCountMessage()
|
|
|
|
{
|
2022-11-10 11:34:05 +01:00
|
|
|
/** @var d3_totp_user $user */
|
|
|
|
$user = oxNew(User::class);
|
|
|
|
$userId = $user->d3TotpGetCurrentUser();
|
|
|
|
|
2022-11-10 00:00:50 +01:00
|
|
|
$oBackupCodeList = $this->d3GetBackupCodeListObject();
|
2022-11-10 11:34:05 +01:00
|
|
|
$iCount = $oBackupCodeList->getAvailableCodeCount($userId);
|
2022-11-10 00:00:50 +01:00
|
|
|
|
|
|
|
if ($iCount < 4) {
|
|
|
|
return sprintf(
|
|
|
|
Registry::getLang()->translateString('D3_TOTP_AVAILBACKUPCODECOUNT'),
|
|
|
|
$iCount
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-10 16:45:57 +01:00
|
|
|
* @return string
|
2022-11-10 00:00:50 +01:00
|
|
|
*/
|
2022-11-10 16:45:57 +01:00
|
|
|
public function d3CancelLogin(): string
|
2022-11-10 00:00:50 +01:00
|
|
|
{
|
2022-11-10 16:45:57 +01:00
|
|
|
$oUser = $this->d3TotpGetUserObject();
|
|
|
|
$oUser->logout();
|
|
|
|
return "login";
|
2022-11-10 00:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-10 16:45:57 +01:00
|
|
|
* @return d3_totp_user
|
2022-11-10 00:00:50 +01:00
|
|
|
*/
|
2022-11-10 16:45:57 +01:00
|
|
|
public function d3TotpGetUserObject(): d3_totp_user
|
2022-11-10 00:00:50 +01:00
|
|
|
{
|
|
|
|
return oxNew(User::class);
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:34:05 +01:00
|
|
|
/**
|
|
|
|
* @return string|void
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
2022-11-10 00:00:50 +01:00
|
|
|
public function checklogin()
|
|
|
|
{
|
2022-11-10 16:45:57 +01:00
|
|
|
$session = $this->d3TotpGetSession();
|
2022-11-10 11:34:05 +01:00
|
|
|
/** @var d3_totp_user $user */
|
|
|
|
$user = oxNew(User::class);
|
|
|
|
$userId = $user->d3TotpGetCurrentUser();
|
2022-11-10 00:00:50 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$sTotp = Registry::getRequest()->getRequestEscapedParameter('d3totp');
|
|
|
|
|
2022-11-10 16:45:57 +01:00
|
|
|
$totp = $this->d3TotpGetTotpObject();
|
2022-11-10 00:00:50 +01:00
|
|
|
$totp->loadByUserId($userId);
|
|
|
|
|
|
|
|
$this->d3TotpHasValidTotp($sTotp, $totp);
|
|
|
|
|
|
|
|
$adminProfiles = $session->getVariable("aAdminProfiles");
|
|
|
|
|
|
|
|
$session->initNewSession();
|
|
|
|
$session->setVariable("aAdminProfiles", $adminProfiles);
|
2022-11-10 11:34:05 +01:00
|
|
|
$session->setVariable(d3totp_conf::OXID_ADMIN_AUTH, $userId);
|
|
|
|
$session->setVariable(d3totp_conf::SESSION_AUTH, $userId);
|
|
|
|
$session->deleteVariable(d3totp_conf::SESSION_CURRENTUSER);
|
2022-11-10 00:00:50 +01:00
|
|
|
|
|
|
|
return "admin_start";
|
|
|
|
} catch (d3totp_wrongOtpException $e) {
|
|
|
|
Registry::getUtilsView()->addErrorToDisplay($e);
|
2022-11-10 16:45:57 +01:00
|
|
|
$this->getLogger()->error($e->getMessage(), ['UserId' => $userId]);
|
|
|
|
$this->getLogger()->debug($e->getTraceAsString());
|
2022-11-10 00:00:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-10 16:45:57 +01:00
|
|
|
* @param string|null $sTotp
|
2022-11-10 00:00:50 +01:00
|
|
|
* @param d3totp $totp
|
|
|
|
* @return bool
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws d3totp_wrongOtpException
|
|
|
|
*/
|
2022-11-10 16:45:57 +01:00
|
|
|
public function d3TotpHasValidTotp(string $sTotp = null, d3totp $totp): bool
|
2022-11-10 00:00:50 +01:00
|
|
|
{
|
2022-11-10 16:45:57 +01:00
|
|
|
return $this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_AUTH) ||
|
2022-11-10 00:00:50 +01:00
|
|
|
(
|
|
|
|
$sTotp && $totp->verify($sTotp)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Utils
|
|
|
|
*/
|
2022-11-10 16:45:57 +01:00
|
|
|
public function d3TotpGetUtils(): Utils
|
2022-11-10 00:00:50 +01:00
|
|
|
{
|
|
|
|
return Registry::getUtils();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-10 16:45:57 +01:00
|
|
|
* @return Session
|
2022-11-10 00:00:50 +01:00
|
|
|
*/
|
2022-11-10 16:45:57 +01:00
|
|
|
public function d3TotpGetSession(): Session
|
2022-11-10 00:00:50 +01:00
|
|
|
{
|
2022-11-10 16:45:57 +01:00
|
|
|
return Registry::getSession();
|
|
|
|
}
|
2022-11-10 00:00:50 +01:00
|
|
|
|
2022-11-10 16:45:57 +01:00
|
|
|
/**
|
|
|
|
* @return LoggerInterface
|
|
|
|
*/
|
|
|
|
public function getLogger(): LoggerInterface
|
|
|
|
{
|
|
|
|
return Registry::getLogger();
|
2022-11-10 00:00:50 +01:00
|
|
|
}
|
|
|
|
}
|