webauthn/src/Application/Controller/Admin/d3webauthnadminlogin.php

165 lines
6.3 KiB
PHP
Raw Normal View History

2022-10-29 00:19:34 +02:00
<?php
/**
2022-11-04 22:45:47 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-10-29 00:19:34 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-11-04 22:45:47 +01:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
2022-10-29 00:19:34 +02:00
*/
2022-11-04 22:02:44 +01:00
declare(strict_types=1);
2022-10-29 00:19:34 +02:00
namespace D3\Webauthn\Application\Controller\Admin;
use D3\TestingTools\Production\IsMockable;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnAfterLogin;
2022-10-29 00:19:34 +02:00
use D3\Webauthn\Application\Model\WebauthnConf;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\WebauthnLogin;
2022-10-31 23:17:04 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
2022-10-29 00:19:34 +02:00
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
use OxidEsales\Eshop\Application\Controller\FrontendController;
2022-11-27 01:02:23 +01:00
use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver;
use OxidEsales\Eshop\Core\Session;
2022-10-29 00:19:34 +02:00
use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsView;
2022-10-31 23:17:04 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-10-29 00:19:34 +02:00
class d3webauthnadminlogin extends AdminController
{
use IsMockable;
2022-10-29 00:19:34 +02:00
protected $_sThisTemplate = 'd3webauthnadminlogin.tpl';
2022-11-04 22:02:44 +01:00
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
protected function _authorize(): bool
2022-10-29 00:19:34 +02:00
{
return true;
}
/**
2022-11-04 22:02:44 +01:00
* @return string
2022-10-31 23:17:04 +01:00
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws NotFoundExceptionInterface
2022-10-29 00:19:34 +02:00
*/
2022-11-04 22:02:44 +01:00
public function render(): string
2022-10-29 00:19:34 +02:00
{
if ($this->d3GetMockableRegistryObject(Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)
) {
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=admin_start');
} elseif (!$this->d3GetMockableRegistryObject(Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER)
) {
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login');
2022-10-29 00:19:34 +02:00
}
$this->generateCredentialRequest();
$this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS));
$this->addTplParam('currentProfile', $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE));
$this->d3GetMockableRegistryObject(Session::class)
->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE);
$this->addTplParam('currentChLanguage', $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE));
$afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class);
$afterLogin->changeLanguage();
return $this->d3CallMockableFunction([AdminController::class, 'render']);
2022-10-29 00:19:34 +02:00
}
2022-10-31 23:17:04 +01:00
/**
* @return void
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2022-11-04 22:02:44 +01:00
public function generateCredentialRequest(): void
2022-10-29 00:19:34 +02:00
{
$userId = $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER);
2022-10-31 00:11:06 +01:00
try {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
2022-10-31 00:11:06 +01:00
$publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId);
$this->d3GetMockableRegistryObject(Session::class)
->setVariable(WebauthnConf::WEBAUTHN_ADMIN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions);
2022-10-31 00:11:06 +01:00
$this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions);
$this->addTplParam('isAdmin', isAdmin());
} catch (WebauthnException $e) {
$this->d3GetMockableRegistryObject(Session::class)
->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
$this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString());
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login');
2022-10-31 00:11:06 +01:00
}
2022-10-29 00:19:34 +02:00
}
2022-11-04 22:02:44 +01:00
/**
* @return string|null
*/
public function d3AssertAuthn(): ?string
2022-10-29 00:19:34 +02:00
{
try {
$login = $this->d3GetMockableOxNewObject(WebauthnLogin::class,
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'),
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error')
);
return $login->adminLogin(
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('profile')
);
} catch (WebauthnGetException $e) {
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
return 'login';
2022-10-29 00:19:34 +02:00
}
}
2022-11-04 22:02:44 +01:00
/**
* @return string|null
*/
2022-11-27 01:02:23 +01:00
public function d3GetPreviousClass(): ?string
2022-10-29 00:19:34 +02:00
{
return $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS);
2022-10-29 00:19:34 +02:00
}
2022-11-04 22:02:44 +01:00
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
public function previousClassIsOrderStep(): bool
2022-10-29 00:19:34 +02:00
{
2022-11-27 01:02:23 +01:00
$sClassKey = $this->d3GetPreviousClass();
$resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class)
->getClassNameById($sClassKey);
2022-10-30 00:27:11 +02:00
$resolvedClass = $resolvedClass ?: 'start';
2022-10-29 00:19:34 +02:00
/** @var FrontendController $oController */
$oController = oxNew($resolvedClass);
return $oController->getIsOrderStep();
}
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
public function getIsOrderStep(): bool
2022-10-29 00:19:34 +02:00
{
return $this->previousClassIsOrderStep();
}
}