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

235 lines
6.6 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
{
2022-11-25 15:42:33 +01:00
/** @var d3_totp_user $user */
2022-11-10 16:45:57 +01:00
$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
{
$user = $this->d3TotpGetUserObject();
return !$user->d3TotpGetCurrentUser();
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
{
if ($this->isTotpLoginNotPossible()) {
$this->d3TotpGetUtils()->redirect('index.php?cl=login', false);
} elseif ($this->isTotpIsNotRequired()) {
$this->d3TotpGetUtils()->redirect('index.php?cl=admin_start', false);
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 */
2022-11-24 23:52:16 +01:00
$loginController = $this->d3GetLoginController();
2022-11-24 20:27:07 +01:00
$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-25 15:42:33 +01:00
/** @var d3_totp_user $oUser */
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-25 15:42:33 +01:00
* @return User
2022-11-10 00:00:50 +01:00
*/
2022-11-25 15:42:33 +01:00
public function d3TotpGetUserObject(): 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-25 15:42:33 +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 */
2022-11-24 23:52:16 +01:00
$loginController = $this->d3GetLoginController();
$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
}
2022-11-24 23:52:16 +01:00
/**
2022-11-25 15:42:33 +01:00
* @return LoginController
2022-11-24 23:52:16 +01:00
*/
public function d3GetLoginController(): LoginController
{
return oxNew(LoginController::class);
}
2022-11-25 20:24:09 +01:00
}