oxtotp/src/Application/Controller/Admin/d3totpadminlogin.php

224 lines
6.5 KiB
PHP
Raw Normal View History

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;
use D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController;
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\Controller\Admin\LoginController;
2022-11-10 00:00:50 +01:00
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_ADMIN_AUTH) ||
2022-11-10 16:45:57 +01:00
!$totp->isActive();
}
/**
* @return bool
*/
protected function isTotpLoginNotPossible(): bool
{
return !$this->d3TotpGetSession()->hasVariable(d3totp_conf::OXID_ADMIN_AUTH) &&
!$this->d3TotpGetSession()->hasVariable(d3totp_conf::SESSION_ADMIN_CURRENTUSER);
2022-11-10 16:45:57 +01:00
}
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
}
$this->addTplParam('selectedProfile', Registry::getRequest()->getRequestEscapedParameter('profile'));
$this->addTplParam('selectedChLanguage', Registry::getRequest()->getRequestEscapedParameter('chlanguage'));
2022-11-24 20:27:07 +01:00
/** @var d3_totp_LoginController $loginController */
$loginController = oxNew(LoginController::class);
$loginController->d3totpAfterLoginSetLanguage();
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()
{
/** @var d3_totp_user $user */
$user = oxNew(User::class);
$userId = $user->d3TotpGetCurrentUser();
2022-11-10 00:00:50 +01:00
$oBackupCodeList = $this->d3GetBackupCodeListObject();
$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);
}
/**
* @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();
/** @var d3_totp_user $user */
$user = oxNew(User::class);
$userId = $user->d3TotpGetCurrentUser();
2022-11-10 00:00:50 +01:00
try {
2022-11-14 09:07:34 +01:00
$sTotp = implode('', Registry::getRequest()->getRequestEscapedParameter('d3totp', []));
2022-11-10 00:00:50 +01:00
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);
$selectedProfile = Registry::getRequest()->getRequestEscapedParameter('profile');
$selectedLanguage = Registry::getRequest()->getRequestEscapedParameter('chlanguage');
2022-11-10 00:00:50 +01:00
$session->initNewSession();
$session->setVariable(d3totp_conf::SESSION_ADMIN_PROFILE, $selectedProfile);
$session->setVariable(d3totp_conf::SESSION_ADMIN_CHLANGUAGE, $selectedLanguage);
$session->setVariable(d3totp_conf::OXID_ADMIN_AUTH, $userId);
$session->setVariable(d3totp_conf::SESSION_ADMIN_AUTH, $userId);
$session->deleteVariable(d3totp_conf::SESSION_ADMIN_CURRENTUSER);
2022-11-10 00:00:50 +01:00
/** @var d3_totp_LoginController $loginController */
$loginController = oxNew(LoginController::class);
$loginController->d3totpAfterLogin();
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
{
return $this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_ADMIN_AUTH)
2022-11-13 23:40:54 +01:00
|| $totp->verify($sTotp);
2022-11-10 00:00:50 +01:00
}
/**
* @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
}
}